Skip to main content

The easiest, safest, and cleanest way to use AI in your Python code. Inspired by the Vercel AI SDK.

Project description

ai-sdk

Python SDK for unified access to large language models, embeddings, and function/tool calling.

Installation

Install via UV (Python package manager):

uv add ai-sdk

Or with pip:

pip install ai-sdk

Quickstart Examples

import asyncio
from ai_sdk import (
    generate_text,
    stream_text,
    generate_object,
    stream_object,
    openai,
    anthropic,
    tool,
    embed_many,
    cosine_similarity,
)
from ai_sdk.types import CoreSystemMessage, CoreUserMessage, TextPart
from pydantic import BaseModel

# 1. Synchronous text completion (prompt)
model = openai("gpt-4o-mini", api_key="YOUR_OPENAI_KEY")
res = generate_text(model=model, prompt="Hello, world!")
print(res.text)

# 2. Chat-based completion (messages)
messages = [
    CoreSystemMessage(content="You are a helpful assistant."),
    CoreUserMessage(content=[TextPart(text="Respond with 'ack'.")]),
]
res = generate_text(model=model, messages=messages)
print(res.text)

# 3. Streaming completion (async)
async def demo_stream():
    stream = stream_text(model=model, prompt="Tell me a joke.")
    async for chunk in stream.text_stream:
        print(chunk, end="")
    print()

asyncio.run(demo_stream())

# 4. Structured object output
class Person(BaseModel):
    name: str
    age: int

res = generate_object(
    model=model,
    schema=Person,
    prompt="Respond with JSON: {'name':'Alice','age':30}"  # no markdown
)
print(res.object)

# 5. Streaming structured output (async)
async def demo_stream_object():
    stream_obj = stream_object(
        model=model,
        schema=Person,
        prompt="Respond with JSON: {'name':'Bob','age':25}"  # no markdown
    )
    async for _ in stream_obj.object_stream:
        pass
    person = await stream_obj.object(Person)
    print(person)

asyncio.run(demo_stream_object())

# 6. Embeddings + cosine similarity
embed_model = openai.embedding("text-embedding-3-small")
values = ["cat", "dog"]
emb = embed_many(model=embed_model, values=values)
print([len(v) for v in emb.embeddings])
print(cosine_similarity(emb.embeddings[0], emb.embeddings[1]))

# 7. Tool/function calling
# Define a simple 'double' tool
def double_exec(x: int) -> int:
    return x * 2

double_tool = tool(
    name="double",
    description="Double an integer.",
    parameters={
        "type": "object",
        "properties": {"x": {"type": "integer"}},
        "required": ["x"],
    },
    execute=double_exec,
)
res = generate_text(
    model=model,
    prompt="Please double 7 using the tool.",
    tools=[double_tool],
)
print(res.text)

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_sdk_python-0.1.0.tar.gz (38.8 kB view details)

Uploaded Source

Built Distribution

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

ai_sdk_python-0.1.0-py3-none-any.whl (27.1 kB view details)

Uploaded Python 3

File details

Details for the file ai_sdk_python-0.1.0.tar.gz.

File metadata

  • Download URL: ai_sdk_python-0.1.0.tar.gz
  • Upload date:
  • Size: 38.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.6.4

File hashes

Hashes for ai_sdk_python-0.1.0.tar.gz
Algorithm Hash digest
SHA256 44233669908eba6c5a36ad51ad7c8539957135c6a0184c02d7c5bea2af54a05e
MD5 362f71c4e591813f89cef62a7b4a86c8
BLAKE2b-256 f66d35ce7f752ca90c4123c3d611aa0b1e13ef6b07d6e3d14b1197a9c272cff9

See more details on using hashes here.

File details

Details for the file ai_sdk_python-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for ai_sdk_python-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8d915111bac7a8dfcb65c0525a996926c842492549ea65bb020ee166bfbee4a
MD5 992494f1819edb822a60cbdcc713654d
BLAKE2b-256 ab4db22d40fac3b05d63fbc3509c1a76a4f6c5c74512afb297075f00545f8dfa

See more details on using hashes here.

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