1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
|
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T14:37:58.792881Z",
"start_time": "2023-03-02T14:37:57.989858Z"
}
},
"outputs": [],
"source": [
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T14:38:04.463263Z",
"start_time": "2023-03-02T14:38:04.454376Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'0.26.5'"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"openai.version.VERSION"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T14:38:20.554869Z",
"start_time": "2023-03-02T14:38:17.579099Z"
},
"collapsed": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Looking in indexes: http://mirrors.aliyun.com/pypi/simple/\n",
"Requirement already satisfied: openai in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (0.26.5)\n",
"Collecting openai\n",
" Downloading http://mirrors.aliyun.com/pypi/packages/2f/15/b29ed5ad2eed1f49b8b3e2067fbbbb63cfbfc496ca2a27b772bcf0907ad8/openai-0.27.0-py3-none-any.whl (70 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m70.1/70.1 KB\u001b[0m \u001b[31m334.3 kB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0ma \u001b[36m0:00:01\u001b[0m\n",
"\u001b[?25hRequirement already satisfied: typing-extensions in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (3.7.4.3)\n",
"Requirement already satisfied: aiohttp in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (3.7.4.post0)\n",
"Requirement already satisfied: tqdm in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (4.42.0)\n",
"Requirement already satisfied: requests>=2.20 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from openai) (2.22.0)\n",
"Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (1.24.3)\n",
"Requirement already satisfied: idna<2.9,>=2.5 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (2.8)\n",
"Requirement already satisfied: certifi>=2017.4.17 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (2021.10.8)\n",
"Requirement already satisfied: chardet<3.1.0,>=3.0.2 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from requests>=2.20->openai) (3.0.4)\n",
"Requirement already satisfied: attrs>=17.3.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (20.2.0)\n",
"Requirement already satisfied: multidict<7.0,>=4.5 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (4.7.6)\n",
"Requirement already satisfied: async-timeout<4.0,>=3.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (3.0.1)\n",
"Requirement already satisfied: yarl<2.0,>=1.0 in /Users/chunhuizhang/anaconda3/envs/py3/lib/python3.7/site-packages (from aiohttp->openai) (1.5.1)\n",
"Installing collected packages: openai\n",
" Attempting uninstall: openai\n",
" Found existing installation: openai 0.26.5\n",
" Uninstalling openai-0.26.5:\n",
" Successfully uninstalled openai-0.26.5\n",
"Successfully installed openai-0.27.0\n"
]
}
],
"source": [
"!pip install --upgrade openai"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:08:53.672876Z",
"start_time": "2023-03-02T15:08:53.669163Z"
}
},
"outputs": [],
"source": [
"import openai"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:08:55.740768Z",
"start_time": "2023-03-02T15:08:55.735695Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'0.27.0'"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"openai.version.VERSION"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:09:02.580246Z",
"start_time": "2023-03-02T15:09:02.577767Z"
}
},
"outputs": [],
"source": [
"openai.api_key = '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'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## doc"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:11:31.593473Z",
"start_time": "2023-03-02T15:11:30.167997Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"<OpenAIObject chat.completion id=chatcmpl-6pesdOnQEmxpPZ6Q4uvHUeYGGifVr at 0x126d67938> JSON: {\n",
" \"choices\": [\n",
" {\n",
" \"finish_reason\": \"stop\",\n",
" \"index\": 0,\n",
" \"message\": {\n",
" \"content\": \"The 2020 World Series between the Los Angeles Dodgers and the Tampa Bay Rays was played at Globe Life Field in Arlington, Texas.\",\n",
" \"role\": \"assistant\"\n",
" }\n",
" }\n",
" ],\n",
" \"created\": 1677769891,\n",
" \"id\": \"chatcmpl-6pesdOnQEmxpPZ6Q4uvHUeYGGifVr\",\n",
" \"model\": \"gpt-3.5-turbo-0301\",\n",
" \"object\": \"chat.completion\",\n",
" \"usage\": {\n",
" \"completion_tokens\": 29,\n",
" \"prompt_tokens\": 56,\n",
" \"total_tokens\": 85\n",
" }\n",
"}"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
" {\"role\": \"user\", \"content\": \"Who won the world series in 2020?\"},\n",
" {\"role\": \"assistant\", \"content\": \"The Los Angeles Dodgers won the World Series in 2020.\"},\n",
" {\"role\": \"user\", \"content\": \"Where was it played?\"}\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:12:57.297668Z",
"start_time": "2023-03-02T15:12:56.579683Z"
}
},
"outputs": [],
"source": [
"resp = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\",\n",
" messages=[\n",
" {'role': 'system', 'content': 'You are a helpful assistant that translates English to Chinese.'},\n",
" {'role': 'user', 'content': 'Translate the following English text to Chinese: \"{Who won the world series in 2020?}\"'}\n",
" ]\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"ExecuteTime": {
"end_time": "2023-03-02T15:13:25.098332Z",
"start_time": "2023-03-02T15:13:25.093547Z"
}
},
"outputs": [
{
"data": {
"text/plain": [
"'\"2020年世界大赛冠军是谁?\"'"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"resp['choices'][0]['message']['content']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## conversation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"ExecuteTime": {
"start_time": "2023-03-02T15:18:06.609Z"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"👨💻: 你是谁\n",
"🤖: 我是一个人工智能助手,可以帮助回答你的问题和提供有用的信息。你有什么需要帮助的吗?\n",
"👨💻: 你从哪里来\n",
"🤖: 我是由一些程序员和工程师团队开发的人工智能软件。我被设计为一种语言处理算法,可以理解和回答用户的问题。我的主要目的是提供有用的信息和帮助解决问题,以尽可能满足用户的需求。\n",
"👨💻: 你要到哪里去\n",
"🤖: 作为一个人工智能助手,我没有自己的旅行计划或目的地。我的任务是随时为您提供帮助和支持。无论您需要什么样的信息或帮助,在您需要我的地方,我将随时等待并准备好为您服务。\n",
"👨💻: 我的第一个问题是什么\n",
"🤖: 您的第一个问题是“你是谁”。\n"
]
}
],
"source": [
"messages = [\n",
" {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"},\n",
"]\n",
"while True:\n",
" message = input(\"👨💻: \")\n",
" if message:\n",
" messages.append(\n",
" {\"role\": \"user\", \"content\": message},\n",
" )\n",
" chat_completion = openai.ChatCompletion.create(\n",
" model=\"gpt-3.5-turbo\", messages=messages\n",
" )\n",
" # 获取回复\n",
" reply = chat_completion.choices[0].message.content\n",
" print(f\"🤖: {reply}\")\n",
" messages.append({\"role\": \"assistant\", \"content\": reply})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.3"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|