先在百度智能云千帆這里,申請文心的API KEY:
https://console.bce.baidu.com/qianfan/overview?_=1697115820672
申請的入口和付費(fèi)入口不好找茶行,可以參考
https://cloud.baidu.com/qianfandev/topic/268184
(注意,在文心一言那里https://wenxin.baidu.com/user/key 申請的API KEY是不行的)
文心一言那里的API可以用HTTP調(diào)用年栓,在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", "能詳細(xì)解釋一下{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)