A Python framework for building AI agents with multi-provider LLM support, persistent memory, and function calling capabilities.
Project description
Linden
A Python framework for building AI agents with multi-provider LLM support, persistent memory, and function calling capabilities.
Table of Contents
- Overview
- Features
- Installation
- Requirements
- Quick Start
- Configuration
- Architecture
- Advanced Usage
- API Reference
- Error Types
- Contributing
- License
- Support
Overview
Linden is a comprehensive AI agent framework that provides a unified interface for interacting with multiple Large Language Model (LLM) providers including OpenAI, Groq, and Ollama. It features persistent conversation memory, automatic tool/function calling, and robust error handling for building production-ready AI applications.
Features
- Multi-Provider LLM Support: Seamless integration with OpenAI, Groq, and Ollama
- Persistent Memory: Long-term conversation memory using FAISS vector storage and embeddings
- Function Calling: Automatic parsing and execution of tools with Google-style docstring support
- Streaming Support: Real-time response streaming for interactive applications
- Thread-Safe Memory: Concurrent agent support with isolated memory per agent
- Configuration Management: Flexible TOML-based configuration with environment variable support
- Type Safety: Full Pydantic model support for structured outputs
- Error Handling: Comprehensive error handling with retry mechanisms
Installation
pip install linden
Requirements
- Python >= 3.9
- Dependencies automatically installed:
openai- OpenAI API clientgroq- Groq API clientollama- Ollama local LLM clientpydantic- Data validation and serializationmem0- Memory managementdocstring_parser- Function documentation parsing
Quick Start
Basic Agent Setup
from linden.core import AgentRunner, Provider
# Create a simple agent
agent = AgentRunner(
name="assistant",
model="gpt-4",
temperature=0.7,
system_prompt="You are a helpful AI assistant.",
client=Provider.OPENAI
)
# Ask a question
response = agent.run("What is the capital of France?")
print(response)
Agent with Function Calling
def get_weather(location: str, units: str = "celsius") -> str:
"""Get current weather for a location.
Args:
location (str): The city name or location
units (str, optional): Temperature units (celsius/fahrenheit). Defaults to celsius.
Returns:
str: Weather information
"""
return f"The weather in {location} is 22°{units[0].upper()}"
# Create agent with tools
agent = AgentRunner(
name="weather_bot",
model="gpt-4",
temperature=0.7,
system_prompt="You are a weather assistant.",
tools=[get_weather],
client=Provider.OPENAI
)
response = agent.run("What's the weather in Paris?")
print(response)
Streaming Responses
# Stream responses for real-time interaction
for chunk in agent.run("Tell me a story", stream=True):
print(chunk, end="", flush=True)
Structured Output with Pydantic
from pydantic import BaseModel
class PersonInfo(BaseModel):
name: str
age: int
occupation: str
agent = AgentRunner(
name="extractor",
model="gpt-4",
temperature=0.1,
system_prompt="Extract person information from text.",
output_type=PersonInfo,
client=Provider.OPENAI
)
result = agent.run("John Smith is a 30-year-old software engineer.")
print(f"Name: {result.name}, Age: {result.age}")
Configuration
Create a config.toml file in your project root:
[models]
dec = "gpt-4"
tool = "gpt-4"
extractor = "gpt-3.5-turbo"
speaker = "gpt-4"
[openai]
api_key = "your-openai-api-key"
timeout = 30
[groq]
base_url = "https://api.groq.com/openai/v1"
api_key = "your-groq-api-key"
timeout = 30
[ollama]
timeout = 60
[memory]
path = "./memory_db"
Environment Variables
Set your API keys as environment variables:
export OPENAI_API_KEY="your-openai-api-key"
export GROQ_API_KEY="your-groq-api-key"
Architecture
Core Components
AgentRunner
The main agent orchestrator that handles:
- LLM interaction and response processing
- Tool calling and execution
- Memory management
- Error handling and retries
- Streaming and non-streaming responses
Memory System
- AgentMemory: Per-agent conversation history and semantic search
- MemoryManager: Thread-safe singleton for shared vector storage
- Persistent Storage: FAISS-based vector database for long-term memory
AI Clients
Abstract interface with concrete implementations:
- OpenAiClient: OpenAI GPT models
- GroqClient: Groq inference API
- Ollama: Local LLM execution
Function Calling
- Automatic parsing of Google-style docstrings
- JSON Schema generation for tool descriptions
- Type-safe argument parsing and validation
- Error handling for tool execution
Memory Architecture
The memory system uses a shared FAISS vector store with agent isolation:
# Each agent has isolated memory
agent1 = AgentRunner(name="agent1", ...)
agent2 = AgentRunner(name="agent2", ...)
# Memories are automatically isolated by agent_id
agent1.run("Remember I like coffee")
agent2.run("Remember I like tea")
# Each agent only retrieves its own memories
Function Tool Definition
Functions must use Google-style docstrings for automatic parsing:
def search_database(query: str, limit: int = 10, filters: dict = None) -> list:
"""Search the knowledge database.
Args:
query (str): The search query string
limit (int, optional): Maximum results to return. Defaults to 10.
filters (dict, optional): Additional search filters:
category (str): Filter by category
date_range (str): Date range in ISO format
Returns:
list: List of search results with metadata
"""
# Implementation here
pass
Advanced Usage
Multi-Turn Conversations
agent = AgentRunner(name="chat_bot", model="gpt-4", temperature=0.7)
# Conversation maintains context automatically
agent.run("My name is Alice")
agent.run("What's my name?") # Will remember "Alice"
agent.run("Tell me about my previous question") # Has full context
Error Handling and Retries
agent = AgentRunner(
name="robust_agent",
model="gpt-4",
temperature=0.7,
retries=3 # Retry failed calls up to 3 times
)
try:
response = agent.run("Complex query that might fail")
except ToolError as e:
print(f"Tool execution failed: {e.message}")
except ToolNotFound as e:
print(f"Tool not found: {e.message}")
Memory Management
# Reset agent memory
agent.reset()
# Add context without user interaction
agent.add_to_context("Important context information", persist=True)
# Get conversation history
history = agent.memory.get_conversation("Current query")
Provider-Specific Features
# Use local Ollama models
local_agent = AgentRunner(
name="local_agent",
model="llama2",
client=Provider.OLLAMA
)
# Use Groq for fast inference
fast_agent = AgentRunner(
name="fast_agent",
model="mixtral-8x7b-32768",
client=Provider.GROQ
)
API Reference
AgentRunner
Constructor Parameters
name(str): Unique agent identifiermodel(str): LLM model nametemperature(int): Response randomness (0-1)system_prompt(str, optional): System instructiontools(list[Callable], optional): Available functionsoutput_type(BaseModel, optional): Structured output schemaclient(Provider): LLM provider selectionretries(int): Maximum retry attempts
Methods
run(user_question: str, stream: bool = False): Execute agent queryreset(): Clear conversation historyadd_to_context(content: str, persist: bool = False): Add contextual information
Memory Classes
AgentMemory
record(message: str, persist: bool = False): Store messageget_conversation(user_input: str): Retrieve relevant contextreset(): Clear agent memory
MemoryManager (Singleton)
get_memory(): Access shared memory instanceget_all_agent_memories(agent_id: str = None): Retrieve stored memories
Configuration
ConfigManager
initialize(config_path: str | Path): Load configuration fileget(config_path: Optional[str | Path] = None): Get configuration instancereload(): Refresh configuration from file
Error Types
ToolNotFound: Requested function not availableToolError: Function execution failedValidationError: Pydantic model validation failedRequestException: HTTP/API communication error
Contributing
- Fork the repository
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Add new feature') - Push to the branch (
git push origin feature/new-feature) - Create a Pull Request
License
This project is licensed under the MIT License - see the LICENSE file for details.
Support
- GitHub Issues: https://github.com/matstech/linden/issues
- Documentation: https://github.com/matstech/linden
Project details
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 linden-0.1.0.tar.gz.
File metadata
- Download URL: linden-0.1.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
65d2457261062866baa2691f62dc48eaaa3bd0f014d909c304c0b9a084fde612
|
|
| MD5 |
2150a657a0f6b636d57edaf2473445e9
|
|
| BLAKE2b-256 |
f024f90845d25f13025747402a7867c80efad09a2c67bddc07d8c979efd0ca68
|
File details
Details for the file linden-0.1.0-py3-none-any.whl.
File metadata
- Download URL: linden-0.1.0-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e189246662e5e4074c83276d9d1186efd2dc35bf01062aae3a7fd438811aeab8
|
|
| MD5 |
59d5e48d664b7c703d9ae94d2813dbe7
|
|
| BLAKE2b-256 |
5ec02bd4303e903ad8bb8e5bdbd3ddd6cfe100c8b738c5b46b161fb2639f2187
|