Skip to main content

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_KEY
  • ANTHROPIC_API_KEY
  • GOOGLE_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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ai_query-0.0.3.tar.gz (53.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ai_query-0.0.3-py3-none-any.whl (19.7 kB view details)

Uploaded Python 3

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

Hashes for ai_query-0.0.3.tar.gz
Algorithm Hash digest
SHA256 08d5eaef649436ddc2484f4bc8741a4e6cd96ba93278b7473352a8d0bfe45e20
MD5 6e94b67a1a6842f8eee83c84d16a4123
BLAKE2b-256 f071817b76daa78421795074bf070c2dcde8627bbb71c77d1a9363b8ae7e2b75

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_query-0.0.3.tar.gz:

Publisher: release.yml on Abdulmumin1/ai-query

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for ai_query-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 90c85cc6afc15169b873703a5f9c0973eda68d65256216570fe99c2913ad02eb
MD5 9e7c0b3a927ca72d7e8b4c8df529c67a
BLAKE2b-256 4eb92698a10e9e66642fd529a6c970a1497609e25402d35ec436505b586830db

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_query-0.0.3-py3-none-any.whl:

Publisher: release.yml on Abdulmumin1/ai-query

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page