124 lines
4.5 KiB
Python
124 lines
4.5 KiB
Python
"""
|
|
@provider OpenAIProvider (swarm/providers/openai_provider.py)
|
|
@purpose Async HTTP provider for OpenAI, DeepSeek, Qwen, or local Ollama endpoints.
|
|
"""
|
|
import time
|
|
import json
|
|
from typing import Type, TypeVar, List, Dict, Tuple
|
|
import httpx
|
|
from pydantic import BaseModel
|
|
from swarm.config import swarm_settings
|
|
from swarm.providers.base import BaseLLMProvider, TokenUsage
|
|
|
|
T = TypeVar("T", bound=BaseModel)
|
|
|
|
|
|
class OpenAIProvider(BaseLLMProvider):
|
|
def __init__(self, api_key: str = None, base_url: str = None, model: str = None):
|
|
self.api_key = api_key or swarm_settings.OPENAI_API_KEY
|
|
self.base_url = (base_url or swarm_settings.OPENAI_BASE_URL).rstrip("/")
|
|
self.model = model or swarm_settings.OPENAI_MODEL
|
|
|
|
async def generate_structured(
|
|
self,
|
|
prompt: str,
|
|
schema: Type[T],
|
|
system_prompt: str = ""
|
|
) -> Tuple[T, TokenUsage]:
|
|
start = time.perf_counter()
|
|
url = f"{self.base_url}/chat/completions"
|
|
|
|
system_instruction = (
|
|
f"{system_prompt}\nYou MUST respond strictly in valid JSON matching this JSON Schema:\n"
|
|
f"{json.dumps(schema.model_json_schema())}"
|
|
)
|
|
|
|
messages = [
|
|
{"role": "system", "content": system_instruction},
|
|
{"role": "user", "content": prompt}
|
|
]
|
|
|
|
payload = {
|
|
"model": self.model,
|
|
"messages": messages,
|
|
"response_format": {"type": "json_object"},
|
|
"temperature": 0.2
|
|
}
|
|
|
|
headers = {
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
|
resp = await client.post(url, json=payload, headers=headers)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
|
|
elapsed_ms = int((time.perf_counter() - start) * 1000)
|
|
raw_text = data["choices"][0]["message"]["content"]
|
|
parsed = schema.model_validate_json(raw_text)
|
|
|
|
usage_meta = data.get("usage", {})
|
|
prompt_tokens = usage_meta.get("prompt_tokens", len(prompt.split()) * 2)
|
|
completion_tokens = usage_meta.get("completion_tokens", len(raw_text.split()) * 2)
|
|
|
|
pricing = swarm_settings.PRICING_MATRIX.get(self.model, {"input_per_million": 0.15, "output_per_million": 0.60})
|
|
cost = (prompt_tokens * pricing["input_per_million"] + completion_tokens * pricing["output_per_million"]) / 1_000_000.0
|
|
|
|
usage = TokenUsage(
|
|
prompt_tokens=prompt_tokens,
|
|
completion_tokens=completion_tokens,
|
|
total_tokens=prompt_tokens + completion_tokens,
|
|
latency_ms=elapsed_ms,
|
|
model_name=self.model,
|
|
estimated_cost_usd=round(cost, 6)
|
|
)
|
|
return parsed, usage
|
|
|
|
async def chat(
|
|
self,
|
|
messages: List[Dict[str, str]],
|
|
system_prompt: str = ""
|
|
) -> Tuple[str, TokenUsage]:
|
|
start = time.perf_counter()
|
|
url = f"{self.base_url}/chat/completions"
|
|
|
|
full_messages = []
|
|
if system_prompt:
|
|
full_messages.append({"role": "system", "content": system_prompt})
|
|
full_messages.extend(messages)
|
|
|
|
payload = {
|
|
"model": self.model,
|
|
"messages": full_messages,
|
|
"temperature": 0.7
|
|
}
|
|
headers = {
|
|
"Authorization": f"Bearer {self.api_key}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
|
|
async with httpx.AsyncClient(timeout=60.0) as client:
|
|
resp = await client.post(url, json=payload, headers=headers)
|
|
resp.raise_for_status()
|
|
data = resp.json()
|
|
|
|
elapsed_ms = int((time.perf_counter() - start) * 1000)
|
|
reply = data["choices"][0]["message"]["content"]
|
|
usage_meta = data.get("usage", {})
|
|
prompt_tokens = usage_meta.get("prompt_tokens", 100)
|
|
completion_tokens = usage_meta.get("completion_tokens", 50)
|
|
|
|
pricing = swarm_settings.PRICING_MATRIX.get(self.model, {"input_per_million": 0.15, "output_per_million": 0.60})
|
|
cost = (prompt_tokens * pricing["input_per_million"] + completion_tokens * pricing["output_per_million"]) / 1_000_000.0
|
|
|
|
usage = TokenUsage(
|
|
prompt_tokens=prompt_tokens,
|
|
completion_tokens=completion_tokens,
|
|
total_tokens=prompt_tokens + completion_tokens,
|
|
latency_ms=elapsed_ms,
|
|
model_name=self.model,
|
|
estimated_cost_usd=round(cost, 6)
|
|
)
|
|
return reply, usage
|