Last active
February 5, 2023 06:44
-
-
Save ShimanoN/6ad515e228d1b366b6bebbd9b753d023 to your computer and use it in GitHub Desktop.
use your API key
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "1f99419f-28b5-4f8d-9c32-cbf768cfc0ff", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Exception in Tkinter callback\n", | |
"Traceback (most recent call last):\n", | |
" File \"D:\\ANACONDA\\lib\\tkinter\\__init__.py\", line 1892, in __call__\n", | |
" return self.func(*args)\n", | |
" File \"C:\\Users\\takumi shimano\\AppData\\Local\\Temp\\ipykernel_3200\\2454258824.py\", line 6, in get_answer\n", | |
" completions = openai.Completion.create(\n", | |
" File \"D:\\ANACONDA\\lib\\site-packages\\openai\\api_resources\\completion.py\", line 25, in create\n", | |
" return super().create(*args, **kwargs)\n", | |
" File \"D:\\ANACONDA\\lib\\site-packages\\openai\\api_resources\\abstract\\engine_api_resource.py\", line 153, in create\n", | |
" response, _, api_key = requestor.request(\n", | |
" File \"D:\\ANACONDA\\lib\\site-packages\\openai\\api_requestor.py\", line 227, in request\n", | |
" resp, got_stream = self._interpret_response(result, stream)\n", | |
" File \"D:\\ANACONDA\\lib\\site-packages\\openai\\api_requestor.py\", line 620, in _interpret_response\n", | |
" self._interpret_response_line(\n", | |
" File \"D:\\ANACONDA\\lib\\site-packages\\openai\\api_requestor.py\", line 680, in _interpret_response_line\n", | |
" raise self.handle_error_response(\n", | |
"openai.error.AuthenticationError: Incorrect API key provided: API key. You can find your API key at https://platform.openai.com/account/api-keys.\n" | |
] | |
} | |
], | |
"source": [ | |
"import tkinter as tk\n", | |
"import openai\n", | |
"\n", | |
"def get_answer():\n", | |
" prompt = entry.get()\n", | |
" completions = openai.Completion.create(\n", | |
" engine=\"text-davinci-003\",\n", | |
" prompt=prompt,\n", | |
" max_tokens=1024,\n", | |
" n=1,\n", | |
" stop=None,\n", | |
" temperature=0.5,\n", | |
" )\n", | |
" message = completions.choices[0].text\n", | |
" answer_box.config(state=\"normal\")\n", | |
" answer_box.delete(\"1.0\", \"end\")\n", | |
" answer_box.insert(\"end\", message)\n", | |
" answer_box.config(state=\"disabled\")\n", | |
"\n", | |
"openai.api_key = \"API key\"\n", | |
"\n", | |
"root = tk.Tk()\n", | |
"root.title(\"ChatGPT-like application\")\n", | |
"\n", | |
"label = tk.Label(root, text=\"Enter your question:\")\n", | |
"entry = tk.Entry(root, width=200)\n", | |
"button = tk.Button(root, text=\"Ask\", command=get_answer)\n", | |
"answer_box = tk.Text(root, height=10, width=200)\n", | |
"answer_box.config(state=\"disabled\")\n", | |
"\n", | |
"label.pack()\n", | |
"entry.pack()\n", | |
"button.pack()\n", | |
"answer_box.pack()\n", | |
"\n", | |
"root.mainloop()\n" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"id": "55c07bf4-35b0-48b6-b4eb-8bf1dd763c10", | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3 (ipykernel)", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.9.13" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 5 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment