-
Notifications
You must be signed in to change notification settings - Fork 664
feat: add Anthropic Claude API support #38
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
andrewalufkin
wants to merge
2
commits into
algorithmicsuperintelligence:main
Choose a base branch
from
andrewalufkin:feature/add-anthropic-support
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 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,96 @@ | ||
| """ | ||
| Anthropic Claude API interface for LLMs | ||
| """ | ||
|
|
||
| import asyncio | ||
| import logging | ||
| from typing import Any, Dict, List, Optional | ||
|
|
||
| import anthropic | ||
|
|
||
| from openevolve.config import LLMConfig | ||
| from openevolve.llm.base import LLMInterface | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AnthropicLLM(LLMInterface): | ||
| """LLM interface using Anthropic's Claude API""" | ||
|
|
||
| def __init__( | ||
| self, | ||
| config: LLMConfig, | ||
| model: Optional[str] = None, | ||
| ): | ||
| self.config = config | ||
| self.model = model or config.primary_model | ||
|
|
||
| # Set up API client | ||
| self.client = anthropic.Anthropic( | ||
| api_key=config.api_key, | ||
| base_url=config.api_base, | ||
| ) | ||
|
|
||
| logger.info(f"Initialized Anthropic LLM with model: {self.model}") | ||
|
|
||
| async def generate(self, prompt: str, **kwargs) -> str: | ||
| """Generate text from a prompt""" | ||
| return await self.generate_with_context( | ||
| system_message=self.config.system_message, | ||
| messages=[{"role": "user", "content": prompt}], | ||
| **kwargs, | ||
| ) | ||
|
|
||
| async def generate_with_context( | ||
| self, system_message: str, messages: List[Dict[str, str]], **kwargs | ||
| ) -> str: | ||
| """Generate text using a system message and conversational context""" | ||
| # Prepare messages for Claude format | ||
| formatted_messages = [] | ||
| for msg in messages: | ||
| formatted_messages.append({"role": msg["role"], "content": msg["content"]}) | ||
|
|
||
| # Set up generation parameters | ||
| params = { | ||
| "model": self.model, | ||
| "system": system_message, | ||
| "messages": formatted_messages, | ||
| "max_tokens": kwargs.get("max_tokens", self.config.max_tokens), | ||
| "temperature": kwargs.get("temperature", self.config.temperature), | ||
| "top_p": kwargs.get("top_p", self.config.top_p), | ||
| } | ||
|
|
||
| # Attempt the API call with retries | ||
| retries = kwargs.get("retries", self.config.retries) | ||
| retry_delay = kwargs.get("retry_delay", self.config.retry_delay) | ||
| timeout = kwargs.get("timeout", self.config.timeout) | ||
|
|
||
| for attempt in range(retries + 1): | ||
| try: | ||
| response = await asyncio.wait_for(self._call_api(params), timeout=timeout) | ||
| return response | ||
| except asyncio.TimeoutError: | ||
| if attempt < retries: | ||
| logger.warning(f"Timeout on attempt {attempt + 1}/{retries + 1}. Retrying...") | ||
| await asyncio.sleep(retry_delay) | ||
| else: | ||
| logger.error(f"All {retries + 1} attempts failed with timeout") | ||
| raise | ||
| except Exception as e: | ||
| if attempt < retries: | ||
| logger.warning( | ||
| f"Error on attempt {attempt + 1}/{retries + 1}: {str(e)}. Retrying..." | ||
| ) | ||
| await asyncio.sleep(retry_delay) | ||
| else: | ||
| logger.error(f"All {retries + 1} attempts failed with error: {str(e)}") | ||
| raise | ||
|
|
||
| async def _call_api(self, params: Dict[str, Any]) -> str: | ||
| """Make the actual API call""" | ||
| # Use asyncio to run the blocking API call in a thread pool | ||
| loop = asyncio.get_event_loop() | ||
| response = await loop.run_in_executor(None, lambda: self.client.messages.create(**params)) | ||
|
|
||
| # Extract the response content | ||
| return response.content[0].text |
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,145 @@ | ||
| """ | ||
| Tests for LLM implementations | ||
| """ | ||
|
|
||
| import asyncio | ||
| import unittest | ||
| from unittest.mock import AsyncMock, MagicMock, patch | ||
|
|
||
| from openevolve.config import LLMConfig | ||
| from openevolve.llm.anthropic import AnthropicLLM | ||
| from openevolve.llm.openai import OpenAILLM | ||
|
|
||
|
|
||
| class TestLLMImplementations(unittest.TestCase): | ||
| """Tests for LLM implementations""" | ||
|
|
||
| def setUp(self): | ||
| """Set up test configuration""" | ||
| self.config = LLMConfig( | ||
| primary_model="test-model", | ||
| api_key="test-key", | ||
| api_base="https://test.api", | ||
| ) | ||
|
|
||
| @patch("anthropic.Anthropic") | ||
| async def test_anthropic_llm_generate(self, mock_anthropic): | ||
| """Test Anthropic LLM generate method""" | ||
| # Set up mock response | ||
| mock_response = MagicMock() | ||
| mock_response.content = [MagicMock(text="Test response")] | ||
| mock_anthropic.return_value.messages.create.return_value = mock_response | ||
|
|
||
| # Create LLM instance | ||
| llm = AnthropicLLM(self.config) | ||
|
|
||
| # Test generate | ||
| response = await llm.generate("Test prompt") | ||
| self.assertEqual(response, "Test response") | ||
|
|
||
| # Verify API call | ||
| mock_anthropic.return_value.messages.create.assert_called_once() | ||
| call_args = mock_anthropic.return_value.messages.create.call_args[1] | ||
| self.assertEqual(call_args["model"], "test-model") | ||
| self.assertEqual(call_args["messages"][0]["role"], "user") | ||
| self.assertEqual(call_args["messages"][0]["content"], "Test prompt") | ||
|
|
||
| @patch("anthropic.Anthropic") | ||
| async def test_anthropic_llm_generate_with_context(self, mock_anthropic): | ||
| """Test Anthropic LLM generate_with_context method""" | ||
| # Set up mock response | ||
| mock_response = MagicMock() | ||
| mock_response.content = [MagicMock(text="Test response")] | ||
| mock_anthropic.return_value.messages.create.return_value = mock_response | ||
|
|
||
| # Create LLM instance | ||
| llm = AnthropicLLM(self.config) | ||
|
|
||
| # Test generate_with_context | ||
| messages = [ | ||
| {"role": "user", "content": "Test message 1"}, | ||
| {"role": "assistant", "content": "Test response 1"}, | ||
| {"role": "user", "content": "Test message 2"}, | ||
| ] | ||
| response = await llm.generate_with_context("Test system", messages) | ||
| self.assertEqual(response, "Test response") | ||
|
|
||
| # Verify API call | ||
| mock_anthropic.return_value.messages.create.assert_called_once() | ||
| call_args = mock_anthropic.return_value.messages.create.call_args[1] | ||
| self.assertEqual(call_args["model"], "test-model") | ||
| self.assertEqual(call_args["system"], "Test system") | ||
| self.assertEqual(len(call_args["messages"]), 3) | ||
| self.assertEqual(call_args["messages"][0]["role"], "user") | ||
| self.assertEqual(call_args["messages"][0]["content"], "Test message 1") | ||
|
|
||
| @patch("openai.OpenAI") | ||
| async def test_openai_llm_generate(self, mock_openai): | ||
| """Test OpenAI LLM generate method""" | ||
| # Set up mock response | ||
| mock_response = MagicMock() | ||
| mock_response.choices = [MagicMock(message=MagicMock(content="Test response"))] | ||
| mock_openai.return_value.chat.completions.create.return_value = mock_response | ||
|
|
||
| # Create LLM instance | ||
| llm = OpenAILLM(self.config) | ||
|
|
||
| # Test generate | ||
| response = await llm.generate("Test prompt") | ||
| self.assertEqual(response, "Test response") | ||
|
|
||
| # Verify API call | ||
| mock_openai.return_value.chat.completions.create.assert_called_once() | ||
| call_args = mock_openai.return_value.chat.completions.create.call_args[1] | ||
| self.assertEqual(call_args["model"], "test-model") | ||
| self.assertEqual(call_args["messages"][0]["role"], "user") | ||
| self.assertEqual(call_args["messages"][0]["content"], "Test prompt") | ||
|
|
||
| @patch("openai.OpenAI") | ||
| async def test_openai_llm_generate_with_context(self, mock_openai): | ||
| """Test OpenAI LLM generate_with_context method""" | ||
| # Set up mock response | ||
| mock_response = MagicMock() | ||
| mock_response.choices = [MagicMock(message=MagicMock(content="Test response"))] | ||
| mock_openai.return_value.chat.completions.create.return_value = mock_response | ||
|
|
||
| # Create LLM instance | ||
| llm = OpenAILLM(self.config) | ||
|
|
||
| # Test generate_with_context | ||
| messages = [ | ||
| {"role": "user", "content": "Test message 1"}, | ||
| {"role": "assistant", "content": "Test response 1"}, | ||
| {"role": "user", "content": "Test message 2"}, | ||
| ] | ||
| response = await llm.generate_with_context("Test system", messages) | ||
| self.assertEqual(response, "Test response") | ||
|
|
||
| # Verify API call | ||
| mock_openai.return_value.chat.completions.create.assert_called_once() | ||
| call_args = mock_openai.return_value.chat.completions.create.call_args[1] | ||
| self.assertEqual(call_args["model"], "test-model") | ||
| self.assertEqual(call_args["messages"][0]["role"], "system") | ||
| self.assertEqual(call_args["messages"][0]["content"], "Test system") | ||
| self.assertEqual(len(call_args["messages"]), 4) # system + 3 messages | ||
|
|
||
| def test_llm_config_model_detection(self): | ||
| """Test LLM configuration model type detection""" | ||
| # Test OpenAI model | ||
| config = LLMConfig(primary_model="gpt-4") | ||
| self.assertEqual(config.api_base, "https://api.openai.com/v1") | ||
|
|
||
| # Test Claude model | ||
| config = LLMConfig(primary_model="claude-3-sonnet") | ||
| self.assertEqual(config.api_base, "https://api.anthropic.com/v1") | ||
|
|
||
| # Test custom API base | ||
| config = LLMConfig( | ||
| primary_model="claude-3-sonnet", | ||
| api_base="https://custom.api", | ||
| ) | ||
| self.assertEqual(config.api_base, "https://custom.api") | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
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.