# 文心千帆 Python SDK
本库为文心千帆 Python SDK, 非官方库 ———— 目前官方还没有 Python 的 SDK。
本库代码经过 100% 测试。
[文心千帆](https://cloud.baidu.com/product/wenxinworkshop)
目前支持:
* ERNIE-Bot
* ERNIE-Bot-turbo
* BLOOMZ-7B
* Embeddings
### 安装
**PyPI地址:[https://pypi.org/project/erniepysdk/](https://pypi.org/project/erniepysdk/)**
```bash
pip install erniePySDK
```
### 使用示例
```py
# ErnieBot 流式对话
# ErnieBotTurbo 和 ErnieBot 的调用方法,甚至参数都完全相同
# Bloozm7B 和 ErnieBot 的调用方法也完全相同,只是参数不同,少了 top_p 等几个参数
import erniePySDK
api_key = ""
secret_key = ""
def testErnieBotChatStream():
bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)
messages = [
{
"role": "user",
"content": "请用Python写一个冒泡排序"
}
]
chuncks = bot.chat(messages=messages, stream=True)
for chunck in chuncks:
print(f"Result Type: {type(chunck)}")
print(chunck)
if __name__ == "__main__":
testErnieBotChatStream()
```
### 获取文心千帆 APIKEY
1. 在百度云官网进行申请:https://cloud.baidu.com/product/wenxinworkshop
2. 申请通过后创建应用:https://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application
3. 获取 apikey 和 api secret
### 其他示例
<details>
<summary>ErnieBot 对话 </summary>
```py
import erniePySDK
api_key = ""
secret_key = ""
def testErnieBotChat():
bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)
messages = [
{
"role": "user",
"content": "介绍一下你自己"
}
]
r = next(bot.chat(messages=messages))
print(f"Result Type: {type(r)}")
print(r)
testErnieBotChat()
```
</details>
<details>
<summary>ErnieBot 对话 异步调用</summary>
```py
import erniePySDK
import asyncio
api_key = ""
secret_key = ""
async def testErnieBotAsyncChat():
bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)
messages = [
{
"role": "user",
"content": "介绍一下你自己"
}
]
r = next(bot.chat(messages=messages))
print(f"Result Type: {type(r)}")
print(r)
asyncio.run(testErnieBotAsyncChat())
```
</details>
<details>
<summary>ErnitBot 流式对话 异步调用</summary>
```py
import erniePySDK
import asyncio
api_key = ""
secret_key = ""
async def testErnieBotAsyncChatStream():
bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)
messages = [
{
"role": "user",
"content": "Python中的生成器可以在异步程序中使用吗?"
}
]
chuncks = bot.chat(messages=messages, stream=True)
for chunck in chuncks:
# print(f"Result Type: {type(chunck)}")
print(chunck.get("result"),end="")
asyncio.run(testErnieBotAsyncChatStream())
```
</details>
<details>
<summary>EmbeddingV1 调用 </summary>
```python
import erniePySDK
apiKey = ""
secretKey = ""
def testEmbeddingV1():
bot = erniePySDK.EmbeddingV1(apiKey=api_key, secretKey=secret_key)
texts = [
"请介绍你自己",
"Python中,子类继承父类后如何修改父类的属性?",
"什么是词向量?"
]
r = bot.embedding(texts=texts)
print(r)
testEmbeddingV1()
```
</details>
<details>
<summary>EmbeddingV1 异步调用 </summary>
```py
import erniePySDK
import asyncio
apiKey = ""
secretKey = ""
async def testAsyncEmbeddingV1():
bot = erniePySDK.EmbeddingV1(apiKey=api_key, secretKey=secret_key)
texts = [
"请介绍你自己",
"Python中,子类继承父类后如何修改父类的属性?",
"什么是词向量?"
]
r = await bot.asyncEmbedding(texts=texts)
print(r)
asyncio.run(testAsyncEmbeddingV1())
```
</details>
Raw data
{
"_id": null,
"home_page": "https://github.com/gty20010709/erniePySDK",
"name": "erniepysdk",
"maintainer": "",
"docs_url": null,
"requires_python": ">=3.11,<4.0",
"maintainer_email": "",
"keywords": "ernie,baidu",
"author": "theocheng",
"author_email": "zlz_gty@foxmail.com",
"download_url": "https://files.pythonhosted.org/packages/b6/75/213103342a2f67c1575cc18cf06c5e0b4492675500fe09906fee4759d9c1/erniepysdk-0.2.1.tar.gz",
"platform": null,
"description": "# \u6587\u5fc3\u5343\u5e06 Python SDK\n\n\u672c\u5e93\u4e3a\u6587\u5fc3\u5343\u5e06 Python SDK, \u975e\u5b98\u65b9\u5e93 \u2014\u2014\u2014\u2014 \u76ee\u524d\u5b98\u65b9\u8fd8\u6ca1\u6709 Python \u7684 SDK\u3002\n\n\u672c\u5e93\u4ee3\u7801\u7ecf\u8fc7 100% \u6d4b\u8bd5\u3002\n\n[\u6587\u5fc3\u5343\u5e06](https://cloud.baidu.com/product/wenxinworkshop)\n\n\u76ee\u524d\u652f\u6301\uff1a\n\n* ERNIE-Bot \n* ERNIE-Bot-turbo\n* BLOOMZ-7B\n* Embeddings\n\n### \u5b89\u88c5\n\n**PyPI\u5730\u5740\uff1a[https://pypi.org/project/erniepysdk/](https://pypi.org/project/erniepysdk/)**\n\n```bash\n\npip install erniePySDK\n\n```\n\n### \u4f7f\u7528\u793a\u4f8b\n```py\n# ErnieBot \u6d41\u5f0f\u5bf9\u8bdd\n# ErnieBotTurbo \u548c ErnieBot \u7684\u8c03\u7528\u65b9\u6cd5\uff0c\u751a\u81f3\u53c2\u6570\u90fd\u5b8c\u5168\u76f8\u540c\n# Bloozm7B \u548c ErnieBot \u7684\u8c03\u7528\u65b9\u6cd5\u4e5f\u5b8c\u5168\u76f8\u540c\uff0c\u53ea\u662f\u53c2\u6570\u4e0d\u540c\uff0c\u5c11\u4e86 top_p \u7b49\u51e0\u4e2a\u53c2\u6570\nimport erniePySDK\n\napi_key = \"\"\nsecret_key = \"\"\n\ndef testErnieBotChatStream():\n bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)\n messages = [\n {\n \"role\": \"user\",\n \"content\": \"\u8bf7\u7528Python\u5199\u4e00\u4e2a\u5192\u6ce1\u6392\u5e8f\"\n }\n ]\n chuncks = bot.chat(messages=messages, stream=True)\n for chunck in chuncks:\n print(f\"Result Type: {type(chunck)}\")\n print(chunck)\n\nif __name__ == \"__main__\":\n testErnieBotChatStream()\n\n```\n\n\n### \u83b7\u53d6\u6587\u5fc3\u5343\u5e06 APIKEY\n1. \u5728\u767e\u5ea6\u4e91\u5b98\u7f51\u8fdb\u884c\u7533\u8bf7\uff1ahttps://cloud.baidu.com/product/wenxinworkshop\n2. \u7533\u8bf7\u901a\u8fc7\u540e\u521b\u5efa\u5e94\u7528\uff1ahttps://console.bce.baidu.com/qianfan/ais/console/applicationConsole/application\n3. \u83b7\u53d6 apikey \u548c api secret\n\n### \u5176\u4ed6\u793a\u4f8b\n<details>\n<summary>ErnieBot \u5bf9\u8bdd </summary>\n\n```py\nimport erniePySDK\n\napi_key = \"\"\nsecret_key = \"\"\n\ndef testErnieBotChat():\n bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)\n messages = [\n {\n \"role\": \"user\",\n \"content\": \"\u4ecb\u7ecd\u4e00\u4e0b\u4f60\u81ea\u5df1\"\n }\n ]\n r = next(bot.chat(messages=messages))\n print(f\"Result Type: {type(r)}\")\n print(r)\n\n\ntestErnieBotChat()\n\n```\n</details>\n\n\n<details>\n<summary>ErnieBot \u5bf9\u8bdd \u5f02\u6b65\u8c03\u7528</summary>\n\n```py\nimport erniePySDK\nimport asyncio\n\napi_key = \"\"\nsecret_key = \"\"\n\nasync def testErnieBotAsyncChat():\n bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)\n messages = [\n {\n \"role\": \"user\",\n \"content\": \"\u4ecb\u7ecd\u4e00\u4e0b\u4f60\u81ea\u5df1\"\n }\n ]\n r = next(bot.chat(messages=messages))\n print(f\"Result Type: {type(r)}\")\n print(r)\n\nasyncio.run(testErnieBotAsyncChat())\n\n```\n</details>\n\n\n\n\n<details>\n<summary>ErnitBot \u6d41\u5f0f\u5bf9\u8bdd \u5f02\u6b65\u8c03\u7528</summary>\n\n```py\nimport erniePySDK\nimport asyncio\n\napi_key = \"\"\nsecret_key = \"\"\n\nasync def testErnieBotAsyncChatStream():\n bot = erniePySDK.ErnieBot(apiKey=api_key, secretKey=secret_key)\n messages = [\n {\n \"role\": \"user\",\n \"content\": \"Python\u4e2d\u7684\u751f\u6210\u5668\u53ef\u4ee5\u5728\u5f02\u6b65\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5417\uff1f\"\n }\n ]\n chuncks = bot.chat(messages=messages, stream=True)\n for chunck in chuncks:\n # print(f\"Result Type: {type(chunck)}\")\n print(chunck.get(\"result\"),end=\"\")\n\n\nasyncio.run(testErnieBotAsyncChatStream())\n```\n</details>\n\n\n<details>\n<summary>EmbeddingV1 \u8c03\u7528 </summary>\n\n```python\nimport erniePySDK\n\napiKey = \"\"\nsecretKey = \"\"\n\ndef testEmbeddingV1():\n bot = erniePySDK.EmbeddingV1(apiKey=api_key, secretKey=secret_key)\n texts = [\n \"\u8bf7\u4ecb\u7ecd\u4f60\u81ea\u5df1\",\n \"Python\u4e2d\uff0c\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u540e\u5982\u4f55\u4fee\u6539\u7236\u7c7b\u7684\u5c5e\u6027\uff1f\",\n \"\u4ec0\u4e48\u662f\u8bcd\u5411\u91cf?\"\n ]\n\n r = bot.embedding(texts=texts)\n print(r)\n\ntestEmbeddingV1()\n```\n\n</details>\n\n<details>\n<summary>EmbeddingV1 \u5f02\u6b65\u8c03\u7528 </summary>\n\n```py\nimport erniePySDK\nimport asyncio\n\napiKey = \"\"\nsecretKey = \"\"\n\nasync def testAsyncEmbeddingV1():\n bot = erniePySDK.EmbeddingV1(apiKey=api_key, secretKey=secret_key)\n texts = [\n \"\u8bf7\u4ecb\u7ecd\u4f60\u81ea\u5df1\",\n \"Python\u4e2d\uff0c\u5b50\u7c7b\u7ee7\u627f\u7236\u7c7b\u540e\u5982\u4f55\u4fee\u6539\u7236\u7c7b\u7684\u5c5e\u6027\uff1f\",\n \"\u4ec0\u4e48\u662f\u8bcd\u5411\u91cf?\"\n ]\n\n r = await bot.asyncEmbedding(texts=texts)\n print(r)\n\nasyncio.run(testAsyncEmbeddingV1())\n\n```\n\n\n</details>",
"bugtrack_url": null,
"license": "Apache-2.0",
"summary": "\u767e\u5ea6\u5343\u5e06\u5927\u6a21\u578b\u5e73\u53f0\u7684\u975e\u5b98\u65b9 Python SDK",
"version": "0.2.1",
"project_urls": {
"Homepage": "https://github.com/gty20010709/erniePySDK",
"Repository": "https://github.com/gty20010709/erniePySDK"
},
"split_keywords": [
"ernie",
"baidu"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9c9d138ca7fbf057160471521157612756f57837501711a389f11ddd5ac0aa06",
"md5": "1c657a78fdae4076de3e8746afdb9fe6",
"sha256": "56c2fed670d5e404b5729af94975691d80672aae8043bc9a24562f46d4e61b61"
},
"downloads": -1,
"filename": "erniepysdk-0.2.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "1c657a78fdae4076de3e8746afdb9fe6",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.11,<4.0",
"size": 10726,
"upload_time": "2023-08-10T05:14:09",
"upload_time_iso_8601": "2023-08-10T05:14:09.357610Z",
"url": "https://files.pythonhosted.org/packages/9c/9d/138ca7fbf057160471521157612756f57837501711a389f11ddd5ac0aa06/erniepysdk-0.2.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "b675213103342a2f67c1575cc18cf06c5e0b4492675500fe09906fee4759d9c1",
"md5": "57295b0dfff4cb429b07399bd27942fe",
"sha256": "3fef899e2df3227216ea4c5f8d23e38821ed2a595ce713b3cdbae40e4fa54338"
},
"downloads": -1,
"filename": "erniepysdk-0.2.1.tar.gz",
"has_sig": false,
"md5_digest": "57295b0dfff4cb429b07399bd27942fe",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.11,<4.0",
"size": 8435,
"upload_time": "2023-08-10T05:14:11",
"upload_time_iso_8601": "2023-08-10T05:14:11.333147Z",
"url": "https://files.pythonhosted.org/packages/b6/75/213103342a2f67c1575cc18cf06c5e0b4492675500fe09906fee4759d9c1/erniepysdk-0.2.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2023-08-10 05:14:11",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "gty20010709",
"github_project": "erniePySDK",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"lcname": "erniepysdk"
}