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>

127 lines (126 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { ObjectIndex: function() { return ObjectIndex; }, SimpleToolNodeMapping: function() { return SimpleToolNodeMapping; } }); const _objects = require("@llamaindex/core/objects"); const _schema = require("@llamaindex/core/schema"); const convertToolToNode = (tool)=>{ const nodeText = ` Tool name: ${tool.metadata.name} Tool description: ${tool.metadata.description} `; return new _schema.TextNode({ text: nodeText, metadata: { name: tool.metadata.name }, excludedEmbedMetadataKeys: [ "name" ], excludedLlmMetadataKeys: [ "name" ] }); }; class SimpleToolNodeMapping extends _objects.BaseObjectNodeMapping { _tools; constructor(objs = []){ super(); this._tools = {}; for (const tool of objs){ this._tools[tool.metadata.name] = tool; } } // eslint-disable-next-line @typescript-eslint/no-explicit-any objNodeMapping() { return this._tools; } toNode(tool) { return convertToolToNode(tool); } _addObj(tool) { this._tools[tool.metadata.name] = tool; } _fromNode(node) { if (!node.metadata) { throw new Error("Metadata must be set"); } return this._tools[node.metadata.name]; } persist(persistDir, objNodeMappingFilename) { // Implement the persist method } toNodes(objs) { return objs.map((obj)=>this.toNode(obj)); } addObj(obj) { this._addObj(obj); } fromNode(node) { return this._fromNode(node); } // eslint-disable-next-line @typescript-eslint/no-explicit-any static fromObjects(objs, ...args) { return new SimpleToolNodeMapping(objs); } // eslint-disable-next-line @typescript-eslint/no-explicit-any fromObjects(objs, ...args) { return new SimpleToolNodeMapping(objs); } } class ObjectIndex { _index; _objectNodeMapping; // eslint-disable-next-line @typescript-eslint/no-explicit-any constructor(index, objectNodeMapping){ this._index = index; this._objectNodeMapping = objectNodeMapping; } static async fromObjects(// eslint-disable-next-line @typescript-eslint/no-explicit-any objects, objectMapping, // eslint-disable-next-line @typescript-eslint/no-explicit-any indexCls, // eslint-disable-next-line @typescript-eslint/no-explicit-any indexKwargs) { if (objectMapping === null) { objectMapping = SimpleToolNodeMapping.fromObjects(objects, {}); } const nodes = objectMapping.toNodes(objects); const index = await indexCls.init({ nodes, ...indexKwargs }); return new ObjectIndex(index, objectMapping); } // eslint-disable-next-line @typescript-eslint/no-explicit-any async insertObject(obj) { this._objectNodeMapping.addObj(obj); const node = this._objectNodeMapping.toNode(obj); await this._index.insertNodes([ node ]); } get tools() { return this._objectNodeMapping.objNodeMapping(); } // eslint-disable-next-line @typescript-eslint/no-explicit-any async asRetriever(kwargs) { return new _objects.ObjectRetriever(this._index.asRetriever(kwargs), this._objectNodeMapping); } // eslint-disable-next-line @typescript-eslint/no-explicit-any asNodeRetriever(kwargs) { return this._index.asRetriever(kwargs); } }