Skip to main content

Baidu Qianfan

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 Embedding corresponding to the package langchain/embeddings 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:

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

from langchain_community.embeddings import QianfanEmbeddingsEndpoint

os.environ["QIANFAN_AK"] = "your_ak"
os.environ["QIANFAN_SK"] = "your_sk"

embed = QianfanEmbeddingsEndpoint(
# qianfan_ak='xxx',
# qianfan_sk='xxx'
res = embed.embed_documents(["hi", "world"])

async def aioEmbed():
res = await embed.aembed_query("qianfan")

await aioEmbed()

async def aioEmbedDocs():
res = await embed.aembed_documents(["hi", "world"])
for r in res:
print("", r[:8])

await aioEmbedDocs()
[INFO] [09-15 20:01:35] [t:140292313159488]: trying to refresh access_token
[INFO] [09-15 20:01:35] [t:140292313159488]: successfully refresh access_token
[INFO] [09-15 20:01:35] [t:140292313159488]: requesting llm api endpoint: /embeddings/embedding-v1
[INFO] [09-15 20:01:35] [t:140292313159488]: async requesting llm api endpoint: /embeddings/embedding-v1
[INFO] [09-15 20:01:35] [t:140292313159488]: async requesting llm api endpoint: /embeddings/embedding-v1
[-0.03313107788562775, 0.052325375378131866, 0.04951248690485954, 0.0077608139254152775, -0.05907672271132469, -0.010798933915793896, 0.03741293027997017, 0.013969100080430508]
[0.0427522286772728, -0.030367236584424973, -0.14847028255462646, 0.055074431002140045, -0.04177454113960266, -0.059512972831726074, -0.043774791061878204, 0.0028191760648041964]
[0.03803155943751335, -0.013231384567916393, 0.0032379645854234695, 0.015074018388986588, -0.006529552862048149, -0.13813287019729614, 0.03297128155827522, 0.044519297778606415]

Use different models in Qianfan

In the case you want to deploy your own model based on Ernie Bot or third-party open sources 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.
    1. Set up the field called endpoint in the initialization:
embed = QianfanEmbeddingsEndpoint(model="bge_large_zh", endpoint="bge_large_zh")

res = embed.embed_documents(["hi", "world"])
for r in res:
[INFO] [09-15 20:01:40] [t:140292313159488]: requesting llm api endpoint: /embeddings/bge_large_zh
[-0.0001582596160005778, -0.025089964270591736, -0.03997539356350899, 0.013156415894627571, 0.000135212714667432, 0.012428865768015385, 0.016216561198234558, -0.04126659780740738]
[0.0019113451708108187, -0.008625439368188381, -0.0531032420694828, -0.0018436014652252197, -0.01818147301673889, 0.010310115292668343, -0.008867680095136166, -0.021067561581730843]

Help us out by providing feedback on this documentation page: