-
Notifications
You must be signed in to change notification settings - Fork 12
feat: add msgspec openai adapter, adapter protocol #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
25e4001
add and use msgspec openai adapter
viraatc 5399801
fix worker
viraatc c32b46a
address comments
viraatc 6677b32
address comments
viraatc 6e6818d
address comments
viraatc 84bb335
address comments
viraatc 0ea42be
address comments
viraatc File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
src/inference_endpoint/endpoint_client/adapter_protocol.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved. | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| """Base class for HTTP request adapters.""" | ||
|
|
||
| import re | ||
| from abc import ABC, abstractmethod | ||
|
|
||
| from inference_endpoint.core.types import Query, QueryResult | ||
|
|
||
|
|
||
| class HttpRequestAdapter(ABC): | ||
| """ | ||
| Abstract base class for HTTP request adapters. | ||
|
|
||
| Adapters convert between internal Query/QueryResult types and | ||
| endpoint-specific formats (e.g., OpenAI, custom formats). | ||
| """ | ||
|
|
||
| # SSE (Server-Sent Events) is an HTTP standard | ||
| # Pre-compiled regex for extracting SSE data fields with JSON content | ||
| # Matches "data: {json content}" and captures the JSON part | ||
| SSE_DATA_PATTERN: re.Pattern[bytes] = re.compile(rb"data:\s*(\{[^\n]+\})") | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def encode_query(query: Query) -> bytes: | ||
| """ | ||
| Encode a Query to bytes for HTTP transmission. | ||
|
|
||
| Args: | ||
| query: Input query with prompt and parameters | ||
|
|
||
| Returns: | ||
| Encoded request bytes ready for HTTP POST | ||
| """ | ||
| raise NotImplementedError("encode_query not implemented") | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def decode_response(response_bytes: bytes, query_id: str) -> QueryResult: | ||
| """ | ||
| Decode HTTP response bytes to QueryResult. | ||
|
|
||
| Args: | ||
| response_bytes: Raw bytes from HTTP response | ||
| query_id: ID for the query (to associate with result) | ||
|
|
||
| Returns: | ||
| QueryResult with extracted content | ||
| """ | ||
| raise NotImplementedError("decode_response not implemented") | ||
|
|
||
| @staticmethod | ||
| @abstractmethod | ||
| def decode_sse_message(json_bytes: bytes) -> str: | ||
| """ | ||
| Decode SSE message and extract content string. | ||
|
|
||
| Args: | ||
| json_bytes: Raw JSON bytes from SSE stream | ||
|
|
||
| Returns: | ||
| Content string from the SSE message | ||
| """ | ||
| raise NotImplementedError("decode_sse_message not implemented") | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.