UNPKG

@langchain/community

Version:
1 lines 2.63 kB
{"version":3,"file":"metal.cjs","names":["BaseRetriever","Document"],"sources":["../../src/retrievers/metal.ts"],"sourcesContent":["import type { Client as Metal } from \"@getmetal/metal-sdk\";\n\nimport { BaseRetriever, BaseRetrieverInput } from \"@langchain/core/retrievers\";\nimport { Document } from \"@langchain/core/documents\";\n\n/**\n * Interface for the fields required during the initialization of a\n * `MetalRetriever` instance. It extends the `BaseRetrieverInput`\n * interface and adds a `client` field of type `Metal`.\n */\nexport interface MetalRetrieverFields extends BaseRetrieverInput {\n client: Metal;\n}\n\n/**\n * Interface to represent a response item from the Metal service. It\n * contains a `text` field and an index signature to allow for additional\n * unknown properties.\n */\ninterface ResponseItem {\n text: string;\n [key: string]: unknown;\n}\n\n/**\n * Class used to interact with the Metal service, a managed retrieval &\n * memory platform. It allows you to index your data into Metal and run\n * semantic search and retrieval on it. It extends the `BaseRetriever`\n * class and requires a `Metal` instance and a dictionary of parameters to\n * pass to the Metal API during its initialization.\n * @example\n * ```typescript\n * const retriever = new MetalRetriever({\n * client: new Metal(\n * process.env.METAL_API_KEY,\n * process.env.METAL_CLIENT_ID,\n * process.env.METAL_INDEX_ID,\n * ),\n * });\n * const docs = await retriever.getRelevantDocuments(\"hello\");\n * ```\n */\nexport class MetalRetriever extends BaseRetriever {\n static lc_name() {\n return \"MetalRetriever\";\n }\n\n lc_namespace = [\"langchain\", \"retrievers\", \"metal\"];\n\n private client: Metal;\n\n constructor(fields: MetalRetrieverFields) {\n super(fields);\n\n this.client = fields.client;\n }\n\n async _getRelevantDocuments(query: string): Promise<Document[]> {\n const res = await this.client.search({ text: query });\n\n const items = (\"data\" in res ? res.data : res) as ResponseItem[];\n return items.map(\n ({ text, metadata }) =>\n new Document({\n pageContent: text,\n metadata: metadata as Record<string, unknown>,\n })\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0CA,IAAa,iBAAb,cAAoCA,2BAAAA,cAAc;CAChD,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe;EAAC;EAAa;EAAc;EAAQ;CAEnD;CAEA,YAAY,QAA8B;AACxC,QAAM,OAAO;AAEb,OAAK,SAAS,OAAO;;CAGvB,MAAM,sBAAsB,OAAoC;EAC9D,MAAM,MAAM,MAAM,KAAK,OAAO,OAAO,EAAE,MAAM,OAAO,CAAC;AAGrD,UADe,UAAU,MAAM,IAAI,OAAO,KAC7B,KACV,EAAE,MAAM,eACP,IAAIC,0BAAAA,SAAS;GACX,aAAa;GACH;GACX,CAAC,CACL"}