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
Release files for aicontext 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| aicontext-0.0.1.tar.gz | 4.2 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| aicontext-0.0.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 8.7 kB
Release files / aicontext-0.0.1.tar.gz
| Download URL | aicontext-0.0.1.tar.gz |
|---|---|
| Size | 4.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
22d2a0fb1fd7a90e07dab32c828f972f80d9023fb6ebafa28865ae2fd7c11154
|
|
BLAKE2b-256 checksum How to use checksums |
11a71047ee5afd0094bba959444180d15aa1cc8b36fce6f7aaaf2982e0e2f833
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0
|
Release files / aicontext-0.0.1-py3-none-any.whl
| Download URL | aicontext-0.0.1-py3-none-any.whl |
|---|---|
| Size | 4.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
e01c0b6e08d34b526432ce342f18631587dbe34871c4a84e1e5705d102b8f5cc
|
|
BLAKE2b-256 checksum How to use checksums |
de9b663f9b003b5d8b8b507affa0f6620a86e05eefc6ddca5cef2c281c13ae97
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.4 CPython/3.13.0 Darwin/24.1.0
|