先在百度智能云千帆这里,申请文心的API KEY:
https://console.bce.baidu.com/qianfan/overview?_=1697115820672
申请的入口和付费入口不好找,可以参考
https://cloud.baidu.com/qianfandev/topic/268184
(注意,在文心一言那里https://wenxin.baidu.com/user/key 申请的API KEY是不行的)
文心一言那里的API可以用HTTP调用,在LangChain这里用不了。参考:
https://download.csdn.net/blog/column/7835468/130958972
直接起飞:
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_community.llms import QianfanLLMEndpoint
WENXIN_APP_Key = "your key"
WENXIN_APP_SECRET = "your secret"
prompt = ChatPromptTemplate.from_messages(
[
("user", "能详细解释一下{item}是什么意思吗?")
]
)
llm = QianfanLLMEndpoint(model="ERNIE-Bot", qianfan_ak=WENXIN_APP_Key, qianfan_sk=WENXIN_APP_SECRET)
output_parser = StrOutputParser() # 把字符串格式的答案从对象中提出来
chain = prompt | llm | output_parser
result = chain.invoke({"item": "苹果"})
print(result)