设置
复制
向 AI 提问
# Install the package
pip install -qU langchain-community dashscope
复制
向 AI 提问
# Get a new token: https://help.aliyun.com/document_detail/611472.html?spm=a2c4g.2399481.0.0
from getpass import getpass
DASHSCOPE_API_KEY = getpass()
复制
向 AI 提问
········
复制
向 AI 提问
import os
os.environ["DASHSCOPE_API_KEY"] = DASHSCOPE_API_KEY
复制
向 AI 提问
from langchain_community.llms import Tongyi
复制
向 AI 提问
Tongyi().invoke("What NFL team won the Super Bowl in the year Justin Bieber was born?")
复制
向 AI 提问
'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.'
在链中使用
复制
向 AI 提问
from langchain_core.prompts import PromptTemplate
复制
向 AI 提问
llm = Tongyi()
复制
向 AI 提问
template = """Question: {question}
Answer: Let's think step by step."""
prompt = PromptTemplate.from_template(template)
复制
向 AI 提问
chain = prompt | llm
复制
向 AI 提问
question = "What NFL team won the Super Bowl in the year Justin Bieber was born?"
chain.invoke({"question": question})
复制
向 AI 提问
'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 获取实时答案。