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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file erniepysdk-0.2.1.tar.gz.

File metadata

  • Download URL: erniepysdk-0.2.1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Windows/10

File hashes

Hashes for erniepysdk-0.2.1.tar.gz
Algorithm Hash digest
SHA256 3fef899e2df3227216ea4c5f8d23e38821ed2a595ce713b3cdbae40e4fa54338
MD5 57295b0dfff4cb429b07399bd27942fe
BLAKE2b-256 b675213103342a2f67c1575cc18cf06c5e0b4492675500fe09906fee4759d9c1

See more details on using hashes here.

File details

Details for the file erniepysdk-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: erniepysdk-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.11.4 Windows/10

File hashes

Hashes for erniepysdk-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 56c2fed670d5e404b5729af94975691d80672aae8043bc9a24562f46d4e61b61
MD5 1c657a78fdae4076de3e8746afdb9fe6
BLAKE2b-256 9c9d138ca7fbf057160471521157612756f57837501711a389f11ddd5ac0aa06

See more details on using hashes here.

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