MCPcopy Create free account
hub / github.com/Tron521/codex-console-temp / send_openai_request

Method send_openai_request

src/core/http_client.py:294–351  ·  view source on GitHub ↗

发送 OpenAI API 请求 Args: endpoint: API 端点 method: HTTP 方法 data: 表单数据 json_data: JSON 数据 headers: 请求头 **kwargs: 其他参数 Returns: 响应 JSON 数据 Raises: HTTPClientError: 请求失败

(
        self,
        endpoint: str,
        method: str = "POST",
        data: Optional[Dict[str, Any]] = None,
        json_data: Optional[Dict[str, Any]] = None,
        headers: Optional[Dict[str, str]] = None,
        **kwargs
    )

Source from the content-addressed store, hash-verified

292 return False, None
293
294 def send_openai_request(
295 self,
296 endpoint: str,
297 method: str = "POST",
298 data: Optional[Dict[str, Any]] = None,
299 json_data: Optional[Dict[str, Any]] = None,
300 headers: Optional[Dict[str, str]] = None,
301 **kwargs
302 ) -> Dict[str, Any]:
303 """
304 发送 OpenAI API 请求
305
306 Args:
307 endpoint: API 端点
308 method: HTTP 方法
309 data: 表单数据
310 json_data: JSON 数据
311 headers: 请求头
312 **kwargs: 其他参数
313
314 Returns:
315 响应 JSON 数据
316
317 Raises:
318 HTTPClientError: 请求失败
319 """
320 # 合并请求头
321 request_headers = self.default_headers.copy()
322 if headers:
323 request_headers.update(headers)
324
325 # 设置 Content-Type
326 if json_data is not None and "Content-Type" not in request_headers:
327 request_headers["Content-Type"] = "application/json"
328 elif data is not None and "Content-Type" not in request_headers:
329 request_headers["Content-Type"] = "application/x-www-form-urlencoded"
330
331 try:
332 response = self.request(
333 method,
334 endpoint,
335 data=data,
336 json=json_data,
337 headers=request_headers,
338 **kwargs
339 )
340
341 # 检查响应状态码
342 response.raise_for_status()
343
344 # 尝试解析 JSON
345 try:
346 return response.json()
347 except json.JSONDecodeError:
348 return {"raw_response": response.text}
349
350 except cffi_requests.RequestsError as e:
351 raise HTTPClientError(f"OpenAI 请求失败: {endpoint} - {e}")

Callers

nothing calls this directly

Calls 3

HTTPClientErrorClass · 0.85
requestMethod · 0.45
jsonMethod · 0.45

Tested by

no test coverage detected