Skip to main content

Simplify the OpenAI chat completions api using type hints.

Project description

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.

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

eidolon_typedai-0.1.8.tar.gz (9.1 kB view details)

Uploaded Source

Built Distribution

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

eidolon_typedai-0.1.8-py3-none-any.whl (10.1 kB view details)

Uploaded Python 3

File details

Details for the file eidolon_typedai-0.1.8.tar.gz.

File metadata

  • Download URL: eidolon_typedai-0.1.8.tar.gz
  • Upload date:
  • Size: 9.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

File hashes

Hashes for eidolon_typedai-0.1.8.tar.gz
Algorithm Hash digest
SHA256 3071637ea24602b93bc7006ee35010afab170c5cedf4c105f55ff207acaa89b5
MD5 0b31d27da7b6520c62d94ad9837cf0fb
BLAKE2b-256 59a89158339bb1c18b824e219ace24b272f47f56e6cc1e0f6ee2fa4c8076b816

See more details on using hashes here.

File details

Details for the file eidolon_typedai-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: eidolon_typedai-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 10.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.10.12 Linux/6.5.0-1023-azure

File hashes

Hashes for eidolon_typedai-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f6fee68a7c30e0da593d93cac885fce4f2e7b89beb32b8d8fbb57a774c3d2f1c
MD5 e8b441e9e301a863719479fd454e4349
BLAKE2b-256 0c218486cec6ccbaa7b9fee764f310b874c40b761288af097f0c1f36f76e9659

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