-
Notifications
You must be signed in to change notification settings - Fork 813
feat(llm): add default framework with OpenAI-compatible client #1797
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 17 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
c5d535d
feat(exceptions): add LLM client error hierarchy
Pouyanpi 22e86ca
feat(llm): add spec-compliant SSE decoder
Pouyanpi 09ce687
feat(llm): add HTTP transport for OpenAI-compatible APIs
Pouyanpi ef2e6d9
feat(llm): add OpenAIChatModel implementing LLMModel protocol
Pouyanpi 2842dbb
test(llm): add recorded fixtures and live integration tests
Pouyanpi 26f604b
feat(llm): add DefaultFramework with client pooling and stream pipeline
Pouyanpi ef85c75
refactor(tests): switch default framework and migrate tests to LLMModel
Pouyanpi 1e0e770
review: apply review suggestions
Pouyanpi ab89ba3
reviw: index by dict
Pouyanpi d7c5d3e
address further review suggestions by greptileai
Pouyanpi 4366839
address review suggestions by Tim
Pouyanpi aab7923
address review suggestions by coderabbit
Pouyanpi 46c4d0b
address review suggestions by greptileai
Pouyanpi 2631a36
fix: make reset framework async
Pouyanpi b506b00
refactor(llm/clients): move provider_name to OpenAIChatModel
Pouyanpi fe06537
refactor(llm/clients): drop payload peek from BaseClient._error_context
Pouyanpi 178f3da
feat(llm/clients): warn on plaintext HTTP with api_key
Pouyanpi a68a1fb
Update nemoguardrails/llm/clients/_errors.py
Pouyanpi a5ce8e7
fix(llm/default_framework): use stable string repr for cache key to a…
Pouyanpi 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 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. | ||
|
|
||
| from nemoguardrails.llm.clients.openai_compatible import OpenAICompatibleClient | ||
|
|
||
| __all__ = ["OpenAICompatibleClient"] |
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,206 @@ | ||
| # SPDX-FileCopyrightText: Copyright (c) 2026 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. | ||
|
|
||
| import json | ||
| import re | ||
| from dataclasses import dataclass | ||
| from datetime import datetime, timezone | ||
| from email.utils import parsedate_to_datetime | ||
| from typing import Any, Dict, Optional, Tuple | ||
|
|
||
| from nemoguardrails.exceptions import ( | ||
| LLMAuthenticationError, | ||
| LLMBadRequestError, | ||
| LLMClientError, | ||
| LLMContextWindowError, | ||
| LLMRateLimitError, | ||
| LLMServerError, | ||
| LLMTimeoutError, | ||
| LLMUnsupportedParamsError, | ||
| ) | ||
|
|
||
| _CONTEXT_WINDOW_KEYWORDS = [ | ||
|
Pouyanpi marked this conversation as resolved.
|
||
| "context length", | ||
| "context_length", | ||
| "context window", | ||
| "maximum token", | ||
| "max_tokens", | ||
| "too many tokens", | ||
| "token limit", | ||
| ] | ||
|
|
||
| _UNSUPPORTED_PARAMS_KEYWORDS = [ | ||
| "unsupported parameter", | ||
| "is not supported", | ||
| "not allowed", | ||
|
Pouyanpi marked this conversation as resolved.
Outdated
|
||
| "unknown parameter", | ||
| "unrecognized parameter", | ||
| ] | ||
|
|
||
| _SECRET_PATTERN = re.compile(r"(sk-|nvapi-|AIza|bearer\s+)\S+", re.IGNORECASE) | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class ErrorContext: | ||
| model_name: Optional[str] = None | ||
| provider_name: Optional[str] = None | ||
| base_url: Optional[str] = None | ||
|
|
||
| def as_kwargs(self) -> Dict[str, Any]: | ||
| return { | ||
| "model_name": self.model_name, | ||
| "provider_name": self.provider_name, | ||
| "base_url": self.base_url, | ||
| } | ||
|
|
||
|
|
||
| _EMPTY_CONTEXT = ErrorContext() | ||
|
|
||
|
|
||
| def _redact_secrets(text: str) -> str: | ||
| return _SECRET_PATTERN.sub(lambda m: m.group(1) + "***", text) | ||
|
|
||
|
|
||
| def _parse_retry_after_value(value: Any) -> Optional[float]: | ||
| try: | ||
| return float(value) | ||
| except (TypeError, ValueError): | ||
| pass | ||
| try: | ||
| parsed = parsedate_to_datetime(str(value)) | ||
| except (TypeError, ValueError): | ||
| return None | ||
| if parsed.tzinfo is None: | ||
| parsed = parsed.replace(tzinfo=timezone.utc) | ||
| return (parsed - datetime.now(tz=timezone.utc)).total_seconds() | ||
|
|
||
|
|
||
| def _parse_retry_after(headers: Any) -> Optional[float]: | ||
| raw = headers.get("retry-after") if headers else None | ||
| if not raw: | ||
| return None | ||
| return _parse_retry_after_value(raw) | ||
|
|
||
|
|
||
| def _extract_from_parsed_body(parsed_body: Any) -> Tuple[str, Optional[str], Optional[str], Optional[str]]: | ||
| error_message = "" | ||
| error_type = None | ||
| error_code = None | ||
| param = None | ||
| if isinstance(parsed_body, dict): | ||
| error_obj = parsed_body.get("error", {}) | ||
| if isinstance(error_obj, dict): | ||
| error_message = error_obj.get("message", "") or "" | ||
| error_type = error_obj.get("type") | ||
| error_code = error_obj.get("code") | ||
| param = error_obj.get("param") | ||
| elif isinstance(error_obj, str): | ||
| error_message = error_obj | ||
| if not error_message: | ||
| error_message = parsed_body.get("message") or parsed_body.get("detail") or "" | ||
| return error_message, error_type, error_code, param | ||
|
|
||
|
|
||
| def _build_error_fields(parsed_body: Any, raw_body: str, headers: Any, ctx: ErrorContext) -> Tuple[str, Dict[str, Any]]: | ||
| error_message, error_type, error_code, param = _extract_from_parsed_body(parsed_body) | ||
| if not error_message: | ||
| error_message = raw_body or "" | ||
| error_message = _redact_secrets(error_message) | ||
|
Pouyanpi marked this conversation as resolved.
|
||
| kwargs = dict( | ||
| error_type=error_type, | ||
| error_code=error_code, | ||
| param=param, | ||
| body=parsed_body, | ||
| response_headers=dict(headers) if headers else None, | ||
| **ctx.as_kwargs(), | ||
| ) | ||
| return error_message, kwargs | ||
|
|
||
|
|
||
| def _classify_bad_request(status_code: int, error_message: str, kwargs: Dict[str, Any]) -> LLMClientError: | ||
| msg_lower = error_message.lower() | ||
| if any(kw in msg_lower for kw in _CONTEXT_WINDOW_KEYWORDS): | ||
| return LLMContextWindowError(status_code, error_message, **kwargs) | ||
| if any(kw in msg_lower for kw in _UNSUPPORTED_PARAMS_KEYWORDS): | ||
| if "stream_options" in msg_lower: | ||
| error_message = ( | ||
| f"{error_message} (set include_usage_in_stream=False on the model " | ||
| "or in config.yml parameters to remove this field from streaming requests)" | ||
| ) | ||
| return LLMUnsupportedParamsError(status_code, error_message, **kwargs) | ||
| return LLMBadRequestError(status_code, error_message, **kwargs) | ||
|
|
||
|
|
||
| def raise_for_status(status_code: int, body: str, headers: Any, ctx: Optional[ErrorContext] = None) -> None: | ||
| ctx = ctx or _EMPTY_CONTEXT | ||
| try: | ||
| parsed_body = json.loads(body) | ||
| except (json.JSONDecodeError, TypeError): | ||
| parsed_body = None | ||
|
|
||
| error_message, kwargs = _build_error_fields(parsed_body, body, headers, ctx) | ||
| if not error_message: | ||
| error_message = f"HTTP {status_code}" | ||
|
|
||
| if status_code in (401, 403): | ||
| raise LLMAuthenticationError(status_code, error_message, **kwargs) | ||
|
|
||
| if status_code == 408: | ||
| raise LLMTimeoutError(status_code, error_message, **kwargs) | ||
|
|
||
| if status_code == 429: | ||
| retry_after = _parse_retry_after(headers) | ||
| raise LLMRateLimitError(status_code, error_message, **kwargs, retry_after_seconds=retry_after) | ||
|
|
||
| if status_code == 400 or status_code == 422: | ||
| raise _classify_bad_request(status_code, error_message, kwargs) | ||
|
|
||
| if status_code >= 500: | ||
| raise LLMServerError(status_code, error_message, **kwargs) | ||
|
|
||
| raise LLMClientError(status_code, error_message, **kwargs) | ||
|
|
||
|
|
||
| _SSE_ERROR_TYPE_TO_STATUS: Dict[str, int] = { | ||
| "invalid_request_error": 400, | ||
| "authentication_error": 401, | ||
| "permission_error": 403, | ||
| "not_found_error": 404, | ||
| "rate_limit_error": 429, | ||
| "api_error": 500, | ||
| "server_error": 500, | ||
| "overloaded_error": 503, | ||
| } | ||
|
|
||
|
|
||
| def raise_for_sse_error(parsed_payload: Dict[str, Any], headers: Any, ctx: Optional[ErrorContext] = None) -> None: | ||
| ctx = ctx or _EMPTY_CONTEXT | ||
| error_obj = parsed_payload.get("error") | ||
| error_type = error_obj.get("type") if isinstance(error_obj, dict) else None | ||
| error_code = error_obj.get("code") if isinstance(error_obj, dict) else None | ||
|
|
||
| status: Optional[int] = None | ||
| if isinstance(error_type, str) and error_type in _SSE_ERROR_TYPE_TO_STATUS: | ||
| status = _SSE_ERROR_TYPE_TO_STATUS[error_type] | ||
| elif isinstance(error_code, int) and 400 <= error_code < 600: | ||
| status = error_code | ||
|
|
||
| if status is not None: | ||
| raise_for_status(status, json.dumps(parsed_payload), headers, ctx) | ||
|
|
||
| error_message, kwargs = _build_error_fields(parsed_payload, json.dumps(parsed_payload), headers, ctx) | ||
| if not error_message: | ||
| error_message = "Streaming error" | ||
| raise LLMClientError(0, error_message, **kwargs) | ||
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.