throttle-openai
A Python library for making concurrent OpenAI API calls with automatic rate limiting and structured outputs using Pydantic models. Credit: Blogpost by Villoro
Installation
pip install throttle-openai
Quick Start
1. Define Your Output Structure
First, create a Pydantic model that defines your expected output structure:
from pydantic import BaseModel, Field
from typing import Literal
class Sentiment_Prediction_Output(BaseModel):
reasoning: str = Field(description="Reasoning for the sentiment prediction in one sentence.")
sentiment: Literal['Positive', 'Negative', 'Neutral']
class Config:
title = "Sentiment Prediction Output"
description = "Sentiment prediction with reasoning."
2. Make API Calls
Here's a complete example showing how to analyze sentiments for multiple product reviews:
from throttle_openai import async_batch_chat_completion
import asyncio
import os
async def analyze_sentiment():
# Prepare batch messages
batch_messages = [
{
"messages": [
{"role": "system", "content": "You are a sentiment analyzer. Analyze the sentiment of the given text."},
{"role": "user", "content": "I like this product. It's good."}
],
"id": "1" # id is optional
},
{
"messages": [
{"role": "system", "content": "You are a sentiment analyzer. Analyze the sentiment of the given text."},
{"role": "user", "content": "I don't like this product. It's bad."}
],
"id": "2"
}
]
# Make concurrent API calls
output, errors = await async_batch_chat_completion(
batch_messages=batch_messages,
gpt_model='gpt-4o-mini',
pydantic_model=Sentiment_Prediction_Output,
api_key=os.getenv("OPENAI_API_KEY")
)
return output, errors
# Run the analysis
output, errors = asyncio.run(analyze_sentiment())
# Process results
print("Results:", output)
if errors:
print("Errors:", errors)
Features
- Concurrent Processing: Automatically handles multiple API calls concurrently
- Rate Limiting: Built-in rate limiting to prevent hitting OpenAI's API limits
- Structured Output: Uses Pydantic models for type-safe and validated outputs
- Error Handling: Separate error collection for failed requests
- ID Tracking: Optional ID field to track individual requests and responses
Environment Variables
Set your OpenAI API key as an environment variable:
export OPENAI_API_KEY=your_api_key
Complete Example
Check out examples/basic_usage_structured_output.py for a complete example featuring structured outputs.
Metadata
Release files for throttle-openai 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| throttle_openai-0.2.0.tar.gz | 8.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| throttle_openai-0.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 18.5 kB
Release files / throttle_openai-0.2.0.tar.gz
| Download URL | throttle_openai-0.2.0.tar.gz |
|---|---|
| Size | 8.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
90e0761cd57a171f4226e1abb551dc3629c5d348431a9fb2774ad46b1637c99f
|
|
BLAKE2b-256 checksum How to use checksums |
ee67b1988e4e63196dbd671e51518261bc5ffc51c6675185b2d74b66f9407217
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure
|
Release files / throttle_openai-0.2.0-py3-none-any.whl
| Download URL | throttle_openai-0.2.0-py3-none-any.whl |
|---|---|
| Size | 9.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d20beaa66bb097b7360e647936c10ddc15464740b7ea0bfc1fa0d929549c5150
|
|
BLAKE2b-256 checksum How to use checksums |
c05086b33f4d0d8c11e3d7205814491e16e4a460d2b1373d96365e2d47700f81
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.8.5 CPython/3.10.12 Linux/6.5.0-1025-azure
|