LangChain 是一套用于构建 AI 智能体(AI Agent)和大语言模型(LLM)应用的开发框架,可以帮助开发者快速构建基于 GPT、Claude、Gemini 等大模型的复杂 AI 应用,可以简化大语言模型应用开发流程。
安装
bash
$ pip install langchain
$ pip install -qU langchain-qwq
配置API Key
DASHSCOPE_API_KEY 是 API Key
DASHSCOPE_API_BASE 是 openAiCompatible

测试代码
python
from langchain_qwq import ChatQwen
from langchain_core.prompts import ChatPromptTemplate
import httpx
custom_client = httpx.Client(verify=False)
# 初始化模型
llm = ChatQwen(
model="qwen-max",
http_client=custom_client
)
# Prompt 模板
prompt = ChatPromptTemplate.from_template(
"请解释:{topic}"
)
# 创建 Chain
chain = prompt | llm
# 调用
result = chain.invoke({
"topic": "什么是 Transformer"
})
print(result.content)

相关链接
https://docs.langchain.com/oss/python/integrations/chat/qwen