From 9251ddda8a7f3c4f45634fa77f3cccdd2b1ebddb Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Mon, 28 Oct 2024 18:30:52 -0400 Subject: [PATCH] Refactor code structure and imports in simplemind package --- simplemind/providers/__init__.py | 3 +- simplemind/providers/anthropic.py | 60 +++++++++++++++++++++++++++++++ t.py | 6 ++-- 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 simplemind/providers/anthropic.py diff --git a/simplemind/providers/__init__.py b/simplemind/providers/__init__.py index f6725d5..3f1ed4a 100644 --- a/simplemind/providers/__init__.py +++ b/simplemind/providers/__init__.py @@ -1,3 +1,4 @@ from .openai import OpenAI +from .anthropic import Anthropic -providers = [OpenAI] +providers = [OpenAI, Anthropic] diff --git a/simplemind/providers/anthropic.py b/simplemind/providers/anthropic.py new file mode 100644 index 0000000..c484425 --- /dev/null +++ b/simplemind/providers/anthropic.py @@ -0,0 +1,60 @@ +import anthropic +import instructor + +# from ..models import Conversation, Message +from ..settings import settings + +PROVIDER_NAME = "anthropic" +DEFAULT_MODEL = "claude-3-5-sonnet-20241022" +DEFAULT_MAX_TOKENS = 1000 + + +class Anthropic: + __name__ = PROVIDER_NAME + DEFAULT_MODEL = DEFAULT_MODEL + + def __init__(self, api_key: str = None): + self.api_key = api_key or settings.ANTHROPIC_API_KEY + + @property + def client(self): + """The raw Anthropic client.""" + return anthropic.Anthropic(api_key=self.api_key) + + @property + def structured_client(self): + """A client patched with Instructor.""" + return instructor.from_anthropic(anthropic.Anthropic(api_key=self.api_key)) + + def send_conversation(self, conversation: "Conversation"): + """Send a conversation to the Anthropic API.""" + from ..models import Message + + messages = [ + {"role": msg.role, "content": msg.text} for msg in conversation.messages + ] + + response = self.client.messages.create( + model=conversation.llm_model or DEFAULT_MODEL, + messages=messages, + max_tokens=DEFAULT_MAX_TOKENS, + ) + + # Get the response content from the OpenAI response + # assistant_message = response.choices[0].message + assistant_message = response.content[0].text + + # Create and return a properly formatted Message instance + return Message( + role="assistant", + text=assistant_message, + raw=response, + llm_model=conversation.llm_model or DEFAULT_MODEL, + llm_provider=PROVIDER_NAME, + ) + + def structured_response(self, model, response_model, **kwargs): + response = self.structured_client.messages.create( + model=model, response_model=response_model, **kwargs + ) + return response diff --git a/t.py b/t.py index b0ecfa4..ff34b99 100644 --- a/t.py +++ b/t.py @@ -1,7 +1,7 @@ import simplemind as sm conversation = sm.create_conversation() -conversation.add_message(role="user", text="Hello, how are you?") -r = conversation.send(llm_model="gpt-4o-mini", llm_provider="openai") +conversation.add_message(role="user", text="Hello, how are you? Do you like poetry?") +r = conversation.send(llm_provider="anthropic") -print(r) +print(r.text)