百川 LLM
百川公司 (https://www.baichuan-ai.com/) 是一家专注于解决人类基本需求的中国初创企业,涵盖效率、健康和幸福。
##Installing the langchain packages needed to use the integration
%pip install -qU langchain-community
前提条件
需要一个 API 密钥才能访问 Baichuan LLM API。访问 https://platform.baichuan-ai.com/ 获取您的 API 密钥。
使用 Baichuan LLM
import os
os.environ["BAICHUAN_API_KEY"] = "YOUR_API_KEY"
from langchain_community.llms import BaichuanLLM
# 加载模型
llm = BaichuanLLM()
res = llm.invoke("What's your name?")
print(res)
res = llm.generate(prompts=["你好!"])
res
for res in llm.stream("Who won the second world war?"):
print(res)
import asyncio
async def run_aio_stream():
async for res in llm.astream("Write a poem about the sun."):
print(res)
asyncio.run(run_aio_stream())