Skip to content

Commit 354b514

Browse files
authoredMar 2, 2023
Add files via upload
1 parent ee97fba commit 354b514

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed
 

‎ChatGPT_API_in_Python.ipynb

+215
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
{
2+
"nbformat": 4,
3+
"nbformat_minor": 0,
4+
"metadata": {
5+
"colab": {
6+
"provenance": []
7+
},
8+
"kernelspec": {
9+
"name": "python3",
10+
"display_name": "Python 3"
11+
},
12+
"language_info": {
13+
"name": "python"
14+
}
15+
},
16+
"cells": [
17+
{
18+
"cell_type": "code",
19+
"execution_count": 1,
20+
"metadata": {
21+
"colab": {
22+
"base_uri": "https://localhost:8080/"
23+
},
24+
"id": "Tpvg4zPywMHs",
25+
"outputId": "580608a4-cbe2-4cff-fbc8-b9baafbf1738"
26+
},
27+
"outputs": [
28+
{
29+
"output_type": "stream",
30+
"name": "stdout",
31+
"text": [
32+
"\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/70.1 KB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.1/70.1 KB\u001b[0m \u001b[31m2.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
33+
"\u001b[?25h"
34+
]
35+
}
36+
],
37+
"source": [
38+
"!pip install -q openai "
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"source": [
44+
"openai.api_key = 'sk-'"
45+
],
46+
"metadata": {
47+
"id": "ODDG2iqZwSSd"
48+
},
49+
"execution_count": 4,
50+
"outputs": []
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"source": [
55+
"# Method 1 - using OpenAI Python Package\n"
56+
],
57+
"metadata": {
58+
"id": "aC5vluO-zrob"
59+
}
60+
},
61+
{
62+
"cell_type": "code",
63+
"source": [
64+
"import openai"
65+
],
66+
"metadata": {
67+
"id": "LtV_zj6-2AKA"
68+
},
69+
"execution_count": null,
70+
"outputs": []
71+
},
72+
{
73+
"cell_type": "code",
74+
"source": [
75+
"messages = [\n",
76+
" {\"role\": \"system\", \"content\": \"You are a kind helpful assistant.\"},\n",
77+
"]"
78+
],
79+
"metadata": {
80+
"id": "OuSeQk_Uwaiz"
81+
},
82+
"execution_count": 5,
83+
"outputs": []
84+
},
85+
{
86+
"cell_type": "code",
87+
"source": [
88+
"while True:\n",
89+
" message = input(\"User : \")\n",
90+
" if message:\n",
91+
" messages.append(\n",
92+
" {\"role\": \"user\", \"content\": message},\n",
93+
" )\n",
94+
" chat = openai.ChatCompletion.create(\n",
95+
" model=\"gpt-3.5-turbo\", messages=messages\n",
96+
" )\n",
97+
" \n",
98+
" reply = chat.choices[0].message.content\n",
99+
" print(f\"ChatGPT: {reply}\")\n",
100+
" messages.append({\"role\": \"assistant\", \"content\": reply})"
101+
],
102+
"metadata": {
103+
"colab": {
104+
"base_uri": "https://localhost:8080/",
105+
"height": 522
106+
},
107+
"id": "Fp7-srfFwfAj",
108+
"outputId": "f89dce21-b1d9-4fea-fef9-ccd24da75b3f"
109+
},
110+
"execution_count": 20,
111+
"outputs": [
112+
{
113+
"output_type": "stream",
114+
"name": "stdout",
115+
"text": [
116+
"User : What is your name?\n",
117+
"ChatGPT: I am a language model developed by OpenAI, and I don't have a specific name. You can call me OpenAI if you'd like. How can I assist you further?\n",
118+
"User : Can you call me Abdul?\n",
119+
"ChatGPT: Sure, Abdul. Is there anything else I can help you with?\n",
120+
"User : What is my name?\n",
121+
"ChatGPT: Your name is Abdul.\n"
122+
]
123+
},
124+
{
125+
"output_type": "error",
126+
"ename": "KeyboardInterrupt",
127+
"evalue": "ignored",
128+
"traceback": [
129+
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
130+
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
131+
"\u001b[0;32m<ipython-input-20-ea7afd863921>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mmessage\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0minput\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"User : \"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mmessage\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m messages.append(\n\u001b[1;32m 5\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m\"role\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0;34m\"user\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"content\"\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mmessage\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
132+
"\u001b[0;32m/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36mraw_input\u001b[0;34m(self, prompt)\u001b[0m\n\u001b[1;32m 858\u001b[0m \u001b[0;34m\"raw_input was called, but this frontend does not support input requests.\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 859\u001b[0m )\n\u001b[0;32m--> 860\u001b[0;31m return self._input_request(str(prompt),\n\u001b[0m\u001b[1;32m 861\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_ident\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 862\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_parent_header\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
133+
"\u001b[0;32m/usr/local/lib/python3.8/dist-packages/ipykernel/kernelbase.py\u001b[0m in \u001b[0;36m_input_request\u001b[0;34m(self, prompt, ident, parent, password)\u001b[0m\n\u001b[1;32m 902\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 903\u001b[0m \u001b[0;31m# re-raise KeyboardInterrupt, to truncate traceback\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 904\u001b[0;31m \u001b[0;32mraise\u001b[0m \u001b[0mKeyboardInterrupt\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Interrupted by user\"\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 905\u001b[0m \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 906\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlog\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwarning\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Invalid Message:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mexc_info\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
134+
"\u001b[0;31mKeyboardInterrupt\u001b[0m: Interrupted by user"
135+
]
136+
}
137+
]
138+
},
139+
{
140+
"cell_type": "markdown",
141+
"source": [
142+
"# Method 2 - Using API Endpoint"
143+
],
144+
"metadata": {
145+
"id": "v7dxLScWztlp"
146+
}
147+
},
148+
{
149+
"cell_type": "code",
150+
"source": [
151+
"import requests\n",
152+
"\n",
153+
"URL = \"https://api.openai.com/v1/chat/completions\"\n",
154+
"\n",
155+
"payload = {\n",
156+
"\"model\": \"gpt-3.5-turbo\",\n",
157+
"\"messages\": [{\"role\": \"user\", \"content\": f\"What is the first computer in the world?\"}],\n",
158+
"\"temperature\" : 1.0,\n",
159+
"\"top_p\":1.0,\n",
160+
"\"n\" : 1,\n",
161+
"\"stream\": False,\n",
162+
"\"presence_penalty\":0,\n",
163+
"\"frequency_penalty\":0,\n",
164+
"}\n",
165+
"\n",
166+
"headers = {\n",
167+
"\"Content-Type\": \"application/json\",\n",
168+
"\"Authorization\": f\"Bearer {openai.api_key}\"\n",
169+
"}\n",
170+
"\n",
171+
"response = requests.post(URL, headers=headers, json=payload, stream=False)"
172+
],
173+
"metadata": {
174+
"id": "7Np__WWwwmTe"
175+
},
176+
"execution_count": 18,
177+
"outputs": []
178+
},
179+
{
180+
"cell_type": "code",
181+
"source": [
182+
"response.content"
183+
],
184+
"metadata": {
185+
"colab": {
186+
"base_uri": "https://localhost:8080/"
187+
},
188+
"id": "93wHyedwz6vK",
189+
"outputId": "32d4fef4-6f08-4d29-a352-29f83cee5fe6"
190+
},
191+
"execution_count": 19,
192+
"outputs": [
193+
{
194+
"output_type": "execute_result",
195+
"data": {
196+
"text/plain": [
197+
"b'{\"id\":\"chatcmpl-6pWCWPka1Z3jlVmQDU8iH6NQFZ5ty\",\"object\":\"chat.completion\",\"created\":1677736528,\"model\":\"gpt-3.5-turbo-0301\",\"usage\":{\"prompt_tokens\":16,\"completion_tokens\":33,\"total_tokens\":49},\"choices\":[{\"message\":{\"role\":\"assistant\",\"content\":\"\\\\n\\\\nThe first computer in the world was the Electronic Numerical Integrator and Computer (ENIAC), created in 1945 at the University of Pennsylvania.\"},\"finish_reason\":\"stop\",\"index\":0}]}\\n'"
198+
]
199+
},
200+
"metadata": {},
201+
"execution_count": 19
202+
}
203+
]
204+
},
205+
{
206+
"cell_type": "code",
207+
"source": [],
208+
"metadata": {
209+
"id": "Nm_aW1HVz8To"
210+
},
211+
"execution_count": null,
212+
"outputs": []
213+
}
214+
]
215+
}

0 commit comments

Comments
 (0)