@kaibanjs/tools
Version:
A set of tools to work with LLMs and KaibanJS
113 lines (112 loc) • 3.72 kB
TypeScript
/**
* Simple RAG Tool
*
* This tool integrates with various components from the langchain library to provide
* a simple interface for asking questions and retrieving answers using the Retrieval-Augmented Generation (RAG) approach.
*
* The tool uses the following components:
* - A RAG Toolkit instance, which handles the RAG process
* - A Loader instance, which loads and processes documents for the RAG model
* - A Chunker instance, 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 SimpleRAG
* @typedef {string} SimpleRAGParams
* @example
* {
* content: "content text"
*/
type SimpleRAGParams = {
query: string;
content?: string;
};
/**
* Type for the response in SimpleRAG
* @typedef {string} RagToolkitAnswerResponse
* @example
* "The answer to your question is: [answer]"
*/
type RagToolkitAnswerResponse = string;
/**
* Type for the error in SimpleRAG
* @typedef {string} SimpleRAGError
* @example
* "ERROR_MISSING_CONTENT: No text content was provided for analysis. Agent should provide content in the 'content' field."
*/
type SimpleRAGError = string;
/**
* Type for the response in SimpleRAG
* @typedef {RagToolkitAnswerResponse | SimpleRAGError} SimpleRAGResponse
* @example
* "The answer to your question is: [answer]"
*/
type SimpleRAGResponse = RagToolkitAnswerResponse | SimpleRAGError;
/**
/**
* Configuration options for the SimpleRAG tool
* @interface SimpleRAGFields
* @property {string} OPENAI_API_KEY - The OpenAI API key for authentication
* @property {string} [content] - The content text to process
* @property {any} [chunkOptions] - Chunking options for the RAG model
* @property {any} [embeddings] - Embeddings instance for the RAG model
*/
interface SimpleRAGFields {
OPENAI_API_KEY: string;
content?: string;
chunkOptions?: {
chunkSize: number;
chunkOverlap: number;
};
embeddings?: OpenAIEmbeddings;
vectorStore?: MemoryVectorStore;
llmInstance?: ChatOpenAI;
promptQuestionTemplate?: string;
}
/**
* SimpleRAG tool class
* @extends StructuredTool
*/
export declare class SimpleRAG extends StructuredTool {
private OPENAI_API_KEY;
private content?;
private chunkOptions?;
private embeddings?;
private vectorStore?;
private llmInstance?;
private promptQuestionTemplate?;
private ragToolkit;
name: string;
description: string;
schema: z.ZodObject<{
content: z.ZodString;
query: z.ZodString;
}, "strip", z.ZodTypeAny, {
content: string;
query: string;
}, {
content: string;
query: string;
}>;
/**
* Constructor for the SimpleRAG tool
* @param {SimpleRAGFields} fields - The configuration fields for the tool
*/
constructor(fields: SimpleRAGFields);
/**
* Call the SimpleRAG tool
* @param {SimpleRAGParams} input - The input parameters for the tool
* @returns {Promise<SimpleRAGResponse>} The response from the tool
*/
_call(input: SimpleRAGParams): Promise<SimpleRAGResponse>;
}
export {};