A unified Python SDK for querying AI models from multiple providers
Project description
ai-query
The framework for building stateful, distributed AI agents.
ai-query is a unified Python SDK that transforms AI models into stateful Actors. It provides a robust foundation for building agents that maintain memory, persist identity, and communicate via type-safe RPC.
Key Features
- Actor Model: Sequential message processing to prevent race conditions.
- Serverless Ready: Adapters for FastAPI, Vercel, and AWS Lambda.
- Location Transparency: Call agents locally or remotely using the same API.
- Durable Identity: Native support for SQLite, Redis, and Memory storage.
- Durable Event Log: Persist every event and replay automatically on reconnection.
- Type-Safe RPC: Call other agents fluently with full IDE autocompletion.
- Unified Providers: One interface for OpenAI, Anthropic, Google, DeepSeek, and more.
- MCP Native: Seamlessly use tools from any Model Context Protocol server.
Installation
pip install ai-query
# with MCP support
pip install "ai-query[mcp]"
Quick Start: The Stateful Agent
Create an agent that remembers context and persists history automatically.
import asyncio
from ai_query.agents import Agent, SQLiteStorage
from ai_query.providers import openai
async def main():
# Persistent agent with SQLite storage
agent = Agent(
"my-assistant",
model=openai("gpt-4o"),
storage=SQLiteStorage("agents.db")
)
async with agent:
# Agent remembers conversation history automatically
response = await agent.chat("Hi, I'm Alice!")
print(response) # "Hello Alice! How can I help you today?"
response = await agent.chat("What's my name?")
print(response) # "Your name is Alice."
asyncio.run(main())
Multi-User Routing
Host thousands of independent agent instances on a single server with automatic routing.
from ai_query.agents import Agent, AgentServer
from ai_query.providers import google
class UserAssistant(Agent):
def __init__(self, id):
super().__init__(
id,
model=google("gemini-2.0-flash"),
system="You are a personal assistant."
)
# Start server - routes to /agent/{id}/ws and /agent/{id}/chat automatically
AgentServer(UserAssistant).serve(port=8080)
Serverless & Distributed
Run your agents anywhere using the built-in Registry and Adapters.
1. Deploy to Serverless (FastAPI/Vercel/Lambda)
from fastapi import FastAPI
from ai_query.adapters.fastapi import AgentRouter
from my_agent import MyAgent
app = FastAPI()
# Mounts /agent/bot/{chat, invoke, state}
app.include_router(AgentRouter(MyAgent("bot")), prefix="/agent/bot")
2. Consume Remotely
from ai_query import connect
# Connect to the remote agent - looks exactly like a local object
agent = connect("https://api.myapp.com/agent/bot")
response = await agent.chat("Hello!")
3. Compose Local & Remote
Mix and match agents in your workflow without changing your business logic.
from ai_query import AgentRegistry, AgentServer, HTTPTransport
registry = AgentRegistry()
registry.register("writer", WriterAgent) # Local
registry.register("researcher", HTTPTransport("https://lambda...")) # Remote
# The server handles routing automatically
AgentServer(registry).serve()
Type-Safe RPC
Agents can expose structured Actions and call each other fluently.
from ai_query.agents import Agent, action
class Researcher(Agent):
@action
async def get_summary(self, topic: str):
return await self.chat(f"Summarize {topic}")
class Manager(Agent):
async def handle_request(self, topic: str):
# Call another agent with full type safety and autocompletion
researcher = self.call("research-bot", agent_cls=Researcher)
summary = await researcher.get_summary(topic=topic)
return summary
Real-time Events
Send custom feedback or status updates to connected clients using emit.
class ResearchAgent(Agent):
async def on_message(self, conn, msg):
await self.emit("status", {"text": "Searching web..."})
# ... logic ...
await self.emit("status", {"text": "Synthesizing results..."})
Durability & Replay
Enable the enable_event_log flag to persist every event. If a client disconnects, they can reconnect with their last_event_id and the agent will automatically replay missed events.
class MyAgent(Agent):
enable_event_log = True # Persists events for automatic replay
async def on_start(self):
await self.emit("ready", {"timestamp": "..."})
Core Generation
If you don't need state, use the core functions directly for one-off tasks.
from ai_query import generate_text, stream_text
from ai_query.providers import anthropic
# Complete response
result = await generate_text(
model=anthropic("claude-3-5-sonnet-latest"),
prompt="Write a poem about agents."
)
# Real-time streaming
result = stream_text(
model=anthropic("claude-3-5-sonnet-latest"),
prompt="Explain quantum physics."
)
async for chunk in result.text_stream:
print(chunk, end="", flush=True)
Modular Imports
The library is strictly divided for a clean developer experience:
ai_query: Core generation (generate_text,stream_text,embed).ai_query.agents: Stateful orchestration (Agent,AgentServer,Storage).ai_query.providers: Model gateways (openai,anthropic,google, etc.).ai_query.mcp: Model Context Protocol integration.
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
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 ai_query-1.7.3.tar.gz.
File metadata
- Download URL: ai_query-1.7.3.tar.gz
- Upload date:
- Size: 626.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d27db1dc8d2a05c05b140a743192cdbe2a71058e28135d4de609276baf5b0db
|
|
| MD5 |
76d659ea5c21616a490bda8808f48201
|
|
| BLAKE2b-256 |
50c85d5d6c99509fd15f71b28cc624f6737103053bf7c98fdcd4a692fa955e70
|
Provenance
The following attestation bundles were made for ai_query-1.7.3.tar.gz:
Publisher:
release.yml on Abdulmumin1/ai-query
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ai_query-1.7.3.tar.gz -
Subject digest:
0d27db1dc8d2a05c05b140a743192cdbe2a71058e28135d4de609276baf5b0db - Sigstore transparency entry: 855618593
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@eeb2dac205c8671f9a8ea2dc7de034bc7083919b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Abdulmumin1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@eeb2dac205c8671f9a8ea2dc7de034bc7083919b -
Trigger Event:
push
-
Statement type:
File details
Details for the file ai_query-1.7.3-py3-none-any.whl.
File metadata
- Download URL: ai_query-1.7.3-py3-none-any.whl
- Upload date:
- Size: 79.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf82418e3061799f4a2d1c6adcd9cac3aacbda6b846a7398af6ac76700860111
|
|
| MD5 |
b6d59f3d5e4bcbc35717ac712b059535
|
|
| BLAKE2b-256 |
ab65c2de2ee5b13fafd9456db5d2b974b0f2960fce7adfc79729b29275e76371
|
Provenance
The following attestation bundles were made for ai_query-1.7.3-py3-none-any.whl:
Publisher:
release.yml on Abdulmumin1/ai-query
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ai_query-1.7.3-py3-none-any.whl -
Subject digest:
bf82418e3061799f4a2d1c6adcd9cac3aacbda6b846a7398af6ac76700860111 - Sigstore transparency entry: 855618652
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@eeb2dac205c8671f9a8ea2dc7de034bc7083919b -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Abdulmumin1
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yml@eeb2dac205c8671f9a8ea2dc7de034bc7083919b -
Trigger Event:
push
-
Statement type: