Skip to content

Prompt Generator

Pavel Buchnev edited this page Sep 19, 2024 · 1 revision

Feature Explanation

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:

  1. A flexible pipeline system that allows for the addition of multiple interceptors.
  2. Interceptors that can modify the prompt at different stages, including injecting agent memory, handling linked agents, and formatting user prompts.
  3. A context-aware prompt generation process that considers the agent's configuration, memory, and associated tools.
  4. 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.

Use Cases

1. Personalized Customer Support Chatbot

  • 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])
);

2. Multi-Agent Collaborative Task Solver

  • 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])
);

3. Adaptive Learning Assistant

  • 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])
);

4. Dynamic Code Documentation Generator

  • 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'])
);

5. Context-Aware Virtual Assistant

  • 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'])
);

Configuration Options

  • 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()
    );
  • 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);
  • 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);

Related Classes

PromptGeneratorPipeline

  • 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.

PromptInterceptorInterface

  • 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.

InstructionGenerator

  • 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.

AgentMemoryInjector

  • 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.

LinkedAgentsInjector

  • 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.

UserPromptInjector

  • 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.

Mermaid Class Diagram

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
Loading

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.