繁體中文
qwen-agent
pip install -U qwen-agent mcp
from openai import OpenAI client = OpenAI( api_key="sk-***", # 🔑 換成你在 AiHubMix 生成的密鑰 base_url="https://aihubmix.com/v1", ) completion = client.chat.completions.create( model="Qwen/Qwen3-30B-A3B", messages=[ { "role": "user", "content": "Explain the Occam's Razor concept and provide everyday examples of it" } ], stream=True ) # 某些 chunk 物件可能沒有 choices 屬性或 choices 是一個空列表,處理方法: for chunk in completion: if hasattr(chunk.choices, '__len__') and len(chunk.choices) > 0: if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="")
chunk.choices[0].delta.content
Qwen/QVQ-72B-Preview
Qwen2-VL-72B
from openai import OpenAI import base64 import os client = OpenAI( api_key="sk-***", # 🔑 換成你在 AiHubMix 生成的密鑰 base_url="https://aihubmix.com/v1", ) image_path = "yourpath/file.png" # 讀取並編碼圖片 def encode_image(image_path): if not os.path.exists(image_path): raise FileNotFoundError(f"圖片文件不存在:{image_path}") with open(image_path, "rb") as image_file: return base64.b64encode(image_file.read()).decode('utf-8') # 獲取圖片的 base64 編碼 base64_image = encode_image(image_path) # 創建包含文本和圖像的消息 completion = client.chat.completions.create( model="qwen2.5-vl-72b-instruct", #qwen2.5-vl-72b-instruct 或 Qwen/QVQ-72B-Preview messages=[ { "role": "user", "content": [ {"type": "text", "text": "請詳細描述這張圖片,包括圖片中的內容、風格和可能的含義。"}, { "type": "image_url", "image_url": { "url": f"data:image/png;base64,{base64_image}" } } ] } ], stream=True ) for chunk in completion: # 安全地檢查是否有內容 if hasattr(chunk.choices, '__len__') and len(chunk.choices) > 0: if hasattr(chunk.choices[0].delta, 'content') and chunk.choices[0].delta.content is not None: print(chunk.choices[0].delta.content, end="")