Skip to main content

百度千帆大模型平台的非官方 Python SDK

Project description

文心千帆 Python SDK

本库为文心千帆 Python SDK, 非官方库 ———— 目前官方还没有 Python 的 SDK。

本库代码经过 100% 测试。

文心千帆

目前支持:

  • ERNIE-Bot
  • ERNIE-Bot-turbo
  • BLOOMZ-7B
  • Embeddings

安装

PyPI地址:https://pypi.org/project/erniepysdk/

pip install erniePySDK

使用示例

# 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

其他示例

ErnieBot 对话
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()
ErnieBot 对话 异步调用
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())
ErnitBot 流式对话 异步调用
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())
EmbeddingV1 调用
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()
EmbeddingV1 异步调用
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())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

erniepysdk-0.2.1.tar.gz (8.4 kB view hashes)

Uploaded Source

Built Distribution

erniepysdk-0.2.1-py3-none-any.whl (10.7 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page