OpenAI API#
The OpenAIAPI class provides an interface to interact with OpenAI-compatible LLM APIs. It supports both OpenAI’s official API and third-party services that expose OpenAI-compatible endpoints.
Overview#
The OpenAIAPI class extends the base LLM class and provides a unified interface for making
chat completion requests to OpenAI-compatible APIs. It handles API authentication, request formatting,
and response parsing.
Setup Requirements#
Before using the OpenAIAPI, ensure you have the following:
Python Package: Install the OpenAI Python package:
pip install openai
API Access: You need one of the following: - OpenAI API key from platform.openai.com - Third-party service with OpenAI-compatible API (e.g., Azure OpenAI, local proxies)
API Reference#
Class Definition#
- class OpenAIAPI#
A concrete implementation of the LLM base class that interfaces with OpenAI-compatible APIs.
Constructor#
- __init__(self, base_url: str, api_key: str, model: str, timeout: int = 60, **kwargs)#
Initialize the OpenAI API client.
- Parameters:
base_url (str) – The base URL of the API endpoint. For OpenAI’s official API, use
"https://api.openai.com/v1". For third-party services, use their provided endpoint URL.api_key (str) – Your API key for authentication. For OpenAI, this is your secret API key.
model (str) – The model identifier to use (e.g.,
"gpt-4","gpt-3.5-turbo").timeout (int, optional) – Request timeout in seconds. Defaults to 60.
kwargs (Any, optional) – Additional parameters passed to the
openai.OpenAIclient constructor. See OpenAI Python SDK documentation for available options.
Note
For Azure OpenAI, the base_url should follow the format:
"https://<your-resource-name>.openai.azure.com/openai/deployments/<your-deployment-name>"
Methods#
- draw_sample(self, prompt: str | Any, *args, **kwargs) str#
Generate a response from the LLM based on the provided prompt.
- Parameters:
prompt (str | Any) – The input prompt. Can be either: - A string containing the user message - A list of message dictionaries with ‘role’ and ‘content’ keys
args – Additional positional arguments (unused).
kwargs – Additional keyword arguments passed to the API call. See OpenAI’s Chat Completion API for available parameters.
- Returns:
The generated text content from the LLM response.
- Return type:
str
- Raises:
openai.APIError – If the API returns an error.
openai.APIConnectionError – If there’s a connection issue.
Note
When passing a string prompt, it is automatically wrapped in a user message format:
[{'role': 'user', 'content': prompt.strip()}]
Examples#
Example 1: Using OpenAI’s Official API#
from llm4ad.tools.llm.llm_api_openai import OpenAIAPI
# Initialize with OpenAI API credentials
sampler = OpenAIAPI(
base_url="https://api.openai.com/v1",
api_key="your-api-key-here",
model="gpt-4",
timeout=60
)
# Simple prompt
response = sampler.draw_sample("What is the capital of France?")
print(response) # Output: Paris
# Using chat message format
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Write a function to calculate factorial."}
]
response = sampler.draw_sample(messages)
Example 2: Using Third-Party OpenAI-Compatible API#
from llm4ad.tools.llm.llm_api_openai import OpenAIAPI
# Using a third-party service (e.g., local proxy or alternative provider)
sampler = OpenAIAPI(
base_url="https://api.example.com/v1", # Third-party endpoint
api_key="your-third-party-api-key",
model="gpt-3.5-turbo",
timeout=30
)
response = sampler.draw_sample("Hello, how are you?")
Example 3: Using with Additional API Parameters#
from llm4ad.tools.llm.llm_api_openai import OpenAIAPI
sampler = OpenAIAPI(
base_url="https://api.openai.com/v1",
api_key="your-api-key-here",
model="gpt-4",
timeout=60
)
# Passing additional parameters via kwargs
response = sampler.draw_sample(
prompt="Explain quantum computing in simple terms.",
temperature=0.7,
max_tokens=500,
top_p=0.9
)
Example 4: Integration with LLM4AD Platform#
from llm4ad.tools.llm.llm_api_openai import OpenAIAPI
import llm4ad
# Create LLM sampler
sampler = OpenAIAPI(
base_url="https://api.openai.com/v1",
api_key="your-api-key-here",
model="gpt-4"
)
# Use with an LLM4AD method (example with EoH)
task = llm4ad.tasks.optimization.SymReg( # Example task
dimension=5,
num_samples=100,
eval_budget=1000
)
method = llm4ad.methods.eoh.EoH(
task=task,
sampler=sampler,
num_iterations=50
)
result = method.run()
print(f"Best solution: {result.best_solution}")
print(f"Best fitness: {result.best_fitness}")
Common Issues and Troubleshooting#
Authentication Error: Ensure your API key is correct and has sufficient credits.
Rate Limiting: If you encounter rate limit errors, implement retry logic or reduce request frequency.
Timeout Issues: Increase the timeout parameter if network latency is high.
Model Not Found: Verify that the model name is correct and available for your API key.
See Also#
HTTPS API - For custom HTTPS API implementations
Ollama API - For local Ollama deployments
vLLM API - For local vLLM deployments