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.2.tar.gz (53.1 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.2-py3-none-any.whl (19.0 kB view details)

Uploaded Python 3

File details

Details for the file ai_query-0.0.2.tar.gz.

File metadata

  • Download URL: ai_query-0.0.2.tar.gz
  • Upload date:
  • Size: 53.1 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.2.tar.gz
Algorithm Hash digest
SHA256 eb1670e36cf60e5f57f3992ed0c984b51c74351b47dccbfe4839b959cd11014c
MD5 94e337cd42abe5db6bb82f0ebf869c51
BLAKE2b-256 712bfb6078102203b74a29a5990a337515415d7c8d2683502daac67daf113b76

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_query-0.0.2.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.2-py3-none-any.whl.

File metadata

  • Download URL: ai_query-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 19.0 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.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0a8285964191d0a79d49170aa3a2006742b20c9eec17f889ce4e6524d3cdc33b
MD5 8fc1ca7442861aaf020c0cadeea90305
BLAKE2b-256 d824d3b394d102c5439ead8121c817c430b7141174aca48f62f13e6c9a51a497

See more details on using hashes here.

Provenance

The following attestation bundles were made for ai_query-0.0.2-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