A unified Python SDK for querying AI models from multiple providers
Project description
ai-query
A unified Python SDK for querying AI models from multiple providers with a consistent interface.
Installation
uv add ai-query
# or
pip install ai-query
Quick Start
import asyncio
from ai_query import generate_text, openai
async def main():
result = await generate_text(
model=openai("gpt-4o"),
prompt="What is the capital of France?"
)
print(result.text)
asyncio.run(main())
Streaming
from ai_query import stream_text, google
async def main():
result = stream_text(
model=google("gemini-2.0-flash"),
prompt="Write a short story."
)
async for chunk in result.text_stream:
print(chunk, end="", flush=True)
usage = await result.usage
print(f"\nTokens: {usage.total_tokens}")
Tool Calling
Define tools and let the model use them automatically. The library handles the execution loop. Tools can be defined using the @tool decorator with type hints and the Field class for descriptions.
from ai_query import generate_text, google, tool, Field, step_count_is
# Define tools using decorators
@tool(description="Get the current weather for a location")
async def get_weather(
location: str = Field(description="City name")
) -> str:
# Function implementation
return f"Weather in {location}: 72°F, Sunny"
@tool(description="Perform math calculations")
def calculate(
expression: str = Field(description="Math expression")
) -> str:
return str(eval(expression))
async def main():
result = await generate_text(
model=google("gemini-2.0-flash"),
prompt="What's the weather in Paris? Also, what is 25 * 4?",
tools={
"weather": get_weather,
"calculator": calculate
},
stop_when=step_count_is(5), # Max 5 model calls
)
print(result.text)
print(f"Steps: {len(result.response['steps'])}")
Stop Conditions
Control when the tool execution loop stops:
from ai_query import step_count_is, has_tool_call
# Stop after N model calls
stop_when=step_count_is(5)
# Stop when a specific tool is called
stop_when=has_tool_call("final_answer")
# Multiple conditions (stops when any is true)
stop_when=[step_count_is(10), has_tool_call("done")]
Step Callbacks
Monitor and react to each step in the execution loop with on_step_start and on_step_finish.
from ai_query import generate_text, google, StepStartEvent, StepFinishEvent
def on_start(event: StepStartEvent):
print(f"Step {event.step_number} starting...")
print(f" Messages: {len(event.messages)}")
# event.messages can be modified before the model call
def on_finish(event: StepFinishEvent):
print(f"Step {event.step_number} finished")
# Current step details
if event.step.tool_calls:
for tc in event.step.tool_calls:
print(f" Called: {tc.name}({tc.arguments})")
if event.step.tool_results:
for tr in event.step.tool_results:
print(f" Result: {tr.result}")
# Accumulated state
print(f" Total tokens: {event.usage.total_tokens}")
print(f" Text so far: {event.text[:50]}...")
result = await generate_text(
model=google("gemini-2.0-flash"),
prompt="What's the weather in Tokyo?",
tools={"weather": get_weather},
on_step_start=on_start,
on_step_finish=on_finish,
)
StepStartEvent
| Field | Type | Description |
|---|---|---|
step_number |
int |
1-indexed step number |
messages |
list[Message] |
Conversation history (modifiable) |
tools |
ToolSet | None |
Available tools |
StepFinishEvent
| Field | Type | Description |
|---|---|---|
step_number |
int |
1-indexed step number |
step |
StepResult |
Current step (text, tool_calls, tool_results) |
text |
str |
Accumulated text from all steps |
usage |
Usage |
Accumulated token usage |
steps |
list[StepResult] |
All completed steps |
Both callbacks support sync and async functions.
Providers
Built-in support for:
- OpenAI:
openai("gpt-4o")- usesOPENAI_API_KEY - Anthropic:
anthropic("claude-sonnet-4-20250514")- usesANTHROPIC_API_KEY - Google:
google("gemini-2.0-flash")- usesGOOGLE_API_KEY
Pass API keys directly if needed:
model = google("gemini-2.0-flash", api_key="your_key")
Provider Options
Pass provider-specific parameters:
result = await generate_text(
model=google("gemini-2.0-flash"),
prompt="Tell me a story",
provider_options={
"google": {
"safety_settings": {"HARM_CATEGORY_VIOLENCE": "BLOCK_NONE"}
}
}
)
Examples
See the examples/ folder for agent implementations
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.1.0.tar.gz.
File metadata
- Download URL: ai_query-1.1.0.tar.gz
- Upload date:
- Size: 72.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a995aa1ac97f470c0a95d047a94a623034ca4413e30f76d85157b60b4c0ec279
|
|
| MD5 |
f5213b4655a447aff77ccf9b085df04f
|
|
| BLAKE2b-256 |
14989b490ecef6dfb71b1a1f84bfa082f52d3850780c8647cf6ece60c5637820
|
Provenance
The following attestation bundles were made for ai_query-1.1.0.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.1.0.tar.gz -
Subject digest:
a995aa1ac97f470c0a95d047a94a623034ca4413e30f76d85157b60b4c0ec279 - Sigstore transparency entry: 798615863
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@b218184a92d9b2da9c728d1c3416414d455bc164 -
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@b218184a92d9b2da9c728d1c3416414d455bc164 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ai_query-1.1.0-py3-none-any.whl.
File metadata
- Download URL: ai_query-1.1.0-py3-none-any.whl
- Upload date:
- Size: 29.3 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 |
54e40c0b698ed050df3d3e55aa489d0d151f3a62055f3b60c95fa176e1810974
|
|
| MD5 |
6b1c0e1018205203ffb9b1921f4a614c
|
|
| BLAKE2b-256 |
e33bb3f4e16a277c06d25c2dee2ec076dd7db2a8e785a346a6775bdd66c4be85
|
Provenance
The following attestation bundles were made for ai_query-1.1.0-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.1.0-py3-none-any.whl -
Subject digest:
54e40c0b698ed050df3d3e55aa489d0d151f3a62055f3b60c95fa176e1810974 - Sigstore transparency entry: 798615873
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@b218184a92d9b2da9c728d1c3416414d455bc164 -
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@b218184a92d9b2da9c728d1c3416414d455bc164 -
Trigger Event:
push
-
Statement type: