2 Commits

37 changed files with 196 additions and 1362 deletions
+2 -4
View File
@@ -1,6 +1,4 @@
export ANTHROPIC_API_KEY=""
export GEMINI_API_KEY=""
export GROQ_API_KEY=""
export OLLAMA_HOST_URL=""
export OPENAI_API_KEY="" export OPENAI_API_KEY=""
export ANTHROPIC_API_KEY=""
export XAI_API_KEY="" export XAI_API_KEY=""
export GROQ_API_KEY=""
-2
View File
@@ -166,5 +166,3 @@ cython_debug/
.env .env
src/** src/**
requirements.txt
Pipfile
-30
View File
@@ -1,36 +1,6 @@
Release History 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)
- Introduce `Session` class to manage repeatability.
- General improvements.
## 0.1.3 (2024-10-30)
- Make Conversation a context manager.
- Add more robust conversation plugin hooks — replace `send_hook` with `pre_send_hook` and `post_send_hook`.
- Change plugin hooks to try/except NotImplementedError.
- Implement 'did you mean' with provider names. Can do this eventually with model names, as well.
## 0.1.2 (2024-10-29)
- Add ollama provider.
## 0.1.1 (2024-10-29) ## 0.1.1 (2024-10-29)
- Fix Groq provider. - Fix Groq provider.
+12
View File
@@ -0,0 +1,12 @@
FROM python:3.12.0
RUN apt-get update -y && apt-get upgrade -y
RUN pip install --upgrade pip
COPY requirements.txt /src/requirements.txt
WORKDIR /src
RUN pip install -r requirements.txt
ENTRYPOINT ["python", "build.py"]
+28 -56
View File
@@ -1,46 +1,38 @@
# Simplemind: AI for Humans™ # SimpleMind: AI for Humans™
**Keep it simple, keep it human.** [![Auto Wiki](https://img.shields.io/badge/Auto_Wiki-Mutable.ai-blue)](https://mutable.ai/kennethreitz/simplemind)
Simplemind is AI library designed to simplify your experience with AI APIs in Python. Inspired by a "for humans" philosophy, it abstracts away complexity, giving developers an intuitive and human-friendly way to interact with powerful AI capabilities. SimpleMind is an AI library designed to simplify your experience with AI APIs in Python. Inspired by a "for humans" philosophy, it abstracts away complexity, giving developers an intuitive and human-friendly way to interact with powerful AI capabilities. With SimpleMind, tapping into AI is as easy as a friendly conversation.
![simplemind](https://github.com/user-attachments/assets/36df2103-2583-4958-ad5e-19cda7740256) ```bash
$ pip install simplemind
```
## Features ## Features
With Simplemind, tapping into AI is as easy as a friendly conversation.
- **Easy-to-use AI tools**: SimpleMind provides simple interfaces to popular AI services. - **Easy-to-use AI tools**: SimpleMind provides simple interfaces to popular AI services.
- **Human-centered design**: The library prioritizes readability and usability—no need to be an expert to start experimenting. - **Human-centered design**: The library prioritizes readability and usability—no need to be an expert to start experimenting.
- **Minimal configuration**: Get started quickly, without worrying about configuration headaches. - **Minimal configuration**: Get started quickly, without worrying about configuration headaches.
## Supported APIs ## 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) - **[OpenAI's GPT](https://openai.com/gpt)**
- [**Google's Gemini**](https://gemini.google/) - **[Anthropic's Claude](https://www.anthropic.com/claude)**
- [**Groq's Groq**](https://groq.com/) - **[xAI's Grok](https://x.ai/)**
- [**Ollama**](https://ollama.com) - **[Groq's Groq](https://groq.com/)**
- [**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'd like to see SimpleMind support additional providers or models, please send a pull request!
## Why SimpleMind? ## Why SimpleMind?
- **Intuitive**: Built with Pythonic simplicity and readability in mind. - **Intuitive**: Built with Pythonic simplicity and readability in mind.
- **For Humans**: Emphasizes a human-friendly interface, just like `requests` for HTTP. - **For Humans**: Emphasizes a human-friendly interface, just like `requests` for HTTP.
- **Open Source**: Simplemind is open source, and contributions are always welcome! - **Open Source**: SimpleMind is open source, and contributions are always welcome!
Also, why not? :)
## Quickstart ## Quickstart
Simplemind takes care of the complex API calls so you can focus on what matters—building, experimenting, and creating. SimpleMind takes care of the complex API calls so you can focus on what matters—building, experimenting, and creating.
```bash
$ pip install simplemind
```
First, authenticate your API keys by setting them in the environment variables: First, authenticate your API keys by setting them in the environment variables:
@@ -48,9 +40,9 @@ First, authenticate your API keys by setting them in the environment variables:
$ export OPENAI_API_KEY="sk-..." $ 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`, `GROK_API_KEY`, `XAI_API_KEY`, and `GROQ_API_KEY`.
Next, import Simplemind and start using it: Next, import SimpleMind and start using it:
```python ```python
import simplemind as sm import simplemind as sm
@@ -59,7 +51,7 @@ import simplemind as sm
## Examples ## Examples
Here are some examples of how to use Simplemind: Here are some examples of how to use SimpleMind:
### Text Completion ### Text Completion
@@ -112,34 +104,15 @@ To continue the conversation, you can call `conversation.send()` again, which re
<Message role=assistant text="The meaning of life is a profound philosophical question that has been explored by cultures, religions, and philosophers for centuries. Different people and belief systems offer varying interpretations:\n\n1. **Religious Perspectives:** Many religions propose that the meaning of life is to fulfill a divine purpose, serve God, or reach an afterlife. For example, Christianity often emphasizes love, faith, and service to God and others as central to lifes meaning.\n\n2. **Philosophical Views:** Philosophers offer diverse answers. Existentialists like Jean-Paul Sartre argue that life has no inherent meaning, and it is up to individuals to create their own purpose. Others, like Aristotle, suggest that achieving eudaimonia (flourishing or happiness) through virtuous living is the key to a meaningful life.\n\n3. **Scientific and Secular Approaches:** Some people find meaning through understanding the natural world, contributing to human knowledge, or through personal accomplishments and happiness. They may view lifes meaning as a product of connection, legacy, or the pursuit of knowledge and creativity.\n\n4. **Personal Perspective:** For many, the meaning of life is deeply personal, involving their relationships, passions, and goals. These individuals define lifes purpose through experiences, connections, and the impact they have on others and the world.\n\nUltimately, the meaning of life is a subjective question, with each person finding their own answers based on their beliefs, experiences, and reflections."> <Message role=assistant text="The meaning of life is a profound philosophical question that has been explored by cultures, religions, and philosophers for centuries. Different people and belief systems offer varying interpretations:\n\n1. **Religious Perspectives:** Many religions propose that the meaning of life is to fulfill a divine purpose, serve God, or reach an afterlife. For example, Christianity often emphasizes love, faith, and service to God and others as central to lifes meaning.\n\n2. **Philosophical Views:** Philosophers offer diverse answers. Existentialists like Jean-Paul Sartre argue that life has no inherent meaning, and it is up to individuals to create their own purpose. Others, like Aristotle, suggest that achieving eudaimonia (flourishing or happiness) through virtuous living is the key to a meaningful life.\n\n3. **Scientific and Secular Approaches:** Some people find meaning through understanding the natural world, contributing to human knowledge, or through personal accomplishments and happiness. They may view lifes meaning as a product of connection, legacy, or the pursuit of knowledge and creativity.\n\n4. **Personal Perspective:** For many, the meaning of life is deeply personal, involving their relationships, passions, and goals. These individuals define lifes purpose through experiences, connections, and the impact they have on others and the world.\n\nUltimately, the meaning of life is a subjective question, with each person finding their own answers based on their beliefs, experiences, and reflections.">
``` ```
### Stop Repeating Yourself
You can use the `Session` class to set default parameters for all calls:
```python
# Create a session with defaults
gpt_4o_mini = sm.Session(llm_provider="openai", llm_model="gpt-4o-mini")
# Now all calls use these defaults
response = gpt_4o_mini.generate_text("Hello!")
conversation = gpt_4o_mini.create_conversation()
```
This maintains the simplicity of the original API while reducing repetition. The session object also supports overriding defaults on a per-call basis:
```python
response = gpt_4o_mini.generate_text(
"Complex task here",
llm_model="gpt-4"
)
```
### Basic Memory Plugin ### Basic Memory Plugin
Harnessing the power of Python, you can easily create your own plugins to add additional functionality to your conversations: Harnessing the power of Python, you can easily create your own plugins to add additional functionality to your conversations:
```python ```python
class SimpleMemoryPlugin(sm.BasePlugin): import simplemind as sm
class SimpleMemoryPlugin:
def __init__(self): def __init__(self):
self.memories = [ self.memories = [
"the earth has fictionally beeen destroyed.", "the earth has fictionally beeen destroyed.",
@@ -149,7 +122,7 @@ class SimpleMemoryPlugin(sm.BasePlugin):
def yield_memories(self): def yield_memories(self):
return (m for m in self.memories) return (m for m in self.memories)
def pre_send_hook(self, conversation: sm.Conversation): def send_hook(self, conversation: sm.Conversation):
for m in self.yield_memories(): for m in self.yield_memories():
conversation.add_message(role="system", text=m) conversation.add_message(role="system", text=m)
@@ -160,7 +133,7 @@ conversation.add_plugin(SimpleMemoryPlugin())
conversation.add_message( conversation.add_message(
role="user", role="user",
text="Please write a poem about the moon", text="Write a poem about the moon",
) )
``` ```
```pycon ```pycon
@@ -196,12 +169,8 @@ A reminder that in tales and fun,
The universe is never done. The universe is never done.
``` ```
Simple, yet effective.
Please see the [examples](examples) directory for executable examples. Please see the [examples](examples) directory for executable examples.
-------------------
## Contributing ## Contributing
We welcome contributions of all kinds. Feel free to open issues for bug reports or feature requests, and submit pull requests to make SimpleMind even better. We welcome contributions of all kinds. Feel free to open issues for bug reports or feature requests, and submit pull requests to make SimpleMind even better.
@@ -213,8 +182,11 @@ To get started:
4. Submit a pull request. 4. Submit a pull request.
## License ## License
Simplemind is licensed under the Apache 2.0 License. SimpleMind is licensed under the Apache 2.0 License.
## Acknowledgements ## 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. 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.
---------------
SimpleMind: Keep it simple, keep it human.
+10
View File
@@ -0,0 +1,10 @@
services:
simplemind:
build:
context: .
dockerfile: Dockerfile
volumes:
- ./simplemind:/src/simplemind
- ./build.py:/src/build.py
env_file:
- .env
-20
View File
@@ -1,20 +0,0 @@
# Minimal makefile for Sphinx documentation
#
# You can set these variables from the command line, and also
# from the environment for the first two.
SPHINXOPTS ?=
SPHINXBUILD ?= sphinx-build
SOURCEDIR = .
BUILDDIR = _build
# Put it first so that "make" without argument is like "make help".
help:
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
.PHONY: help Makefile
# Catch-all target: route all unknown targets to Sphinx using the new
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS).
%: Makefile
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O)
-34
View File
@@ -1,34 +0,0 @@
# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information
import os
import sys
sys.path.insert(0, os.path.abspath(".."))
import simplemind
project = "simplemind"
copyright = "2024 Kenneth Reitz"
author = "Kenneth Reitz"
release = "v0.1.6"
# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration
extensions = ["sphinx.ext.autodoc"]
templates_path = ["_templates"]
exclude_patterns = ["_build", "Thumbs.db", ".DS_Store"]
# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output
html_theme = "alabaster"
html_static_path = ["_static"]
-236
View File
@@ -1,236 +0,0 @@
.. simplemind documentation master file, created by
sphinx-quickstart on Wed Oct 30 08:08:14 2024.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
SimpleMind: AI for Humans™
==========================
**SimpleMind** is a versatile Python library designed to simplify interactions with various AI models. It provides a consistent and user-friendly interface to numerous AI providers, enabling developers to seamlessly integrate powerful AI capabilities into their applications without the overhead of managing multiple APIs and configurations.
Features
--------
- **Unified Interface**: Interact with multiple AI providers using a single, consistent API
- **Plugin Architecture**: Extend functionality with custom plugins for tasks like memory management and sentiment analysis
- **Structured Data Support**: Generate and manipulate structured data using Pydantic models
- **Human-Centered Design**: Prioritizes readability and ease of use, making AI integration accessible to all developers
- **Minimal Configuration**: Quickly get started without extensive setup or configuration
Supported Providers
------------------
SimpleMind supports a variety of AI providers:
- `OpenAI's GPT <https://openai.com/gpt>`_
- `Anthropic's Claude <https://www.anthropic.com/claude>`_
- `xAI's Grok <https://x.ai/>`_
- `Groq's Groq <https://groq.com/>`_
- `Ollama <https://ollama.com>`_
Installation
-----------
Install SimpleMind using pip:
.. code-block:: shell
$ pip install simplemind
Quickstart
----------
1. Set your API keys as environment variables:
.. code-block:: bash
$ export OPENAI_API_KEY="sk-..."
$ export ANTHROPIC_API_KEY="..."
$ export XAI_API_KEY="..."
$ export GROQ_API_KEY="..."
This is the only required configuration.
2. Import and use SimpleMind:
.. code-block:: python
import simplemind as sm
# Generate text using the default provider (OpenAI)
response = sm.generate_text("Write a poem about the moon.", llm_model="gpt-4o-mini")
print(response)
Things to know:
- The primary function for generating text is ``generate_text()``, which is used in the example above.
- To generate structured data, use ``generate_data()``, which most providers support. This is extremely useful.
- The third function, ``create_conversation()``, is used to engage in conversations with AI models.
All of these functions accept an ``llm_model`` and ``llm_provider`` parameter, which allows you to specify the AI model to use. If not provided, the default model for the given provider will be used.
Usage Examples
--------------
Here are some examples demonstrating SimpleMind's key features. From generating creative text and structured data to engaging in conversations and extending functionality with plugins, these examples showcase the library's versatility and ease of use.
Feel free to adapt these examples to your specific use cases!
Text Generation
~~~~~~~~~~~~~~~
This example generates a poem about the moon using the ``gpt-4o-mini`` model.
.. code-block:: python
import simplemind as sm
poem = sm.generate_text("Write a poem about the moon.", llm_model="gpt-4o-mini")
print(poem)
Structured Data Generation
~~~~~~~~~~~~~~~~~~~~~~~~~~
This example generates a poem about love using the ``gpt-4o-mini`` model.
.. code-block:: python
from pydantic import BaseModel
class Poem(BaseModel):
title: str
content: str
poem = sm.generate_data(
prompt="Write a poem about love",
llm_model="gpt-4o-mini",
response_model=Poem,
)
print(poem)
Conversational AI
~~~~~~~~~~~~~~~~~
This example engages in a conversation with the ``gpt-4o-mini`` model.
.. code-block:: python
conversation = sm.create_conversation(llm_model="gpt-4o-mini")
conversation.add_message("user", "Hi there, how are you?")
response = conversation.send()
print(response.text)
Plugins
~~~~~~~
This example adds a simple custom memory plugin to the conversation.
.. code-block:: python
class SimpleMemoryPlugin:
def __init__(self):
self.memories = ["the moon is made of cheese."]
def send_hook(self, conversation):
for memory in self.memories:
conversation.add_message(role="system", text=memory)
conversation = sm.create_conversation()
conversation.add_plugin(SimpleMemoryPlugin())
conversation.add_message("user", "Write a poem about the moon")
print(conversation.send().text)
Plugin Development
~~~~~~~~~~~~~~~~~~
Plugins in SimpleMind follow a simple hook-based architecture. The ``send_hook`` method shown above is just one of several hooks available. Here's a more detailed example showing the complete plugin interface:
.. code-block:: python
from simplemind.plugins import BasePlugin
class CustomPlugin(BasePlugin):
def __init__(self):
self.conversation_history = []
def initialize_hook(self, conversation):
"""Called when the plugin is first added to a conversation."""
print("Plugin initialized!")
def pre_send_hook(self, conversation):
"""Called before the conversation is sent to the AI provider."""
# Add any system messages or modify the conversation
conversation.add_message("system", "Remember to be helpful.")
def send_hook(self, conversation):
"""Called during the send process."""
# Add messages or modify the conversation
self.conversation_history.append(conversation.messages)
def post_send_hook(self, conversation, response):
"""Called after receiving a response from the AI provider."""
# Process or modify the response
return response
def cleanup_hook(self):
"""Called when the plugin is removed or the conversation ends."""
self.conversation_history.clear()
All plugins should inherit from ``BasePlugin``, which provides default no-op implementations of these hooks. You only need to implement the hooks you want to use. Here's a simpler example:
.. code-block:: python
from simplemind.plugins import BasePlugin
class LoggingPlugin(BasePlugin):
def pre_send_hook(self, conversation):
print(f"Sending conversation with {len(conversation.messages)} messages")
def post_send_hook(self, conversation, response):
print(f"Received response: {response.text[:50]}...")
return response
conversation = sm.create_conversation()
conversation.add_plugin(LoggingPlugin())
conversation.add_message("user", "Hello!")
response = conversation.send()
Plugins can be used to implement features like:
- Conversation logging
- Memory management
- Response filtering
- Token counting
- Custom prompt engineering
- Analytics and monitoring
Multiple plugins can be added to a single conversation, and they will be executed in the order they were added.
Contributing
-----------
1. Fork the Repository
2. Create a New Branch
3. Make Your Changes
4. Submit a Pull Request
Please review our `Code of Conduct <LICENSE>`_ before contributing.
License
-------
SimpleMind is licensed under the `Apache 2.0 License <LICENSE>`_.
.. toctree::
:maxdepth: 2
:caption: Contents:
installation
usage
api
contributing
changelog
-35
View File
@@ -1,35 +0,0 @@
@ECHO OFF
pushd %~dp0
REM Command file for Sphinx documentation
if "%SPHINXBUILD%" == "" (
set SPHINXBUILD=sphinx-build
)
set SOURCEDIR=.
set BUILDDIR=_build
%SPHINXBUILD% >NUL 2>NUL
if errorlevel 9009 (
echo.
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx
echo.installed, then set the SPHINXBUILD environment variable to point
echo.to the full path of the 'sphinx-build' executable. Alternatively you
echo.may add the Sphinx directory to PATH.
echo.
echo.If you don't have Sphinx installed, grab it from
echo.https://www.sphinx-doc.org/
exit /b 1
)
if "%1" == "" goto help
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
goto end
:help
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O%
:end
popd
+13 -21
View File
@@ -1,20 +1,15 @@
from _context import sm
from pydantic import BaseModel
import openai
import faiss
import numpy as np
import os import os
import pickle import pickle
import faiss
import numpy as np
import openai
from _context import sm
from pydantic import BaseModel
class ContextualMemoryPlugin:
class ContextualMemoryPlugin(sm.BasePlugin): def __init__(self, api_key: str, memory_file: str = "memories.pkl", embedding_model: str = "text-embedding-ada-002"):
def __init__(
self,
api_key: str,
memory_file: str = "memories.pkl",
embedding_model: str = "text-embedding-ada-002",
):
openai.api_key = api_key openai.api_key = api_key
self.memory_file = memory_file self.memory_file = memory_file
self.embedding_model = embedding_model self.embedding_model = embedding_model
@@ -40,29 +35,29 @@ class ContextualMemoryPlugin(sm.BasePlugin):
def build_faiss_index(self): def build_faiss_index(self):
if self.embeddings: if self.embeddings:
self.index = faiss.IndexFlatL2(len(self.embeddings[0])) self.index = faiss.IndexFlatL2(len(self.embeddings[0]))
self.index.add(np.array(self.embeddings).astype("float32")) self.index.add(np.array(self.embeddings).astype('float32'))
else: else:
self.index = faiss.IndexFlatL2(1536) self.index = faiss.IndexFlatL2(1536)
def get_embedding(self, text: str) -> list: def get_embedding(self, text: str) -> list:
response = openai.Embedding.create(input=text, model=self.embedding_model) response = openai.Embedding.create(input=text, model=self.embedding_model)
return response["data"][0]["embedding"] return response['data'][0]['embedding']
def add_memory(self, memory: str): def add_memory(self, memory: str):
embedding = self.get_embedding(memory) embedding = self.get_embedding(memory)
self.memories.append(memory) self.memories.append(memory)
self.embeddings.append(embedding) self.embeddings.append(embedding)
self.index.add(np.array([embedding]).astype("float32")) self.index.add(np.array([embedding]).astype('float32'))
self.save_memories() self.save_memories()
def retrieve_memories(self, query: str, top_k: int = 3) -> list: def retrieve_memories(self, query: str, top_k: int = 3) -> list:
if not self.index or len(self.embeddings) == 0: if not self.index or len(self.embeddings) == 0:
return [] return []
query_embedding = self.get_embedding(query) query_embedding = self.get_embedding(query)
D, I = self.index.search(np.array([query_embedding]).astype("float32"), top_k) D, I = self.index.search(np.array([query_embedding]).astype('float32'), top_k)
return [self.memories[i] for i in I[0] if i < len(self.memories)] return [self.memories[i] for i in I[0] if i < len(self.memories)]
def pre_send_hook(self, conversation: sm.Conversation): def send_hook(self, conversation: sm.Conversation):
# Retrieve relevant memories based on the latest user message # Retrieve relevant memories based on the latest user message
if conversation.messages: if conversation.messages:
last_user_message = conversation.messages[-1].text last_user_message = conversation.messages[-1].text
@@ -74,16 +69,13 @@ class ContextualMemoryPlugin(sm.BasePlugin):
# Optionally, add the AI's response to memories # Optionally, add the AI's response to memories
self.add_memory(response) self.add_memory(response)
# Example Usage # Example Usage
# Define a Pydantic model if needed # Define a Pydantic model if needed
class Story(BaseModel): class Story(BaseModel):
title: str title: str
content: str content: str
# Initialize the conversation with the ContextualMemoryPlugin # Initialize the conversation with the ContextualMemoryPlugin
memory_plugin = ContextualMemoryPlugin(api_key=sm.settings.OPENAI_API_KEY) memory_plugin = ContextualMemoryPlugin(api_key=sm.settings.OPENAI_API_KEY)
+7 -21
View File
@@ -1,43 +1,29 @@
from typing import Iterator, List from typing import List
from pydantic import BaseModel
from _context import sm from _context import sm
from pydantic import BaseModel
class Movie(BaseModel): class Movie(BaseModel):
title: str title: str
year: int year: int
class MovieCharecter(BaseModel): class MovieCharecter(BaseModel):
name: str name: str
actor: str actor: str
class MovieQuote(BaseModel): class MovieQuote(BaseModel):
quote: str quote: str
movie: Movie movie: Movie
charecter: MovieCharecter charecter: MovieCharecter
class QuotesList(BaseModel): class QuotesList(BaseModel):
quotes: List[MovieQuote] quotes: List[MovieQuote]
theme: str
def gen_quotes(n: int = 10) -> Iterator[MovieQuote]: quotes = sm.generate_data(llm_provider="openai", llm_model="gpt-4o-mini", prompt="Generate 20 quotes from famous movies", response_model=QuotesList)
"""Generate a list of quotes from famous movies."""
for q in sm.generate_data( for quote in quotes.quotes:
llm_provider="openai", print(f"{quote.charecter.name} from {quote.movie.title} ({quote.movie.year}): {quote.quote!r}")
llm_model="gpt-4o-mini",
prompt=f"Generate {n} quotes from famous movies",
response_model=QuotesList,
).quotes:
yield q
if __name__ == "__main__":
for quote in gen_quotes(n=20):
print(
f"{quote.charecter.name} from {quote.movie.title} ({quote.movie.year}): {quote.quote!r}"
)
+2 -4
View File
@@ -1,11 +1,9 @@
from _context import sm from _context import sm
class MathPlugin(sm.BasePlugin): class MathPlugin:
def send_hook(self, conversation: sm.Conversation): def send_hook(self, conversation: sm.Conversation):
last_user_message = conversation.get_last_message(role="user") last_user_message = conversation.get_last_message(role="user")
if last_user_message is None:
return
if "calculate" in last_user_message.text.lower(): if "calculate" in last_user_message.text.lower():
expression = last_user_message.text.lower().replace("calculate", "").strip() expression = last_user_message.text.lower().replace("calculate", "").strip()
try: try:
@@ -16,7 +14,7 @@ class MathPlugin(sm.BasePlugin):
except Exception: except Exception:
conversation.add_message( conversation.add_message(
role="assistant", role="assistant",
text="I'm sorry, I couldn't compute that expression. Please try again.", text="I'm sorry, I couldn't compute that expression.",
) )
+2 -2
View File
@@ -1,7 +1,7 @@
from typing import Literal
from _context import sm from _context import sm
from pydantic import BaseModel from pydantic import BaseModel
from typing import Literal
class SentimentAnalysis(BaseModel): class SentimentAnalysis(BaseModel):
-31
View File
@@ -1,31 +0,0 @@
import simplemind as sm
class LoggingPlugin(sm.BasePlugin):
def pre_send_hook(self, conversation):
print(f"Sending conversation with {len(conversation.messages)} messages")
def add_message_hook(self, conversation, message):
print(f"Adding message to conversation: {message.text}")
def cleanup_hook(self, conversation):
print(f"Cleaning up conversation with {len(conversation.messages)} messages")
def initialize_hook(self, conversation):
print("Initializing conversation")
def post_send_hook(self, conversation, response):
print(f"Received response: {response.text}")
with sm.create_conversation() as conversation:
# Add the logging plugin.
conversation.add_plugin(LoggingPlugin())
# Add a message to the conversation.
conversation.add_message("user", "Hello!", meta={})
# Send the conversation.
response = conversation.send()
print(f"Response: {response.text}")
+3 -3
View File
@@ -11,9 +11,9 @@ class SimpleMemoryPlugin:
def yield_memories(self): def yield_memories(self):
return (m for m in self.memories) return (m for m in self.memories)
def initialize_hook(self, conversation: sm.Conversation): def send_hook(self, conversation: sm.Conversation):
for m in self.yield_memories(): for m in self.yield_memories():
conversation.prepend_system_message(role="system", text=m) conversation.add_message(role="system", text=m)
conversation = sm.create_conversation(llm_model="grok-beta", llm_provider="xai") conversation = sm.create_conversation(llm_model="grok-beta", llm_provider="xai")
@@ -21,7 +21,7 @@ conversation.add_plugin(SimpleMemoryPlugin())
conversation.add_message( conversation.add_message(
role="user", role="user",
text="Please write a poem about the moon", text="Write a poem about the moon",
) )
r = conversation.send() r = conversation.send()
+5 -9
View File
@@ -1,13 +1,9 @@
from _context import sm from _context import sm
conversation = sm.create_conversation(llm_model="gpt-4o", llm_provider="openai")
def translate_to_french(text: str) -> str: conversation.add_message(
conversation = sm.create_conversation(llm_model="gpt-4o", llm_provider="openai") "user", "Translate the following text to French: 'Hello, world!'"
)
conversation.add_message( print(conversation.send().text)
"user", f"Translate the following text to French: {text!r}"
)
return conversation.send().text
print(translate_to_french("an omlette with cheese"))
-59
View File
@@ -1,59 +0,0 @@
import time
import simplemind as sm
class ConversationPlugin(sm.BasePlugin):
def post_send_hook(self, conversation, response):
# Print the LLM model and the response text.
print(f"{conversation.llm_model}:\n{response.text.strip()}\n\n------------\n")
def have_conversation(rounds: int = 3):
# Create two conversations - one for each AI
with (
sm.create_conversation(
llm_model="claude-3-5-sonnet-20241022", llm_provider="anthropic"
) as claude_conv,
sm.create_conversation(
llm_model="llama3.2", llm_provider="ollama"
) as llama_conv,
):
# Add our plugin to both
plugin = ConversationPlugin()
claude_conv.add_plugin(plugin)
llama_conv.add_plugin(plugin)
# Start the conversation
prompt = "What do you think about the future of artificial intelligence? Please keep your response brief."
claude_conv.add_message("user", prompt, meta={})
claude_response = claude_conv.send()
# Have them discuss back and forth
for _ in range(rounds):
# Llama responds to Claude
llama_conv.add_message(
"user",
f"Respond to this statement from another AI: {claude_response.text}",
meta={},
)
llama_response = llama_conv.send()
time.sleep(1) # Add a small delay between responses
# Claude responds to Llama
claude_conv.add_message(
"user",
f"Respond to this statement from another AI: {llama_response.text}",
meta={},
)
claude_response = claude_conv.send()
time.sleep(1)
if __name__ == "__main__":
print("Starting AI conversation...\n")
have_conversation()
print("\nConversation ended.")
Binary file not shown.

Before

Width:  |  Height:  |  Size: 944 KiB

+3 -3
View File
@@ -1,10 +1,10 @@
[project] [project]
name = "simplemind" name = "simplemind"
version = "0.1.6" version = "0.1.1"
description = "An experimental client for AI providers that intends to replace LangChain and LangGraph for most common use cases." description = "An experimental client for AI providers that intends to replace LangChain and LangGraph for most common use cases."
readme = "README.md" readme = "README.md"
requires-python = ">=3.10" requires-python = ">=3.11"
dependencies = ["pydantic", "pydantic-settings", "instructor", "openai", "anthropic", "ollama", "groq", "google-generativeai"] dependencies = ["pydantic", "pydantic-settings", "instructor", "openai", "anthropic", "groq"]
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
+8 -96
View File
@@ -1,95 +1,21 @@
from typing import List, Type from .models import Conversation
from .models import BaseModel, BasePlugin, Conversation
from .settings import settings
from .utils import find_provider from .utils import find_provider
from .settings import settings
class Session: def create_conversation(llm_model=None, llm_provider=None):
"""A session object that maintains configuration across multiple API calls.
Similar to `requests.Session`, this allows you to specify default settings
that will be used for all operations within the session.
"""
def __init__(
self,
*,
llm_provider: str = settings.DEFAULT_LLM_PROVIDER,
llm_model: str | None = None,
**kwargs,
):
self.llm_provider = llm_provider
self.llm_model = llm_model
self.default_kwargs = kwargs
def generate_text(self, prompt: str, **kwargs) -> str:
"""Generate text using the session's default provider and model."""
merged_kwargs = {**self.default_kwargs, **kwargs}
return generate_text(
prompt=prompt,
llm_provider=self.llm_provider,
llm_model=self.llm_model,
**merged_kwargs,
)
def generate_data(
self, prompt: str, response_model: Type[BaseModel], **kwargs
) -> BaseModel:
"""Generate structured data using the session's default provider and model."""
merged_kwargs = {**self.default_kwargs, **kwargs}
return generate_data(
prompt=prompt,
response_model=response_model,
llm_provider=self.llm_provider,
llm_model=self.llm_model,
**merged_kwargs,
)
def create_conversation(self, **kwargs) -> Conversation:
"""Create a conversation using the session's default provider and model."""
merged_kwargs = {**self.default_kwargs, **kwargs}
return create_conversation(
llm_provider=self.llm_provider, llm_model=self.llm_model, **merged_kwargs
)
def create_conversation(
*,
llm_model: str | None = None,
llm_provider: str | None = None,
plugins: List[BasePlugin] | None = None,
**kwargs,
) -> Conversation:
"""Create a new conversation.""" """Create a new conversation."""
# Create the conversation. return Conversation(
conversation = Conversation( llm_model=llm_model, llm_provider=llm_provider or settings.DEFAULT_LLM_PROVIDER
llm_model=llm_model,
llm_provider=llm_provider or settings.DEFAULT_LLM_PROVIDER,
) )
# Add plugins to the conversation.
for plugin in plugins or []:
conversation.add_plugin(plugin)
return conversation def generate_data(prompt, *, llm_model=None, llm_provider=None, response_model=None):
def generate_data(
prompt: str,
*,
llm_model: str | None = None,
llm_provider: str | None = None,
response_model: Type[BaseModel],
**kwargs,
) -> BaseModel:
"""Generate structured data from a given prompt.""" """Generate structured data from a given prompt."""
# Find the provider.
provider = find_provider(llm_provider or settings.DEFAULT_LLM_PROVIDER) provider = find_provider(llm_provider or settings.DEFAULT_LLM_PROVIDER)
# Generate the data.
return provider.structured_response( return provider.structured_response(
prompt=prompt, prompt=prompt,
llm_model=llm_model, llm_model=llm_model,
@@ -97,32 +23,18 @@ def generate_data(
) )
def generate_text( def generate_text(prompt, *, llm_model=None, llm_provider=None, **kwargs):
prompt: str,
*,
llm_model: str | None = None,
llm_provider: str | None = None,
**kwargs,
) -> str:
"""Generate text from a given prompt.""" """Generate text from a given prompt."""
# Find the provider.
provider = find_provider(llm_provider or settings.DEFAULT_LLM_PROVIDER) provider = find_provider(llm_provider or settings.DEFAULT_LLM_PROVIDER)
# Generate the text.
return provider.generate_text(prompt=prompt, llm_model=llm_model, **kwargs) return provider.generate_text(prompt=prompt, llm_model=llm_model, **kwargs)
# Syntax sugar.
Plugin = BasePlugin
__all__ = [ __all__ = [
"Conversation",
"create_conversation", "create_conversation",
"find_provider", "find_provider",
"generate_data", "generate_data",
"generate_text", "generate_text",
"settings", "settings",
"BasePlugin",
"Session",
"Plugin",
] ]
-27
View File
@@ -1,27 +0,0 @@
import time
from typing import Any, Callable
import logfire
from .settings import settings
def logger(func: Callable[..., Any]) -> Callable[..., Any]:
"""A @logger decorator that logs the function parameters, function returns, and exceptions raised if logging is enabled."""
def wrapper(*args, **kwargs) -> Any:
if not settings.logging.enabled:
return func(*args, **kwargs)
logfire.info(f"Calling {func.__name__} with args: {args}, kwargs: {kwargs}")
t1 = time.perf_counter()
try:
result = func(*args, **kwargs)
t2 = time.perf_counter()
logfire.info(f"{func.__name__} returned: {result} in {t2-t1} seconds")
return result
except Exception as e:
t2 = time.perf_counter()
logfire.error(f"Error in {func.__name__}: {e} in {t2-t1} seconds")
raise e
return wrapper
+13 -108
View File
@@ -1,18 +1,18 @@
import uuid import uuid
from abc import ABC, abstractmethod
from datetime import datetime from datetime import datetime
from types import TracebackType
from typing import Any, Dict, List, Literal, Optional from typing import Any, Dict, List, Literal, Optional
from pydantic import BaseModel, Field from pydantic import BaseModel, Field
from .utils import find_provider from .utils import find_provider
MESSAGE_ROLE = Literal["system", "user", "assistant"] MESSAGE_ROLE = Literal["system", "user", "assistant"]
class SMBaseModel(BaseModel): class SMBaseModel(BaseModel):
"""The base SimpleMind model class."""
date_created: datetime = Field(default_factory=datetime.now) date_created: datetime = Field(default_factory=datetime.now)
def __str__(self): def __str__(self):
@@ -22,36 +22,16 @@ class SMBaseModel(BaseModel):
return str(self) return str(self)
class BasePlugin(SMBaseModel): class BasePlugin(ABC):
"""The base conversation plugin class.""" """The base conversation plugin class."""
# Plugin metadata. @abstractmethod
meta: Dict[str, Any] = {} def send_hook(self, conversation: "Conversation"):
"""Send a hook to the plugin."""
def initialize_hook(self, conversation: "Conversation") -> Any:
"""Initialize a hook for the plugin."""
raise NotImplementedError
def cleanup_hook(self, conversation: "Conversation") -> Any:
"""Cleanup a hook for the plugin."""
raise NotImplementedError
def add_message_hook(self, conversation: "Conversation", message: "Message") -> Any:
"""Add a message hook for the plugin."""
raise NotImplementedError
def pre_send_hook(self, conversation: "Conversation") -> Any:
"""Pre-send hook for the plugin."""
raise NotImplementedError
def post_send_hook(self, conversation: "Conversation", response: "Message") -> Any:
"""Post-send hook for the plugin."""
raise NotImplementedError raise NotImplementedError
class Message(SMBaseModel): class Message(SMBaseModel):
"""A message in a conversation."""
role: MESSAGE_ROLE role: MESSAGE_ROLE
text: str text: str
meta: Dict[str, Any] = {} meta: Dict[str, Any] = {}
@@ -63,16 +43,7 @@ class Message(SMBaseModel):
return f"<Message role={self.role} text={self.text!r}>" return f"<Message role={self.role} text={self.text!r}>"
@classmethod @classmethod
def from_raw_response(cls, *, text: str, raw: Any) -> "Message": def from_raw_response(cls, *, text: str, raw):
"""Create a Message instance from a raw response.
Args:
text (str): The message text.
raw (Any): The raw response data.
Returns:
Message: A new Message instance.
"""
self = cls() self = cls()
self.text = text self.text = text
self.raw = raw self.raw = raw
@@ -80,106 +51,40 @@ class Message(SMBaseModel):
class Conversation(SMBaseModel): class Conversation(SMBaseModel):
"""A conversation between a user and an assistant."""
id: str = Field(default_factory=lambda: str(uuid.uuid4())) id: str = Field(default_factory=lambda: str(uuid.uuid4()))
messages: List[Message] = [] messages: List[Message] = []
llm_model: Optional[str] = None llm_model: Optional[str] = None
llm_provider: Optional[str] = None llm_provider: Optional[str] = None
plugins: List[BasePlugin] = [] plugins: List[Any] = []
def __str__(self): def __str__(self):
return f"<Conversation id={self.id!r}>" return f"<Conversation id={self.id!r}>"
def __enter__(self):
# Execute all initialize hooks.
for plugin in self.plugins:
if hasattr(plugin, "initialize_hook"):
try:
plugin.initialize_hook(self)
except NotImplementedError:
pass
return self
def __exit__(
self,
exc_type: type[BaseException],
exc_value: BaseException,
traceback: TracebackType,
) -> None:
"""Execute all cleanup hooks."""
for plugin in self.plugins:
if hasattr(plugin, "cleanup_hook"):
try:
plugin.cleanup_hook(self)
except NotImplementedError:
pass
def prepend_system_message(
self, role: MESSAGE_ROLE, text: str, meta: Dict[str, Any] | None = None
):
"""Prepend a system message to the conversation."""
self.messages = [Message(role=role, text=text, meta=meta or {})] + self.messages
def add_message( def add_message(
self, role: MESSAGE_ROLE, text: str, meta: Optional[Dict[str, Any]] = None self, role: MESSAGE_ROLE, text: str, meta: Optional[Dict[str, Any]] = None
): ):
"""Add a new message to the conversation.""" """Add a new message to the conversation."""
# Ensure meta is a dict.
if meta is None: if meta is None:
meta = {} meta = {}
# Execute all add-message hooks.
for plugin in self.plugins:
if hasattr(plugin, "add_message_hook"):
try:
plugin.add_message_hook(
self, Message(role=role, text=text, meta=meta)
)
except NotImplementedError:
pass
# Add the message to the conversation.
self.messages.append(Message(role=role, text=text, meta=meta)) self.messages.append(Message(role=role, text=text, meta=meta))
def send( def send(
self, self, llm_model: Optional[str] = None, llm_provider: Optional[str] = None
llm_model: str | None = None,
llm_provider: str | None = None,
) -> Message: ) -> Message:
"""Send the conversation to the LLM.""" """Send the conversation to the LLM."""
# Execute all pre send hooks.
for plugin in self.plugins: for plugin in self.plugins:
if hasattr(plugin, "pre_send_hook"): plugin.send_hook(self)
try:
plugin.pre_send_hook(self)
except NotImplementedError:
pass
# Find the provider and send the conversation.
provider = find_provider(llm_provider or self.llm_provider) provider = find_provider(llm_provider or self.llm_provider)
response = provider.send_conversation(self) response = provider.send_conversation(self)
# Execute all post-send hooks.
for plugin in self.plugins:
if hasattr(plugin, "post_send_hook"):
try:
plugin.post_send_hook(self, response)
except NotImplementedError:
pass
# Add the response to the conversation.
self.add_message(role="assistant", text=response.text, meta=response.meta) self.add_message(role="assistant", text=response.text, meta=response.meta)
return response return response
def get_last_message(self, role: MESSAGE_ROLE) -> Message | None: def get_last_message(self, role: MESSAGE_ROLE) -> Optional[Message]:
"""Get the last message with the given role.""" """Get the last message with the given role."""
return next((m for m in reversed(self.messages) if m.role == role), None) return next((m for m in reversed(self.messages) if m.role == role), None)
def add_plugin(self, plugin: BasePlugin) -> None: def add_plugin(self, plugin: Any):
"""Add a plugin to the conversation.""" """Add a plugin to the conversation."""
self.plugins.append(plugin) self.plugins.append(plugin)
+6 -8
View File
@@ -1,11 +1,9 @@
from typing import List, Type from typing import List, Type
from ._base import BaseProvider from simplemind.providers._base import BaseProvider
from .anthropic import Anthropic from simplemind.providers.anthropic import Anthropic
from .gemini import Gemini from simplemind.providers.groq import Groq
from .groq import Groq from simplemind.providers.openai import OpenAI
from .ollama import Ollama from simplemind.providers.xai import XAI
from .openai import OpenAI
from .xai import XAI
providers: List[Type[BaseProvider]] = [Anthropic, Gemini, Groq, OpenAI, Ollama, XAI] providers: List[Type[BaseProvider]] = [Anthropic, Groq, OpenAI, XAI]
+4 -12
View File
@@ -1,14 +1,6 @@
from abc import ABC, abstractmethod from abc import ABC, abstractmethod
from functools import cached_property
from typing import TYPE_CHECKING, Any, Type, TypeVar
from instructor import Instructor from instructor import Instructor
from pydantic import BaseModel
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
class BaseProvider(ABC): class BaseProvider(ABC):
@@ -17,13 +9,13 @@ class BaseProvider(ABC):
NAME: str NAME: str
DEFAULT_MODEL: str DEFAULT_MODEL: str
@cached_property @property
@abstractmethod @abstractmethod
def client(self) -> Any: def client(self):
"""The instructor client for the provider.""" """The instructor client for the provider."""
raise NotImplementedError raise NotImplementedError
@cached_property @property
@abstractmethod @abstractmethod
def structured_client(self) -> Instructor: def structured_client(self) -> Instructor:
"""The structured client for the provider.""" """The structured client for the provider."""
@@ -35,7 +27,7 @@ class BaseProvider(ABC):
raise NotImplementedError raise NotImplementedError
@abstractmethod @abstractmethod
def structured_response(self, prompt: str, response_model: Type[T], **kwargs) -> T: def structured_response(self, prompt: str, response_model, **kwargs):
"""Get a structured response.""" """Get a structured response."""
raise NotImplementedError raise NotImplementedError
+19 -50
View File
@@ -1,54 +1,36 @@
from functools import cached_property from typing import Union
from typing import TYPE_CHECKING, Type, TypeVar
import anthropic
import instructor import instructor
from pydantic import BaseModel
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider from ._base import BaseProvider
from ..settings import settings
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "anthropic" PROVIDER_NAME = "anthropic"
DEFAULT_MODEL = "claude-3-5-sonnet-20241022" DEFAULT_MODEL = "claude-3-5-sonnet-20241022"
DEFAULT_MAX_TOKENS = 1_000 DEFAULT_MAX_TOKENS = 1000
DEFAULT_KWARGS = {"max_tokens": DEFAULT_MAX_TOKENS}
class Anthropic(BaseProvider): class Anthropic(BaseProvider):
NAME = PROVIDER_NAME NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
def __init__(self, api_key: str | None = None): def __init__(self, api_key: Union[str, None] = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
@cached_property @property
def client(self): def client(self):
"""The raw Anthropic client.""" """The raw Anthropic client."""
if not self.api_key: if not self.api_key:
raise ValueError("Anthropic API key is required") raise ValueError("Anthropic API key is required")
try:
import anthropic
except ImportError as exc:
raise ImportError(
"Please install the `anthropic` package: `pip install anthropic`"
) from exc
return anthropic.Anthropic(api_key=self.api_key) return anthropic.Anthropic(api_key=self.api_key)
@cached_property @property
def structured_client(self): def structured_client(self):
"""A client patched with Instructor.""" """A client patched with Instructor."""
return instructor.from_anthropic(self.client) return instructor.from_anthropic(self.client)
@logger def send_conversation(self, conversation: "Conversation", **kwargs):
def send_conversation(self, conversation: "Conversation", **kwargs) -> "Message":
"""Send a conversation to the Anthropic API.""" """Send a conversation to the Anthropic API."""
from ..models import Message from ..models import Message
@@ -57,9 +39,10 @@ class Anthropic(BaseProvider):
] ]
response = self.client.messages.create( response = self.client.messages.create(
model=conversation.llm_model or self.DEFAULT_MODEL, model=conversation.llm_model or DEFAULT_MODEL,
messages=messages, messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs}, max_tokens=DEFAULT_MAX_TOKENS,
**kwargs,
) )
# Get the response content from the Anthropic response # Get the response content from the Anthropic response
@@ -70,40 +53,26 @@ class Anthropic(BaseProvider):
role="assistant", role="assistant",
text=assistant_message, text=assistant_message,
raw=response, raw=response,
llm_model=conversation.llm_model or self.DEFAULT_MODEL, llm_model=conversation.llm_model or DEFAULT_MODEL,
llm_provider=PROVIDER_NAME, llm_provider=PROVIDER_NAME,
) )
@logger def structured_response(self, model, response_model, **kwargs):
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}]
response = self.structured_client.messages.create( response = self.structured_client.messages.create(
model=model, model=model, response_model=response_model, **kwargs
messages=messages, # Add the messages parameter
response_model=response_model,
**{**self.DEFAULT_KWARGS, **kwargs},
) )
return response_model.model_validate(response) return response
@logger def generate_text(self, prompt, *, llm_model, **kwargs):
def generate_text(self, prompt: str, *, llm_model: str, **kwargs):
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
] ]
response = self.client.messages.create( response = self.client.messages.create(
model=llm_model or self.DEFAULT_MODEL, model=llm_model,
messages=messages, messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs}, max_tokens=DEFAULT_MAX_TOKENS,
**kwargs,
) )
return response.content[0].text return response.content[0].text
-109
View File
@@ -1,109 +0,0 @@
# TODO: this is a placeholder file for the Gemini provider
# IT is not currently working as desired.
from functools import cached_property
from typing import TYPE_CHECKING, Type, TypeVar
import instructor
from pydantic import BaseModel
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "gemini"
DEFAULT_MODEL = "models/gemini-1.5-flash-latest"
class Gemini(BaseProvider):
NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL
def __init__(self, api_key: str | None = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
self.model_name = DEFAULT_MODEL
def set_model(self, model_name: str):
self.model_name = model_name
@cached_property
def client(self):
"""The raw Gemini client."""
if not self.api_key:
raise ValueError("Gemini API key is required")
try:
import google.generativeai as genai
except ImportError as exc:
raise ImportError(
"Please install the `google-generativeai` package: `pip install google-generativeai`"
) from exc
genai.configure(api_key=self.api_key)
return genai.GenerativeModel(model_name=self.model_name)
@cached_property
def structured_client(self):
"""A Gemini client patched with Instructor."""
return instructor.from_gemini(self.client)
@logger
def send_conversation(self, conversation: "Conversation") -> "Message":
"""Send a conversation to the Gemini API."""
from ..models import Message
# Convert messages to Gemini's format
chat = self.client.start_chat()
# Send all previous messages to establish context
for msg in conversation.messages[:-1]: # All messages except the last one
chat.send_message(msg.text)
# Send the final message and get response
try:
response = chat.send_message(conversation.messages[-1].text)
except Exception as e:
raise RuntimeError(f"Failed to send conversation to Gemini API: {e}") from e
# Create and return a properly formatted Message instance
return Message(
role="assistant",
text=response.text,
raw=response,
llm_model=self.model_name,
llm_provider=PROVIDER_NAME,
)
@logger
def structured_response(self, prompt: str, response_model: Type[T], **kwargs) -> T:
"""Send a structured response to the Gemini API."""
# Only try to pop if the key exists
kwargs.pop("llm_model", None) # Add default value of None
try:
response = self.structured_client.chat.completions.create(
messages=[{"role": "user", "content": prompt}],
response_model=response_model,
**kwargs,
)
except Exception as e:
# Handle the exception appropriately, e.g., log the error or raise a custom exception
raise RuntimeError(
f"Failed to send structured response to Gemini API: {e}"
) from e
return response_model.model_validate(response)
@logger
def generate_text(self, prompt: str, **kwargs) -> str:
"""Generate text using the Gemini API."""
kwargs.pop("llm_model")
try:
response = self.client.generate_content(prompt, **kwargs)
except Exception as e:
# Handle the exception appropriately, e.g., log the error or raise a custom exception
raise RuntimeError(f"Failed to generate text with Gemini API: {e}") from e
return response.text
+16 -37
View File
@@ -1,52 +1,34 @@
from functools import cached_property from typing import Union
from typing import TYPE_CHECKING, Type, TypeVar
import groq
import instructor import instructor
from pydantic import BaseModel
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider from ._base import BaseProvider
from ..settings import settings
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "groq" PROVIDER_NAME = "groq"
DEFAULT_MODEL = "llama3-8b-8192" DEFAULT_MODEL = "llama3-8b-8192"
DEFAULT_MAX_TOKENS = 1_000
DEFAULT_KWARGS = {"max_tokens": DEFAULT_MAX_TOKENS}
class Groq(BaseProvider): class Groq(BaseProvider):
NAME = PROVIDER_NAME NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
def __init__(self, api_key: str | None = None): def __init__(self, api_key: Union[str, None] = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
@cached_property @property
def client(self): def client(self):
"""The raw Groq client.""" """The raw Groq client."""
if not self.api_key: if not self.api_key:
raise ValueError("Groq API key is required") raise ValueError("Groq API key is required")
try:
import groq
except ImportError as exc:
raise ImportError(
"Please install the `groq` package: `pip install groq`"
) from exc
return groq.Groq(api_key=self.api_key) return groq.Groq(api_key=self.api_key)
@cached_property @property
def structured_client(self): def structured_client(self):
"""A client patched with Instructor.""" """A client patched with Instructor."""
return instructor.from_groq(self.client) return instructor.from_groq(self.client)
@logger
def send_conversation( def send_conversation(
self, self,
conversation: "Conversation", conversation: "Conversation",
@@ -60,9 +42,9 @@ class Groq(BaseProvider):
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
model=conversation.llm_model or self.DEFAULT_MODEL, model=conversation.llm_model or DEFAULT_MODEL,
messages=messages, messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs}, **kwargs,
) )
# Get the response content from the Groq response # Get the response content from the Groq response
@@ -73,12 +55,11 @@ class Groq(BaseProvider):
role="assistant", role="assistant",
text=assistant_message.content or "", text=assistant_message.content or "",
raw=response, raw=response,
llm_model=conversation.llm_model or self.DEFAULT_MODEL, llm_model=conversation.llm_model or DEFAULT_MODEL,
llm_provider=PROVIDER_NAME, llm_provider=PROVIDER_NAME,
) )
@logger def structured_response(self, prompt: str, response_model, **kwargs):
def structured_response(self, prompt: str, response_model: Type[T], **kwargs) -> T:
# Ensure messages are provided in kwargs # Ensure messages are provided in kwargs
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
@@ -87,27 +68,25 @@ class Groq(BaseProvider):
response = self.structured_client.chat.completions.create( response = self.structured_client.chat.completions.create(
messages=messages, messages=messages,
response_model=response_model, response_model=response_model,
model=kwargs.pop("llm_model", self.DEFAULT_MODEL), **kwargs,
**{**self.DEFAULT_KWARGS, **kwargs},
) )
return response_model.model_validate(response) return response
@logger
def generate_text( def generate_text(
self, self,
prompt: str, prompt: str,
*, *,
llm_model: str, llm_model: str,
**kwargs, **kwargs,
) -> str: ):
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
messages=messages, messages=messages,
model=llm_model or self.DEFAULT_MODEL, model=llm_model,
**{**self.DEFAULT_KWARGS, **kwargs}, **kwargs,
) )
return str(response.choices[0].message.content) return response.choices[0].message.content
-118
View File
@@ -1,118 +0,0 @@
from functools import cached_property
from typing import TYPE_CHECKING, Type, TypeVar
import instructor
from openai import OpenAI
from pydantic import BaseModel
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "ollama"
DEFAULT_MODEL = "llama3.2"
DEFAULT_TIMEOUT = 60
DEFAULT_KWARGS = {}
class Ollama(BaseProvider):
NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
TIMEOUT = DEFAULT_TIMEOUT
def __init__(self, host_url: str | None = None):
self.host_url = host_url or settings.OLLAMA_HOST_URL
@cached_property
def client(self):
"""The raw Ollama client."""
if not self.host_url:
raise ValueError("No ollama host url provided")
try:
import ollama as ol
except ImportError as exc:
raise ImportError(
"Please install the `ollama` package: `pip install ollama`"
) from exc
return ol.Client(timeout=self.TIMEOUT, host=self.host_url)
@cached_property
def structured_client(self) -> instructor.Instructor:
"""A client patched with Instructor."""
return instructor.from_openai(
OpenAI(
base_url=f"{self.host_url}/v1",
api_key="ollama",
),
mode=instructor.Mode.JSON,
)
@logger
def send_conversation(self, conversation: "Conversation", **kwargs) -> "Message":
"""Send a conversation to the Ollama API."""
from ..models import Message
messages = [
{"role": msg.role, "content": msg.text} for msg in conversation.messages
]
response = self.client.chat(
model=conversation.llm_model or DEFAULT_MODEL,
messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs},
)
assistant_message = response.get("message")
# Create and return a properly formatted Message instance
return Message(
role="assistant",
text=assistant_message.get("content"),
raw=response,
llm_model=conversation.llm_model or self.DEFAULT_MODEL,
llm_provider=PROVIDER_NAME,
)
@logger
def structured_response(
self,
prompt: str,
response_model: Type[T],
*,
llm_model: str | None = None,
**kwargs,
) -> T:
"""Get a structured response from the Ollama API."""
messages = [
{"role": "user", "content": prompt},
]
response = self.structured_client.chat.completions.create(
messages=messages,
model=llm_model or self.DEFAULT_MODEL,
response_model=response_model,
**{**self.DEFAULT_KWARGS, **kwargs},
)
return response_model.model_validate(response)
@logger
def generate_text(
self, prompt: str, *, llm_model: str | None = None, **kwargs
) -> str:
"""Generate text using the Ollama API."""
messages = [
{"role": "user", "content": prompt},
]
response = self.client.chat(
messages=messages,
model=llm_model or self.DEFAULT_MODEL,
**{**self.DEFAULT_KWARGS, **kwargs},
)
return response.get("message", {}).get("content", "")
+18 -50
View File
@@ -1,52 +1,35 @@
from functools import cached_property from typing import Union
from typing import TYPE_CHECKING, Type, TypeVar
import instructor import instructor
from pydantic import BaseModel import openai as oa
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider from ._base import BaseProvider
from ..settings import settings
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "openai" PROVIDER_NAME = "openai"
DEFAULT_MODEL = "gpt-4o-mini" DEFAULT_MODEL = "gpt-4o-mini"
DEFAULT_MAX_TOKENS = 1_000
DEFAULT_KWARGS = {"max_tokens": DEFAULT_MAX_TOKENS}
class OpenAI(BaseProvider): class OpenAI(BaseProvider):
NAME = PROVIDER_NAME NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
def __init__(self, api_key: str | None = None): def __init__(self, api_key: Union[str, None] = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
@cached_property @property
def client(self): def client(self):
"""The raw OpenAI client.""" """The raw OpenAI client."""
if not self.api_key: if not self.api_key:
raise ValueError("OpenAI API key is required") raise ValueError("OpenAI API key is required")
try:
import openai as oa
except ImportError as exc:
raise ImportError(
"Please install the `openai` package: `pip install openai`"
) from exc
return oa.OpenAI(api_key=self.api_key) return oa.OpenAI(api_key=self.api_key)
@cached_property @property
def structured_client(self): def structured_client(self):
"""A OpenAI client with Instructor.""" """A OpenAI client with Instructor."""
return instructor.from_openai(self.client) return instructor.from_openai(self.client)
@logger def send_conversation(self, conversation: "Conversation", **kwargs):
def send_conversation(self, conversation: "Conversation", **kwargs) -> "Message":
"""Send a conversation to the OpenAI API.""" """Send a conversation to the OpenAI API."""
from ..models import Message from ..models import Message
@@ -55,9 +38,7 @@ class OpenAI(BaseProvider):
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
model=conversation.llm_model or DEFAULT_MODEL, model=conversation.llm_model or DEFAULT_MODEL, messages=messages, **kwargs
messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs},
) )
# Get the response content from the OpenAI response # Get the response content from the OpenAI response
@@ -72,37 +53,24 @@ class OpenAI(BaseProvider):
llm_provider=PROVIDER_NAME, llm_provider=PROVIDER_NAME,
) )
@logger def structured_response(self, prompt, response_model, *, llm_model: str, **kwargs):
def structured_response(
self,
prompt: str,
response_model: Type[T],
*,
llm_model: str | None = None,
**kwargs,
) -> T:
"""Get a structured response from the OpenAI API."""
# Ensure messages are provided in kwargs # Ensure messages are provided in kwargs
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
] ]
response = self.structured_client.chat.completions.create(
messages=messages,
model=llm_model or self.DEFAULT_MODEL,
response_model=response_model,
**{**self.DEFAULT_KWARGS, **kwargs},
)
return response_model.model_validate(response)
@logger response = self.structured_client.chat.completions.create(
def generate_text(self, prompt: str, *, llm_model: str | None = None, **kwargs): messages=messages, model=llm_model, response_model=response_model, **kwargs
"""Generate text using the OpenAI API.""" )
return response
def generate_text(self, prompt, *, llm_model, **kwargs):
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
messages=messages, messages=messages, model=llm_model, **kwargs
model=llm_model or self.DEFAULT_MODEL,
**{**self.DEFAULT_KWARGS, **kwargs},
) )
return response.choices[0].message.content return response.choices[0].message.content
+15 -36
View File
@@ -1,57 +1,40 @@
from functools import cached_property from typing import Union
from typing import TYPE_CHECKING, Type, TypeVar
import instructor import instructor
from pydantic import BaseModel import openai as oa
from ..logging import logger
from ..settings import settings
from ._base import BaseProvider from ._base import BaseProvider
from ..settings import settings
if TYPE_CHECKING:
from ..models import Conversation, Message
T = TypeVar("T", bound=BaseModel)
PROVIDER_NAME = "xai" PROVIDER_NAME = "xai"
DEFAULT_MODEL = "grok-beta" DEFAULT_MODEL = "grok-beta"
BASE_URL = "https://api.x.ai/v1" BASE_URL = "https://api.x.ai/v1"
DEFAULT_MAX_TOKENS = 1000 DEFAULT_MAX_TOKENS = 1000
DEFAULT_KWARGS = {"max_tokens": DEFAULT_MAX_TOKENS}
class XAI(BaseProvider): class XAI(BaseProvider):
NAME = PROVIDER_NAME NAME = PROVIDER_NAME
DEFAULT_MODEL = DEFAULT_MODEL DEFAULT_MODEL = DEFAULT_MODEL
DEFAULT_KWARGS = DEFAULT_KWARGS
def __init__(self, api_key: str | None = None): def __init__(self, api_key: Union[str, None] = None):
self.api_key = api_key or settings.get_api_key(PROVIDER_NAME) self.api_key = api_key or settings.get_api_key(PROVIDER_NAME)
@cached_property @property
def client(self): def client(self):
"""The raw OpenAI client.""" """The raw OpenAI client."""
if not self.api_key: if not self.api_key:
raise ValueError("XAI API key is required") raise ValueError("XAI API key is required")
try:
import openai as oa
except ImportError as exc:
raise ImportError(
"Please install the `openai` package: `pip install openai`"
) from exc
return oa.OpenAI( return oa.OpenAI(
api_key=self.api_key, api_key=self.api_key,
base_url=BASE_URL, base_url=BASE_URL,
) )
@cached_property @property
def structured_client(self): def structured_client(self):
"""A client patched with Instructor.""" """A client patched with Instructor."""
return instructor.from_openai(self.client) return instructor.from_openai(self.client)
@logger def send_conversation(self, conversation: "Conversation", **kwargs):
def send_conversation(self, conversation: "Conversation", **kwargs) -> "Message":
"""Send a conversation to the OpenAI API.""" """Send a conversation to the OpenAI API."""
from ..models import Message from ..models import Message
@@ -60,9 +43,9 @@ class XAI(BaseProvider):
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
model=conversation.llm_model or self.DEFAULT_MODEL, model=conversation.llm_model or DEFAULT_MODEL,
messages=messages, messages=messages,
**{**self.DEFAULT_KWARGS, **kwargs}, **kwargs,
) )
# Get the response content from the OpenAI response # Get the response content from the OpenAI response
@@ -73,26 +56,22 @@ class XAI(BaseProvider):
role="assistant", role="assistant",
text=assistant_message.content, text=assistant_message.content,
raw=response, raw=response,
llm_model=conversation.llm_model or self.DEFAULT_MODEL, llm_model=conversation.llm_model or DEFAULT_MODEL,
llm_provider=PROVIDER_NAME, llm_provider=PROVIDER_NAME,
) )
@logger def structured_response(self, prompt: str, response_model, *, llm_model):
def structured_response(
self, prompt: str, response_model: Type[T], *, llm_model: str
) -> T:
raise NotImplementedError("XAI does not support structured responses") raise NotImplementedError("XAI does not support structured responses")
@logger def generate_text(self, prompt, *, llm_model, **kwargs):
def generate_text(self, prompt: str, *, llm_model: str, **kwargs) -> str:
messages = [ messages = [
{"role": "user", "content": prompt}, {"role": "user", "content": prompt},
] ]
response = self.client.chat.completions.create( response = self.client.chat.completions.create(
messages=messages, messages=messages,
model=llm_model or self.DEFAULT_MODEL, model=llm_model,
**{**self.DEFAULT_KWARGS, **kwargs}, **kwargs,
) )
return str(response.choices[0].message.content) return response.choices[0].message.content
-39
View File
@@ -4,40 +4,6 @@ from pydantic import Field, SecretStr, field_validator
from pydantic_settings import BaseSettings, SettingsConfigDict from pydantic_settings import BaseSettings, SettingsConfigDict
class LoggingConfig(BaseSettings):
"""The class that holds all the logging settings for the application."""
enabled: bool = Field(False, description="Enable logging")
model_config = SettingsConfigDict(extra="forbid")
def enable_logfire(self, **kwargs) -> None:
"""Enable logging for the application."""
# adding imports here to avoid forced dependencies
try:
import logfire
from logging import basicConfig
except ImportError as e:
raise ImportError(
"To enable logging, please install logfire: `pip install logfire`"
) from e
self.enabled = True
logfire.configure(**kwargs)
basicConfig(handlers=[logfire.LogfireLoggingHandler()])
try:
logfire.configure(**kwargs)
basicConfig(handlers=[logfire.LogfireLoggingHandler()])
except Exception as e:
self.enabled = False # Reset flag on failure
raise RuntimeError("Failed to configure logging") from e
def disable_logfire(self) -> None:
"""Disable logging for the application."""
self.enabled = False
class Settings(BaseSettings): class Settings(BaseSettings):
"""The class that holds all the API keys for the application.""" """The class that holds all the API keys for the application."""
@@ -45,18 +11,13 @@ class Settings(BaseSettings):
None, description="API key for Anthropic" None, description="API key for Anthropic"
) )
GROQ_API_KEY: Optional[SecretStr] = Field(None, description="API key for Groq") GROQ_API_KEY: Optional[SecretStr] = Field(None, description="API key for Groq")
GEMINI_API_KEY: Optional[SecretStr] = Field(None, description="API key for Gemini")
OPENAI_API_KEY: Optional[SecretStr] = Field(None, description="API key for OpenAI") OPENAI_API_KEY: Optional[SecretStr] = Field(None, description="API key for OpenAI")
OLLAMA_HOST_URL: Optional[str] = Field(
"http://127.0.0.1:11434", description="Fully qualified host URL for Ollama"
)
XAI_API_KEY: Optional[SecretStr] = Field(None, description="API key for xAI") 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_PROVIDER: str = Field("openai", description="The default LLM provider")
model_config = SettingsConfigDict( model_config = SettingsConfigDict(
env_file=".env", env_file_encoding="utf-8", case_sensitive=True, extra="ignore" env_file=".env", env_file_encoding="utf-8", case_sensitive=True, extra="ignore"
) )
logging: LoggingConfig = LoggingConfig()
@field_validator("*", mode="before") @field_validator("*", mode="before")
@classmethod @classmethod
+10 -35
View File
@@ -1,38 +1,13 @@
import difflib from typing import Union
from .providers import BaseProvider, providers from .providers import providers
_PROVIDER_NAMES = [provider.NAME.lower() for provider in providers]
def find_provider(provider_name: str | None) -> BaseProvider: def find_provider(provider_name: Union[str, None]):
""" """Find a provider by name."""
Find and instantiate a provider by name. if provider_name:
for provider_class in providers:
Parameters: if provider_class.NAME.lower() == provider_name.lower():
provider_name (Union[str, None]): The name of the provider to find. # Instantiate the provider
return provider_class()
Returns: raise ValueError(f"Provider {provider_name} not found")
An instance of the provider class if found.
Raises:
ValueError: If the provider is not specified or is not found, with a suggestion for the closest match.
"""
if provider_name is None:
raise ValueError("No provider specified.")
# Find the provider by name.
for provider_class in providers:
if provider_class.NAME.lower() == provider_name.lower():
# Instantiate the provider
return provider_class()
# Find the closest match
provider_found = difflib.get_close_matches(
provider_name.lower(), _PROVIDER_NAMES, n=1
)
if provider_found:
raise ValueError(
f"Provider {provider_name!r} not found. Did you mean {provider_found[0]!r}?"
)
raise ValueError(f"Provider {provider_name} not found.")
-15
View File
@@ -1,15 +0,0 @@
import os
import sys
import pytest
# 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 @@
from pydantic import BaseModel
import pytest
from simplemind.providers import Anthropic, Gemini, Groq, Ollama, OpenAI
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 isinstance(data.result, int)
-22
View File
@@ -1,22 +0,0 @@
import pytest
from simplemind.providers import Anthropic, Gemini, Groq, Ollama, OpenAI
@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