2
2
3
3
<h1 >ERNIE Bot SDK</h1 >
4
4
5
- ERNIE Bot SDK提供便捷易用的接口,可以调用文心大模型的能力 ,包含文本创作、通用对话、语义向量、AI作图等。
5
+ ERNIE Bot SDK提供便捷易用的接口,可以调用文心一言的能力 ,包含文本创作、通用对话、语义向量、AI作图等。
6
6
7
7
[ ![ License] ( https://img.shields.io/badge/license-Apache%202-blue.svg )] ( LICENSE )
8
8
[ ![ Version] ( https://img.shields.io/github/release/PaddlePaddle/ERNIE-Bot-SDK.svg )] ( https://github.com/PaddlePaddle/ERNIE-Bot-SDK/releases )
@@ -13,17 +13,17 @@ ERNIE Bot SDK提供便捷易用的接口,可以调用文心大模型的能力
13
13
14
14
## 快速安装
15
15
16
- 执行如下命令,快速安装Python语言的ERNIE Bot SDK(推荐Python >= 3.8)。
16
+ 执行如下命令,快速安装Python语言的最新版本ERNIE Bot SDK(推荐Python >= 3.8)。
17
17
18
18
``` shell
19
19
pip install --upgrade erniebot
20
20
```
21
21
22
22
## 快速使用
23
23
24
- 大家使用ERNIE Bot SDK之前,请申请AK/SK进行认证鉴权 ,具体请参考[ 认证鉴权] ( ./docs/authentication.md ) 。
24
+ 大家使用ERNIE Bot SDK之前,请首先申请并设置鉴权参数 ,具体请参考[ 认证鉴权] ( ./docs/authentication.md ) 。
25
25
26
- ### Python 接口
26
+ ### Python接口
27
27
28
28
``` python
29
29
import erniebot
@@ -43,9 +43,9 @@ erniebot.ak = "<ak-for-qianfan>"
43
43
erniebot.sk = " <sk-for-qianfan>"
44
44
45
45
# Create a chat completion
46
- chat_completion = erniebot.ChatCompletion.create(model = " ernie-bot-3.5" , messages = [{" role" : " user" , " content" : " 你好,请介绍下你自己" }])
46
+ response = erniebot.ChatCompletion.create(model = " ernie-bot-3.5" , messages = [{" role" : " user" , " content" : " 你好,请介绍下你自己" }])
47
47
48
- print (chat_completion .result)
48
+ print (response .result)
49
49
```
50
50
51
51
### 命令行接口 (CLI)
@@ -59,7 +59,7 @@ export EB_API_TYPE="qianfan"
59
59
export EB_AK=" <ak-for-qianfan>"
60
60
export EB_SK=" <sk-for-qianfan>"
61
61
62
- # Create a chat completion (ernie-bot-3.5, ernie-bot-turbo, etc.)
62
+ # Create a chat completion (using ernie-bot-3.5, ernie-bot-turbo, etc.)
63
63
erniebot api chat_completion.create --model ernie-bot-3.5 --message user " 请介绍下你自己"
64
64
65
65
# Set authentication params for image.create
@@ -75,7 +75,7 @@ erniebot api image.create --model ernie-vilg-v2 --prompt "画一只驴肉火烧"
75
75
76
76
### 对话补全 (Chat Completion)
77
77
78
- ERNIE Bot SDK提供了对话补全能力的文心一言旗舰版模型 ` ernie-bot-3.5 ` 和文心一言迅捷版模型` ernie-bot-turbo ` 。
78
+ ERNIE Bot SDK提供具备对话补全能力的文心一言旗舰版模型 ` ernie-bot-3.5 ` 和文心一言迅捷版模型` ernie-bot-turbo ` 。
79
79
旗舰版模型的效果更好,迅捷版模型的响应速度更快、推理时延更低,大家可以根据实际场景的需求选择合适的模型。
80
80
81
81
以下是调用文心一言旗舰版模型进行多轮对话的示例。
@@ -87,7 +87,7 @@ erniebot.api_type = "qianfan"
87
87
erniebot.ak = " <ak-for-qianfan>"
88
88
erniebot.sk = " <sk-for-qianfan>"
89
89
90
- completion = erniebot.ChatCompletion.create(
90
+ response = erniebot.ChatCompletion.create(
91
91
model = " ernie-bot-3.5" ,
92
92
messages = [{
93
93
" role" : " user" ,
@@ -101,13 +101,12 @@ completion = erniebot.ChatCompletion.create(
101
101
" content" : " 我在深圳,周末可以去哪里玩?"
102
102
}])
103
103
104
- print (completion )
104
+ print (response )
105
105
```
106
106
107
+ ### 语义向量生成 (Embedding Generation)
107
108
108
- ### 语义向量 (Embedding)
109
-
110
- ERNIE Bot SDK提供了提取语义向量的Embedding模型。
109
+ ERNIE Bot SDK提供用于提取语义向量的Embedding模型。
111
110
该模型基于文心大模型,使用海量数据训练得到,为[ 文心百中] ( https://wenxin.baidu.com/baizhong/index/ ) 系统提供关键能力。该模型可以将字符串转为384维浮点数表达的语义向量,语义向量具备极其精准的语义表达能力,可以用于度量两个字符串之间的语义相似度。
112
111
113
112
大家可以使用以下代码提取句子的语义向量。
@@ -119,21 +118,21 @@ erniebot.api_type = "qianfan"
119
118
erniebot.ak = " <ak-for-qianfan>"
120
119
erniebot.sk = " <sk-for-qianfan>"
121
120
122
- embedding = erniebot.Embedding.create(
121
+ response = erniebot.Embedding.create(
123
122
model = " ernie-text-embedding" ,
124
123
input = [
125
124
" 我是百度公司开发的人工智能语言模型,我的中文名是文心一言,英文名是ERNIE-Bot,可以协助您完成范围广泛的任务并提供有关各种主题的信息,比如回答问题,提供定义和解释及建议。如果您有任何问题,请随时向我提问。" ,
126
125
" 2018年深圳市各区GDP"
127
126
])
128
127
129
- print (embedding )
128
+ print (response )
130
129
```
131
130
132
131
大家可以登陆[ 文心百中体验中心] ( https://wenxin.baidu.com/baizhong/knowledgesearch ) ,体验更多大模型语义搜索的能力。
133
132
134
133
### 文生图(Image Generation)
135
134
136
- ERNIE Bot SDK提供了文生图能力的ERNIE -ViLG大模型。
135
+ ERNIE Bot SDK提供具备文生图能力的ERNIE -ViLG大模型。
137
136
该模型具备丰富的风格与强大的中文理解能力,支持生成多种尺寸的图片。
138
137
139
138
``` python
@@ -152,7 +151,6 @@ response = erniebot.Image.create(
152
151
)
153
152
154
153
print (response)
155
-
156
154
```
157
155
158
156
<img width =" 512 " alt =" image " src =" https://github.com/PaddlePaddle/ERNIE-Bot-SDK/assets/1371212/73911c97-ef42-4803-8dc6-d385486c128c " >
@@ -164,9 +162,65 @@ print(response)
164
162
165
163
大家也可登陆[ 文心一格] ( https://yige.baidu.com/ ) 平台,体验更多AI艺术与创意辅助的能力。
166
164
165
+ ## 【beta】函数调用(Function Calling)
166
+
167
+ ERNIE Bot SDK提供函数调用功能,即通过大模型根据对话上下文确定何时以及如何调用函数。该功能目前处于测试状态。
168
+
169
+ 以下是调用文心一言旗舰版模型进行函数调用的示例。
170
+
171
+ ``` python
172
+ import json
173
+ import erniebot
174
+
175
+ erniebot.api_type = " qianfan"
176
+ erniebot.ak = " <ak-for-qianfan>"
177
+ erniebot.sk = " <sk-for-qianfan>"
178
+
179
+ response = erniebot.ChatCompletion.create(
180
+ model = " ernie-bot-3.5" ,
181
+ messages = [
182
+ {
183
+ " role" : " user" ,
184
+ " content" : " 深圳市今天气温如何?" ,
185
+ }, ],
186
+ functions = [{
187
+ " name" : " get_current_temperature" ,
188
+ " description" : " 获取指定城市的气温" ,
189
+ " parameters" : {
190
+ " type" : " object" ,
191
+ " properties" : {
192
+ " location" : {
193
+ " type" : " string" ,
194
+ " description" : " 城市名称" ,
195
+ },
196
+ " unit" : {
197
+ " type" : " string" ,
198
+ " enum" : [" 摄氏度" , " 华氏度" ],
199
+ },
200
+ },
201
+ " required" : [" location" , " unit" ],
202
+ },
203
+ " responses" : {
204
+ " type" : " object" ,
205
+ " properties" : {
206
+ " temperature" : {
207
+ " type" : " int" ,
208
+ " description" : " 城市气温" ,
209
+ },
210
+ " unit" : {
211
+ " type" : " string" ,
212
+ " enum" : [" 摄氏度" , " 华氏度" ],
213
+ },
214
+ },
215
+ },
216
+ }, ],
217
+ )
218
+ print (response)
219
+ ```
220
+
167
221
## Gradio可视化应用
168
222
169
- 为了让开发者可以更全面更低门槛的了解ERNIE Bot SDK的全功能,我们基于Gradio实现了一个功能丰富的可视化界面,参阅[ 示例说明] ( examples ) 可以快速本地测试ChatCompletion、Embedding和Image的可视化交互示例。
223
+ 为了让开发者可以更全面更低门槛的了解ERNIE Bot SDK的全功能,我们基于Gradio实现了一个功能丰富的可视化界面,参阅[ 示例说明] ( ./ examples) 可以快速本地测试ChatCompletion、Embedding和Image的可视化交互示例。
170
224
171
225
<img width =" 1296 " alt =" 36dd85dbe30682a287b6a5c5d13e0cdc " src =" https://user-images.githubusercontent.com/19339784/264367116-600c34b9-0103-4fb7-bbe5-6d71ddc6af09.gif " >
172
226
@@ -178,17 +232,16 @@ print(response)
178
232
* [ 参数配置] ( ./docs/configuration.md )
179
233
* 使用指南
180
234
* [ 对话补全ChatCompletion] ( ./docs/guide/chat_completion.md )
181
- * [ 语义向量Embedding ] ( ./docs/guide/embedding.md )
235
+ * [ 语义向量生成Embedding ] ( ./docs/guide/embedding.md )
182
236
* [ 文生图Image] ( ./docs/guide/image.md )
183
237
* API文档
184
238
* [ 对话补全ChatCompletion] ( ./docs/api_reference/chat_completion.md )
185
- * [ 语义向量Embedding ] ( ./docs/api_reference/embedding.md )
239
+ * [ 语义向量生成Embedding ] ( ./docs/api_reference/embedding.md )
186
240
* [ 文生图Image] ( ./docs/api_reference/image.md )
187
241
188
-
189
242
## Acknowledgement
190
243
191
- 我们借鉴了[ OpenAI Python Library] ( https://github.com/openai/openai-python ) 部分API设计 ,在此对OpenAI Python Library作者及开源社区表示感谢。
244
+ 我们借鉴了[ OpenAI Python Library] ( https://github.com/openai/openai-python ) 的部分API设计 ,在此对OpenAI Python Library作者及开源社区表示感谢。
192
245
193
246
## License
194
247
0 commit comments