设置
Copy
# 安装包
pip install -qU langchain-community dashscope
Copy
# 获取新令牌:https://help.aliyun.com/document_detail/611472.html?spm=a2c4g.2399481.0.0
from getpass import getpass
DASHSCOPE_API_KEY = getpass()
Copy
········
Copy
import os
os.environ["DASHSCOPE_API_KEY"] = DASHSCOPE_API_KEY
Copy
from langchain_community.llms import Tongyi
Copy
Tongyi().invoke("What NFL team won the Super Bowl in the year Justin Bieber was born?")
Copy
'Justin Bieber was born on March 1, 1994. The Super Bowl that took place in the same year was Super Bowl XXVIII, which was played on January 30, 1994. The winner of that Super Bowl was the Dallas Cowboys, who defeated the Buffalo Bills with a score of 30-13.'
在链中使用
Copy
from langchain_core.prompts import PromptTemplate
Copy
llm = Tongyi()
Copy
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
Copy
chain = prompt | llm
Copy
question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"
chain.invoke({"question": question})
Copy
'Justin Bieber was born on March 1, 1994. The Super Bowl that took place in the same calendar year was Super Bowl XXVIII, which was played on January 30, 1994. The winner of Super Bowl XXVIII was the Dallas Cowboys, who defeated the Buffalo Bills with a score of 30-13.'
连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。

