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.db)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 requestsbase_url: use when calling vLLM APIs.
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")
Output
INFO - Cache disabled
Making 2 synchronous API requests using model 'gpt-3.5-turbo'? ([y]/n):
API Requests: 100%|████████████████████████████████| 2/2 [00:04<00:00, 2.09s/request]
INFO - Completed 2 synchronous API requests
INFO - Successful requests: 2/2
INFO - Failed requests: 0/2
[
{
"index": 0,
"conversation": [
{
"role": "user",
"content": "What is the capital of France?"
}
],
"response": "The capital of France is Paris.",
"error": null,
"is_cached_response": false
},
...
]
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")
Output
Making 2 batch API requests using model 'gpt-4o-mini' with batch run name 'ac3275cd-8b2e-4d8d-97a0-092916b6b9ce'? ([y]/n):
INFO - Batch request file created: /Users/user/.gpt_batch_requests/ac3275cd-8b2e-4d8d-97a0-092916b6b9ce_batch_request.jsonl
INFO - Batch submitted with file ID: file-wEdX4pNawx7XmUocTVcwzAF9
INFO - Batch processing started. Batch ID: batch_TgV5j1LPf3nYWwKgBCfF4qZ7
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")
Metadata
Release files for simple-openai-requests 1.1.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 | |
|---|---|---|---|
| simple_openai_requests-1.1.0.tar.gz | 21.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| simple_openai_requests-1.1.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 38.2 kB
Release files / simple_openai_requests-1.1.0.tar.gz
| Download URL | simple_openai_requests-1.1.0.tar.gz |
|---|---|
| Size | 21.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
5d2f4867bc7a5916469a5aa75d3e69c11a125c445c5f98e90599d60578ca8929
|
|
BLAKE2b-256 checksum How to use checksums |
9465304da037a74b7172dfb6042bb434c9c6762646678bf0e10b2ba9d0292541
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|
Release files / simple_openai_requests-1.1.0-py3-none-any.whl
| Download URL | simple_openai_requests-1.1.0-py3-none-any.whl |
|---|---|
| Size | 16.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
cc360116c535691cf6372eb9b95b3e219702f842e0a71fa5747c020efd345006
|
|
BLAKE2b-256 checksum How to use checksums |
f46c85bbd019dad3cca9531e1b04f2267bad8d29800292a8a1a64281edf9ceb4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.1 CPython/3.9.6
|