UNPKG

llamaindex

Version:

<p align="center"> <img height="100" width="100" alt="LlamaIndex logo" src="https://ts.llamaindex.ai/square.svg" /> </p> <h1 align="center">LlamaIndex.TS</h1> <h3 align="center"> Data framework for your LLM application. </h3>

46 lines (43 loc) 1.45 kB
export * from '@llamaindex/core/tools'; const DEFAULT_NAME = "query_engine_tool"; const DEFAULT_DESCRIPTION = "Useful for running a natural language query against a knowledge base and get back a natural language response."; const DEFAULT_PARAMETERS = { type: "object", properties: { query: { type: "string", description: "The query to search for" } }, required: [ "query" ] }; class QueryEngineTool { constructor({ queryEngine, metadata, includeSourceNodes }){ this.queryEngine = queryEngine; this.metadata = { name: metadata?.name ?? DEFAULT_NAME, description: metadata?.description ?? DEFAULT_DESCRIPTION, parameters: metadata?.parameters ?? DEFAULT_PARAMETERS }; this.includeSourceNodes = includeSourceNodes ?? false; } async call({ query }) { const response = await this.queryEngine.query({ query }); if (!this.includeSourceNodes) { return { content: response.message.content }; } // Use JSON.parse(JSON.stringify()) to remove undefined values from sourceNodes // since undefined is not a valid JSONValue return { content: response.message.content, sourceNodes: JSON.parse(JSON.stringify(response.sourceNodes ?? [])) }; } } export { QueryEngineTool };