UNPKG

@langchain/community

Version:
1 lines 3.3 kB
{"version":3,"file":"chaindesk.cjs","names":["BaseRetriever","AsyncCaller","Document"],"sources":["../../src/retrievers/chaindesk.ts"],"sourcesContent":["import {\n BaseRetriever,\n type BaseRetrieverInput,\n} from \"@langchain/core/retrievers\";\nimport { Document } from \"@langchain/core/documents\";\nimport {\n AsyncCaller,\n type AsyncCallerParams,\n} from \"@langchain/core/utils/async_caller\";\n\nexport interface ChaindeskRetrieverArgs\n extends AsyncCallerParams, BaseRetrieverInput {\n datastoreId: string;\n topK?: number;\n filter?: Record<string, unknown>;\n apiKey?: string;\n}\n\ninterface Berry {\n text: string;\n score: number;\n source?: string;\n [key: string]: unknown;\n}\n\n/**\n * @example\n * ```typescript\n * const retriever = new ChaindeskRetriever({\n * datastoreId: \"DATASTORE_ID\",\n * apiKey: \"CHAINDESK_API_KEY\",\n * topK: 8,\n * });\n * const docs = await retriever.getRelevantDocuments(\"hello\");\n * ```\n */\nexport class ChaindeskRetriever extends BaseRetriever {\n static lc_name() {\n return \"ChaindeskRetriever\";\n }\n\n lc_namespace = [\"langchain\", \"retrievers\", \"chaindesk\"];\n\n caller: AsyncCaller;\n\n datastoreId: string;\n\n topK?: number;\n\n filter?: Record<string, unknown>;\n\n apiKey?: string;\n\n constructor({\n datastoreId,\n apiKey,\n topK,\n filter,\n ...rest\n }: ChaindeskRetrieverArgs) {\n super();\n\n this.caller = new AsyncCaller(rest);\n this.datastoreId = datastoreId;\n this.apiKey = apiKey;\n this.topK = topK;\n this.filter = filter;\n }\n\n async getRelevantDocuments(query: string): Promise<Document[]> {\n const r = await this.caller.call(\n fetch,\n `https://app.chaindesk.ai/api/datastores/${this.datastoreId}/query`,\n {\n method: \"POST\",\n body: JSON.stringify({\n query,\n ...(this.topK ? { topK: this.topK } : {}),\n ...(this.filter ? { filters: this.filter } : {}),\n }),\n headers: {\n \"Content-Type\": \"application/json\",\n ...(this.apiKey ? { Authorization: `Bearer ${this.apiKey}` } : {}),\n },\n }\n );\n\n const { results } = (await r.json()) as { results: Berry[] };\n\n return results.map(\n ({ text, score, source, ...rest }) =>\n new Document({\n pageContent: text,\n metadata: {\n score,\n source,\n ...rest,\n },\n })\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;AAoCA,IAAa,qBAAb,cAAwCA,2BAAAA,cAAc;CACpD,OAAO,UAAU;AACf,SAAO;;CAGT,eAAe;EAAC;EAAa;EAAc;EAAY;CAEvD;CAEA;CAEA;CAEA;CAEA;CAEA,YAAY,EACV,aACA,QACA,MACA,QACA,GAAG,QACsB;AACzB,SAAO;AAEP,OAAK,SAAS,IAAIC,mCAAAA,YAAY,KAAK;AACnC,OAAK,cAAc;AACnB,OAAK,SAAS;AACd,OAAK,OAAO;AACZ,OAAK,SAAS;;CAGhB,MAAM,qBAAqB,OAAoC;EAkB7D,MAAM,EAAE,YAAa,OAjBX,MAAM,KAAK,OAAO,KAC1B,OACA,2CAA2C,KAAK,YAAY,SAC5D;GACE,QAAQ;GACR,MAAM,KAAK,UAAU;IACnB;IACA,GAAI,KAAK,OAAO,EAAE,MAAM,KAAK,MAAM,GAAG,EAAE;IACxC,GAAI,KAAK,SAAS,EAAE,SAAS,KAAK,QAAQ,GAAG,EAAE;IAChD,CAAC;GACF,SAAS;IACP,gBAAgB;IAChB,GAAI,KAAK,SAAS,EAAE,eAAe,UAAU,KAAK,UAAU,GAAG,EAAE;IAClE;GACF,CACF,EAE4B,MAAM;AAEnC,SAAO,QAAQ,KACZ,EAAE,MAAM,OAAO,QAAQ,GAAG,WACzB,IAAIC,0BAAAA,SAAS;GACX,aAAa;GACb,UAAU;IACR;IACA;IACA,GAAG;IACJ;GACF,CAAC,CACL"}