mirror of
https://github.com/kennethreitz/simplemind.git
synced 2026-06-05 22:50:18 +00:00
Refactor code structure and imports in simplemind package
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
from .openai import OpenAI
|
||||
from .anthropic import Anthropic
|
||||
|
||||
providers = [OpenAI]
|
||||
providers = [OpenAI, Anthropic]
|
||||
|
||||
@@ -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
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user