Skip to content

Repository files navigation

openai-utils

github license

A Python utility library for managing OpenAI API conversations, tracking usage costs, and working with structured outputs. Provides convenient abstractions for conversation management, model selection, and cost calculation.

Features

  • 💬 Conversation Management: Easily manage multi-turn conversations with the OpenAI API
  • 💸 Cost Tracking: Track token usage and estimate costs for each conversation
  • 🏷️ Model Utilities: Enum-based model selection with built-in cost rates
  • 🧩 Structured Output: Support for requesting and parsing structured responses (e.g., Pydantic models)
  • 🧪 Testable: Designed for easy testing and extension

Installation

Clone this repository and install dependencies using UV:

git clone https://github.com/Omegaice/openai-utils.git
cd openai-utils
uv sync

Alternatively, add this repository directly to your own project's dependencies with UV:

uv add "openai-utils @ git+https://github.com/Omegaice/openai-utils.git@master"

Quick Example

from openai import OpenAI
from pydantic import BaseModel
from openai_utils import Conversation, ConversationManager, Model

# Setup manager with model and client
manager = ConversationManager(client=OpenAI(), model=Model.GPT_4O_MINI, log_on_exit=True)

# Standard conversation
with manager.new_conversation() as conversation:
    result = conversation.ask("What is your name?")
    print(result)  # str


# Structured response conversation
class User(BaseModel):
    name: str

with manager.new_conversation() as conversation:
    result = conversation.ask("What is your name?", format=User)
    print(result)  # User(name=...)

Requirements

  • Python 3.11+
  • openai >= 1.75.0
  • [See pyproject.toml for full dependency list]

License

MIT

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages