summaryrefslogtreecommitdiff
path: root/llm/tutorials/01_openai_api.ipynb
blob: 38de0fc02b588a3dd89ea6b421e7deb74ef1be72 (plain)
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T14:55:17.764476Z",
     "start_time": "2023-02-27T14:55:17.761677Z"
    }
   },
   "outputs": [],
   "source": [
    "import os\n",
    "import openai"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T14:56:33.903156Z",
     "start_time": "2023-02-27T14:56:33.898811Z"
    }
   },
   "outputs": [],
   "source": [
    "openai.api_key = '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'"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##  openai.Completion.create"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- engine => model\n",
    "    - engine\n",
    "      - ada:350M params, fast\n",
    "      - babbge:1.3B\n",
    "      - curie:6.7B\n",
    "      - davinci:175B,most powerful;\n",
    "- 注意看标点符号;"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T14:58:56.780716Z",
     "start_time": "2023-02-27T14:58:53.940821Z"
    },
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZFntROV0vq5CpYsy207UFhZ4bSv at 0x12a16c0f8> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": \"length\",\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \"\\\");\\n    });\\n\\n    it('AddText#can get text content with\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677509935,\n",
       "  \"id\": \"cmpl-6oZFntROV0vq5CpYsy207UFhZ4bSv\",\n",
       "  \"model\": \"text-davinci-002\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 16,\n",
       "    \"prompt_tokens\": 5,\n",
       "    \"total_tokens\": 21\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-002\", prompt=\"Say this is a test\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Say this is a test\n",
    "\n",
    "\n",
    "This is indeed a test."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T14:59:15.828861Z",
     "start_time": "2023-02-27T14:59:12.705447Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZG5UcSCWJ8KiOyisfxlY28tstvX at 0x12a16c4c0> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": null,\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \"\\n\\nThis is indeed a test.\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677509953,\n",
       "  \"id\": \"cmpl-6oZG5UcSCWJ8KiOyisfxlY28tstvX\",\n",
       "  \"model\": \"text-davinci-003\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 8,\n",
       "    \"prompt_tokens\": 5,\n",
       "    \"total_tokens\": 13\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Say this is a test\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:00:03.335078Z",
     "start_time": "2023-02-27T15:00:01.731030Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZGsgvsVprUhx0OpA4MCW44szBya at 0x12a154d00> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": \"length\",\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \", there was a young girl named Anastasia. Anastasia was a\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677510002,\n",
       "  \"id\": \"cmpl-6oZGsgvsVprUhx0OpA4MCW44szBya\",\n",
       "  \"model\": \"text-davinci-003\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 16,\n",
       "    \"prompt_tokens\": 4,\n",
       "    \"total_tokens\": 20\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Once upon a time\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:01:58.866141Z",
     "start_time": "2023-02-27T15:01:46.228840Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZIYbYLMrUEawSmh1zvu5Ciy80e5 at 0x12a738780> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": \"stop\",\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \"\\n\\nthere was a beautiful princess who lived in a magical kingdom. She was kind and generous, and she had a lot of admirers, both human and magical.\\n\\nOne day, a dark force appeared in the kingdom, and the princess was worried. She set out to find the source of the evil and put an end to it.\\n\\nShe traveled through forests, across rivers, and up mountains. Along the way, she met strange creatures and made new allies. Finally, after weeks of searching, she reached the peak of a high mountain and discovered the source of the evil -- a powerful sorcerer!\\n\\nThe princess bravely confronted the sorcerer and asked him to leave. The sorcerer refused, and a battle ensued. The princess used all of her magical skills to outwit the sorcerer, and eventually he was vanquished.\\n\\nThe kingdom was safe once more, and the princess was hailed as a hero. All of the creatures that she had befriended along the way celebrated her bravery and thanked her for saving their kingdom.\\n\\nThe princess returned to the castle, ready to enjoy a long and peaceful reign.\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677510106,\n",
       "  \"id\": \"cmpl-6oZIYbYLMrUEawSmh1zvu5Ciy80e5\",\n",
       "  \"model\": \"text-davinci-003\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 227,\n",
       "    \"prompt_tokens\": 4,\n",
       "    \"total_tokens\": 231\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-003\", prompt=\"Once upon a time\", max_tokens=256)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:02:44.314526Z",
     "start_time": "2023-02-27T15:02:40.954387Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZJROYB08l97UNvGeKEILgHRciyy at 0x12a7386d0> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": \"stop\",\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \"\\n\\nThe moon landing was when people from Earth explored the moon for the first time. They flew a special spaceship to the moon, and then people got out and walked on the moon! They explored and collected rocks from the moon, and then came back to Earth.\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677510161,\n",
       "  \"id\": \"cmpl-6oZJROYB08l97UNvGeKEILgHRciyy\",\n",
       "  \"model\": \"text-davinci-003\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 54,\n",
       "    \"prompt_tokens\": 15,\n",
       "    \"total_tokens\": 69\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-003\", \n",
    "                         prompt=\"Explain the moon landing to a 6 year old in a few sentences.\", max_tokens=512)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### tasks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:04:12.599004Z",
     "start_time": "2023-02-27T15:04:09.906722Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject text_completion id=cmpl-6oZKtdrPmiXEAlQTK7DDooYZqdgyI at 0x12a7383b8> JSON: {\n",
       "  \"choices\": [\n",
       "    {\n",
       "      \"finish_reason\": \"stop\",\n",
       "      \"index\": 0,\n",
       "      \"logprobs\": null,\n",
       "      \"text\": \"\\n\\nShe did not go to the market.\"\n",
       "    }\n",
       "  ],\n",
       "  \"created\": 1677510251,\n",
       "  \"id\": \"cmpl-6oZKtdrPmiXEAlQTK7DDooYZqdgyI\",\n",
       "  \"model\": \"text-davinci-003\",\n",
       "  \"object\": \"text_completion\",\n",
       "  \"usage\": {\n",
       "    \"completion_tokens\": 10,\n",
       "    \"prompt_tokens\": 15,\n",
       "    \"total_tokens\": 25\n",
       "  }\n",
       "}"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "openai.Completion.create(model=\"text-davinci-003\", \n",
    "                         prompt=\"Correct this to standard English:\\n\\nShe no went to the market.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## openai.Image.create"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:05:42.135448Z",
     "start_time": "2023-02-27T15:05:42.132668Z"
    }
   },
   "outputs": [],
   "source": [
    "prompt = 'a photograph of an astronaut riding a horse'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:05:49.140402Z",
     "start_time": "2023-02-27T15:05:43.188866Z"
    }
   },
   "outputs": [],
   "source": [
    "\n",
    "generation_response = openai.Image.create(\n",
    "    prompt=prompt,\n",
    "    n=1,\n",
    "    size=\"1024x1024\",\n",
    "    response_format=\"url\",\n",
    ")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2023-02-27T15:05:50.359537Z",
     "start_time": "2023-02-27T15:05:50.354815Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<OpenAIObject at 0x12a16c678> JSON: {\n",
       "  \"created\": 1677510348,\n",
       "  \"data\": [\n",
       "    {\n",
       "      \"url\": \"https://oaidalleapiprodscus.blob.core.windows.net/private/org-SiC9BnWmtIrrvb4buhYLx3LA/user-GswrqPcaLILHe9R1JbfuOFmD/img-cRInhoHKRTJt0hRK4mtTeQqR.png?st=2023-02-27T14%3A05%3A48Z&se=2023-02-27T16%3A05%3A48Z&sp=r&sv=2021-08-06&sr=b&rscd=inline&rsct=image/png&skoid=6aaadede-4fb3-4698-a8f6-684d7786b067&sktid=a48cca56-e6da-484e-a814-9c849652bcb3&skt=2023-02-27T09%3A35%3A43Z&ske=2023-02-28T09%3A35%3A43Z&sks=b&skv=2021-08-06&sig=J5oExlCiahr6ItdOmH97iLH%2B4VZoFiP8LPql%2B0dugGU%3D\"\n",
       "    }\n",
       "  ]\n",
       "}"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "generation_response"
   ]
  },
  {
   "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": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}