-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cross-model fallback support and error handling
- Add model-agnostic fallback system in base ChatModel - Implement error handling with retryable errors - Add model tracking in responses - Add comprehensive test suite for fallback behavior - Update Gemini model to use new error handling system
- Loading branch information
1 parent
f32e8dc
commit 0b36103
Showing
21 changed files
with
1,600 additions
and
167 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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,6 @@ | ||
from .base import ChatModel | ||
from .openai_model import OpenAIChatModel | ||
from .gemini_model import GeminiChatModel | ||
from .anthropic_model import AnthropicChatModel | ||
|
||
__all__ = ["ChatModel", "OpenAIChatModel", "GeminiChatModel", "AnthropicChatModel"] |
This file contains 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,44 @@ | ||
from typing import List, Dict, Any | ||
|
||
from anthropic import Anthropic | ||
|
||
from .base import ChatModel | ||
|
||
class AnthropicChatModel(ChatModel): | ||
def __init__(self, model_name: str = "claude-3-opus-20240229", temperature: float = 0.1): | ||
super().__init__(model_name, temperature) | ||
self.client = Anthropic() | ||
|
||
def generate_response( | ||
self, | ||
messages: List[Dict[str, str]], | ||
max_tokens: int = 1000, | ||
) -> Dict[str, Any]: | ||
# Convert messages to Anthropic format | ||
anthropic_messages = [] | ||
for msg in messages: | ||
role = msg["role"] | ||
if role == "system": | ||
anthropic_messages.append({"role": "assistant", "content": msg["content"]}) | ||
elif role == "user": | ||
anthropic_messages.append({"role": "user", "content": msg["content"]}) | ||
elif role == "assistant": | ||
anthropic_messages.append({"role": "assistant", "content": msg["content"]}) | ||
|
||
response = self.client.messages.create( | ||
model=self.model_name, | ||
messages=anthropic_messages, | ||
temperature=self.temperature, | ||
max_tokens=max_tokens, | ||
) | ||
|
||
return { | ||
"content": response.content[0].text, | ||
"total_tokens": response.usage.input_tokens + response.usage.output_tokens, | ||
"prompt_tokens": response.usage.input_tokens, | ||
"completion_tokens": response.usage.output_tokens, | ||
} | ||
|
||
@property | ||
def system_role_key(self) -> str: | ||
return "system" # Will be converted to assistant role in generate_response |
Oops, something went wrong.