Refactor code structure and imports in simplemind package

This commit is contained in:
2024-10-28 16:31:50 -04:00
parent 25b523e372
commit 3e3457d168
10 changed files with 24 additions and 14 deletions
+1 -1
View File
@@ -1,7 +1,7 @@
from typing import Dict, Any, Optional
from .models import AIResponse
from ..concepts.context import Context
from ..integrations.base import BaseClientProvider
from ..providers.base import BaseClientProvider
class SimpleMind:
+8 -6
View File
@@ -1,11 +1,11 @@
from typing import Optional
from simplemind.core.models import Conversation, AIResponse
from simplemind.concepts.context import Context
from simplemind.integrations.openai import OpenAI
from simplemind.integrations.anthropic import Anthropic
from simplemind.providers.openai import OpenAI
from simplemind.providers.anthropic import Anthropic
import logging
logger = logging.getLogger(__name__)
from .errors import ProviderError
from .logger import logger
class Client:
@@ -20,7 +20,9 @@ class Client:
"anthropic": Anthropic(api_key=self.api_key),
}
def create_conversation(self, provider: str = "openai") -> Conversation:
def create_conversation(
self, provider: str = "openai", context: Optional[Context] = None
) -> Conversation:
if provider not in self.providers:
raise ValueError(f"Provider '{provider}' not supported.")
return self.providers[provider].create_conversation(
@@ -30,7 +32,7 @@ class Client:
def _handle_api_error(self, error: Exception, operation: str):
"""Handle API errors in a consistent way."""
logger.error(f"Error during {operation}: {str(error)}")
raise RuntimeError(f"Failed to {operation}: {str(error)}")
raise ProviderError(f"Failed to {operation}: {str(error)}") from error
def send_message(
self, conversation: Conversation, message: str, provider: str = "openai"
+12 -5
View File
@@ -1,13 +1,20 @@
from pydantic import Field, field_validator
from pydantic_settings import BaseSettings
from typing import Optional
class Settings(BaseSettings):
openai_api_key: Optional[str] = None
anthropic_api_key: Optional[str] = None
ollama_host_url: Optional[str] = None
default_model: str = "gpt-4"
log_level: str = "INFO"
openai_api_key: Optional[str] = Field(None, env="OPENAI_API_KEY")
anthropic_api_key: Optional[str] = Field(None, env="ANTHROPIC_API_KEY")
ollama_host_url: Optional[str] = Field(None, env="OLLAMA_HOST_URL")
default_model: str = Field("gpt-4", env="DEFAULT_MODEL")
log_level: str = Field("INFO", env="LOG_LEVEL")
@field_validator("*", mode="before")
def check_required(cls, v, info):
if info.field_name in info.data and info.data[info.field_name] is None:
raise ValueError(f"{info.field_name} is required")
return v
class Config:
env_file = ".env"
+2 -1
View File
@@ -27,7 +27,8 @@ aiclient = Client(
print(aiclient.available_models)
# Example usage
conversation = aiclient.create_conversation(provider="anthropic", context=ctx)
conversation = aiclient.create_conversation(provider="anthropic")
conversation.set_context(ctx)
response = aiclient.send_message(
conversation, "Who is Kenneth Reitz?", provider="anthropic"
)
+1 -1
View File
@@ -1,6 +1,6 @@
import unittest
from unittest.mock import patch, MagicMock
from simplemind.integrations.openai import OpenAI
from simplemind.providers.openai import OpenAI
from simplemind.core.errors import AuthenticationError, ProviderError