File size: 9,184 Bytes
1ebb69b
 
 
 
 
 
 
 
 
 
 
dc2e1e1
1ebb69b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Nancy β€” OpenAI-compatible Pydantic v2 schemas.

These models exactly match the OpenAI Chat Completions API response format
so that the official ``openai`` Python SDK works seamlessly.
"""

from __future__ import annotations

import time
import uuid
from typing import Literal

from pydantic import BaseModel, Field


# ── Helpers ───────────────────────────────────────────────────────────────────


def _chatcmpl_id() -> str:
    """Generate an OpenAI-style completion ID."""
    return f"chatcmpl-{uuid.uuid4().hex[:29]}"


def _unix_ts() -> int:
    """Current UTC Unix timestamp."""
    return int(time.time())


# ── Request Models ────────────────────────────────────────────────────────────


class ChatMessage(BaseModel):
    """A single chat message in the OpenAI format."""

    role: Literal["system", "user", "assistant", "function", "tool"] = Field(
        ..., description="The role of the message author."
    )
    content: str | None = Field(
        default=None, description="The text content of the message."
    )
    name: str | None = Field(
        default=None, description="Optional name for the message author."
    )
    tool_call_id: str | None = Field(
        default=None, description="Tool call that this message is responding to."
    )


class ChatCompletionRequest(BaseModel):
    """
    Incoming request body for ``POST /v1/chat/completions``.

    Mirrors the subset of OpenAI fields that Nancy supports.
    """

    model: str = Field(
        ..., description="Model/provider name, e.g. 'chatgpt', 'gemini'."
    )
    messages: list[ChatMessage] = Field(
        ..., min_length=1, description="Conversation messages."
    )
    stream: bool = Field(
        default=False, description="Whether to stream the response via SSE."
    )
    temperature: float | None = Field(
        default=None, ge=0.0, le=2.0, description="Sampling temperature."
    )
    max_tokens: int | None = Field(
        default=None, ge=1, description="Maximum tokens to generate."
    )
    top_p: float | None = Field(
        default=None, ge=0.0, le=1.0, description="Nucleus sampling parameter."
    )
    stop: str | list[str] | None = Field(
        default=None, description="Stop sequences."
    )
    user: str | None = Field(
        default=None, description="End-user identifier for abuse tracking."
    )
    tools: list[dict] | None = Field(
        default=None, description="A list of tools the model may call."
    )
    tool_choice: str | dict | None = Field(
        default=None, description="Controls which (if any) tool is called."
    )


# ── Response Models β€” Non-streaming ──────────────────────────────────────────


class UsageInfo(BaseModel):
    """Token usage statistics."""

    prompt_tokens: int = 0
    completion_tokens: int = 0
    total_tokens: int = 0


class ChoiceMessage(BaseModel):
    """The assistant's response message in a non-streaming completion."""

    role: Literal["assistant"] = "assistant"
    content: str | None = ""
    tool_calls: list[dict] | None = Field(
        default=None, description="The tool calls generated by the model."
    )


class Choice(BaseModel):
    """A single choice in a non-streaming completion response."""

    index: int = 0
    message: ChoiceMessage = Field(default_factory=ChoiceMessage)
    finish_reason: Literal["stop", "length", "content_filter", "tool_calls"] | None = None


class ChatCompletionResponse(BaseModel):
    """
    Non-streaming response for ``POST /v1/chat/completions``.

    Matches ``openai.types.chat.ChatCompletion``.
    """

    id: str = Field(default_factory=_chatcmpl_id)
    object: Literal["chat.completion"] = "chat.completion"
    created: int = Field(default_factory=_unix_ts)
    model: str = ""
    choices: list[Choice] = Field(default_factory=lambda: [Choice()])
    usage: UsageInfo = Field(default_factory=UsageInfo)
    system_fingerprint: str | None = None

    @classmethod
    def from_content(
        cls,
        content: str,
        model: str,
        finish_reason: str = "stop",
    ) -> ChatCompletionResponse:
        """Build a complete response from a single content string."""
        return cls(
            model=model,
            choices=[
                Choice(
                    index=0,
                    message=ChoiceMessage(content=content),
                    finish_reason=finish_reason,  # type: ignore[arg-type]
                )
            ],
            usage=UsageInfo(
                prompt_tokens=0,
                completion_tokens=len(content.split()),
                total_tokens=len(content.split()),
            ),
        )


# ── Response Models β€” Streaming (SSE chunks) ─────────────────────────────────


class DeltaContent(BaseModel):
    """
    Delta object inside a streaming chunk.

    On the first chunk, ``role`` is set to ``"assistant"`` with no content.
    On subsequent chunks, ``content`` carries the text fragment.
    On the final chunk, both may be absent (empty delta).
    """

    role: Literal["assistant"] | None = None
    content: str | None = None
    tool_calls: list[dict] | None = Field(
        default=None, description="The tool calls generated by the model."
    )


class StreamChoice(BaseModel):
    """A single choice in a streaming chunk."""

    index: int = 0
    delta: DeltaContent = Field(default_factory=DeltaContent)
    finish_reason: Literal["stop", "length", "content_filter", "tool_calls"] | None = None


class ChatCompletionChunk(BaseModel):
    """
    A single SSE chunk for streaming ``POST /v1/chat/completions``.

    Matches ``openai.types.chat.ChatCompletionChunk``.
    """

    id: str = Field(default_factory=_chatcmpl_id)
    object: Literal["chat.completion.chunk"] = "chat.completion.chunk"
    created: int = Field(default_factory=_unix_ts)
    model: str = ""
    choices: list[StreamChoice] = Field(default_factory=lambda: [StreamChoice()])
    system_fingerprint: str | None = None

    def to_sse_data(self) -> str:
        """Serialize to the JSON string used in ``data: ...`` SSE frames."""
        return self.model_dump_json(exclude_none=False)

    # ── Convenience factories ─────────────────────────────────────────

    @classmethod
    def first_chunk(cls, completion_id: str, model: str) -> ChatCompletionChunk:
        """Role-only opening chunk (no content)."""
        return cls(
            id=completion_id,
            model=model,
            choices=[
                StreamChoice(
                    delta=DeltaContent(role="assistant"),
                    finish_reason=None,
                )
            ],
        )

    @classmethod
    def content_chunk(
        cls, completion_id: str, model: str, content: str
    ) -> ChatCompletionChunk:
        """A chunk carrying a text fragment."""
        return cls(
            id=completion_id,
            model=model,
            choices=[
                StreamChoice(
                    delta=DeltaContent(content=content),
                    finish_reason=None,
                )
            ],
        )

    @classmethod
    def final_chunk(
        cls,
        completion_id: str,
        model: str,
        finish_reason: str = "stop",
    ) -> ChatCompletionChunk:
        """Terminal chunk with ``finish_reason`` and empty delta."""
        return cls(
            id=completion_id,
            model=model,
            choices=[
                StreamChoice(
                    delta=DeltaContent(),
                    finish_reason=finish_reason,  # type: ignore[arg-type]
                )
            ],
        )


# ── /v1/models response ──────────────────────────────────────────────────────


class ModelInfo(BaseModel):
    """A single model entry returned by ``GET /v1/models``."""

    id: str
    object: Literal["model"] = "model"
    created: int = Field(default_factory=_unix_ts)
    owned_by: str = "nancy"


class ModelListResponse(BaseModel):
    """Response body for ``GET /v1/models``."""

    object: Literal["list"] = "list"
    data: list[ModelInfo] = Field(default_factory=list)


# ── Error response ────────────────────────────────────────────────────────────


class ErrorDetail(BaseModel):
    """OpenAI-style error detail."""

    message: str
    type: str = "invalid_request_error"
    param: str | None = None
    code: str | None = None


class ErrorResponse(BaseModel):
    """OpenAI-style error envelope."""

    error: ErrorDetail