-
Notifications
You must be signed in to change notification settings - Fork 2k
/
vectorstore.ts
155 lines (145 loc) Β· 4.42 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import type { BaseLanguageModelInterface } from "@langchain/core/language_models/base";
import type { VectorStoreInterface } from "@langchain/core/vectorstores";
import { ToolInterface } from "@langchain/core/tools";
import { Toolkit } from "@langchain/community/agents/toolkits/base";
import { VectorStoreQATool } from "../../../tools/vectorstore.js";
import { ZeroShotCreatePromptArgs, ZeroShotAgent } from "../../mrkl/index.js";
import { VECTOR_PREFIX, VECTOR_ROUTER_PREFIX } from "./prompt.js";
import { SUFFIX } from "../../mrkl/prompt.js";
import { LLMChain } from "../../../chains/llm_chain.js";
import { AgentExecutor } from "../../executor.js";
/**
* Interface that defines the information about a vector store, including
* the vector store itself, its name, and description.
*/
export interface VectorStoreInfo {
vectorStore: VectorStoreInterface;
name: string;
description: string;
}
/**
* Class representing a toolkit for working with a single vector store. It
* initializes the vector store QA tool based on the provided vector store
* information and language model.
* @example
* ```typescript
* const toolkit = new VectorStoreToolkit(
* {
* name: "state_of_union_address",
* description: "the most recent state of the Union address",
* vectorStore: new HNSWLib(),
* },
* new ChatOpenAI({ temperature: 0 }),
* );
* const result = await toolkit.invoke({
* input:
* "What did biden say about Ketanji Brown Jackson in the state of the union address?",
* });
* console.log(`Got output ${result.output}`);
* ```
*/
export class VectorStoreToolkit extends Toolkit {
tools: ToolInterface[];
llm: BaseLanguageModelInterface;
constructor(
vectorStoreInfo: VectorStoreInfo,
llm: BaseLanguageModelInterface
) {
super();
const description = VectorStoreQATool.getDescription(
vectorStoreInfo.name,
vectorStoreInfo.description
);
this.llm = llm;
this.tools = [
new VectorStoreQATool(vectorStoreInfo.name, description, {
vectorStore: vectorStoreInfo.vectorStore,
llm: this.llm,
}),
];
}
}
/**
* Class representing a toolkit for working with multiple vector stores.
* It initializes multiple vector store QA tools based on the provided
* vector store information and language model.
*/
export class VectorStoreRouterToolkit extends Toolkit {
tools: ToolInterface[];
vectorStoreInfos: VectorStoreInfo[];
llm: BaseLanguageModelInterface;
constructor(
vectorStoreInfos: VectorStoreInfo[],
llm: BaseLanguageModelInterface
) {
super();
this.llm = llm;
this.vectorStoreInfos = vectorStoreInfos;
this.tools = vectorStoreInfos.map((vectorStoreInfo) => {
const description = VectorStoreQATool.getDescription(
vectorStoreInfo.name,
vectorStoreInfo.description
);
return new VectorStoreQATool(vectorStoreInfo.name, description, {
vectorStore: vectorStoreInfo.vectorStore,
llm: this.llm,
});
});
}
}
/** @deprecated Create a specific agent with a custom tool instead. */
export function createVectorStoreAgent(
llm: BaseLanguageModelInterface,
toolkit: VectorStoreToolkit,
args?: ZeroShotCreatePromptArgs
) {
const {
prefix = VECTOR_PREFIX,
suffix = SUFFIX,
inputVariables = ["input", "agent_scratchpad"],
} = args ?? {};
const { tools } = toolkit;
const prompt = ZeroShotAgent.createPrompt(tools, {
prefix,
suffix,
inputVariables,
});
const chain = new LLMChain({ prompt, llm });
const agent = new ZeroShotAgent({
llmChain: chain,
allowedTools: tools.map((t) => t.name),
});
return AgentExecutor.fromAgentAndTools({
agent,
tools,
returnIntermediateSteps: true,
});
}
/** @deprecated Create a specific agent with a custom tool instead. */
export function createVectorStoreRouterAgent(
llm: BaseLanguageModelInterface,
toolkit: VectorStoreRouterToolkit,
args?: ZeroShotCreatePromptArgs
) {
const {
prefix = VECTOR_ROUTER_PREFIX,
suffix = SUFFIX,
inputVariables = ["input", "agent_scratchpad"],
} = args ?? {};
const { tools } = toolkit;
const prompt = ZeroShotAgent.createPrompt(tools, {
prefix,
suffix,
inputVariables,
});
const chain = new LLMChain({ prompt, llm });
const agent = new ZeroShotAgent({
llmChain: chain,
allowedTools: tools.map((t) => t.name),
});
return AgentExecutor.fromAgentAndTools({
agent,
tools,
returnIntermediateSteps: true,
});
}