A unified Python SDK for querying AI models from multiple providers
Project description
ai-query
A unified Python SDK for querying AI models from various providers with a consistent interface.
Installation
Install using uv or pip:
# Using uv
uv add ai-query
# Using pip
pip install ai-query
Usage
Basic Text Generation
The library provides a simple generate_text function for non-streaming requests.
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 Text
For real-time responses, use stream_text. It returns a TextStreamResult that can be iterated directly.
import asyncio
from ai_query import stream_text, google
async def main():
result = stream_text(
model=google("gemini-2.0-flash"),
prompt="Write a short story about a robot."
)
# Stream the text chunks
async for chunk in result.text_stream:
print(chunk, end="", flush=True)
# Access usage statistics after streaming finishes
usage = await result.usage
print(f"\nTotal tokens: {usage.total_tokens}")
asyncio.run(main())
Multi-modal Messages
You can pass complex message structures, including files and images.
import asyncio
from ai_query import stream_text, google
async def main():
result = stream_text(
model=google("gemini-2.0-flash"),
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": "Summarize this PDF:"},
{
"type": "file",
"data": "https://example.com/document.pdf",
"media_type": "application/pdf"
}
]
}
]
)
async for chunk in result.text_stream:
print(chunk, end="", flush=True)
asyncio.run(main())
Providers
The library supports various AI providers through a unified interface. Built-in support is available for:
- OpenAI:
openai("model-name") - Anthropic:
anthropic("model-name") - Google:
google("model-name")
Configuration
You can configure providers using environment variables (recommended) or by passing credentials directly.
Using Environment Variables:
By default, providers look for standard environment variables:
OPENAI_API_KEYANTHROPIC_API_KEYGOOGLE_API_KEY
Passing API Keys Dynamically:
You can explicitly pass the API key (and other parameters) when initializing a provider.
from ai_query import generate_text, google
async def main():
# Pass API key directly
model = google("gemini-2.0-flash", api_key="your_api_key_here")
result = await generate_text(
model=model,
prompt="Explain quantum computing"
)
print(result.text)
Provider-Specific Options
You can pass specific parameters to the underlying provider using provider_options.
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"}
}
}
)
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-0.0.3.tar.gz.
File metadata
- Download URL: ai_query-0.0.3.tar.gz
- Upload date:
- Size: 53.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
08d5eaef649436ddc2484f4bc8741a4e6cd96ba93278b7473352a8d0bfe45e20
|
|
| MD5 |
6e94b67a1a6842f8eee83c84d16a4123
|
|
| BLAKE2b-256 |
f071817b76daa78421795074bf070c2dcde8627bbb71c77d1a9363b8ae7e2b75
|
Provenance
The following attestation bundles were made for ai_query-0.0.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-0.0.3.tar.gz -
Subject digest:
08d5eaef649436ddc2484f4bc8741a4e6cd96ba93278b7473352a8d0bfe45e20 - Sigstore transparency entry: 798231788
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@e032e510ad632984106cbe100a73b3fee5821a2b -
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@e032e510ad632984106cbe100a73b3fee5821a2b -
Trigger Event:
push
-
Statement type:
File details
Details for the file ai_query-0.0.3-py3-none-any.whl.
File metadata
- Download URL: ai_query-0.0.3-py3-none-any.whl
- Upload date:
- Size: 19.7 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 |
90c85cc6afc15169b873703a5f9c0973eda68d65256216570fe99c2913ad02eb
|
|
| MD5 |
9e7c0b3a927ca72d7e8b4c8df529c67a
|
|
| BLAKE2b-256 |
4eb92698a10e9e66642fd529a6c970a1497609e25402d35ec436505b586830db
|
Provenance
The following attestation bundles were made for ai_query-0.0.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-0.0.3-py3-none-any.whl -
Subject digest:
90c85cc6afc15169b873703a5f9c0973eda68d65256216570fe99c2913ad02eb - Sigstore transparency entry: 798231790
- Sigstore integration time:
-
Permalink:
Abdulmumin1/ai-query@e032e510ad632984106cbe100a73b3fee5821a2b -
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@e032e510ad632984106cbe100a73b3fee5821a2b -
Trigger Event:
push
-
Statement type: