Simple context management for AI conversations
Project description
AIContext
Simple context management for AI chat applications. Handles message history and formatting across different LLM providers.
Example
from aicontext import AIChatContext, ai_chat_context
from openai import OpenAI
from anthropic import Anthropic
# Initialize
context = AIChatContext(system_prompt="You are a helpful assistant.")
openai, anthropic = OpenAI(), Anthropic()
# Create chat functions
@ai_chat_context(context)
def ask_gpt(prompt: str):
return openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "system", "content": context.system_prompt}] + context.get_messages()
)
@ai_chat_context(context)
def ask_claude(prompt: str):
return anthropic.messages.create(
model="claude-3-5-sonnet-20240620",
system=context.system_prompt,
messages=context.get_messages(),
max_tokens=1024
)
# Chat with different models
ask_gpt("What's the capital of France?")
ask_claude("What's interesting about it?")
print(context.format()) # Print conversation
More Examples
Named Assistants
@ai_chat_context(context, assistant_name="Researcher")
def researcher(prompt: str):
return anthropic.messages.create(
model="claude-3-5-sonnet-20240620",
system=context.system_prompt,
messages=context.get_messages(),
max_tokens=1024
)
@ai_chat_context(context, assistant_name="Coder")
def coder(prompt: str):
return openai.chat.completions.create(
model="gpt-4",
messages=[{"role": "system", "content": context.system_prompt}] + context.get_messages()
)
# Use different assistants
researcher("Explain quantum computing")
coder("Show me a quantum random number generator")
# Get messages by assistant
print(context.format(assistant_name="Researcher"))
print(context.latest(assistant_name="Coder"))
Message Filtering
# Last 5 messages
print(context.format(limit=5))
# Filter by assistant and role
print(context.format(
assistant_name="Researcher",
role="assistant",
limit=2
))
# Get raw messages for API
messages = context.get_messages()
Save & Load
# Save context
saved = context.to_json()
# Load in new context
new_context = AIChatContext(messages=saved)
Installation
pip install aicontext
API Reference
AIChatContext
AIChatContext(
system_prompt: Optional[str] = None, # System instructions
max_messages: Optional[int] = None, # Message limit
messages: Optional[Union[List[Dict], str]] = None # Load existing
)
Methods
| Method | Description |
|---|---|
get_messages() |
Get messages for API calls |
filter(assistant_name=None, role=None, limit=None) |
Filter messages |
format(**kwargs) |
Get formatted history |
latest(assistant_name=None) |
Get latest message |
to_json() |
Export to JSON |
load_messages(messages) |
Load messages |
clear(assistant_name=None) |
Clear history |
Decorator
@ai_chat_context(
context: AIChatContext, # Context instance
assistant_name: str = "default" # Optional name
)
Filtering Options
context.filter(
assistant_name="Researcher", # Filter by assistant
role="assistant", # Filter by role
limit=5, # Limit results
offset=2 # Skip messages
)
License
MIT
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
aicontext-0.0.1.tar.gz
(4.2 kB
view details)
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 aicontext-0.0.1.tar.gz.
File metadata
- Download URL: aicontext-0.0.1.tar.gz
- Upload date:
- Size: 4.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
22d2a0fb1fd7a90e07dab32c828f972f80d9023fb6ebafa28865ae2fd7c11154
|
|
| MD5 |
7051834032c8d52c29f58547c559dd3a
|
|
| BLAKE2b-256 |
11a71047ee5afd0094bba959444180d15aa1cc8b36fce6f7aaaf2982e0e2f833
|
File details
Details for the file aicontext-0.0.1-py3-none-any.whl.
File metadata
- Download URL: aicontext-0.0.1-py3-none-any.whl
- Upload date:
- Size: 4.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e01c0b6e08d34b526432ce342f18631587dbe34871c4a84e1e5705d102b8f5cc
|
|
| MD5 |
2c4e1d9ab01ed9aa063924db8a7409d7
|
|
| BLAKE2b-256 |
de9b663f9b003b5d8b8b507affa0f6620a86e05eefc6ddca5cef2c281c13ae97
|