A framework for managing AI assistants
Project description
LLM Assistant Framework
LLM Assistant Framework is an open-source Python library for building AI assistants using various Large Language Models (LLMs). Inspired by OpenAI's Assistants API, this framework is designed to be flexible, easily adoptable, and compatible with various LLMs.
Key Features
- Easy-to-use API for creating and managing AI assistants
- Flexible integration with custom LLM providers
- Thread-based conversation management
- Asynchronous execution of assistant runs
- Customizable function calling with type-safe implementations
- Support for multiple assistants in a single thread
- Fine-grained control over assistant behavior and model parameters
- Robust error handling and exception management
- Extensible architecture for adding new tools and capabilities
TODO
The LLM Assistant Framework is an ongoing project, and there are several features and improvements planned for future releases:
- Adding more tools: Expand the library of built-in tools for common tasks.
- Adding streaming: Implement support for streaming responses from LLMs.
- Adding JSON Schema support: Enhance function definitions with JSON Schema for better type validation.
- Implement caching mechanisms: Add caching for LLM responses to improve performance and reduce API calls.
- Enhance error handling: Provide more granular error types and improve error messages for better debugging.
- Add support for file attachments: Allow file uploads and downloads in conversations.
- Implement conversation memory management: Add features to manage long-term memory and context for assistants.
- Improve documentation: Expand and enhance the documentation with more examples and use cases.
- Implement logging and monitoring: Add comprehensive logging and monitoring features for better observability.
Installation
You can install the LLM Assistant Framework using pip:
pip install assinstants
For developers who want to contribute or modify the framework:
git clone https://github.com/lahfir/assinstants.git
cd assinstants
pip install -e .
Project Structure
assinstants/
├── core/
│ ├── __init__.py
│ ├── assistant_manager.py
│ ├── thread_manager.py
│ └── run_manager.py
├── models/
│ ├── __init__.py
│ ├── assistant.py
│ ├── base.py
│ ├── function.py
│ ��── message.py
│ ├── run.py
│ ├── shared.py
│ ├── thread.py
│ └── tool.py
└── utils/
└── exceptions.py
Setup and Execution Flow Diagram
Run Execution and Tool Usage Diagram
Asynchronous Operation Diagram
Quick Start Guide
Here's a basic example of how to use the LLM Assistant Framework:
import asyncio
from assinstants.core.assistant_manager import AssistantManager
from assinstants.core.thread_manager import ThreadManager
from assinstants.core.run_manager import RunManager
from assinstants.models.function import FunctionDefinition, FunctionParameter
# Define a custom LLM function (replace with your actual implementation)
async def custom_llm_function(model: str, prompt: str, **kwargs):
# Implement your LLM API call here
return "LLM response"
# Initialize managers
assistant_manager = AssistantManager()
thread_manager = ThreadManager()
run_manager = RunManager(assistant_manager, thread_manager)
# Define a tool
def calculate_sum(a: float, b: float) -> float:
return a + b
sum_function = FunctionDefinition(
name="calculate_sum",
description="Calculate the sum of two numbers",
parameters={
"a": FunctionParameter(type="number", description="First number"),
"b": FunctionParameter(type="number", description="Second number")
},
implementation=calculate_sum
)
async def main():
# Create an assistant
assistant = await assistant_manager.create_assistant(
name="Math Tutor",
instructions="You are a helpful math tutor.",
model="llama3.1",
custom_llm_function=custom_llm_function,
tools=[sum_function]
)
# Create a thread
thread = await thread_manager.create_thread()
# Add the assistant to the thread
await thread_manager.add_assistant_to_thread(thread.id, assistant)
# Add a message to the thread
await thread_manager.add_message(thread.id, "user", "What's 2 + 2?")
# Run the assistant
run = await run_manager.create_and_execute_run(thread.id)
# Get the assistant's response
messages = await thread_manager.get_messages(thread.id)
print(messages[-1].content)
asyncio.run(main())
Core Components
The LLM Assistant Framework consists of four main components:
- Assistants: AI models with specific instructions and capabilities.
- Threads: Conversations or contexts for interactions.
- Messages: Individual pieces of communication within a thread.
- Runs: Executions of an assistant on a specific thread.
The framework provides three main manager classes to interact with these components:
AssistantManager: For creating and managing assistantsThreadManager: For managing conversation threads and messagesRunManager: For executing and managing assistant runs
Advanced Usage
Using Multiple Assistants in a Thread
async def main():
assistant1 = await assistant_manager.create_assistant(name="Math Tutor", ...)
assistant2 = await assistant_manager.create_assistant(name="Writing Assistant", ...)
thread = await thread_manager.create_thread()
await thread_manager.add_assistant_to_thread(thread.id, assistant1)
await thread_manager.add_assistant_to_thread(thread.id, assistant2)
await thread_manager.add_message(thread.id, "user", "Can you help me with my math homework?")
run = await run_manager.create_and_execute_run(thread.id)
await thread_manager.add_message(thread.id, "user", "Now, can you help me write an essay about the math concepts I just learned?")
run = await run_manager.create_and_execute_run(thread.id)
Defining Custom Functions
from assinstants.models.function import FunctionDefinition, FunctionParameter
from assinstants.models.tool import Tool, FunctionTool
def calculate_square_root(x: float) -> float:
return x ** 0.5
tools = [
Tool(
tool=FunctionTool(
function=FunctionDefinition(
name="calculate_square_root",
description="Calculate the square root of a number",
parameters={
"x": FunctionParameter(
type="number",
description="The number to calculate the square root of"
)
},
implementation=calculate_square_root
)
)
)
]
assistant = await assistant_manager.create_assistant(
name="Math Assistant",
tools=tools,
...
)
Error Handling
from assinstants.utils.exceptions import AssistantNotFoundError, ThreadNotFoundError
try:
assistant = await assistant_manager.get_assistant("non_existent_id")
except AssistantNotFoundError as e:
print(f"Error: {e}")
try:
thread = await thread_manager.get_thread("non_existent_id")
except ThreadNotFoundError as e:
print(f"Error: {e}")
Customization
Integrating Custom LLM Providers
To integrate a custom LLM provider, create a function that implements the LLM API call and pass it to the create_assistant method:
async def my_custom_llm_function(model: str, prompt: str, **kwargs):
# Implement your custom LLM API call here
return "LLM response"
assistant = await assistant_manager.create_assistant(
name="Custom Assistant",
custom_llm_function=my_custom_llm_function,
...
)
Adding New Tools and Functions
To add new tools or functions to assistants, create FunctionDefinition objects with the necessary parameters and logic, then pass them to the assistant during creation:
from assinstants.models.function import FunctionDefinition, FunctionParameter
from assinstants.models.tool import Tool, FunctionTool
def my_custom_function(param1: str, param2: float) -> str:
# Your function logic here
return f"Result: {param1}, {param2}"
tools = [
Tool(
tool=FunctionTool(
function=FunctionDefinition(
name="my_custom_function",
description="Description of what the function does",
parameters={
"param1": FunctionParameter(
type="string", description="Description of param1"
),
"param2": FunctionParameter(
type="number", description="Description of param2"
),
},
implementation=my_custom_function,
)
)
)
]
assistant = await assistant_manager.create_assistant(
name="Custom Assistant",
tools=tools,
...
)
Error Handling
The LLM Assistant Framework provides several exception classes for handling specific errors:
AssistantNotFoundError: Raised when an assistant is not foundThreadNotFoundError: Raised when a thread is not foundInvalidProviderError: Raised when an invalid LLM provider is specifiedRunExecutionError: Raised when there's an error during run executionFunctionNotFoundError: Raised when a function is not foundFunctionExecutionError: Raised when there's an error executing a function
Use these exceptions in try-except blocks to handle specific error cases in your application.
Contributing
I welcome contributions from the community to help implement these features and improve the framework. If you're interested in working on any of these items, please check our issues page or open a new issue to discuss your ideas:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and write tests if applicable
- Ensure all tests pass
- Submit a pull request with a clear description of your changes
For bug reports or feature requests, please open an issue on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Version Management
For detailed information on our version management and branching strategy, please refer to the VERSION_MANAGEMENT.md file. This document outlines our approach to semantic versioning, branching strategy, and release process.
Contributing
I welcome contributions from the community to help implement these features and improve the framework. If you're interested in working on any of these items, please check our issues page or open a new issue to discuss your ideas:
- Fork the repository
- Create a new branch for your feature or bug fix
- Make your changes and write tests if applicable
- Ensure all tests pass
- Submit a pull request with a clear description of your changes
For bug reports or feature requests, please open an issue on the GitHub repository.
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 assinstants-1.1.0.tar.gz.
File metadata
- Download URL: assinstants-1.1.0.tar.gz
- Upload date:
- Size: 355.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8f857f49cbd060141ab215a88ae7d9dff14123e4d94cafb153eb8803d173da24
|
|
| MD5 |
f4326bf1b4d46d3c23b0fc04ee04a9e2
|
|
| BLAKE2b-256 |
8f251c70a16d5f16d7ced82c127bbf2d88f74e4e94542de48da0a53ca645375f
|
File details
Details for the file assinstants-1.1.0-py3-none-any.whl.
File metadata
- Download URL: assinstants-1.1.0-py3-none-any.whl
- Upload date:
- Size: 17.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.1 CPython/3.12.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
732482cc939dc43ff4fe4a61f2926956e27375508a93be641e49d77037c4ad3a
|
|
| MD5 |
c76bacf5cc44d52957be9ab107219287
|
|
| BLAKE2b-256 |
1957c5a0aa14acb0a6c1aa9900344816284718f0e831b55a8059a8c76c4aeff0
|