-
Notifications
You must be signed in to change notification settings - Fork 0
Prompt Generator
The Prompt Generator is a crucial component in the LLM Agents system, designed to dynamically create and manage prompts for AI agents. It uses a pipeline architecture with interceptors to build complex, context-aware prompts. The core functionality includes:
- A flexible pipeline system that allows for the addition of multiple interceptors.
- Interceptors that can modify the prompt at different stages, including injecting agent memory, handling linked agents, and formatting user prompts.
- A context-aware prompt generation process that considers the agent's configuration, memory, and associated tools.
- Support for different types of prompts, including system messages and user inputs.
This feature enables the creation of sophisticated, tailored prompts that can significantly enhance the performance and capabilities of AI agents in various scenarios.
- A chatbot that uses the prompt generator to create context-aware responses based on customer history and preferences.
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new CustomerHistoryInjector(),
new ProductRecommendationInterceptor(),
new ToneAdjustmentInterceptor()
);
$prompt = $generator->generate(
agent: $customerSupportAgent,
userPrompt: $customerQuery,
context: new Context(['customer_id' => $customerId])
);
- A system where multiple specialized agents work together to solve complex problems, using the prompt generator to facilitate inter-agent communication.
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new AgentSpecializationInjector(),
new InterAgentCommunicationFormatter(),
new TaskDecompositionInterceptor()
);
$prompt = $generator->generate(
agent: $coordinatorAgent,
userPrompt: $complexProblem,
context: new Context(['available_agents' => $specializedAgents])
);
- An AI tutor that adjusts its teaching style and content based on the student's progress and learning preferences.
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new LearningStyleAdaptor(),
new ProgressTrackerInjector(),
new ContentDifficultyAdjuster()
);
$prompt = $generator->generate(
agent: $tutorAgent,
userPrompt: $studentQuestion,
context: new Context(['student_profile' => $studentData])
);
- A system that generates comprehensive code documentation by analyzing code structure and developer comments.
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new CodeStructureAnalyzer(),
new CommentExtractor(),
new DocumentationStyleFormatter()
);
$prompt = $generator->generate(
agent: $documentationAgent,
userPrompt: $codeSnippet,
context: new Context(['language' => 'PHP', 'framework' => 'Laravel'])
);
- A personal assistant that adapts its responses based on the user's location, schedule, and preferences.
$generator = new PromptGeneratorPipeline();
$generator = $generator->withInterceptor(
new UserProfileInjector(),
new LocationContextProvider(),
new ScheduleAwarenessInterceptor()
);
$prompt = $generator->generate(
agent: $personalAssistantAgent,
userPrompt: $userRequest,
context: new Context(['user_id' => $userId, 'device' => 'mobile'])
);
-
Interceptors:
- Default Value:
[]
- Description: An array of interceptors to be used in the prompt generation pipeline.
$generator = new PromptGeneratorPipeline(); $generator = $generator->withInterceptor( new InstructionGenerator(), new AgentMemoryInjector(), new LinkedAgentsInjector($agents, $schemaMapper), new UserPromptInjector() );
- Default Value:
-
Output Format:
- Default Value:
'markdown'
- Description: Specifies the format in which the AI should provide its response.
$instructionGenerator = new InstructionGenerator(outputFormat: 'json'); $generator = $generator->withInterceptor($instructionGenerator);
- Default Value:
-
Context:
- Default Value:
new Context()
- Description: Provides additional context for prompt generation, such as user information or session data.
$context = Context::new()->setAuthContext(['user_id' => 123, 'role' => 'admin']); $prompt = $generator->generate($agent, $userPrompt, $context);
- Default Value:
- Purpose and Functionality: The main class that orchestrates the prompt generation process. It manages the pipeline of interceptors and executes them in order.
- Interaction with Main Feature: Acts as the entry point for prompt generation, allowing the addition of interceptors and handling the generation process.
- Purpose and Functionality: Defines the contract for all interceptors used in the prompt generation pipeline.
- Interaction with Main Feature: Allows for the creation of custom interceptors that can modify or enhance the prompt at various stages of generation.
- Purpose and Functionality: An interceptor that adds the agent's primary instruction and output format to the prompt.
- Interaction with Main Feature: Ensures that every prompt includes the agent's core instructions and desired response format.
- Purpose and Functionality: Injects the agent's memory (past experiences and learned information) into the prompt.
- Interaction with Main Feature: Enhances the prompt with historical context, allowing for more informed and consistent responses.
- Purpose and Functionality: Adds information about associated agents that the current agent can interact with.
- Interaction with Main Feature: Enables multi-agent collaboration by providing information on available agent resources.
- Purpose and Functionality: Incorporates the user's input into the generated prompt.
- Interaction with Main Feature: Ensures that the user's query or command is properly formatted and included in the final prompt.
classDiagram
class PromptGeneratorPipeline {
-interceptors: PromptInterceptorInterface[]
+withInterceptor(interceptor: PromptInterceptorInterface): self
+generate(agent: AgentInterface, userPrompt: string, context: PromptContextInterface): PromptInterface
}
class PromptInterceptorInterface {
<<interface>>
+generate(input: PromptGeneratorInput, next: InterceptorHandler): PromptInterface
}
class InstructionGenerator {
-outputFormat: string
+generate(input: PromptGeneratorInput, next: InterceptorHandler): PromptInterface
}
class AgentMemoryInjector {
+generate(input: PromptGeneratorInput, next: InterceptorHandler): PromptInterface
}
class LinkedAgentsInjector {
-agents: AgentRepositoryInterface
-schemaMapper: SchemaMapperInterface
+generate(input: PromptGeneratorInput, next: InterceptorHandler): PromptInterface
}
class UserPromptInjector {
+generate(input: PromptGeneratorInput, next: InterceptorHandler): PromptInterface
}
class Context {
-authContext: array|JsonSerializable|null
+setAuthContext(authContext: array|JsonSerializable): self
+getAuthContext(): array|JsonSerializable|null
}
PromptGeneratorPipeline --> "0..*" PromptInterceptorInterface
InstructionGenerator ..|> PromptInterceptorInterface
AgentMemoryInjector ..|> PromptInterceptorInterface
LinkedAgentsInjector ..|> PromptInterceptorInterface
UserPromptInjector ..|> PromptInterceptorInterface
PromptGeneratorPipeline ..> Context
This class diagram illustrates the relationships between the main PromptGeneratorPipeline
class and the various interceptors and supporting classes. The PromptGeneratorPipeline
manages multiple PromptInterceptorInterface
implementations, which can include specific interceptors like InstructionGenerator
, AgentMemoryInjector
, LinkedAgentsInjector
, and UserPromptInjector
. The Context
class is used to provide additional contextual information during the prompt generation process.