-
Notifications
You must be signed in to change notification settings - Fork 44
feat: add LiteLLM as chat model provider for 100+ LLM backends #456
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
Open
RheagalFire
wants to merge
7
commits into
xorbitsai:main
Choose a base branch
from
RheagalFire:feat/add-litellm-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
bcbecd7
feat: add LiteLLM as chat model provider for 100+ LLM backends
RheagalFire 6c109d9
fix: rename LiteLLMLLM to LiteLLMChat
RheagalFire cc9ca25
fix: rename class to LiteLLM
RheagalFire 4455ca0
test: add 20 unit tests for LiteLLM provider
RheagalFire 67988a5
fix: address gemini review - add raw field, use ChunkType.TOKEN, guar…
RheagalFire 242e489
fix: full stream_chat tool call support and raise on empty choices
RheagalFire dbc2cb3
fix: isort import ordering and stream_chat tool call formatting
RheagalFire 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,239 @@ | ||
| import logging | ||
| from typing import Any, AsyncIterator, Dict, List, Optional, Union | ||
|
|
||
| from ..exceptions import LLMRetryableError, LLMTimeoutError | ||
| from ..timeout_config import TimeoutConfig | ||
| from ..token_context import add_token_usage | ||
| from ..types import ChunkType, StreamChunk | ||
| from .base import BaseLLM | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class LiteLLM(BaseLLM): | ||
| """ | ||
| LiteLLM client providing access to 100+ LLM providers through a unified interface. | ||
| Uses provider-prefixed model names (e.g. openai/gpt-4o, anthropic/claude-sonnet-4-6). | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| model_name: str = "openai/gpt-4o-mini", | ||
| api_key: Optional[str] = None, | ||
| api_base: Optional[str] = None, | ||
| default_temperature: Optional[float] = None, | ||
| default_max_tokens: Optional[int] = None, | ||
| timeout: float = 180.0, | ||
| abilities: Optional[List[str]] = None, | ||
| timeout_config: Optional[TimeoutConfig] = None, | ||
| ): | ||
| self._model_name = model_name | ||
| self._api_key = api_key | ||
| self._api_base = api_base | ||
| self.default_temperature = default_temperature | ||
| self.default_max_tokens = default_max_tokens | ||
| self.timeout = timeout | ||
| self.timeout_config = timeout_config or TimeoutConfig() | ||
|
|
||
| if abilities: | ||
| self._abilities = abilities | ||
| else: | ||
| self._abilities = ["chat", "tool_calling"] | ||
|
|
||
| @property | ||
| def model_name(self) -> str: | ||
| return self._model_name | ||
|
|
||
| @property | ||
| def abilities(self) -> List[str]: | ||
| return self._abilities | ||
|
|
||
| @property | ||
| def supports_thinking_mode(self) -> bool: | ||
| return False | ||
|
|
||
| async def chat( | ||
| self, | ||
| messages: List[Dict[str, str]], | ||
| temperature: Optional[float] = None, | ||
| max_tokens: Optional[int] = None, | ||
| tools: Optional[List[Dict[str, Any]]] = None, | ||
| tool_choice: Optional[Union[str, Dict[str, Any]]] = None, | ||
| response_format: Optional[Dict[str, Any]] = None, | ||
| thinking: Optional[Dict[str, Any]] = None, | ||
| output_config: Optional[Dict[str, Any]] = None, | ||
| **kwargs: Any, | ||
| ) -> Any: | ||
| """Perform a chat completion via LiteLLM.""" | ||
| import litellm | ||
|
|
||
| completion_params: Dict[str, Any] = { | ||
| "model": self._model_name, | ||
| "messages": self._sanitize_unicode_content(messages), | ||
| "drop_params": True, | ||
| "timeout": self.timeout, | ||
| **kwargs, | ||
| } | ||
|
|
||
| if max_tokens is not None: | ||
| completion_params["max_tokens"] = max_tokens | ||
| elif self.default_max_tokens is not None: | ||
| completion_params["max_tokens"] = self.default_max_tokens | ||
|
|
||
| if temperature is not None: | ||
| completion_params["temperature"] = temperature | ||
| elif self.default_temperature is not None: | ||
| completion_params["temperature"] = self.default_temperature | ||
|
|
||
| if tools: | ||
| completion_params["tools"] = tools | ||
| if tool_choice: | ||
| completion_params["tool_choice"] = tool_choice | ||
| if response_format: | ||
| completion_params["response_format"] = response_format | ||
|
|
||
| if self._api_key: | ||
| completion_params["api_key"] = self._api_key | ||
| if self._api_base: | ||
| completion_params["api_base"] = self._api_base | ||
|
|
||
| try: | ||
| response = await litellm.acompletion(**completion_params) | ||
| except litellm.Timeout as e: | ||
| raise LLMTimeoutError(str(e)) from e | ||
| except ( | ||
| litellm.RateLimitError, | ||
| litellm.APIConnectionError, | ||
| litellm.ServiceUnavailableError, | ||
| litellm.InternalServerError, | ||
| ) as e: | ||
| raise LLMRetryableError(str(e)) from e | ||
|
|
||
| if not response.choices: | ||
| raise LLMRetryableError("LiteLLM returned an empty response (no choices).") | ||
| choice = response.choices[0] | ||
| message = choice.message | ||
|
|
||
| if hasattr(response, "usage") and response.usage: | ||
| add_token_usage( | ||
| input_tokens=getattr(response.usage, "prompt_tokens", 0) or 0, | ||
| output_tokens=getattr(response.usage, "completion_tokens", 0) or 0, | ||
| ) | ||
|
|
||
| if hasattr(message, "tool_calls") and message.tool_calls: | ||
| tool_calls = [] | ||
| for tc in message.tool_calls: | ||
| tool_calls.append( | ||
| { | ||
| "id": tc.id, | ||
| "type": "function", | ||
| "function": { | ||
| "name": tc.function.name, | ||
| "arguments": tc.function.arguments, | ||
| }, | ||
| } | ||
| ) | ||
| return { | ||
| "type": "tool_call", | ||
| "tool_calls": tool_calls, | ||
| "raw": response.model_dump() | ||
| if hasattr(response, "model_dump") | ||
| else str(response), | ||
| } | ||
|
|
||
| return message.content or "" | ||
|
|
||
| async def stream_chat( | ||
| self, | ||
| messages: List[Dict[str, str]], | ||
| temperature: Optional[float] = None, | ||
| max_tokens: Optional[int] = None, | ||
| tools: Optional[List[Dict[str, Any]]] = None, | ||
| tool_choice: Optional[Union[str, Dict[str, Any]]] = None, | ||
| response_format: Optional[Dict[str, Any]] = None, | ||
| **kwargs: Any, | ||
| ) -> AsyncIterator[StreamChunk]: | ||
| """Stream a chat completion via LiteLLM.""" | ||
| import litellm | ||
|
|
||
| completion_params: Dict[str, Any] = { | ||
| "model": self._model_name, | ||
| "messages": self._sanitize_unicode_content(messages), | ||
| "stream": True, | ||
| "drop_params": True, | ||
| "timeout": self.timeout, | ||
| **kwargs, | ||
| } | ||
|
|
||
| if max_tokens is not None: | ||
| completion_params["max_tokens"] = max_tokens | ||
| elif self.default_max_tokens is not None: | ||
| completion_params["max_tokens"] = self.default_max_tokens | ||
|
|
||
| if temperature is not None: | ||
| completion_params["temperature"] = temperature | ||
| elif self.default_temperature is not None: | ||
| completion_params["temperature"] = self.default_temperature | ||
|
|
||
| if tools: | ||
| completion_params["tools"] = tools | ||
| if tool_choice: | ||
| completion_params["tool_choice"] = tool_choice | ||
| if response_format: | ||
| completion_params["response_format"] = response_format | ||
|
|
||
| if self._api_key: | ||
| completion_params["api_key"] = self._api_key | ||
| if self._api_base: | ||
| completion_params["api_base"] = self._api_base | ||
|
|
||
| try: | ||
| response = await litellm.acompletion(**completion_params) | ||
| except litellm.Timeout as e: | ||
| raise LLMTimeoutError(str(e)) from e | ||
| except ( | ||
| litellm.RateLimitError, | ||
| litellm.APIConnectionError, | ||
| litellm.ServiceUnavailableError, | ||
| litellm.InternalServerError, | ||
| ) as e: | ||
| raise LLMRetryableError(str(e)) from e | ||
|
|
||
| async for chunk in response: | ||
| if not chunk.choices: | ||
| continue | ||
| delta = chunk.choices[0].delta | ||
|
|
||
| content = delta.content if hasattr(delta, "content") else None | ||
| if content: | ||
| yield StreamChunk(type=ChunkType.TOKEN, content=content, delta=content) | ||
|
|
||
| if hasattr(delta, "tool_calls") and delta.tool_calls: | ||
| tool_calls = [] | ||
| for tc in delta.tool_calls: | ||
| tool_calls.append( | ||
| { | ||
| "index": tc.index | ||
| if hasattr(tc, "index") and tc.index is not None | ||
| else 0, | ||
| "id": tc.id if hasattr(tc, "id") else None, | ||
| "type": "function", | ||
| "function": { | ||
| "name": tc.function.name | ||
| if hasattr(tc, "function") | ||
| and hasattr(tc.function, "name") | ||
| else None, | ||
| "arguments": tc.function.arguments | ||
| if hasattr(tc, "function") | ||
| and hasattr(tc.function, "arguments") | ||
| else "", | ||
| }, | ||
| } | ||
| ) | ||
| yield StreamChunk( | ||
| type=ChunkType.TOOL_CALL, | ||
| tool_calls=tool_calls, | ||
| raw=chunk.model_dump() | ||
| if hasattr(chunk, "model_dump") | ||
| else str(chunk), | ||
| ) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code accesses
response.choices[0]without verifying thatchoicesis not empty. While LiteLLM typically returns at least one choice on success, certain edge cases (like content filtering or provider-specific errors) could result in an empty list, leading to anIndexError.