Skip to main content

QianfanChatEndpoint

Baidu AI Cloud Qianfan Platform is a one-stop large model development and service operation platform for enterprise developers. Qianfan not only provides including the model of Wenxin Yiyan (ERNIE-Bot) and the third-party open-source models, but also provides various AI development tools and the whole set of development environment, which facilitates customers to use and develop large model applications easily.

Basically, those model are split into the following type:

  • Embedding
  • Chat
  • Completion

In this notebook, we will introduce how to use langchain with Qianfan mainly in Chat corresponding to the package langchain/chat_models in langchain:

API Initializationโ€‹

To use the LLM services based on Baidu Qianfan, you have to initialize these parameters:

You could either choose to init the AK,SK in environment variables or init params:

export QIANFAN_AK=XXX
export QIANFAN_SK=XXX

Current supported models:โ€‹

  • ERNIE-Bot-turbo (default models)
  • ERNIE-Bot
  • BLOOMZ-7B
  • Llama-2-7b-chat
  • Llama-2-13b-chat
  • Llama-2-70b-chat
  • Qianfan-BLOOMZ-7B-compressed
  • Qianfan-Chinese-Llama-2-7B
  • ChatGLM2-6B-32K
  • AquilaChat-7B

Set upโ€‹

"""For basic init and call"""
import os

from langchain_community.chat_models import QianfanChatEndpoint
from langchain_core.language_models.chat_models import HumanMessage

os.environ["QIANFAN_AK"] = "Your_api_key"
os.environ["QIANFAN_SK"] = "You_secret_Key"

Usageโ€‹

chat = QianfanChatEndpoint(streaming=True)
messages = [HumanMessage(content="Hello")]
chat.invoke(messages)
AIMessage(content='ๆ‚จๅฅฝ๏ผ่ฏท้—ฎๆ‚จ้œ€่ฆไป€ไนˆๅธฎๅŠฉ๏ผŸๆˆ‘ๅฐ†ๅฐฝๅŠ›ๅ›ž็ญ”ๆ‚จ็š„้—ฎ้ข˜ใ€‚')
await chat.ainvoke(messages)
AIMessage(content='ๆ‚จๅฅฝ๏ผๆœ‰ไป€ไนˆๆˆ‘ๅฏไปฅๅธฎๅŠฉๆ‚จ็š„ๅ—๏ผŸ')
chat.batch([messages])
[AIMessage(content='ๆ‚จๅฅฝ๏ผๆœ‰ไป€ไนˆๆˆ‘ๅฏไปฅๅธฎๅŠฉๆ‚จ็š„ๅ—๏ผŸ')]

Streamingโ€‹

try:
for chunk in chat.stream(messages):
print(chunk.content, end="", flush=True)
except TypeError as e:
print("")
ๆ‚จๅฅฝ๏ผๆœ‰ไป€ไนˆๆˆ‘ๅฏไปฅๅธฎๅŠฉๆ‚จ็š„ๅ—๏ผŸ

Use different models in Qianfanโ€‹

The default model is ERNIE-Bot-turbo, in the case you want to deploy your own model based on Ernie Bot or third-party open-source model, you could follow these steps:

  1. (Optional, if the model are included in the default models, skip it) Deploy your model in Qianfan Console, get your own customized deploy endpoint.
  2. Set up the field called endpoint in the initialization:
chatBot = QianfanChatEndpoint(
streaming=True,
model="ERNIE-Bot",
)

messages = [HumanMessage(content="Hello")]
chatBot.invoke(messages)
AIMessage(content='Hello๏ผŒๅฏไปฅๅ›ž็ญ”้—ฎ้ข˜ไบ†๏ผŒๆˆ‘ไผš็ซญๅฐฝๅ…จๅŠ›ไธบๆ‚จ่งฃ็ญ”๏ผŒ่ฏท้—ฎๆœ‰ไป€ไนˆ้—ฎ้ข˜ๅ—๏ผŸ')

Model Params:โ€‹

For now, only ERNIE-Bot and ERNIE-Bot-turbo support model params below, we might support more models in the future.

  • temperature
  • top_p
  • penalty_score
chat.invoke(
[HumanMessage(content="Hello")],
**{"top_p": 0.4, "temperature": 0.1, "penalty_score": 1},
)
AIMessage(content='ๆ‚จๅฅฝ๏ผๆœ‰ไป€ไนˆๆˆ‘ๅฏไปฅๅธฎๅŠฉๆ‚จ็š„ๅ—๏ผŸ')

Was this page helpful?


You can also leave detailed feedback on GitHub.