Unify OpenAI requests use cases into a simple interface
Project description
Simple OpenAI Request
This script provides a simple interface for making OpenAI API requests with various use cases:
- Synchronous requests: parallel requests with retry when rate limit hit
- Batch requests: create, check and merge result of batch API requests
- Caching: exact match caching to avoid redundant API calls
Main Function
make_openai_requests(conversations, model, use_batch=False, use_cache=True, ...)
Key Parameters:
conversations: List of conversations/messages (supports multiple formats)model: OpenAI model to use (e.g., "gpt-3.5-turbo")use_batch: Set to True for batch API, False for synchronous APIuse_cache: Enable/disable cachingapi_key: OpenAI API key (if not provided, it will be read from the environment variableOPENAI_API_KEY)
Additional Options:
generation_args: Additional arguments for the API call (e.g., max_tokens, temperature)cache_file: Path to the cache file (default: environment variableSIMPLE_OPENAI_REQUESTS_CACHE_FILEor default as~/.gpt_cache.pkl)batch_dir: Directory for batch processing files (default: environment variableSIMPLE_OPENAI_REQUESTS_BATCH_DIRor default as~/.gpt_batch_requests)full_response: Return full API response or just the message contentuser_confirm: If True, prompts for user confirmation before making API requests
and other parameters in function make_openai_requests()'s documentation.
Return Format:
The function returns a list of dictionaries, where each dictionary contains:
index: The index of the conversationconversation: The original conversationresponse: The API response (full response object iffull_response=True, otherwise just the message content)is_cached_response: Boolean indicating if the response was from cacheerror: Any error message (None if no error occurred)
Installation
Option 1: Install using pip
You can install the Simple OpenAI Request package using pip:
pip install simple-openai-requests
Option 2: Install from source
To install the package from source, follow these steps:
-
Clone the repository:
git clone https://github.com/lehoanganh298/simple_openai_requests.git
-
Navigate to the project directory:
cd simple_openai_requests
-
Install the package:
pip install .
Usage Examples:
1. Simple string prompts
from simple_openai_requests import make_openai_requests
conversations = [
"What is the capital of France?",
"How does photosynthesis work?"
]
results = make_openai_requests(
conversations=conversations,
model="gpt-3.5-turbo",
use_batch=False,
use_cache=True
)
for result in results:
print(f"Question: {result['conversation'][0]['content']}")
print(f"Answer: {result['response']}\n")
2. Conversation format
conversations = [
[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": "What is the best way to learn programming?"}
],
[
{"role": "system", "content": "You are a knowledgeable historian."},
{"role": "user", "content": "Explain the significance of the Industrial Revolution."}
]
]
results = make_openai_requests(
conversations=conversations,
model="gpt-4",
use_batch=True,
use_cache=False,
generation_args={"max_tokens": 150}
)
for result in results:
print(f"Question: {result['conversation'][-1]['content']}")
print(f"Answer: {result['response']}\n")
3. Indexed conversation format
conversations = [
{
"index": 0,
"conversation": [
{"role": "system", "content": "You are a math tutor."},
{"role": "user", "content": "Explain the Pythagorean theorem."}
]
},
{
"index": 1,
"conversation": [
{"role": "system", "content": "You are a creative writing assistant."},
{"role": "user", "content": "Give me a writing prompt for a short story."}
]
}
]
results = make_openai_requests(
conversations=conversations,
model="gpt-3.5-turbo",
use_batch=False,
use_cache=True,
max_workers=2
)
for result in results:
print(f"Index: {result['index']}")
print(f"Question: {result['conversation'][-1]['content']}")
print(f"Answer: {result['response']}\n")
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file simple_openai_requests-0.1.0.tar.gz.
File metadata
- Download URL: simple_openai_requests-0.1.0.tar.gz
- Upload date:
- Size: 17.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
365a0d668ca24dcab90f526e0116b54fafe7d464e144888b9ad17e5da1111d62
|
|
| MD5 |
dab594d073742c14bd0ace3f1a4c4ddc
|
|
| BLAKE2b-256 |
640cf1f9a067e9ea5ef457e3d1c0287b5c2e637636e4aa169c6cbd548736fbce
|
File details
Details for the file simple_openai_requests-0.1.0-py3-none-any.whl.
File metadata
- Download URL: simple_openai_requests-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.9.6
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df2d0ae6a59b554f663ed5506180de603f11c837a8a208d011f8366d9f51a2dd
|
|
| MD5 |
242b266542c99ec2072c24b030669b94
|
|
| BLAKE2b-256 |
3856ded56150c48fac8d5669fd39ff5fcb5888bbec2d1d0926e6bbef7f5a0c5d
|