| 123456789101112131415161718192021222324252627282930313233343536373839404142434445 |
- from typing import Dict, Any, List, Optional
- class OpenAICompatibleService:
- def __init__(self, http_client):
- self.http_client = http_client
-
- def chat_completion(self, chat_id: str, messages: List[Dict[str, Any]],
- stream: bool = False, model: str = "model",
- extra_body: Dict = None) -> Dict[str, Any]:
- endpoint = f"/api/v1/chats_openai/{chat_id}/chat/completions"
-
- data = {
- "model": model,
- "messages": messages,
- "stream": stream
- }
- if extra_body is not None:
- data["extra_body"] = extra_body
-
- response = self.http_client.post(endpoint, json=data)
-
- if response.get("code") == 0:
- return response.get("data", response)
- else:
- raise Exception(f"聊天完成失败: {response.get('message', '未知错误')}")
-
- def agent_completion(self, agent_id: str, messages: List[Dict[str, Any]],
- stream: bool = False, model: str = "model",
- session_id: str = None) -> Dict[str, Any]:
- endpoint = f"/api/v1/agents_openai/{agent_id}/chat/completions"
-
- data = {
- "model": model,
- "messages": messages,
- "stream": stream
- }
- if session_id is not None:
- data["session_id"] = session_id
-
- response = self.http_client.post(endpoint, json=data)
-
- if response.get("code") == 0:
- return response.get("data", response)
- else:
- raise Exception(f"代理完成失败: {response.get('message', '未知错误')}")
|