|
| 1 | +import asyncio |
| 2 | + |
| 3 | +from volcenginesdkarkruntime import AsyncArk |
| 4 | +from volcenginesdkarkruntime.types.responses.response_completed_event import ResponseCompletedEvent |
| 5 | +from volcenginesdkarkruntime.types.responses.response_output_item_done_event import ResponseOutputItemDoneEvent |
| 6 | +from volcenginesdkarkruntime.types.responses.response_function_tool_call import ResponseFunctionToolCall |
| 7 | +from volcenginesdkarkruntime.types.responses.response_mcp_item import McpApprovalRequest |
| 8 | + |
| 9 | +""" |
| 10 | +示例代码:演示 Responses API 的常见用法 |
| 11 | +------------------------------------------------- |
| 12 | +1. 多轮对话中使用缓存 (caching) |
| 13 | +2. 调用外部函数 (function calling) |
| 14 | +3. Web 搜索工具 (web search) |
| 15 | +4. 使用MCP工具 (MCP) |
| 16 | +""" |
| 17 | + |
| 18 | +client = AsyncArk(api_key="${YOUR_API_KEY}") |
| 19 | + |
| 20 | + |
| 21 | +async def main(): |
| 22 | + # ========================================================== |
| 23 | + # 示例 1:多轮对话,开启 caching |
| 24 | + # ========================================================== |
| 25 | + print("Example 1: Use caching for multi-round chat") |
| 26 | + # ---------- 第 1 轮 ---------- |
| 27 | + # 说明:开启 caching,store=True 表示把对话存储在服务端,以便后续引用 |
| 28 | + stream = await client.responses.create( |
| 29 | + model="${YOUR_ENDPOINT_ID}", |
| 30 | + input=[ |
| 31 | + {"role": "system", "content": "你是豆包,是由字节跳动开发的 AI 人工智能助手"}, |
| 32 | + {"role": "user", "content": "你好"}, |
| 33 | + ], |
| 34 | + caching={ |
| 35 | + "type": "enabled", |
| 36 | + }, |
| 37 | + store=True, |
| 38 | + stream=True |
| 39 | + ) |
| 40 | + response_id = "" |
| 41 | + async for event in stream: |
| 42 | + print(event) |
| 43 | + if isinstance(event, ResponseCompletedEvent): |
| 44 | + response_id = event.response.id |
| 45 | + |
| 46 | + # ---------- 第 2 轮 ---------- |
| 47 | + # 说明:通过 previous_response_id 关联上一轮的上下文 |
| 48 | + stream = await client.responses.create( |
| 49 | + model="${YOUR_ENDPOINT_ID}", |
| 50 | + previous_response_id=response_id, |
| 51 | + input=[ |
| 52 | + {"role": "user", "content": "你是谁"}, |
| 53 | + ], |
| 54 | + caching={ |
| 55 | + "type": "enabled", |
| 56 | + }, |
| 57 | + store=True, |
| 58 | + stream=True |
| 59 | + ) |
| 60 | + async for event in stream: |
| 61 | + print(event) |
| 62 | + |
| 63 | + # ========================================================== |
| 64 | + # 示例 2:函数调用 (Function Calling) |
| 65 | + # ========================================================== |
| 66 | + print("Example 2: Use responses API for function calling") |
| 67 | + |
| 68 | + # ---------- 第 1 轮 ---------- |
| 69 | + # 用户询问北京天气,模型会触发工具调用 |
| 70 | + stream = await client.responses.create( |
| 71 | + model="${YOUR_ENDPOINT_ID}", |
| 72 | + input=[ |
| 73 | + {"role": "user", "content": "请问北京今天天气怎么样"}, |
| 74 | + ], |
| 75 | + tools=[ |
| 76 | + { |
| 77 | + "type": "function", |
| 78 | + "name": "get_current_weather", |
| 79 | + "description": "获取当前城市的天气", |
| 80 | + "parameters": { |
| 81 | + "type": "object", |
| 82 | + "properties": { |
| 83 | + "location": { |
| 84 | + "type": "string", |
| 85 | + "description": "城市名称,例如北京" |
| 86 | + }, |
| 87 | + "unit": { |
| 88 | + "type": "string", |
| 89 | + "description": "温度单位,例如摄氏度" |
| 90 | + } |
| 91 | + }, |
| 92 | + "required": ["location"] |
| 93 | + } |
| 94 | + } |
| 95 | + ], |
| 96 | + caching={ |
| 97 | + "type": "enabled", |
| 98 | + }, |
| 99 | + store=True, |
| 100 | + stream=True |
| 101 | + ) |
| 102 | + call_id = "" |
| 103 | + response_id = "" |
| 104 | + async for event in stream: |
| 105 | + print(event) |
| 106 | + if isinstance(event, ResponseCompletedEvent): |
| 107 | + response_id = event.response.id |
| 108 | + if isinstance(event, ResponseOutputItemDoneEvent) and isinstance(event.item, ResponseFunctionToolCall): |
| 109 | + call_id = event.item.call_id |
| 110 | + |
| 111 | + # ---------- 第 2 轮 ---------- |
| 112 | + # 把函数返回结果传回模型,让它继续生成最终回答 |
| 113 | + stream = await client.responses.create( |
| 114 | + model="${YOUR_ENDPOINT_ID}", |
| 115 | + previous_response_id=response_id, |
| 116 | + input=[ |
| 117 | + { |
| 118 | + "type": "function_call_output", |
| 119 | + "call_id": call_id, |
| 120 | + "output": "{\"temperature\": \"30\"}", |
| 121 | + }, |
| 122 | + ], |
| 123 | + caching={ |
| 124 | + "type": "enabled", |
| 125 | + }, |
| 126 | + store=True, |
| 127 | + stream=True |
| 128 | + ) |
| 129 | + async for event in stream: |
| 130 | + print(event) |
| 131 | + |
| 132 | + # ========================================================== |
| 133 | + # 示例 3:Web 搜索工具 |
| 134 | + # ========================================================== |
| 135 | + print("Example 3: Use responses API for web search") |
| 136 | + stream = await client.responses.create( |
| 137 | + model="${YOUR_ENDPOINT_ID}", |
| 138 | + input=[ |
| 139 | + {"role": "user", "content": "今天的新闻"}, |
| 140 | + ], |
| 141 | + tools=[ |
| 142 | + { |
| 143 | + "type": "web_search", |
| 144 | + "limit": 3, |
| 145 | + "sources": ["toutiao"], |
| 146 | + "user_location": { |
| 147 | + "type": "approximate", |
| 148 | + "city": "北京", |
| 149 | + "country": "中国", |
| 150 | + "region": "北京", |
| 151 | + } |
| 152 | + } |
| 153 | + ], |
| 154 | + store=True, |
| 155 | + stream=True |
| 156 | + ) |
| 157 | + async for event in stream: |
| 158 | + print(event) |
| 159 | + |
| 160 | + # ========================================================== |
| 161 | + # 示例 4:使用 MCP |
| 162 | + # ========================================================== |
| 163 | + # ---------- 第 1 轮 ---------- |
| 164 | + # 用户询问repo信息,模型会触发mcp工具调用 |
| 165 | + stream = await client.responses.create( |
| 166 | + model="${YOUR_ENDPOINT_ID}", |
| 167 | + input=[{ |
| 168 | + "role": "user", |
| 169 | + "content": [ |
| 170 | + { |
| 171 | + "type": "input_text", |
| 172 | + "text": "查看这个 repo的文档 expressjs/express " |
| 173 | + } |
| 174 | + ] |
| 175 | + }], |
| 176 | + tools=[ |
| 177 | + { |
| 178 | + "type": "mcp", |
| 179 | + "server_label": "deepwiki-test", |
| 180 | + "server_url": "https://mcp.deepwiki.com/mcp", |
| 181 | + "require_approval": "always" |
| 182 | + } |
| 183 | + ], |
| 184 | + store=True, |
| 185 | + stream=True, |
| 186 | + ) |
| 187 | + approval_id = "" |
| 188 | + response_id = "" |
| 189 | + async for event in stream: |
| 190 | + print(event) |
| 191 | + if isinstance(event, ResponseCompletedEvent): |
| 192 | + response_id = event.response.id |
| 193 | + if isinstance(event, ResponseOutputItemDoneEvent) and isinstance(event.item, McpApprovalRequest): |
| 194 | + approval_id = event.item.id |
| 195 | + |
| 196 | + # ---------- 第 2 轮 ---------- |
| 197 | + # 用户同意mcp工具调用,模型会继续生成最终回答 |
| 198 | + stream = await client.responses.create( |
| 199 | + model="${YOUR_ENDPOINT_ID}", |
| 200 | + input=[ |
| 201 | + { |
| 202 | + "type": "mcp_approval_response", |
| 203 | + "approval_request_id": approval_id, |
| 204 | + "approve": True |
| 205 | + } |
| 206 | + ], |
| 207 | + previous_response_id=response_id, |
| 208 | + tools=[ |
| 209 | + { |
| 210 | + "type": "mcp", |
| 211 | + "server_label": "deepwiki-test", |
| 212 | + "server_url": "https://mcp.deepwiki.com/mcp", |
| 213 | + "require_approval": "always" |
| 214 | + } |
| 215 | + ], |
| 216 | + store=True, |
| 217 | + stream=True, |
| 218 | + ) |
| 219 | + async for event in stream: |
| 220 | + print(event) |
| 221 | + |
| 222 | + |
| 223 | +if __name__ == "__main__": |
| 224 | + asyncio.run(main()) |
0 commit comments