Skip to content

Commit 6b2e26d

Browse files
committed
Added: OpenAI notebook cs001
1 parent 5783b7a commit 6b2e26d

File tree

2 files changed

+181
-0
lines changed

2 files changed

+181
-0
lines changed

docs/3x-libs.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,16 @@
99
"ver":"1.0.0",
1010
"downloads": 1000,
1111
"cat":"script"
12+
},
13+
{
14+
"src":"http://qpython.org",
15+
"rdate":"2023-12-05",
16+
"link":"http://io.qpython.org/notebooks/aipy-openai-001.ipynb",
17+
"smodule":"aipy-openai-001.ipynb",
18+
"description":"OpenAI notebook",
19+
"title":"OpenAI notebook - cs001",
20+
"ver":"1.0.0",
21+
"downloads": 1000,
22+
"cat":"notebooks"
1223
}
1324
]
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "c6c1e68d-a4b1-4200-a36e-a821862c9ecc",
6+
"metadata": {},
7+
"source": [
8+
"导入基本的os和OpenAI库, 其中os库用于后面读取环境变量(能够影响OpenAI配置)"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 3,
14+
"id": "0874350f-09f0-4c02-b05a-a4226cf24bac",
15+
"metadata": {},
16+
"outputs": [],
17+
"source": [
18+
"import os\n",
19+
"from openai import OpenAI"
20+
]
21+
},
22+
{
23+
"cell_type": "markdown",
24+
"id": "365b948a-1d4c-40b8-a04a-385cb252e197",
25+
"metadata": {},
26+
"source": [
27+
"设定OpenAI参数,在这里,我们可以使用PGPT提供的OpenAI密钥,申请地址 https://qpython.pgpt.cloud/qpy-get-key"
28+
]
29+
},
30+
{
31+
"cell_type": "code",
32+
"execution_count": 4,
33+
"id": "1d00e8ff-1830-4a46-85be-5d0b034ffaaa",
34+
"metadata": {},
35+
"outputs": [],
36+
"source": [
37+
"os.environ[\"OPENAI_API_KEY\"] = \"AG-MjE2NTBjMWUxY2VhNWI2OWJmYzA4YjhmY2EzYTdmODc=\"\n",
38+
"os.environ[\"OPENAI_API_BASE\"] = \"https://openai.pgpt.cloud\""
39+
]
40+
},
41+
{
42+
"cell_type": "markdown",
43+
"id": "8c8c4484-9a3a-4030-8430-150876f468b0",
44+
"metadata": {},
45+
"source": [
46+
"初始化OpenAI操作句柄"
47+
]
48+
},
49+
{
50+
"cell_type": "code",
51+
"execution_count": 5,
52+
"id": "904d2776-9617-4669-a4b7-b7539a0cfc8c",
53+
"metadata": {},
54+
"outputs": [],
55+
"source": [
56+
"client = OpenAI(\n",
57+
" api_key=os.environ.get(\"OPENAI_API_KEY\"),\n",
58+
" base_url=os.environ.get(\"OPENAI_API_BASE\")\n",
59+
" )"
60+
]
61+
},
62+
{
63+
"cell_type": "markdown",
64+
"id": "326c1af1-45e9-4914-b37d-56ff650fbcf2",
65+
"metadata": {},
66+
"source": [
67+
"调用OpenAI的聊天补全接口,让OpenAI使用gpt-3.5-turbo模型来将这些词组从英文翻译为法语 {'apple': 'pomme', 'table': 'table', 'car': 'voiture', 'beach': 'plage'}"
68+
]
69+
},
70+
{
71+
"cell_type": "code",
72+
"execution_count": 14,
73+
"id": "0b5f5426-f0b4-4af7-8968-9af747efb7ca",
74+
"metadata": {},
75+
"outputs": [],
76+
"source": [
77+
"chat_completion = client.chat.completions.create(\n",
78+
" messages=[\n",
79+
" {\n",
80+
" \"role\": \"user\",\n",
81+
" \"content\": \"Translate these English words to French: {'apple': 'pomme', 'table': 'table', 'car': 'voiture', 'beach': 'plage'}\",\n",
82+
" }\n",
83+
" ],\n",
84+
" model=\"gpt-3.5-turbo\",\n",
85+
")\n"
86+
]
87+
},
88+
{
89+
"cell_type": "markdown",
90+
"id": "d8029b71-8e0a-4327-96fc-65e43f8bbcfb",
91+
"metadata": {},
92+
"source": [
93+
"最后打印结果"
94+
]
95+
},
96+
{
97+
"cell_type": "code",
98+
"execution_count": 15,
99+
"id": "ab4189df-3f97-454c-8f1a-c74f10fdddc5",
100+
"metadata": {},
101+
"outputs": [
102+
{
103+
"name": "stdout",
104+
"output_type": "stream",
105+
"text": [
106+
"Choice(finish_reason='stop', index=0, message=ChatCompletionMessage(content=\"{'apple': 'pomme', 'table': 'table', 'car': 'voiture', 'beach': 'plage'}\", role='assistant', function_call=None, tool_calls=None), content_filter_results={'hate': {'filtered': False, 'severity': 'safe'}, 'self_harm': {'filtered': False, 'severity': 'safe'}, 'sexual': {'filtered': False, 'severity': 'safe'}, 'violence': {'filtered': False, 'severity': 'safe'}})\n"
107+
]
108+
}
109+
],
110+
"source": [
111+
"print(chat_completion.choices[0])"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"id": "4fca79de-d636-409e-a7db-ee4d2853c38c",
117+
"metadata": {},
118+
"source": [
119+
"如果你能成功得到结果,那么恭喜你,到目前为止你就成功运行了一个OpenAI小脚本,你可以试试改变聊天补全里的内容(content里的指令),来让OpenAI做任何你想要的时候,你也可以试试将最后一个语句改为 print(chat_completion.choices[0].message.content)"
120+
]
121+
},
122+
{
123+
"cell_type": "code",
124+
"execution_count": 17,
125+
"id": "c36cebf8-674d-4271-8b99-5740525cc957",
126+
"metadata": {},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"{'apple': 'pomme', 'table': 'table', 'car': 'voiture', 'beach': 'plage'}\n"
133+
]
134+
}
135+
],
136+
"source": [
137+
"print(chat_completion.choices[0].message.content)"
138+
]
139+
},
140+
{
141+
"cell_type": "markdown",
142+
"id": "92a2abb9-ee63-4f7f-9309-7add5145927a",
143+
"metadata": {},
144+
"source": [
145+
"可以看到,这样就仅仅打印出来了你要求OpenAI翻译后的内容了"
146+
]
147+
}
148+
],
149+
"metadata": {
150+
"kernelspec": {
151+
"display_name": "Python 3 (ipykernel)",
152+
"language": "python",
153+
"name": "python3"
154+
},
155+
"language_info": {
156+
"codemirror_mode": {
157+
"name": "ipython",
158+
"version": 3
159+
},
160+
"file_extension": ".py",
161+
"mimetype": "text/x-python",
162+
"name": "python",
163+
"nbconvert_exporter": "python",
164+
"pygments_lexer": "ipython3",
165+
"version": "3.9.0"
166+
}
167+
},
168+
"nbformat": 4,
169+
"nbformat_minor": 5
170+
}

0 commit comments

Comments
 (0)