这将帮助您开始使用 Qwen 聊天模型。有关所有 ChatQwen 功能和配置的详细文档,请访问 API 参考.
概述
集成详情
| 类 | 包 | 可序列化 | 下载量 | 版本 |
|---|---|---|---|---|
ChatQwen | langchain-qwq | beta | !PyPI - 下载量 | !PyPI - 版本 |
模型特性
| 工具调用 | 结构化输出 | 图像输入 | 音频输入 | 视频输入 | 令牌级流式输出 | 原生异步 | 令牌使用量 | 对数概率 |
|---|---|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ❌ | ✅ | ✅ | ✅ | ✅ | ❌ |
设置
要访问 Qwen 模型,您需要创建一个阿里云账户,获取 API 密钥,并安装 langchain-qwq 集成包。
凭据
前往 阿里云 API 密钥页面 注册阿里云并生成 API 密钥。完成后设置 DASHSCOPE_API_KEY 环境变量:
if not os.getenv("DASHSCOPE_API_KEY"):
os.environ["DASHSCOPE_API_KEY"] = getpass.getpass("Enter your Dashscope API key: ")
安装
LangChain Qwen 集成位于 langchain-qwq package:
pip install -qU langchain-qwq
实例化
现在我们可以实例化模型对象并生成聊天补全:
from langchain_qwq import ChatQwen
llm = ChatQwen(
model="qwen-flash",
max_tokens=3_000,
timeout=None,
max_retries=2,
# other params...
)
调用
messages = [
(
"system",
"You are a helpful assistant that translates English to French."
"Translate the user sentence.",
),
("human", "I love programming."),
]
ai_msg = llm.invoke(messages)
ai_msg
AIMessage(content="J'adore la programmation.", additional_kwargs={}, response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-flash'}, id='run--40f2e75b-7d28-4a71-8f5f-561509ac2010-0', usage_metadata={'input_tokens': 32, 'output_tokens': 8, 'total_tokens': 40, 'input_token_details': {}, 'output_token_details': {}})
工具调用
ChatQwen 支持工具调用 API,可让您描述工具及其参数,并让模型返回包含要调用的工具及其输入的 JSON 对象。
配合使用 bind_tools
from langchain.tools import tool
from langchain_qwq import ChatQwen
@tool
def multiply(first_int: int, second_int: int) -> int:
"""Multiply two integers together."""
return first_int * second_int
llm = ChatQwen(model="qwen-flash")
llm_with_tools = llm.bind_tools([multiply])
msg = llm_with_tools.invoke("What's 5 times forty two")
print(msg)
content='' additional_kwargs={'tool_calls': [{'index': 0, 'id': 'call_f0c2cc49307f480db78a45', 'function': {'arguments': '{"first_int": 5, "second_int": 42}', 'name': 'multiply'}, 'type': 'function'}]} response_metadata={'finish_reason': 'tool_calls', 'model_name': 'qwen-flash'} id='run--27c5aafb-9710-42f5-ab78-5a2ad1d9050e-0' tool_calls=[{'name': 'multiply', 'args': {'first_int': 5, 'second_int': 42}, 'id': 'call_f0c2cc49307f480db78a45', 'type': 'tool_call'}] usage_metadata={'input_tokens': 166, 'output_tokens': 27, 'total_tokens': 193, 'input_token_details': {}, 'output_token_details': {}}
### 视觉支持 #### 图像
from langchain.messages import HumanMessage
model = ChatQwen(model="qwen-vl-max-latest")
messages = [
HumanMessage(content=[
{
"type": "image_url",
"image_url": {
"url": "https://example.com/image/image.png"
},
},
{"type": "text", "text": "What do you see in this image?"},
])
]
response = model.invoke(messages)
print(response)
content='In this image, I see a heartwarming scene on a sandy beach during what appears to be either sunrise or sunset, given the warm, soft golden light. A woman is sitting on the sand, smiling warmly as she gently holds the paw of a large, light-colored dog—likely a Labrador Retriever. The dog is sitting upright, facing her, and seems to be engaging in a friendly handshake or high-five gesture with her. \n\nThe woman is wearing a plaid shirt and dark pants, and her long hair flows loosely. She looks happy and relaxed, enjoying the moment with her pet. The dog is wearing a colorful harness with patterns, and a leash lies loosely on the sand nearby. \n\nIn the background, gentle waves roll onto the shore, and the horizon blends softly into the sky, creating a serene and peaceful atmosphere. The overall mood of the image is joyful, affectionate, and tranquil, capturing a special bond between the woman and her dog in a beautiful natural setting.' additional_kwargs={} response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-vl-max-latest'} id='run--22fecc27-9455-4426-bbd4-aa5a5bde400c-0' usage_metadata={'input_tokens': 1265, 'output_tokens': 202, 'total_tokens': 1467, 'input_token_details': {}, 'output_token_details': {}}
视频
from langchain.messages import HumanMessage
model = ChatQwen(model="qwen-vl-max-latest")
messages = [
HumanMessage(content=[
{
"type": "video_url",
"video_url": {
"url": "https://example.com/video/1.mp4"
},
},
{"type": "text", "text": "Can you tell me about this video?"},
])
]
response = model.invoke(messages)
print(response)
content='This video features a young woman with short, neatly styled brown hair and bangs. She is wearing a soft pink knitted cardigan over a white top, accessorized with a delicate necklace. The background is softly blurred, suggesting an outdoor urban setting with modern buildings, possibly during daytime with natural lighting.\n\nThroughout the video, the woman maintains a warm and friendly demeanor, smiling gently and occasionally laughing. Her expressions are animated and cheerful, conveying a sense of happiness and approachability. The lighting highlights her features beautifully, giving the scene a pleasant and inviting atmosphere.\n\nThe watermark in the top-right corner indicates that this video was created using AI synthesis technology ("通义·AI合成"), suggesting that the content may be generated or enhanced by artificial intelligence. This could mean the visuals are highly polished and stylized, typical of AI-generated media.\n\nOverall, the video appears to be a short, positive, and uplifting clip, possibly intended for use in promotional content, social media, or as part of a digital character presentation.' additional_kwargs={} response_metadata={'finish_reason': 'stop', 'model_name': 'qwen-vl-max-latest'} id='run--c567a6e2-14e0-47e1-acb6-cfe2953fb1ad-0' usage_metadata={'input_tokens': 3607, 'output_tokens': 204, 'total_tokens': 3811, 'input_token_details': {}, 'output_token_details': {}}
API 参考
有关所有 ChatQwen 功能和配置的详细文档,请访问 API 参考