| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- """LLM provider — thin wrapper around the OpenAI SDK."""
- from __future__ import annotations
- import json
- from dataclasses import dataclass, field
- from typing import Any
- from openai import AsyncOpenAI
- @dataclass
- class ToolCallRequest:
- """A single tool-call request from the LLM."""
- id: str
- name: str
- arguments: dict[str, Any]
- @dataclass
- class LLMResponse:
- """Normalised response from an LLM provider."""
- content: str | None = None
- tool_calls: list[ToolCallRequest] = field(default_factory=list)
- finish_reason: str = "stop"
- @property
- def has_tool_calls(self) -> bool:
- return len(self.tool_calls) > 0
- class OpenAIProvider:
- """OpenAI-compatible chat-completion provider.
- Works with any endpoint that speaks the OpenAI API format
- (OpenRouter, DeepSeek, local vLLM, etc.).
- """
- def __init__(
- self,
- api_key: str,
- api_base: str = "https://api.openai.com/v1",
- default_model: str = "gpt-4o",
- ):
- self.default_model = default_model
- self._client = AsyncOpenAI(api_key=api_key, base_url=api_base)
- async def chat(
- self,
- messages: list[dict[str, Any]],
- tools: list[dict[str, Any]] | None = None,
- model: str | None = None,
- temperature: float = 0.1,
- max_tokens: int = 4096,
- ) -> LLMResponse:
- """Send a chat-completion request and return a normalised response."""
- kwargs: dict[str, Any] = {
- "model": model or self.default_model,
- "messages": messages,
- "temperature": temperature,
- "max_tokens": max_tokens,
- }
- if tools:
- kwargs["tools"] = tools
- kwargs["tool_choice"] = "auto"
- try:
- resp = await self._client.chat.completions.create(**kwargs)
- return self._parse(resp)
- except Exception as exc:
- return LLMResponse(content=f"Error calling LLM: {exc}", finish_reason="error")
- # ------------------------------------------------------------------
- # Internals
- # ------------------------------------------------------------------
- @staticmethod
- def _parse(resp: Any) -> LLMResponse:
- choice = resp.choices[0]
- msg = choice.message
- tool_calls: list[ToolCallRequest] = []
- if msg.tool_calls:
- for tc in msg.tool_calls:
- args = tc.function.arguments
- if isinstance(args, str):
- try:
- args = json.loads(args)
- except json.JSONDecodeError:
- args = {}
- tool_calls.append(
- ToolCallRequest(id=tc.id, name=tc.function.name, arguments=args)
- )
- return LLMResponse(
- content=msg.content,
- tool_calls=tool_calls,
- finish_reason=choice.finish_reason or "stop",
- )
|