Skip to content

Commit

Permalink
chore: 增加openai模型映射cookbook (#612)
Browse files Browse the repository at this point in the history
* feat: update openai adapter cookbook

* chore: add openai adapter in inference.md

* chore: add qianfan openai model mapping usage in cookbook

* chore: fix md

* chore: add openai_conf.yaml

* chore: fix quicklink

* chore: fix quicklink

* chore: fix quicklink

* chore: fix quicklink

* chore: fix install

* chore: fix with install
  • Loading branch information
danielhjz authored Jun 25, 2024
1 parent 76128e3 commit badca9b
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
18 changes: 18 additions & 0 deletions cookbook/assets/openai_conf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
openai_adapter:
# 绑定的 host
host: 0.0.0.0
# 运行端口
port: 8001
# 是否后台运行
detach: false
# 日志文件路径,不设置则不输出至文件
log_file: null
# 模型映射,将模型名称从 OpenAI 的模型映射至千帆模型
# 当所有映射都失败时,会使用原始名称
# 映射的 key 与 value 均支持正则表达式
model_mapping:
gpt-3.5.*: ERNIE-3.5-8K
gpt-4.*: ERNIE-4.0-8K
text-embedding.*: Embedding-V1
# 支持正则替换,如下仅作示意
# gpt-3.5(.*): ERNIE-3.5\1
56 changes: 33 additions & 23 deletions cookbook/openai_adapter.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,42 @@
"2. 编写 OpenAI规范 接口代码,通过调用本地千帆OpenAI适配器,请求千帆大模型平台。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install 'qianfan[openai]' -U"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 启动千帆 OpenAI 适配器"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"可以通过配置模型映射,将模型名称从 OpenAI 的模型映射至千帆模型:\n",
"```yaml\n",
"gpt-4.*: ERNIE-4.0-8K\n",
"```\n",
"具体示例可参考[openai_conf.yaml](./assets/openai_conf.yaml):"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[1mINFO\u001b[0m: `--no-ignore-system` is not set. System messages will be ignored by \n",
"default since most system messages for openai is not suitable for ERNIE model.\n",
"\n",
"OpenAI wrapper server is running at \n",
"\n",
"\u001b[1;33m • \u001b[0mhttp://127.0.0.1:8001 \n",
Expand All @@ -49,7 +66,7 @@
"name": "stderr",
"output_type": "stream",
"text": [
"INFO: Started server process [40058]\n",
"INFO: Started server process [66674]\n",
"INFO: Waiting for application startup.\n",
"INFO: Application startup complete.\n",
"INFO: Uvicorn running on http://0.0.0.0:8001 (Press CTRL+C to quit)\n"
Expand All @@ -75,8 +92,8 @@
"# os.environ[\"QIANFAN_SK\"]=\"your_app_sk\"\n",
"\n",
"# 启动千帆 SDK 的 OpenAI Adapter 服务,用于代理转发指向 OpenAI 的请求,并替换为千帆服务的返回\n",
"server = sp.Popen(\"qianfan openai -p 8001\", shell=True)\n",
"# 也可以在终端直接执行qianfan openai -p 8001"
"server = sp.Popen(\"qianfan openai -p 8001 --config-file ./assets/openai_conf.yaml --no-ignore-system\", shell=True)\n",
"# 也可以在终端直接执行qianfan openai -p 8001 --config-file assets/openai_conf.yaml --no-ignore-system"
]
},
{
Expand All @@ -88,23 +105,15 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"[INFO][2024-06-25 14:47:30.943] oauth.py:228 [t:8344509248]: trying to refresh access_token for ak `rRlk1M***`\n",
"[INFO][2024-06-25 14:47:31.137] oauth.py:243 [t:8344509248]: sucessfully refresh access_token\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"INFO: 127.0.0.1:55075 - \"POST /v1/chat/completions HTTP/1.1\" 200 OK\n",
"ChatCompletionMessage(content='炒西红柿鸡蛋是一道家常菜,口感鲜美,营养丰富。以下是制作炒西红柿鸡蛋的基本步骤:\\n\\n所需材料:\\n- 西红柿 2-3个\\n- 鸡蛋 3-4个\\n- 葱 1根\\n- 蒜 2-3瓣\\n- 盐 适量\\n- 白糖 少许(可选)\\n- 生抽 少许(可选)\\n- 食用油 适量\\n\\n步骤:\\n\\n1. **准备材料**:\\n - 将西红柿洗净,切成小块。\\n - 鸡蛋打入碗中,加少许盐,搅拌均匀。\\n - 葱、蒜切碎备用。\\n\\n2. **炒鸡蛋**:\\n - 热锅凉油,待油温微热时,倒入搅拌好的鸡蛋液。\\n - 用筷子或铲子轻轻搅拌,使鸡蛋液形成蛋花状,待鸡蛋基本凝固后,盛出备用。\\n\\n3. **炒西红柿**:\\n - 锅中再倒入适量食用油,烧热后放入葱、蒜碎爆香。\\n - 加入切好的西红柿块,翻炒至西红柿稍微变软出汁。\\n\\n4. **调味**:\\n - 根据个人口味,加入适量盐、少许白糖(提鲜)和少许生抽(增色)。\\n - 翻炒均匀,使西红柿充分吸收调味料。\\n\\n5. **合炒**:\\n - 将之前炒好的鸡蛋倒入锅中,与西红柿一起翻炒均匀。\\n - 确保鸡蛋和西红柿充分混合,让鸡蛋吸收西红柿的汤汁。\\n\\n6. **出锅**:\\n - 翻炒均匀后,即可关火出锅。\\n - 可以撒上一些葱花作为装饰和提香。\\n\\n注意事项:\\n- 炒鸡蛋时油温不宜过高,以免鸡蛋炒糊。\\n- 西红柿炒至稍微变软出汁即可,不要炒得过烂。\\n- 调味时可根据个人口味调整盐和糖的用量。\\n\\n这样一道色香味俱佳的炒西红柿鸡蛋就做好了,希望您会喜欢!', role='assistant', function_call=None, tool_calls=None)\n"
"INFO: 127.0.0.1:63556 - \"POST /v1/chat/completions HTTP/1.1\" 200 OK\n",
"ChatCompletionMessage(content='炒西红柿鸡蛋的第一步就是准备好食材,包括:西红柿、鸡蛋、盐、油、葱花等。接着,打散鸡蛋,并加入少许盐搅拌均匀。然后,西红柿切块备用。在热锅中加油,油热后倒入鸡蛋液,翻炒至刚刚凝固就盛出备用。再次加油,放入葱花爆香,然后放入西红柿翻炒,炒到西红柿出汁后,放入炒好的鸡蛋,再加盐调味,翻炒均匀后即可出锅。这样就可以得到一盘即好看又好吃的西红柿炒鸡蛋了。', role='assistant', function_call=None, tool_calls=None)\n"
]
}
],
Expand All @@ -114,7 +123,7 @@
"client = OpenAI()\n",
"\n",
"completion = client.chat.completions.create(\n",
" model=\"ERNIE-3.5-8K\",\n",
" model=\"gpt-4\", # model=\"ERNIE-4.0-8K\",\n",
" messages=[{'role': 'system', 'content': 'You are a helpful assistant.'},\n",
" {'role': 'user', 'content': '如何做炒西红柿鸡蛋?'}]\n",
")\n",
Expand All @@ -125,10 +134,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# 常用模型清单\n",
"# 千帆平台常用模型清单\n",
"ERNIE-3.5-8K<br>\n",
"ERNIE-4.0-8K<br>\n",
"ERNIE-Speed-8K<br>"
"ERNIE-Speed-8K<br>\n",
"ERNIE-Lite-8K<br>"
]
},
{
Expand All @@ -140,7 +150,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -204,7 +214,7 @@
" 'Yi-34B-Chat'}"
]
},
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
Expand Down
4 changes: 2 additions & 2 deletions docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ $ qianfan evaluation list-evaluable-models [OPTIONS]
* `--name`: 过滤模型名称,可以通过 `,` 分隔多个名称
* `--help`: 展示帮助信息

### OpenAI 接口模拟
### 千帆 OpenAI 适配器

对于已经适配 OpenAI 而千帆 SDK 尚未适配的第三方库,可以通过该功能模拟出 OpenAI 格式的接口,从而实现快速接入千帆功能。

Expand All @@ -429,7 +429,7 @@ export OPENAI_BASE_URL='http://127.0.0.1:8001/v1' # 模拟 OpenAI 接口的地
* `--detach / -d`: 后台运行
* `--log-file`: 日志文件路径,默认不输出至文件
* `--ignore-system / --no-ignore-system`: 是否忽略消息中的 system 字段,默认忽略
* `--config-file / -c`: 配置文件路径,具体文件格式请参考下方配置文件说明
* `--config-file`: 配置文件路径,具体文件格式请参考下方配置文件说明
* `--help`: 展示帮助信息

#### 配置文件
Expand Down
2 changes: 1 addition & 1 deletion docs/inference.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
### OpenAI迁移
如果您已经使用了OpenAI SDK 或集成了 OpenAI API,那么您可以直接使用千帆Python SDK提供的OpenAI适配器,以快速接入千帆大模型平台:

[QuickLink](./cli.md#openai-接口模拟)
[QuickLink](./cli.md#千帆-openai-适配器)

#### **Chat 对话**

Expand Down

0 comments on commit badca9b

Please sign in to comment.