The OpenAIAgent
is a powerful agent class in the Multi-Agent Orchestrator framework that integrates with OpenAI’s Chat Completion API. This agent allows you to leverage OpenAI’s language models for various natural language processing tasks.
Key Features
Integration with OpenAI’s Chat Completion API
Support for multiple OpenAI models (e.g., GPT-4, GPT-3.5)
Streaming and non-streaming response options
Customizable inference configuration
Conversation history handling for context-aware responses
Customizable system prompts with variable support
Support for retrievers to enhance responses with additional context
Flexible initialization with API key or custom client
Configuration Options
The OpenAIAgentOptions
extends the base AgentOptions
with the following fields:
Required Fields
name
: Name of the agent
description
: Description of the agent’s capabilities
Authentication (one of the following is required):
apiKey
: Your OpenAI API key
client
: Custom OpenAI client instance
Optional Fields
model
: OpenAI model identifier (e.g., ‘gpt-4’, ‘gpt-3.5-turbo’). Defaults to OPENAI_MODEL_ID_GPT_O_MINI
streaming
: Enable streaming responses. Defaults to false
retriever
: Custom retriever instance for enhancing responses with additional context
inferenceConfig
: Configuration for model inference:
maxTokens
: Maximum tokens to generate (default: 1000)
temperature
: Controls randomness (0-1)
topP
: Controls diversity via nucleus sampling
stopSequences
: Sequences that stop generation
customSystemPrompt
: System prompt configuration:
template
: Template string with optional variable placeholders
variables
: Key-value pairs for template variables
Creating an OpenAIAgent
Python Package
If you haven’t already installed the OpenAI-related dependencies, make sure to install them:
pip install " multi-agent-orchestrator[openai] "
Here are various examples showing different ways to create and configure an OpenAIAgent:
Basic Examples
1. Minimal Configuration
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' A versatile AI assistant ' ,
apiKey: ' your-openai-api-key '
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' A versatile AI assistant ' ,
api_key = ' your-openai-api-key '
2. Using Custom Client
import OpenAI from ' openai ' ;
const customClient = new OpenAI ( { apiKey: ' your-openai-api-key ' } );
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' A versatile AI assistant ' ,
from openai import OpenAI
custom_client = OpenAI ( api_key = ' your-openai-api-key ' )
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' A versatile AI assistant ' ,
3. Custom Model and Streaming
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' A streaming-enabled assistant ' ,
apiKey: ' your-openai-api-key ' ,
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' A streaming-enabled assistant ' ,
api_key = ' your-openai-api-key ' ,
4. With Inference Configuration
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' An assistant with custom inference settings ' ,
apiKey: ' your-openai-api-key ' ,
stopSequences: [ ' Human: ' , ' AI: ' ]
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An assistant with custom inference settings ' ,
api_key = ' your-openai-api-key ' ,
' stopSequences ' : [ ' Human: ' , ' AI: ' ]
5. With Simple System Prompt
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' An assistant with custom prompt ' ,
apiKey: ' your-openai-api-key ' ,
template: ' You are a helpful AI assistant focused on technical support. '
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An assistant with custom prompt ' ,
api_key = ' your-openai-api-key ' ,
' template ' : ' You are a helpful AI assistant focused on technical support. '
6. With System Prompt Variables
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' An assistant with variable prompt ' ,
apiKey: ' your-openai-api-key ' ,
template: ' You are an AI assistant specialized in {{DOMAIN}}. Always use a {{TONE}} tone. ' ,
DOMAIN: ' customer support ' ,
TONE: ' friendly and helpful '
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An assistant with variable prompt ' ,
api_key = ' your-openai-api-key ' ,
' template ' : ' You are an AI assistant specialized in {{ DOMAIN }} . Always use a {{ TONE }} tone. ' ,
' DOMAIN ' : ' customer support ' ,
' TONE ' : ' friendly and helpful '
7. With Custom Retriever
const retriever = new CustomRetriever ( {
// Retriever configuration
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' An assistant with retriever ' ,
apiKey: ' your-openai-api-key ' ,
retriever = CustomRetriever (
# Retriever configuration
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An assistant with retriever ' ,
api_key = ' your-openai-api-key ' ,
8. Combining Multiple Options
const agent = new OpenAIAgent ( {
name: ' OpenAI Assistant ' ,
description: ' An assistant with multiple options ' ,
apiKey: ' your-openai-api-key ' ,
template: ' You are an AI assistant specialized in {{DOMAIN}}. ' ,
DOMAIN: ' technical support '
agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An assistant with multiple options ' ,
api_key = ' your-openai-api-key ' ,
' template ' : ' You are an AI assistant specialized in {{ DOMAIN }} . ' ,
' DOMAIN ' : ' technical support '
9. Complete Example with All Options
Here’s a comprehensive example showing all available configuration options:
import { OpenAIAgent } from ' multi-agent-orchestrator ' ;
const agent = new OpenAIAgent ( {
name: ' Advanced OpenAI Assistant ' ,
description: ' A fully configured AI assistant powered by OpenAI models ' ,
apiKey: ' your-openai-api-key ' ,
model: ' gpt-4 ' , // Choose OpenAI model
streaming: true , // Enable streaming responses
retriever: customRetriever , // Custom retriever for additional context
// Inference configuration
maxTokens: 500 , // Maximum tokens to generate
temperature: 0.7 , // Control randomness (0-1)
topP: 0.9 , // Control diversity via nucleus sampling
stopSequences: [ ' Human: ' , ' AI: ' ] // Sequences that stop generation
// Custom system prompt with variables
template: ` You are an AI assistant specialized in {{DOMAIN}}.
- Maintain a {{TONE}} tone
- Prioritize {{PRIORITY}} ` ,
DOMAIN: ' scientific research ' ,
' - Advanced data analysis ' ,
' - Statistical methodology ' ,
TONE: ' professional and academic ' ,
FOCUS: ' accuracy and clarity ' ,
PRIORITY: ' evidence-based insights '
from multi_agent_orchestrator import OpenAIAgent, OpenAIAgentOptions
agent = OpenAIAgent ( OpenAIAgentOptions (
name = ' Advanced OpenAI Assistant ' ,
description = ' A fully configured AI assistant powered by OpenAI models ' ,
api_key = ' your-openai-api-key ' ,
model = ' gpt-4 ' , # Choose OpenAI model
streaming = True , # Enable streaming responses
retriever = custom_retriever , # Custom retriever for additional context
# Inference configuration
' maxTokens ' : 500 , # Maximum tokens to generate
' temperature ' : 0.7 , # Control randomness (0-1)
' topP ' : 0.9 , # Control diversity via nucleus sampling
' stopSequences ' : [ ' Human: ' , ' AI: ' ] # Sequences that stop generation
# Custom system prompt with variables
' template ' : """ You are an AI assistant specialized in {{ DOMAIN }} .
- Maintain a {{ TONE }} tone
- Prioritize {{ PRIORITY }} """ ,
' DOMAIN ' : ' scientific research ' ,
' - Advanced data analysis ' ,
' - Statistical methodology ' ,
' TONE ' : ' professional and academic ' ,
' FOCUS ' : ' accuracy and clarity ' ,
' PRIORITY ' : ' evidence-based insights '
Using the OpenAIAgent
There are two ways to use the OpenAIAgent: directly or through the Multi-Agent Orchestrator.
Direct Usage
Call the agent directly when you want to use a single agent without orchestrator routing:
const classifierResult = {
const response = await orchestrator . agentProcessRequest (
" What is the capital of France? " ,
classifier_result = ClassifierResult ( selected_agent = agent , confidence = 1.0 )
response = await orchestrator. agent_process_request (
" What is the capital of France? " ,
Using with the Orchestrator
Add the agent to Multi-Agent Orchestrator for use in a multi-agent system:
const orchestrator = new MultiAgentOrchestrator ();
orchestrator . addAgent (agent);
const response = await orchestrator . routeRequest (
" What is the capital of France? " ,
orchestrator = MultiAgentOrchestrator ()
orchestrator. add_agent ( agent )
response = await orchestrator. route_request (
" What is the capital of France? " ,