Last active
August 24, 2023 07:41
-
-
Save noumi0k/d17792e08392ea249c7840c1691fe4d8 to your computer and use it in GitHub Desktop.
chatgpt_api.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"authorship_tag": "ABX9TyNstD3CA3XnOkt/edUs2AFi", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/noumi0k/d17792e08392ea249c7840c1691fe4d8/chatgpt_api.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"OpenAIモジュールをImportするために、予めOpenAIモジュールインストールをする。" | |
], | |
"metadata": { | |
"id": "tlSdJiCM7Set" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"!pip install openai" | |
], | |
"metadata": { | |
"id": "pVZVDRAu4g_I" | |
}, | |
"execution_count": null, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "markdown", | |
"source": [ | |
"OpenAIのAPIにアクセスして、ChatGPTを利用する。\n", | |
"\n" | |
], | |
"metadata": { | |
"id": "dXcWO3x77fnG" | |
} | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"id": "4fnaAn92tHPa" | |
}, | |
"outputs": [], | |
"source": [ | |
"import openai\n", | |
"openai.api_key = \"\" #@param {type:\"string\"}\n", | |
"\n", | |
"prompt = \"\" #@param {type:\"string\"}\n", | |
"model = \"gpt-3.5-turbo\" #@param [\"text-davinci-002\", \"text-davinci-003\", \"gpt-3.5-turbo\", \"gpt-4\"]\n", | |
"messages = [\n", | |
" # {\"role\": \"system\", \"content\": system_prompt},\n", | |
" {\"role\": \"user\", \"content\": prompt},\n", | |
" # {\"role\": \"assistant\", \"content\": assistant_prompt},\n", | |
"]\n", | |
"\n", | |
"response = openai.ChatCompletion.create(\n", | |
" model=model,\n", | |
" messages=messages,\n", | |
")\n", | |
"\n", | |
"message = response.choices[0]['message']['content'].strip()\n", | |
"\n", | |
"print(message)\n" | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment