@kaibanjs/tools
Version:
A set of tools to work with LLMs and KaibanJS
111 lines (110 loc) • 3.77 kB
TypeScript
/**
* Text File (txt) Search Tool
*
* This tool is used to perform a RAG (Retrieval-Augmented Generation) search within the content of a text file.
* It allows for semantic searching of a query within a specified text file's content, making it an invaluable resource
* for quickly extracting information or finding specific sections of text based on the query provided.
*
* The tool uses the following components:
* - A Chunker options, which chunks and processes text for the RAG model
* - An Embeddings instance, which handles embeddings for the RAG model
* - A VectorStore instance, which stores vectors for the RAG model
* - An LLM instance, which handles the language model for the RAG model
* - A promptQuestionTemplate, which defines the template for asking questions
* - An OpenAI API key, which is used for interacting with the OpenAI API
*/
import { StructuredTool } from '@langchain/core/tools';
import { z } from 'zod';
import { OpenAIEmbeddings } from '@langchain/openai';
import { MemoryVectorStore } from 'langchain/vectorstores/memory';
import { ChatOpenAI } from '@langchain/openai';
/**
* Type for the parameters in TextFileSearch
* @typedef {string} TextFileSearchParams
* @example
* {
* query: "What is the main idea of the document?"
* file: "path/to/file.txt",
* }
*/
type TextFileSearchParams = {
query: string;
file?: string;
};
/**
* Response type for the PdfSearch tool
* @typedef {string} RagToolkitAnswerResponse
* @example
* "The answer to your question is: [answer]"
*/
type RagToolkitAnswerResponse = string;
/**
* Error type for the TextFileSearch tool
* @typedef {string} TextFileSearchError
* @example
* "ERROR_MISSING_FILE: No file was provided for analysis. Agent should provide valid file in the 'file' field."
*/
type TextFileSearchError = string;
/**
* Type for the response from the TextFileSearch tool
* @typedef {RagToolkitAnswerResponse | TextFileSearchError} TextFileSearchResponse
* @example
* "The answer to your question is: [answer]"
*/
type TextFileSearchResponse = RagToolkitAnswerResponse | TextFileSearchError;
/**
* Interface for the TextFileSearch tool
* @typedef {Object} TextFileSearchFields
* @property {string} OPENAI_API_KEY - The OpenAI API key
* @property {string} [file] - The text file (txt) path to process
* @property {Object} [chunkOptions] - The chunk options for the RAG model
*/
interface TextFileSearchFields {
OPENAI_API_KEY: string;
file?: string | File;
chunkOptions?: {
chunkSize: number;
chunkOverlap: number;
};
embeddings?: OpenAIEmbeddings;
vectorStore?: MemoryVectorStore;
llmInstance?: ChatOpenAI;
promptQuestionTemplate?: string;
}
/**
* TextFileSearch tool class
* @extends StructuredTool
*/
export declare class TextFileSearch extends StructuredTool {
private OPENAI_API_KEY;
private file?;
private chunkOptions?;
private embeddings?;
private vectorStore?;
private llmInstance?;
private promptQuestionTemplate?;
private ragToolkit;
private httpClient;
name: string;
description: string;
schema: z.ZodObject<{
file: z.ZodString;
query: z.ZodString;
}, "strip", z.ZodTypeAny, {
query: string;
file: string;
}, {
query: string;
file: string;
}>;
/**
* @param {TextFileSearchFields} fields - The fields for the TextFileSearch tool
*/
constructor(fields: TextFileSearchFields);
/**
* @param {TextFileSearchParams} input - The input for the TextFileSearch tool
* @returns {Promise<TextFileSearchResponse>} The response from the TextFileSearch tool
*/
_call(input: TextFileSearchParams): Promise<TextFileSearchResponse>;
}
export {};