Skip to main content

TypedAI

PyPI - Version Tests - Status

TypedAi is a Python library that simplifies the OpenAI chat completions api using type hints.

Think Typer / FastApi for LLM interactions.

The library has no dependencies other than openai and pydantic.

pip install eidolon-typedai

Easily Define Output Format

Defining json schema by hand error-prone. With TypedAI, you can define the output format with python types that are transformed to JsonSchema.

TypedAI then stores those types with the response to be used later parsing.

from typedai import TypedAI
from pydantic import BaseModel

class Response(BaseModel):
    philosopher: str
    meaning_of_life: str

typed_completion = TypedAI().completions.create(  # returns TypedChatCompletion[Response]
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the meaning of life according to Douglas Adams?"},
    ],
    response_type=Response
)
print(typed_completion.parse_content().meaning_of_life)  # 42

IDE Type Hinting

Sure you can code without them. You can live on rice and beans too, but you sure don't want to.

Alt text

Define tool calls with function signatures

from typedai import TypedAI
from typing import Literal

def get_meaning_of_life(philosopher: Literal["Douglas Adams"]) -> str:
    """Call this tool to get the meaning of life according to a philosopher."""
    if philosopher == "Douglas Adams":
        return "42"

completion_with_tool_calls = TypedAI().completions.create(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the meaning of life according to Douglas Adams?"},
    ],
    fn_tools=[get_meaning_of_life],
)

Easily Construct Response Messages

When a llm completion has tool calls, you normally need to parse the response and execute the function.

Since you defined your tools with functions, TypedAI can execute them and build the response messages for you.

from typedai import TypedAI
from above_example import get_meaning_of_life

messages=[
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "What is the meaning of life according to Douglas Adams?"},
]
completion_with_tool_calls = TypedAI().completions.create(
    model="gpt-3.5-turbo", messages=messages, fn_tools=[get_meaning_of_life]
)
if completion_with_tool_calls.has_tool_calls():
    messages.extend(completion_with_tool_calls.build_messages())
    completion_with_tool_calls = TypedAI().completions.create(
        model="gpt-3.5-turbo", messages=messages, fn_tools=[get_meaning_of_life]
    )

Full Streaming Support (even with typed responses)

And this all works great with streaming completions too!

from typedai import TypedAI
from above_example import Response


typed_stream = TypedAI().completions.stream(
    model="gpt-3.5-turbo",
    messages=[
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "What is the meaning of life according to Douglas Adams?"},
    ],
    response_type=Response,
)

for chunk in typed_stream:
    print(chunk)  # iterates ChatCompletionChunk just like you would expect

typed_stream.completion()  # aggregated TypedCompletion[Response] (with type hints!)

Purely Additive

TypedAI is purely additive. You can use it as much or as little as you want. It doesn't change the way you use OpenAI's API, it just makes it easier.

The objects used are either the raw openai objects or a minimally altered TypedAI child object with some additional functionality. Even dumping the models will give you unchanged results.

Similarly, no parsing or validation is done without explicit method calls, so the additional functionality will never slow you down.

Metadata

Release files for eidolon-typedai 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for eidolon-typedai 0.1.8
File Size Uploaded
eidolon_typedai-0.1.8.tar.gz 9.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for eidolon-typedai 0.1.8
File Interpreter ABI Platform
eidolon_typedai-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 19.1 kB

Release files / eidolon_typedai-0.1.8.tar.gz

Download URL eidolon_typedai-0.1.8.tar.gz
Size 9.1 kB
Tags Source
SHA-256 checksum
How to use checksums
3071637ea24602b93bc7006ee35010afab170c5cedf4c105f55ff207acaa89b5
BLAKE2b-256 checksum
How to use checksums
59a89158339bb1c18b824e219ace24b272f47f56e6cc1e0f6ee2fa4c8076b816
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

Release files / eidolon_typedai-0.1.8-py3-none-any.whl

Download URL eidolon_typedai-0.1.8-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f6fee68a7c30e0da593d93cac885fce4f2e7b89beb32b8d8fbb57a774c3d2f1c
BLAKE2b-256 checksum
How to use checksums
0c218486cec6ccbaa7b9fee764f310b874c40b761288af097f0c1f36f76e9659
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

Release history Release notifications | RSS feed

This release

0.1.8 This release

2 release files

0.1.7

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page