Qwen3-VL/qwen3_vl_example.py

87 lines
2.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""
Qwen3-VL-Plus 使用示例
Qwen3-VL 是通义千问系列中最强大的视觉语言模型,具备以下能力:
- 高精度物体识别与定位包括3D定位
- Agent工具调用
- 文档和网页解析
- 复杂题目解答
- 长视频理解
模型选型:
- qwen3-vl-plus: 性能最强的模型
- qwen3-vl-flash: 速度更快,成本更低,适用于对响应速度敏感的场景
使用前请确保:
1. 设置环境变量 DASHSCOPE_API_KEY
2. 安装依赖: uv add openai python-dotenv
"""
import os
from dotenv import load_dotenv
from openai import OpenAI
# 加载 .env 文件中的环境变量
load_dotenv()
def analyze_image(image_url: str, question: str, model: str = "qwen3-vl-plus") -> str:
"""
使用 Qwen3-VL 分析图片
Args:
image_url: 图片 URL 地址
question: 关于图片的问题
model: 模型名称,默认 qwen3-vl-plus
Returns:
模型的回答
"""
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
# 北京地域
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
# 新加坡地域使用:
# base_url="https://dashscope-intl.aliyuncs.com/compatible-mode/v1",
)
completion = client.chat.completions.create(
model=model,
messages=[
{
"role": "user",
"content": [
{
"type": "image_url",
"image_url": {"url": image_url},
},
{"type": "text", "text": question},
],
},
],
)
return completion.choices[0].message.content
def main():
"""主函数"""
# 示例图片 URL
image_url = "https://help-static-aliyun-doc.aliyuncs.com/file-manage-files/zh-CN/20241022/emyrja/dog_and_girl.jpeg"
# 分析图片
print("正在分析图片...")
result = analyze_image(
image_url=image_url,
question="图中描绘的是什么景象?请详细描述。",
)
print("\n" + "=" * 50)
print("Qwen3-VL-Plus 分析结果:")
print("=" * 50)
print(result)
if __name__ == "__main__":
main()