shenmali's picture
Upload folder using huggingface_hub
44de0e7 verified
Raw
History Blame Contribute Delete
1.44 kB
from dataclasses import dataclass, field
from typing import Any
from openai import OpenAI
@dataclass
class LLMResponse:
content: str | None
tool_calls: list[dict] = field(default_factory=list)
prompt_tokens: int = 0
completion_tokens: int = 0
class LLMClient:
BASE_URL = "https://openrouter.ai/api/v1"
def __init__(self, api_key: str, model: str):
if not api_key:
raise ValueError("An OpenRouter API key is required.")
self.model = model
self._client = OpenAI(api_key=api_key, base_url=self.BASE_URL)
def chat(self, messages: list[dict], tools: list[dict] | None = None) -> LLMResponse:
kwargs: dict[str, Any] = {"model": self.model, "messages": messages}
if tools:
kwargs["tools"] = tools
resp = self._client.chat.completions.create(**kwargs)
msg = resp.choices[0].message
tool_calls = []
if getattr(msg, "tool_calls", None):
for tc in msg.tool_calls:
tool_calls.append(
{"id": tc.id, "name": tc.function.name, "arguments": tc.function.arguments}
)
usage = resp.usage
return LLMResponse(
content=msg.content,
tool_calls=tool_calls,
prompt_tokens=getattr(usage, "prompt_tokens", 0) if usage else 0,
completion_tokens=getattr(usage, "completion_tokens", 0) if usage else 0,
)