Open AI Agent
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 agentdescription
: Description of the agent’s capabilities- Authentication (one of the following is required):
apiKey
: Your OpenAI API keyclient
: Custom OpenAI client instance
Optional Fields
model
: OpenAI model identifier (e.g., ‘gpt-4’, ‘gpt-3.5-turbo’). Defaults toOPENAI_MODEL_ID_GPT_O_MINI
streaming
: Enable streaming responses. Defaults tofalse
retriever
: Custom retriever instance for enhancing responses with additional contextinferenceConfig
: Configuration for model inference:maxTokens
: Maximum tokens to generate (default: 1000)temperature
: Controls randomness (0-1)topP
: Controls diversity via nucleus samplingstopSequences
: Sequences that stop generation
customSystemPrompt
: System prompt configuration:template
: Template string with optional variable placeholdersvariables
: 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( name='OpenAI Assistant', 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', client: customClient});
from openai import OpenAI
custom_client = OpenAI(api_key='your-openai-api-key')
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='A versatile AI assistant', client=custom_client))
3. Custom Model and Streaming
const agent = new OpenAIAgent({ name: 'OpenAI Assistant', description: 'A streaming-enabled assistant', apiKey: 'your-openai-api-key', model: 'gpt-4', streaming: true});
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='A streaming-enabled assistant', api_key='your-openai-api-key', model='gpt-4', streaming=True))
4. With Inference Configuration
const agent = new OpenAIAgent({ name: 'OpenAI Assistant', description: 'An assistant with custom inference settings', apiKey: 'your-openai-api-key', inferenceConfig: { maxTokens: 500, temperature: 0.7, topP: 0.9, stopSequences: ['Human:', 'AI:'] }});
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='An assistant with custom inference settings', api_key='your-openai-api-key', inference_config={ 'maxTokens': 500, 'temperature': 0.7, 'topP': 0.9, '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', customSystemPrompt: { template: 'You are a helpful AI assistant focused on technical support.' }});
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='An assistant with custom prompt', api_key='your-openai-api-key', custom_system_prompt={ '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', customSystemPrompt: { template: 'You are an AI assistant specialized in {{DOMAIN}}. Always use a {{TONE}} tone.', variables: { DOMAIN: 'customer support', TONE: 'friendly and helpful' } }});
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='An assistant with variable prompt', api_key='your-openai-api-key', custom_system_prompt={ 'template': 'You are an AI assistant specialized in {{DOMAIN}}. Always use a {{TONE}} tone.', 'variables': { '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: retriever});
retriever = CustomRetriever( # Retriever configuration)
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='An assistant with retriever', api_key='your-openai-api-key', retriever=retriever))
8. Combining Multiple Options
const agent = new OpenAIAgent({ name: 'OpenAI Assistant', description: 'An assistant with multiple options', apiKey: 'your-openai-api-key', model: 'gpt-4', streaming: true, inferenceConfig: { maxTokens: 500, temperature: 0.7 }, customSystemPrompt: { template: 'You are an AI assistant specialized in {{DOMAIN}}.', variables: { DOMAIN: 'technical support' } }});
agent = OpenAIAgent(OpenAIAgentOptions( name='OpenAI Assistant', description='An assistant with multiple options', api_key='your-openai-api-key', model='gpt-4', streaming=True, inference_config={ 'maxTokens': 500, 'temperature': 0.7 }, custom_system_prompt={ 'template': 'You are an AI assistant specialized in {{DOMAIN}}.', 'variables': { '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({ // Required fields name: 'Advanced OpenAI Assistant', description: 'A fully configured AI assistant powered by OpenAI models', apiKey: 'your-openai-api-key',
// Optional fields model: 'gpt-4', // Choose OpenAI model streaming: true, // Enable streaming responses retriever: customRetriever, // Custom retriever for additional context
// Inference configuration inferenceConfig: { 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 customSystemPrompt: { template: `You are an AI assistant specialized in {{DOMAIN}}. Your core competencies: {{SKILLS}}
Communication style: - Maintain a {{TONE}} tone - Focus on {{FOCUS}} - Prioritize {{PRIORITY}}`, variables: { DOMAIN: 'scientific research', SKILLS: [ '- Advanced data analysis', '- Statistical methodology', '- Research design', '- Technical writing' ], TONE: 'professional and academic', FOCUS: 'accuracy and clarity', PRIORITY: 'evidence-based insights' } }});
from multi_agent_orchestrator import OpenAIAgent, OpenAIAgentOptions
agent = OpenAIAgent(OpenAIAgentOptions( # Required fields name='Advanced OpenAI Assistant', description='A fully configured AI assistant powered by OpenAI models', api_key='your-openai-api-key',
# Optional fields model='gpt-4', # Choose OpenAI model streaming=True, # Enable streaming responses retriever=custom_retriever, # Custom retriever for additional context
# Inference configuration inference_config={ '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 custom_system_prompt={ 'template': """You are an AI assistant specialized in {{DOMAIN}}. Your core competencies: {{SKILLS}}
Communication style: - Maintain a {{TONE}} tone - Focus on {{FOCUS}} - Prioritize {{PRIORITY}}""", 'variables': { 'DOMAIN': 'scientific research', 'SKILLS': [ '- Advanced data analysis', '- Statistical methodology', '- Research design', '- Technical writing' ], '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 = { selectedAgent: agent, confidence: 1.0};
const response = await orchestrator.agentProcessRequest( "What is the capital of France?", "user123", "session456", classifierResult);
classifier_result = ClassifierResult(selected_agent=agent, confidence=1.0)
response = await orchestrator.agent_process_request( "What is the capital of France?", "user123", "session456", classifier_result)
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?", "user123", "session456");
orchestrator = MultiAgentOrchestrator()orchestrator.add_agent(agent)
response = await orchestrator.route_request( "What is the capital of France?", "user123", "session456")