-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.py
More file actions
45 lines (34 loc) · 1.39 KB
/
Copy pathdemo.py
File metadata and controls
45 lines (34 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
from openai import OpenAI
client = OpenAI(
api_key=os.getenv("DASHSCOPE_API_KEY"),
base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
)
def run():
print("正在请求 Qwen3-Max (含联网搜索)...")
response = client.responses.create(
model="qwen3-max-2026-01-23",
input="杭州天气",
tools=[{"type": "web_search"}],
extra_body={"enable_thinking": True}
)
print("-" * 20 + " 响应处理 " + "-" * 20)
# 遍历 output 列表中的每一个 item
for item in response.output:
# 1. 提取思考过程 (Reasoning/Summary)
if item.type == 'reasoning' and hasattr(item, 'summary'):
for s in item.summary:
print(f"\n【思考逻辑】: {s.text.strip()}")
# 2. 提取最终回答 (Message)
if item.type == 'message' and hasattr(item, 'content'):
for content_item in item.content:
# 对应你数据中的 ResponseOutputText 类
if hasattr(content_item, 'text'):
print("\n" + "=" * 10 + " 最终回答 " + "=" * 10)
print(content_item.text)
# 3. 打印消耗情况 (可选)
if hasattr(response, 'usage'):
u = response.usage
print(f"\n[Token消耗] 输入: {u.input_tokens}, 输出: {u.output_tokens}, 总计: {u.total_tokens}")
if __name__ == "__main__":
run()