1 Commits

14 changed files with 18 additions and 117 deletions
-1
View File
@@ -167,4 +167,3 @@ cython_debug/
src/**
requirements.txt
Pipfile
-9
View File
@@ -1,19 +1,10 @@
Release History
===============
## 0.1.6 (2024-10-31)
- Add `sm.Plugin` syntax sugar.
- Improvements to Anthropic provider, related to max tokens.
- General improvements.
- Add tests for structured response.
- Add `llm_model` to `structured_response`.
## 0.1.5 (2024-10-31)
- Add Gemini provider.
- Add structured response to Gemini provider.
- Support for Python 3.10.
## 0.1.4 (2024-10-30)
+5 -4
View File
@@ -6,6 +6,8 @@ Simplemind is AI library designed to simplify your experience with AI APIs in Py
![simplemind](https://github.com/user-attachments/assets/36df2103-2583-4958-ad5e-19cda7740256)
[![Auto Wiki](https://img.shields.io/badge/Auto_Wiki-Mutable.ai-blue)](https://mutable.ai/kennethreitz/simplemind)
## Features
With Simplemind, tapping into AI is as easy as a friendly conversation.
@@ -16,7 +18,7 @@ With Simplemind, tapping into AI is as easy as a friendly conversation.
## Supported APIs
To specify a specific provider or model, you can use the `llm_provider` and `llm_model` parameters when calling: `generate_text`, `generate_data`, or `create_conversation`. The APIs remain identital between all supported providers/models.
To specify a specific provider or model, you can use the `llm_provider` and `llm_model` parameters when calling: `generate_text`, `generate_data`, or `create_conversation`.
- [**Anthropic's Claude**](https://www.anthropic.com/claude)
- [**Google's Gemini**](https://gemini.google/)
@@ -25,7 +27,7 @@ To specify a specific provider or model, you can use the `llm_provider` and `llm
- [**OpenAI's GPT**](https://openai.com/gpt)
- [**xAI's Grok**](https://x.ai/)
If you want to see Simplemind support, additional providers or models, please send a pull request!
If you want to see Simplemind support, additional providers or models, please request a pull!
## Why SimpleMind?
- **Intuitive**: Built with Pythonic simplicity and readability in mind.
@@ -48,7 +50,7 @@ First, authenticate your API keys by setting them in the environment variables:
$ export OPENAI_API_KEY="sk-..."
```
This pattern allows you to keep your API keys private and out of your codebase. Other supported environment variables: `ANTHROPIC_API_KEY`, `XAI_API_KEY`, `GROQ_API_KEY`, and `GEMINI_API_KEY`.
This pattern allows you to keep your API keys private and out of your codebase. Other supported environment variables: `ANTHROPIC_API_KEY`, `XAI_API_KEY`, and `GROQ_API_KEY`.
Next, import Simplemind and start using it:
@@ -217,4 +219,3 @@ Simplemind is licensed under the Apache 2.0 License.
## Acknowledgements
Simplemind is inspired by the philosophy of "code for humans" and aims to make working with AI models accessible to all. Special thanks to the open-source community for their contributions and inspiration.
+1 -1
View File
@@ -16,7 +16,7 @@ import simplemind
project = "simplemind"
copyright = "2024 Kenneth Reitz"
author = "Kenneth Reitz"
release = "v0.1.6"
release = "v0.1.5"
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
+1 -1
View File
@@ -1,6 +1,6 @@
[project]
name = "simplemind"
version = "0.1.6"
version = "0.1.5"
description = "An experimental client for AI providers that intends to replace LangChain and LangGraph for most common use cases."
readme = "README.md"
requires-python = ">=3.10"
+1 -5
View File
@@ -16,7 +16,7 @@ class Session:
self,
*,
llm_provider: str = settings.DEFAULT_LLM_PROVIDER,
llm_model: str | None = None,
llm_model: str = settings.DEFAULT_LLM_MODEL,
**kwargs,
):
self.llm_provider = llm_provider
@@ -113,9 +113,6 @@ def generate_text(
return provider.generate_text(prompt=prompt, llm_model=llm_model, **kwargs)
# Syntax sugar.
Plugin = BasePlugin
__all__ = [
"create_conversation",
"find_provider",
@@ -124,5 +121,4 @@ __all__ = [
"settings",
"BasePlugin",
"Session",
"Plugin",
]
+7 -20
View File
@@ -13,14 +13,12 @@ T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "anthropic"
DEFAULT_MODEL = "claude-3-5-sonnet-20241022"
DEFAULT_MAX_TOKENS = 1_000
DEFAULT_KWARGS = {"max_tokens": DEFAULT_MAX_TOKENS}
DEFAULT_MAX_TOKENS = 1000
class Anthropic(BaseProvider):
NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
def __init__(self, api_key: str | None = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
@@ -48,7 +46,8 @@ class Anthropic(BaseProvider):
response = self.client.messages.create(
model=conversation.llm_model or self.DEFAULT_MODEL,
messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs},
max_tokens=DEFAULT_MAX_TOKENS,
**kwargs,
)
# Get the response content from the Anthropic response
@@ -63,22 +62,9 @@ class Anthropic(BaseProvider):
llm_provider=PROVIDER_NAME,
)
def structured_response(
self, response_model: Type[T], *, llm_model: str | None = None, **kwargs
) -> T:
model = llm_model or self.DEFAULT_MODEL
# Extract the prompt from kwargs if it exists
prompt = kwargs.pop("prompt", kwargs.pop("messages", ""))
# Format the messages properly
messages = [{"role": "user", "content": prompt}]
def structured_response(self, model: str, response_model: Type[T], **kwargs) -> T:
response = self.structured_client.messages.create(
model=model,
messages=messages, # Add the messages parameter
response_model=response_model,
**{**self.DEFAULT_KWARGS, **kwargs},
model=model or self.DEFAULT_MODEL, response_model=response_model, **kwargs
)
return response
@@ -90,7 +76,8 @@ class Anthropic(BaseProvider):
response = self.client.messages.create(
model=llm_model or self.DEFAULT_MODEL,
messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs},
max_tokens=DEFAULT_MAX_TOKENS,
**kwargs,
)
return response.content[0].text
-1
View File
@@ -72,7 +72,6 @@ class Groq(BaseProvider):
response = self.structured_client.chat.completions.create(
messages=messages,
response_model=response_model,
model=kwargs.pop("llm_model", self.DEFAULT_MODEL),
**kwargs,
)
return response
+2 -7
View File
@@ -65,12 +65,7 @@ class Ollama(BaseProvider):
)
def structured_response(
self,
prompt: str,
response_model: Type[T],
*,
llm_model: str | None = None,
**kwargs,
self, prompt: str, response_model: Type[T], *, llm_model: str, **kwargs
) -> T:
"""Get a structured response from the Ollama API."""
messages = [
@@ -85,7 +80,7 @@ class Ollama(BaseProvider):
)
return response
def generate_text(self, prompt: str, *, llm_model: str | None = None) -> str:
def generate_text(self, prompt: str, *, llm_model: str) -> str:
"""Generate text using the Ollama API."""
messages = [
{"role": "user", "content": prompt},
+1
View File
@@ -29,6 +29,7 @@ class Settings(BaseSettings):
)
XAI_API_KEY: Optional[SecretStr] = Field(None, description="API key for xAI")
DEFAULT_LLM_PROVIDER: str = Field("openai", description="The default LLM provider")
DEFAULT_LLM_MODEL: str = Field("gpt-4o-mini", description="The default LLM model")
model_config = SettingsConfigDict(
env_file=".env", env_file_encoding="utf-8", case_sensitive=True, extra="ignore"
-15
View File
@@ -1,15 +0,0 @@
import pytest
import os
import sys
# Add the project root to the Python path.
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from simplemind import Session
@pytest.fixture
def sm():
"""Fixture that provides a simplemind Session instance with default settings."""
return Session()
-2
View File
@@ -1,2 +0,0 @@
def test_basic_math():
assert 1 + 1 == 2
-28
View File
@@ -1,28 +0,0 @@
import pytest
from simplemind.providers import Anthropic, Gemini, OpenAI, Groq, Ollama
from pydantic import BaseModel
class ResponseModel(BaseModel):
result: int
@pytest.mark.parametrize(
"provider_cls",
[
Anthropic,
Gemini,
OpenAI,
Groq,
Ollama,
],
)
def test_generate_data(provider_cls):
provider = provider_cls()
prompt = "What is 2+2?"
data = provider.structured_response(prompt=prompt, response_model=ResponseModel)
assert isinstance(data, ResponseModel)
assert type(data.result) == int
-23
View File
@@ -1,23 +0,0 @@
import pytest
from simplemind.providers import Anthropic, Gemini, OpenAI, Groq, Ollama
@pytest.mark.parametrize(
"provider_cls",
[
Anthropic,
Gemini,
OpenAI,
Groq,
Ollama,
],
)
def test_generate_text(provider_cls):
provider = provider_cls()
prompt = "What is 2+2?"
response = provider.generate_text(prompt=prompt, llm_model=provider.DEFAULT_MODEL)
assert isinstance(response, str)
assert len(response) > 0