UNPKG

adk-typescript

Version:

TypeScript port of Google's Agent Development Kit (ADK)

94 lines (93 loc) 3.39 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExampleTool = void 0; exports.buildExampleInstructions = buildExampleInstructions; exports.createExampleTool = createExampleTool; const BaseTool_1 = require("./BaseTool"); /** * Utility to build example instructions * * @param examples The examples to use * @param query The user query * @param model The model being used (optional) * @returns Formatted examples as a string */ function buildExampleInstructions(examples, query, model) { // Get examples - either directly from the array or from the provider const examplesList = Array.isArray(examples) ? examples : examples.getExamples(query, model); if (examplesList.length === 0) { return ''; } // Format the examples let exampleText = 'Here are some examples of how to respond to similar queries:\n\n'; examplesList.forEach((example, index) => { exampleText += `Example ${index + 1}:\n`; exampleText += `User: ${example.input}\n`; exampleText += `Assistant: ${example.output}\n\n`; }); exampleText += 'Now, respond to the actual query following these examples.\n'; return exampleText; } /** * A tool that adds (few-shot) examples to the LLM request */ class ExampleTool extends BaseTool_1.BaseTool { /** * Create a new example tool * * @param examples The examples to use */ constructor(examples) { // Name and description are not used because this tool only changes llmRequest super({ name: 'example_tool', description: 'A tool that adds examples to guide the model responses' }); this.examples = examples; } /** * Process the LLM request to add examples * * @param params The parameters for processing * @param params.toolContext The tool context * @param params.llmRequest The LLM request to process */ async processLlmRequest({ toolContext, llmRequest }) { // Get user content from context const userContent = toolContext.userContent || toolContext.user_content; // Return if no user content or parts if (!userContent || !userContent.parts || !userContent.parts.length || !userContent.parts[0].text) { return; } // Build example instructions const exampleInstructions = buildExampleInstructions(this.examples, userContent.parts[0].text, llmRequest.model); // Add the example instructions to the LLM request if (exampleInstructions && llmRequest.appendInstructions) { llmRequest.appendInstructions([exampleInstructions]); } } /** * Execute the example tool - not meant to be called directly * * @param params The parameters for execution * @param context The tool context * @returns A message indicating this tool is not meant to be called directly */ async execute(params, context) { return { error: 'ExampleTool is not meant to be executed directly. It works automatically during LLM request processing.' }; } } exports.ExampleTool = ExampleTool; /** * Creates a new example tool * * @param examples The examples to use * @returns A new ExampleTool instance */ function createExampleTool(examples) { return new ExampleTool(examples); }