代码解释器Agent对话
更新时间:2026-01-07
POST
https://qianfan.baidubce.com/v2/agent/code_interpreter/run
本接口用于调用“代码解释器”智能体。该 Agent 会自动分析用户需求,生成代码并运行,基于运行结果回答问题。
权限说明
调用本文API,需使用API Key鉴权方式。使用API Key鉴权调用API流程,具体调用流程,请查看认证鉴权。
请求参数
Headers 参数
除公共头域外,无其它特殊头域
请求结构
POST /v2/agent/code_interpreter/run HTTP/1.1
HOST: qianfan.baidubce.com
Authorization: Bearer <API Key>
Content-Type: application/json
{
"query": "读取上传的文件并分析数据",
"file_ids": ["file-****55xw", "file-w***12m0"]
}
示例代码
请求示例
curl -i --location 'https://qianfan.baidubce.com/v2/agent/code_interpreter/run' \
--header 'Authorization: Bearer bce-v3/ALTAK-********/86356c2efcefb5776024ddb7d7958640f2fc16bf' \
--header 'Content-Type: application/json' \
--data '{
"query": "写一个冒泡排序",
"conversation_id": "ce7e7280-****-****-a2a7-2d8f67ffc4b4"
}'
返回响应
Headers 参数
除公共头域外,无其它特殊头域
返回参数
响应示例
data: {"role": "assistant", "status": "running", "content": [{"name": "/thought/thought_agent", "type": "text", "text": {"info": "用户需要冒泡排序的代码。我将编写一个Python函数来实现它,并进行简单的测试。"}, "event": {"id": "1", "status": "running", "name": "/thought/thought_agent"}}]}
data: {"role": "tool", "status": "running", "content": [{"name": "/toolcall/code_interpreter_general", "type": "code", "text": {"code": "def bubble_sort(arr):\n"}, "event": {"id": "2", "status": "running", "name": "/toolcall/code_interpreter_general"}}]}
data: {"role": "tool", "status": "running", "content": [{"name": "/toolcall/code_interpreter_general", "type": "code", "text": {"code": " n = len(arr)\n for i in range(n):\n for j in range(0, n-i-1):\n if arr[j] > arr[j+1]:\n arr[j], arr[j+1] = arr[j+1], arr[j]\n return arr\n\n# 测试代码\ntest_arr = [64, 34, 25, 12, 22, 11, 90]\nprint(bubble_sort(test_arr))"}, "event": {"id": "2", "status": "running", "name": "/toolcall/code_interpreter_general"}}]}
data: {"role": "tool", "status": "running", "content": [{"name": "/toolcall/code_interpreter_general", "type": "text", "text": {"info": "[11, 12, 22, 25, 34, 64, 90]\n"}, "event": {"id": "2", "status": "done", "name": "/toolcall/code_interpreter_general"}}]}
data: {"role": "assistant", "status": "running", "content": [{"name": "/chat/chat_agent", "type": "text", "text": {"info": "这是"}, "event": {"id": "3", "status": "running", "name": "/chat/chat_agent"}}]}
data: {"role": "assistant", "status": "running", "content": [{"name": "/chat/chat_agent", "type": "text", "text": {"info": "为您编写的冒泡排序代码..."}, "event": {"id": "3", "status": "running", "name": "/chat/chat_agent"}}]}
data: {"role": "tool", "status": "done", "content": [], "conversation_id": "conv-123456", "message_id": "msg-7890"}
