The highest-level interface for various LLM APIs.
Project description
chatterer
chatterer is a Python library that provides a unified interface for interacting with various Language Model (LLM) backends. It abstracts over different providers such as OpenAI, Anthropic, DeepSeek, Ollama, and Langchain, allowing you to generate completions, stream responses, and even validate outputs using Pydantic models.
Features
-
Unified LLM Interface
Define a common interface (LLM) for generating completions and streaming responses regardless of the underlying provider. -
Multiple Backend Support
Built-in support for:- InstructorLLM: Integrates with OpenAI, Anthropic, and DeepSeek.
- OllamaLLM: Supports the Ollama model with optional streaming and formatting.
- LangchainLLM: Leverages Langchain’s chat models with conversion utilities.
-
Pydantic Integration
Easily validate and structure LLM responses by leveraging Pydantic models with methods likegenerate_pydanticandgenerate_pydantic_stream.
Installation
Assuming chatterer is published on PyPI, install it via pip:
pip install chatterer
Alternatively, clone the repository and install manually:
git clone https://github.com/yourusername/chatterer.git
cd chatterer
pip install -r requirements.txt
Usage
Importing the Library
You can import the core components directly from chatterer:
from chatterer import LLM, InstructorLLM, OllamaLLM, LangchainLLM
Example 1: Using InstructorLLM with OpenAI
from chatterer import InstructorLLM
from openai.types.chat import ChatCompletionMessageParam
# Create an instance for OpenAI using the InstructorLLM wrapper
llm = InstructorLLM.openai(call_kwargs={"model": "o3-mini"})
# Define a conversation message list
messages: list[ChatCompletionMessageParam] = [
{"role": "user", "content": "Hello, how can I help you?"}
]
# Generate a completion
response = llm.generate(messages)
print("Response:", response)
# Stream the response incrementally
print("Streaming response:")
for chunk in llm.generate_stream(messages):
print(chunk, end="")
Example 2: Using OllamaLLM
from chatterer import OllamaLLM
from openai.types.chat import ChatCompletionMessageParam
# Initialize an OllamaLLM instance with streaming enabled
llm = OllamaLLM(model="ollama-model", stream=True)
messages: list[ChatCompletionMessageParam] = [
{"role": "user", "content": "Tell me a joke."}
]
# Generate and print the full response
print("Response:", llm.generate(messages))
# Stream the response chunk by chunk
print("Streaming response:")
for chunk in llm.generate_stream(messages):
print(chunk, end="")
Example 3: Using LangchainLLM
from chatterer import LangchainLLM
from openai.types.chat import ChatCompletionMessageParam
# Ensure you have a Langchain chat model instance; for example:
from langchain_core.language_models.chat_models import BaseChatModel
client: BaseChatModel = ... # Initialize your Langchain chat model here
llm = LangchainLLM(client=client)
messages: list[ChatCompletionMessageParam] = [
{"role": "user", "content": "What is the weather like today?"}
]
# Generate a complete response
response = llm.generate(messages)
print("Response:", response)
# Stream the response
print("Streaming response:")
for chunk in llm.generate_stream(messages):
print(chunk, end="")
Example 4: Using Pydantic for Structured Outputs
from pydantic import BaseModel
from chatterer import InstructorLLM
from openai.types.chat import ChatCompletionMessageParam
# Define a response model
class MyResponse(BaseModel):
response: str
# Initialize the InstructorLLM instance
llm = InstructorLLM.openai()
messages: list[ChatCompletionMessageParam] = [
{"role": "user", "content": "Summarize this text."}
]
# Generate a structured response using a Pydantic model
structured_response = llm.generate_pydantic(MyResponse, messages)
print("Structured Response:", structured_response.response)
API Overview
LLM (Abstract Base Class)
- Methods:
-
generate(messages: Sequence[ChatCompletionMessageParam]) -> str
Generate a complete text response from a list of messages. -
generate_stream(messages: Sequence[ChatCompletionMessageParam]) -> Iterator[str]
Stream the response incrementally. -
generate_pydantic(response_model: Type[P], messages: Sequence[ChatCompletionMessageParam]) -> P
Generate and validate the response using a Pydantic model. -
generate_pydantic_stream(response_model: Type[P], messages: Sequence[ChatCompletionMessageParam]) -> Iterator[P]
(Optional) Stream validated responses as Pydantic models.
-
InstructorLLM
- Factory methods to create instances with various backends:
openai()anthropic()deepseek()
OllamaLLM
- Supports additional options such as:
model,stream,format,tools,options,keep_alive
LangchainLLM
- Integrates with Langchain's BaseChatModel and converts messages to a compatible format.
Contributing
Contributions are highly encouraged! If you find a bug or have a feature request, please open an issue or submit a pull request on the repository. When contributing, please ensure your code adheres to the existing style and passes all tests.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chatterer-0.1.2.tar.gz.
File metadata
- Download URL: chatterer-0.1.2.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
baf89e77c45ce1a7ef0c7a38ddbb9b87aab07ddcb8d1b59b5a44235d99110a12
|
|
| MD5 |
01e7965bd4acf56dbeabdf2bbeb13902
|
|
| BLAKE2b-256 |
fc09aa09ab210ca2286b85e0e08d7448a1075c6394430dd39df7b9a0ed8787ae
|
File details
Details for the file chatterer-0.1.2-py3-none-any.whl.
File metadata
- Download URL: chatterer-0.1.2-py3-none-any.whl
- Upload date:
- Size: 6.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.6.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9e57e3f2a7ad5b804819559676add2c6c694adfdfa68d599720d1652a5da0a8b
|
|
| MD5 |
671322a30d60a7bc6b7b6a0c5162c4c4
|
|
| BLAKE2b-256 |
aa8d24af40b8ed4bb0762439133f3cb93c330eea2d82703aae566a6f10d0ad4e
|