Skip to content

Openai Responses 接口 函数调用 文档

接口概述

提供基于 GPT-4.1 模型的智能问答服务,支持函数调用功能。

基础信息

  • Base URL: https://www.dmxapi.cn
  • Endpoint: /v1/responses
  • 请求方式: POST

请求头

python
headers = {
    "Content-Type": "application/json",
    "Authorization": "Bearer sk-******"  # 替换为你的DMXAPI密钥
}

请求参数

参数类型必填说明
modelstring使用的模型版本 (如: gpt-4.1)
inputstring用户输入的查询内容
toolsarray可调用的函数工具列表
tool_choicestring工具调用策略 (auto/function/none)

函数工具示例

python
tools = [
    {
        "type": "function",
        "name": "get_current_weather",
        "description": "获取指定位置的当前天气",
        "parameters": {
            "type": "object",
            "properties": {
                "location": {
                    "type": "string",
                    "description": "城市和州,例如 San Francisco, CA"
                },
                "unit": {
                    "type": "string", 
                    "enum": ["celsius", "fahrenheit"]
                }
            },
            "required": ["location", "unit"]
        }
    }
]

Python调用示例

python
import requests
import json

# 请求数据准备
data = {
    "model": "gpt-4.1",
    "input": "今天北京的天气热吗?",
    "tools": tools,  # 使用上方定义的工具
    "tool_choice": "auto"
}

# 发送请求
response = requests.post(
    "https://www.dmxapi.cn/v1/responses",
    headers=headers,
    data=json.dumps(data)
)

# 处理响应
if response.status_code == 200:
    print("响应成功:", response.json())
else:
    print("请求失败:", response.text)

响应示例

json
{
    'id': 'resp_687a6994a180819b885ba6cbe982da520fbd734412fd8d7e',
    'object': 'response',
    'created_at': 1752852884,
    'status': 'completed',
    'background': False,
    'error': None,
    'incomplete_details': None,
    'instructions': None,
    'max_output_tokens': None,
    'max_tool_calls': None,
    'model': 'gpt-4.1-2025-04-14',
    'output': [
        {
            'id': 'fc_687a69955134819b81364dfcd517688c0fbd734412fd8d7e',
            'type': 'function_call',
            'status': 'completed',
            'arguments': '{
                "location": "北京",
                "unit": "celsius"
            }',
            'call_id': 'call_bsn4CmJuQ7XyZ7aATNF7re00',
            'name': 'get_current_weather'
        }
    ],
    'parallel_tool_calls': True,
    'previous_response_id': None,
    'reasoning': {
        'effort': None,
        'summary': None
    },
    'service_tier': 'default',
    'store': True,
    'temperature': 1.0,
    'text': {
        'format': {
            'type': 'text'
        }
    },
    'tool_choice': 'auto',
    'tools': [
        {
            'type': 'function',
            'description': '获取指定位置的当前天气',
            'name': 'get_current_weather',
            'parameters': {
                'type': 'object',
                'properties': {
                    'location': {
                        'type': 'string',
                        'description': '城市和州,例如SanFrancisco,
                        CA'
                    },
                    'unit': {
                        'type': 'string',
                        'enum': [
                            'celsius',
                            'fahrenheit'
                        ]
                    }
                },
                'required': [
                    'location',
                    'unit'
                ]
            },
            'strict': True
        }
    ],
    'top_logprobs': 0,
    'top_p': 1.0,
    'truncation': 'disabled',
    'usage': {
        'input_tokens': 68,
        'input_tokens_details': {
            'cached_tokens': 0
        },
        'output_tokens': 21,
        'output_tokens_details': {
            'reasoning_tokens': 0
        },
        'total_tokens': 89
    },
    'user': None,
    'metadata': {
        
    }
}

错误代码

状态码说明
401认证失败
400请求参数错误

一个 Key 用全球大模型