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.
- 💬 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
Clone this repository and install dependencies using UV:
git clone https://github.com/Omegaice/openai-utils.git
cd openai-utils
uv syncAlternatively, 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"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=...)- Python 3.11+
- openai >= 1.75.0
- [See
pyproject.tomlfor full dependency list]
MIT