|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +""" |
| 17 | +msgspec types for OpenAI API serialization/deserialization. |
| 18 | +""" |
| 19 | + |
| 20 | +import msgspec |
| 21 | + |
| 22 | +# ============================================================================ |
| 23 | +# SSE (Server-Sent Events) Types for OpenAI streaming format |
| 24 | +# ============================================================================ |
| 25 | + |
| 26 | + |
| 27 | +class SSEDelta(msgspec.Struct): |
| 28 | + """SSE delta object containing content.""" |
| 29 | + |
| 30 | + content: str = "" |
| 31 | + reasoning: str = "" |
| 32 | + |
| 33 | + |
| 34 | +class SSEChoice(msgspec.Struct): |
| 35 | + """SSE choice object containing delta.""" |
| 36 | + |
| 37 | + delta: SSEDelta = msgspec.field(default_factory=SSEDelta) |
| 38 | + finish_reason: str | None = None |
| 39 | + |
| 40 | + |
| 41 | +class SSEMessage(msgspec.Struct): |
| 42 | + """SSE message structure for OpenAI streaming responses.""" |
| 43 | + |
| 44 | + choices: list[SSEChoice] = msgspec.field(default_factory=list) |
| 45 | + |
| 46 | + |
| 47 | +# ============================================================================ |
| 48 | +# OpenAI Chat Completion Types (msgspec-based) |
| 49 | +# ============================================================================ |
| 50 | + |
| 51 | + |
| 52 | +class ChatMessage(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 53 | + """Chat message in OpenAI format.""" |
| 54 | + |
| 55 | + role: str |
| 56 | + content: str |
| 57 | + name: str | None = None |
| 58 | + |
| 59 | + |
| 60 | +class ChatCompletionRequest(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 61 | + """OpenAI chat completion request.""" |
| 62 | + |
| 63 | + model: str |
| 64 | + messages: list[ChatMessage] |
| 65 | + temperature: float | None = None |
| 66 | + max_completion_tokens: int | None = None |
| 67 | + stream: bool | None = None |
| 68 | + top_p: float | None = None |
| 69 | + top_k: int | None = None |
| 70 | + repetition_penalty: float | None = None |
| 71 | + n: int | None = None |
| 72 | + stop: str | list[str] | None = None |
| 73 | + presence_penalty: float | None = None |
| 74 | + frequency_penalty: float | None = None |
| 75 | + logit_bias: dict[str, float] | None = None |
| 76 | + user: str | None = None |
| 77 | + |
| 78 | + |
| 79 | +class ChatCompletionResponseMessage(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 80 | + """Response message from OpenAI.""" |
| 81 | + |
| 82 | + role: str |
| 83 | + content: str | None |
| 84 | + refusal: str | None |
| 85 | + |
| 86 | + |
| 87 | +class ChatCompletionChoice(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 88 | + """A single choice in the completion response.""" |
| 89 | + |
| 90 | + index: int |
| 91 | + message: ChatCompletionResponseMessage |
| 92 | + finish_reason: str | None |
| 93 | + |
| 94 | + |
| 95 | +class CompletionUsage(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 96 | + """Token usage statistics.""" |
| 97 | + |
| 98 | + prompt_tokens: int |
| 99 | + completion_tokens: int |
| 100 | + total_tokens: int |
| 101 | + |
| 102 | + |
| 103 | +class ChatCompletionResponse(msgspec.Struct, kw_only=True, omit_defaults=True): |
| 104 | + """OpenAI chat completion response (msgspec version).""" |
| 105 | + |
| 106 | + id: str |
| 107 | + object: str = "chat.completion" |
| 108 | + created: int |
| 109 | + model: str |
| 110 | + choices: list[ChatCompletionChoice] |
| 111 | + usage: CompletionUsage | None |
| 112 | + system_fingerprint: str | None |
0 commit comments