通过阿里云 DashScope API 调用通义千问
【代码】通过阿里云 DashScope API 调用通义千问。
·
获取API Key
百炼控制台
https://bailian.console.aliyun.com/?tab=model#/api-key
步骤 1:安装 DashScope SDK
pip install dashscope
步骤 2:LangChain 调用
from langchain_community.llms import Tongyi
# 设置阿里云 API Key(从环境变量或直接写入)
import os
os.environ["DASHSCOPE_API_KEY"] = "your-dashscope-api-key"
# 初始化 Qwen
llm = Tongyi(
model_name="qwen-plus", # 可选 qwen-turbo, qwen-plus, qwen-max
temperature=0.7
)
# 调用模型
response = llm("请写一首关于春天的诗。")
print(response)
更多推荐



所有评论(0)