The Agent
abstract class provides a flexible foundation for creating various types of agents. When implementing a custom agent, you can:
Call Language Models : Integrate with LLMs like GPT-3, BERT, or custom models.
API Integration : Make calls to external APIs or services.
Data Processing : Implement data analysis, transformation, or generation logic.
Rule-Based Systems : Create agents with predefined rules and responses.
Hybrid Approaches : Combine multiple techniques for more complex behaviors.
Example of a simple custom agent:
class SimpleGreetingAgent extends Agent {
content: [{ text: ` Hello! You said: ${ inputText } ` }]
from multi_agent_orchestrator.agents import Agent
from multi_agent_orchestrator.types import ConversationMessage, ParticipantRole
class SimpleGreetingAgent ( Agent ):
async def process_request (
chat_history : List[ConversationMessage]
) -> ConversationMessage:
return ConversationMessage (
role = ParticipantRole.ASSISTANT.value ,
content = [ { " text " : f "Hello! You said: {input_text} " } ]
Basic Structure of a Custom Agent
To create a custom agent, you need to extend the base Agent
class or one of its subclasses. Here’s the basic structure:
import { Agent, AgentOptions, Message } from ' ./path-to-agent-module ' ;
class CustomAgent extends Agent {
constructor ( options : AgentOptions ) {
// Additional initialization if needed
additionalParams ?: Record < string , any >
// Implement your custom logic here
from typing import List, Optional, Dict
from multi_agent_orchestrator.agents import Agent, AgentOptions
from multi_agent_orchestrator.types import ConversationMessage
class CustomAgent ( Agent ):
def __init__ ( self , options : AgentOptions ) :
super (). __init__ ( options )
# Additional initialization if needed
async def process_request (
chat_history : List[ConversationMessage],
additional_params : Optional[Dict[ str , str ]] = None
) -> ConversationMessage:
# Implement your custom logic here
Example: OpenAI Agent
Here’s an example of a custom agent that uses the OpenAI API:
import { Agent, AgentOptions, Message } from ' ./path-to-agent-module ' ;
import { Configuration, OpenAIApi } from ' openai ' ;
class OpenAIAgent extends Agent {
private openai : OpenAIApi ;
constructor ( options : AgentOptions & { apiKey : string } ) {
const configuration = new Configuration ( { apiKey: options . apiKey } );
this . openai = new OpenAIApi (configuration);
const response = await this . openai . createCompletion ( {
model: ' text-davinci-002 ' ,
content: [{ text: response . data . choices [ 0 ] . text || ' No response ' }]
from typing import List, Optional, Dict
from multi_agent_orchestrator.agents import Agent, AgentOptions
from multi_agent_orchestrator.types import ConversationMessage, ParticipantRole
class OpenAIAgentOptions ( AgentOptions ):
class OpenAIAgent ( Agent ):
def __init__ ( self , options : OpenAIAgentOptions ) :
super (). __init__ ( options )
openai.api_key = options.api_key
async def process_request (
chat_history : List[ConversationMessage],
additional_params : Optional[Dict[ str , str ]] = None
) -> ConversationMessage:
response = openai.Completion. create (
engine = " text-davinci-002 " ,
return ConversationMessage (
role = ParticipantRole.ASSISTANT.value ,
content = [ { " text " : response.choices [ 0 ] .text. strip () } ]
To use this OpenAI agent:
const openAIAgent = new OpenAIAgent ( {
description: ' An agent that uses OpenAI API for responses ' ,
apiKey: ' your-openai-api-key '
orchestrator . addAgent (openAIAgent);
openai_agent = OpenAIAgent ( OpenAIAgentOptions (
description = ' An agent that uses OpenAI API for responses ' ,
api_key = ' your-openai-api-key '
orchestrator. add_agent ( openai_agent )
By creating custom agents, you can extend the capabilities of the Multi-Agent Orchestrator to meet your specific needs, whether that’s integrating with external AI services like OpenAI, implementing specialized business logic, or interfacing with other systems and APIs.