-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathvectorstore.ts
57 lines (47 loc) · 1.75 KB
/
vectorstore.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { VectorStoreInterface } from "@langchain/core/vectorstores";
import { Tool } from "@langchain/core/tools";
import { VectorDBQAChain } from "../chains/vector_db_qa.js";
/**
* Interface for tools that interact with a Vector Store.
*/
interface VectorStoreTool {
vectorStore: VectorStoreInterface;
llm: BaseLanguageModelInterface;
}
/**
* A tool for the VectorDBQA chain to interact with a Vector Store. It is
* used to answer questions about a specific topic. The input to this tool
* should be a fully formed question.
*/
export class VectorStoreQATool extends Tool implements VectorStoreTool {
static lc_name() {
return "VectorStoreQATool";
}
vectorStore: VectorStoreInterface;
llm: BaseLanguageModelInterface;
name: string;
description: string;
chain: VectorDBQAChain;
constructor(name: string, description: string, fields: VectorStoreTool) {
super(...arguments);
this.name = name;
this.description = description;
this.vectorStore = fields.vectorStore;
this.llm = fields.llm;
this.chain = VectorDBQAChain.fromLLM(this.llm, this.vectorStore);
}
/**
* Returns a string that describes what the tool does.
* @param name The name of the tool.
* @param description A description of what the tool does.
* @returns A string that describes what the tool does.
*/
static getDescription(name: string, description: string): string {
return `Useful for when you need to answer questions about ${name}. Whenever you need information about ${description} you should ALWAYS use this. Input should be a fully formed question.`;
}
/** @ignore */
async _call(input: string) {
return this.chain.run(input);
}
}