summaryrefslogtreecommitdiff
path: root/src/agentux/providers/openai_compatible.py
blob: 939ce9dedcdd6697cfd1492607efe5832746b98d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
"""OpenAI-compatible provider.

Implements the ``/chat/completions`` contract that OpenAI, Ollama, LiteLLM,
vLLM, LM Studio, Qwen and most other servers expose. Nothing here is
OpenAI-*specific* in behaviour -- only in wire format.
"""

from __future__ import annotations

import json
from typing import Any, AsyncIterator, Dict, List, Optional

import httpx

from ..config import is_local_url
from ..errors import (
    AuthenticationError,
    MalformedResponseError,
    ModelNotFoundError,
    NetworkError,
    ProviderError,
    RateLimitError,
    UnsupportedToolCallingError,
)
from ..models import Message, ToolCall, ToolDefinition
from .base import BaseProvider

#: Server-sent-event prefix used by the streaming endpoint.
_SSE_PREFIX = "data:"
#: Sentinel that terminates an OpenAI-compatible SSE stream.
_SSE_DONE = "[DONE]"


def _tool_schema(tools: List[ToolDefinition]) -> List[Dict[str, Any]]:
    return [
        {
            "type": "function",
            "function": {
                "name": tool.name,
                "description": tool.description,
                "parameters": tool.parameters,
            },
        }
        for tool in tools
    ]


def _parse_arguments(raw: Any) -> Dict[str, Any]:
    """Parse the JSON string a provider sends for tool arguments.

    Providers send arguments as a JSON-encoded string; some send an object.
    An empty or malformed string becomes ``{}`` so the tool's own validation
    produces a useful message instead of an opaque JSONDecodeError.
    """
    if raw is None:
        return {}
    if isinstance(raw, dict):
        return raw
    if isinstance(raw, str):
        text = raw.strip()
        if not text:
            return {}
        try:
            parsed = json.loads(text)
        except json.JSONDecodeError as exc:
            raise MalformedResponseError(
                f"Provider sent unparseable tool arguments ({raw!r}): {exc}"
            ) from exc
        if not isinstance(parsed, dict):
            raise MalformedResponseError(
                f"Provider sent tool arguments that were not an object: {raw!r}"
            )
        return parsed
    raise MalformedResponseError(f"Unexpected tool arguments type: {type(raw).__name__}")


def _error_detail(body: str) -> str:
    """Pull the human-readable message out of an error body.

    OpenAI-style servers wrap errors in ``{"error": {"message": ...}}``;
    Ollama uses the same shape, so one parser covers both.
    """
    text = (body or "").strip()
    if not text:
        return ""
    try:
        data = json.loads(text)
    except (json.JSONDecodeError, ValueError):
        return text[:500]
    if isinstance(data, dict):
        error = data.get("error")
        if isinstance(error, dict):
            message = error.get("message") or error.get("detail")
            if message:
                return str(message)[:500]
        elif isinstance(error, str):
            return error[:500]
        if isinstance(data.get("message"), str):
            return data["message"][:500]
        return text[:500]
    return text[:500]


def looks_like_emulated_tool_call(content: str, tools: Optional[List[ToolDefinition]]) -> bool:
    """Detect a model that printed a tool call as text instead of calling it.

    Some local models advertise a ``tools`` capability but, when served through
    an OpenAI-compatible shim, return ``{"name": ..., "arguments": ...}`` as
    ordinary content. Agentux deliberately does **not** execute that: parsing
    arbitrary text as a tool call would be exactly the faking the design
    forbids, and it would let any prose that happens to look like JSON run
    commands. Instead we recognise the shape and report it, so the user can
    pick a model that uses native tool calling.
    """
    if not content or not tools:
        return False
    text = content.strip()
    if not (text.startswith("{") and text.endswith("}")):
        return False
    try:
        parsed = json.loads(text)
    except (json.JSONDecodeError, ValueError):
        return False
    if not isinstance(parsed, dict):
        return False
    # Accept both the bare and the wrapped shapes seen in the wild.
    candidate = parsed.get("tool_call") if isinstance(parsed.get("tool_call"), dict) else parsed
    name = candidate.get("name") or candidate.get("function")
    if isinstance(name, dict):
        name = name.get("name")
    if not isinstance(name, str):
        return False
    known = {tool.name for tool in tools}
    return name in known and isinstance(candidate.get("arguments"), (dict, str))


def _raise_for_status(status: int, body: str, base_url: str = "") -> None:
    """Translate an HTTP error into a message that says how to fix it."""
    detail = _error_detail(body)
    is_local = is_local_url(base_url)

    if status in (401, 403):
        if is_local:
            raise AuthenticationError(
                f"The server at {base_url} rejected the API key (HTTP {status}).\n"
                "Local servers such as Ollama normally ignore the key, so this "
                "is unusual. Another service may be listening on that port.\n"
                f"Server said: {detail}"
            )
        raise AuthenticationError(
            f"The provider rejected the API key (HTTP {status}).\n"
            "Check AGENTUX_API_KEY. If you changed providers, remember that "
            "each one has its own key.\n"
            f"Provider said: {detail}"
        )

    if status == 404:
        raise ModelNotFoundError(
            f"The model was not found (HTTP 404).\n"
            "Run 'agentux --list-models' to see what this server offers.\n"
            "For Ollama, pull the model first: ollama pull <name>\n"
            f"Server said: {detail}"
        )

    if status == 429:
        raise RateLimitError(
            f"Rate limited by the provider (HTTP 429). Wait a moment and retry. "
            f"Provider said: {detail}"
        )

    if status >= 500:
        raise ProviderError(
            f"The provider failed with HTTP {status}. This is a server-side "
            f"problem; retrying later may help. Provider said: {detail}"
        )

    if status == 400:
        # Ollama says this explicitly when the model lacks the tools capability.
        if "does not support tools" in detail.lower():
            raise UnsupportedToolCallingError(
                f"The model {self_model_from_detail(detail)!r} does not support "
                "tool calling.\n"
                "Some models are completion-only and cannot use tools at all. "
                "Pick one that lists 'tools' in its capabilities:\n"
                "  ollama list          # look for 'tools' in the capabilities\n"
                "or run a plain conversation with --no-tools.\n"
                f"Server said: {detail}"
            )
        raise ProviderError(
            f"The provider rejected the request (HTTP 400). Common causes: a "
            "model that cannot use tools, a context window that is too small "
            "for the conversation, or an unsupported parameter.\n"
            f"Server said: {detail}"
        )

    raise ProviderError(f"Provider returned HTTP {status}: {detail}")


def self_model_from_detail(detail: str) -> str:
    """Pull the model name out of an Ollama tool-support error."""
    marker = "library/"
    if marker in detail:
        return detail.split(marker, 1)[1].split()[0].strip()
    return detail.split()[0] if detail else "unknown"


class OpenAICompatibleProvider(BaseProvider):
    """Talks to any server implementing the OpenAI chat-completions API."""

    def _payload(
        self,
        messages: List[Message],
        tools: Optional[List[ToolDefinition]],
        stream: bool,
    ) -> Dict[str, Any]:
        payload: Dict[str, Any] = {
            "model": self.config.model,
            "messages": [m.to_dict() for m in messages],
            "temperature": self.config.temperature,
            "max_tokens": self.config.max_tokens,
        }
        if tools:
            payload["tools"] = _tool_schema(tools)
            payload["tool_choice"] = "auto"
        if stream:
            payload["stream"] = True
        return payload

    def _headers(self) -> Dict[str, str]:
        return {
            "Authorization": f"Bearer {self.config.api_key}",
            "Content-Type": "application/json",
        }

    def supports_tool_calling(self) -> bool:
        return True

    def _parse_message(self, raw: Dict[str, Any]) -> Message:
        """Convert a raw assistant message into a :class:`Message`."""
        if not isinstance(raw, dict):
            raise MalformedResponseError(
                f"Expected an object for the assistant message, got {type(raw).__name__}."
            )

        tool_calls: List[ToolCall] = []
        for index, entry in enumerate(raw.get("tool_calls") or []):
            if not isinstance(entry, dict):
                raise MalformedResponseError(
                    f"Tool call #{index} was not an object: {entry!r}"
                )
            function = entry.get("function")
            if not isinstance(function, dict) or "name" not in function:
                raise MalformedResponseError(
                    f"Tool call #{index} is missing a function name: {entry!r}"
                )
            tool_calls.append(
                ToolCall(
                    id=str(entry.get("id") or f"call_{index}"),
                    name=str(function["name"]),
                    arguments=_parse_arguments(function.get("arguments")),
                )
            )

        return Message(
            role="assistant",
            content=raw.get("content") or None,
            tool_calls=tool_calls or None,
        )

    def _first_choice(self, data: Any) -> Dict[str, Any]:
        if not isinstance(data, dict):
            raise MalformedResponseError(
                f"Provider returned {type(data).__name__}, expected a JSON object."
            )
        choices = data.get("choices")
        if not choices:
            raise MalformedResponseError(
                "Provider returned no choices. The model may have been rejected "
                f"or the response was empty. Full response: {json.dumps(data)[:500]}"
            )
        message = choices[0].get("message") if isinstance(choices[0], dict) else None
        if message is None:
            raise MalformedResponseError(
                f"Provider choice had no 'message' field: {choices[0]!r}"
            )
        return message

    async def chat(
        self,
        messages: List[Message],
        tools: Optional[List[ToolDefinition]] = None,
    ) -> Message:
        async with httpx.AsyncClient(timeout=self.config.timeout) as client:
            try:
                response = await client.post(
                    self.config.chat_url,
                    headers=self._headers(),
                    json=self._payload(messages, tools, stream=False),
                )
            except httpx.TimeoutException as exc:
                raise ProviderError(
                    f"The provider did not respond within {self.config.timeout:g}s. "
                    "Raise AGENTUX_TIMEOUT if the model is simply slow, or check "
                    "that AGENTUX_BASE_URL is reachable."
                ) from exc
            except httpx.RequestError as exc:
                raise NetworkError(
                    f"Could not reach the provider at {self.config.base_url}: {exc}\n"
                    "Check your network connection and AGENTUX_BASE_URL."
                ) from exc

            if response.status_code >= 400:
                _raise_for_status(response.status_code, response.text, self.config.base_url)

            try:
                data = response.json()
            except ValueError as exc:
                raise MalformedResponseError(
                    "Provider returned a response that is not valid JSON. This "
                    "usually means the base URL points at a web page or proxy "
                    "rather than an API endpoint."
                ) from exc

        message = self._parse_message(self._first_choice(data))

        if tools and not message.tool_calls and looks_like_emulated_tool_call(
            message.content, tools
        ):
            raise UnsupportedToolCallingError(
                f"{self.config.model} returned a tool call as plain text instead "
                "of using native tool calling.\n\n"
                "Agentux will not run tool calls that were printed as text: "
                "parsing prose as a command is how an agent runs something it "
                "was never asked to run. It is treated as an error instead.\n\n"
                "This model advertises a 'tools' capability but its chat "
                "template does not emit structured calls. Try a model whose "
                "template supports tool use (run 'agentux --list-models'), "
                "update the model with 'ollama pull', or use --no-tools."
            )

        return message

    async def list_models(self) -> List[str]:
        """List the models this server offers.

        Works against the OpenAI-compatible ``/models`` endpoint. When the
        server is a local Ollama, the native ``/api/tags`` endpoint is also
        queried because it reports each model's capabilities -- which is how a
        user finds out whether a model can call tools at all.
        """
        names: List[str] = []
        async with httpx.AsyncClient(timeout=self.config.timeout) as client:
            try:
                response = await client.get(
                    self.config.models_url, headers=self._headers()
                )
                if response.status_code < 400:
                    data = response.json()
                    for entry in data.get("data") or []:
                        name = entry.get("id")
                        if name:
                            names.append(str(name))
                elif response.status_code == 404:
                    raise ModelNotFoundError(
                        f"{self.config.base_url} does not expose a /models "
                        "endpoint, so Agentux cannot list models. Set "
                        "AGENTUX_MODEL explicitly."
                    )
                else:
                    _raise_for_status(
                        response.status_code, response.text, self.config.base_url
                    )
            except httpx.TimeoutException as exc:
                raise ProviderError(
                    f"Listing models timed out after {self.config.timeout:g}s."
                ) from exc
            except httpx.RequestError as exc:
                raise NetworkError(
                    f"Could not reach the provider at {self.config.base_url}: {exc}\n"
                    "Is Ollama running? Try: ollama serve"
                ) from exc
            except ValueError as exc:
                raise MalformedResponseError(
                    "The model list was not valid JSON; the base URL may point "
                    "at a web page rather than an API endpoint."
                ) from exc

        return names

    async def list_models_detailed(self) -> List[Dict[str, Any]]:
        """Model names plus Ollama capabilities, when the server reports them.

        Returns the same shape as :meth:`list_models` (names only, empty dict
        values) for servers that do not expose capabilities, so callers can use
        one code path.
        """
        names = await self.list_models()
        details: List[Dict[str, Any]] = [{"name": name} for name in names]

        if not self.config.is_local:
            return details

        # Ollama's native endpoint reports capabilities; try it, but do not
        # fail the command if this server is some other local provider.
        ollama_root = self.config.base_url.rstrip("/")
        if ollama_root.endswith("/v1"):
            ollama_root = ollama_root[: -len("/v1")]
        try:
            async with httpx.AsyncClient(timeout=self.config.timeout) as client:
                response = await client.get(f"{ollama_root}/api/tags")
                if response.status_code >= 400:
                    return details
                payload = response.json()
        except Exception:
            return details

        capabilities = {}
        for entry in payload.get("models") or []:
            name = entry.get("name") or entry.get("model")
            if name:
                capabilities[name] = entry.get("capabilities") or []

        for item in details:
            for name, caps in capabilities.items():
                if name == item["name"] or item["name"].startswith(name.split(":")[0]):
                    item["capabilities"] = caps
                    break
        return details

    async def stream(
        self,
        messages: List[Message],
        tools: Optional[List[ToolDefinition]] = None,
    ) -> AsyncIterator[str]:
        """Yield text deltas from a streaming completion.

        Tool calls are not streamed: a partial tool call cannot be executed
        safely. When the model starts calling a tool we stop yielding text and
        let the caller fall back to :meth:`chat`.
        """
        async with httpx.AsyncClient(timeout=self.config.timeout) as client:
            try:
                async with client.stream(
                    "POST",
                    self.config.chat_url,
                    headers=self._headers(),
                    json=self._payload(messages, tools, stream=True),
                ) as response:
                    if response.status_code >= 400:
                        await response.aread()
                        _raise_for_status(
                            response.status_code,
                            response.text_content,
                            self.config.base_url,
                        )
                    async for line in response.aiter_lines():
                        if not line or not line.startswith(_SSE_PREFIX):
                            continue
                        data = line[len(_SSE_PREFIX):].strip()
                        if not data or data == _SSE_DONE:
                            continue
                        try:
                            chunk = json.loads(data)
                        except json.JSONDecodeError:
                            continue
                        for choice in chunk.get("choices") or []:
                            delta = choice.get("delta") or {}
                            if delta.get("tool_calls"):
                                # Partial tool calls are not executable.
                                return
                            piece = delta.get("content")
                            if piece:
                                yield piece
            except httpx.TimeoutException as exc:
                raise ProviderError(
                    f"The provider did not respond within {self.config.timeout:g}s "
                    "while streaming."
                ) from exc
            except httpx.RequestError as exc:
                raise NetworkError(
                    f"Could not reach the provider at {self.config.base_url}: {exc}"
                ) from exc


__all__ = ["OpenAICompatibleProvider"]