langchain 0.0.39 → 0.0.40
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chains/conversational_retrieval_chain.d.ts +42 -0
- package/dist/chains/conversational_retrieval_chain.js +138 -0
- package/dist/chains/conversational_retrieval_chain.js.map +1 -0
- package/dist/chains/index.d.ts +2 -1
- package/dist/chains/index.js +2 -1
- package/dist/chains/index.js.map +1 -1
- package/dist/chains/retrieval_qa.d.ts +32 -0
- package/dist/chains/retrieval_qa.js +80 -0
- package/dist/chains/retrieval_qa.js.map +1 -0
- package/dist/embeddings/cohere.d.ts +1 -0
- package/dist/embeddings/cohere.js +2 -0
- package/dist/embeddings/cohere.js.map +1 -0
- package/dist/retrievers/chatgpt-plugin.d.ts +44 -0
- package/dist/retrievers/chatgpt-plugin.js +79 -0
- package/dist/retrievers/chatgpt-plugin.js.map +1 -0
- package/dist/retrievers/index.d.ts +1 -0
- package/dist/retrievers/index.js +2 -0
- package/dist/retrievers/index.js.map +1 -0
- package/dist/schema/index.d.ts +7 -1
- package/dist/schema/index.js +5 -1
- package/dist/schema/index.js.map +1 -1
- package/dist/vectorstores/base.d.ts +13 -2
- package/dist/vectorstores/base.js +29 -2
- package/dist/vectorstores/base.js.map +1 -1
- package/dist/vectorstores/tests/pinecone.test.js +2 -2
- package/dist/vectorstores/tests/pinecone.test.js.map +1 -1
- package/package.json +7 -1
- package/retrievers.d.ts +1 -0
- package/retrievers.js +1 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { BaseLLM } from "../llms/index.js";
|
|
2
|
+
import { SerializedChatVectorDBQAChain } from "./serde.js";
|
|
3
|
+
import { ChainValues, BaseRetriever } from "../schema/index.js";
|
|
4
|
+
import { BaseChain } from "./base.js";
|
|
5
|
+
import { LLMChain } from "./llm_chain.js";
|
|
6
|
+
export type LoadValues = Record<string, any>;
|
|
7
|
+
export interface ConversationalRetrievalQAChainInput {
|
|
8
|
+
retriever: BaseRetriever;
|
|
9
|
+
combineDocumentsChain: BaseChain;
|
|
10
|
+
questionGeneratorChain: LLMChain;
|
|
11
|
+
outputKey: string;
|
|
12
|
+
inputKey: string;
|
|
13
|
+
}
|
|
14
|
+
export declare class ConversationalRetrievalQAChain extends BaseChain implements ConversationalRetrievalQAChainInput {
|
|
15
|
+
inputKey: string;
|
|
16
|
+
chatHistoryKey: string;
|
|
17
|
+
get inputKeys(): string[];
|
|
18
|
+
outputKey: string;
|
|
19
|
+
retriever: BaseRetriever;
|
|
20
|
+
combineDocumentsChain: BaseChain;
|
|
21
|
+
questionGeneratorChain: LLMChain;
|
|
22
|
+
returnSourceDocuments: boolean;
|
|
23
|
+
constructor(fields: {
|
|
24
|
+
retriever: BaseRetriever;
|
|
25
|
+
combineDocumentsChain: BaseChain;
|
|
26
|
+
questionGeneratorChain: LLMChain;
|
|
27
|
+
inputKey?: string;
|
|
28
|
+
outputKey?: string;
|
|
29
|
+
returnSourceDocuments?: boolean;
|
|
30
|
+
});
|
|
31
|
+
_call(values: ChainValues): Promise<ChainValues>;
|
|
32
|
+
_chainType(): string;
|
|
33
|
+
static deserialize(_data: SerializedChatVectorDBQAChain, _values: LoadValues): Promise<ConversationalRetrievalQAChain>;
|
|
34
|
+
serialize(): SerializedChatVectorDBQAChain;
|
|
35
|
+
static fromLLM(llm: BaseLLM, retriever: BaseRetriever, options?: {
|
|
36
|
+
inputKey?: string;
|
|
37
|
+
outputKey?: string;
|
|
38
|
+
returnSourceDocuments?: boolean;
|
|
39
|
+
questionGeneratorTemplate?: string;
|
|
40
|
+
qaTemplate?: string;
|
|
41
|
+
}): ConversationalRetrievalQAChain;
|
|
42
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { PromptTemplate } from "../prompts/index.js";
|
|
2
|
+
import { BaseChain } from "./base.js";
|
|
3
|
+
import { LLMChain } from "./llm_chain.js";
|
|
4
|
+
import { loadQAStuffChain } from "./question_answering/load.js";
|
|
5
|
+
const question_generator_template = `Given the following conversation and a follow up question, rephrase the follow up question to be a standalone question.
|
|
6
|
+
|
|
7
|
+
Chat History:
|
|
8
|
+
{chat_history}
|
|
9
|
+
Follow Up Input: {question}
|
|
10
|
+
Standalone question:`;
|
|
11
|
+
const qa_template = `Use the following pieces of context to answer the question at the end. If you don't know the answer, just say that you don't know, don't try to make up an answer.
|
|
12
|
+
|
|
13
|
+
{context}
|
|
14
|
+
|
|
15
|
+
Question: {question}
|
|
16
|
+
Helpful Answer:`;
|
|
17
|
+
export class ConversationalRetrievalQAChain extends BaseChain {
|
|
18
|
+
get inputKeys() {
|
|
19
|
+
return [this.inputKey, this.chatHistoryKey];
|
|
20
|
+
}
|
|
21
|
+
constructor(fields) {
|
|
22
|
+
super();
|
|
23
|
+
Object.defineProperty(this, "inputKey", {
|
|
24
|
+
enumerable: true,
|
|
25
|
+
configurable: true,
|
|
26
|
+
writable: true,
|
|
27
|
+
value: "question"
|
|
28
|
+
});
|
|
29
|
+
Object.defineProperty(this, "chatHistoryKey", {
|
|
30
|
+
enumerable: true,
|
|
31
|
+
configurable: true,
|
|
32
|
+
writable: true,
|
|
33
|
+
value: "chat_history"
|
|
34
|
+
});
|
|
35
|
+
Object.defineProperty(this, "outputKey", {
|
|
36
|
+
enumerable: true,
|
|
37
|
+
configurable: true,
|
|
38
|
+
writable: true,
|
|
39
|
+
value: "result"
|
|
40
|
+
});
|
|
41
|
+
Object.defineProperty(this, "retriever", {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: true,
|
|
44
|
+
writable: true,
|
|
45
|
+
value: void 0
|
|
46
|
+
});
|
|
47
|
+
Object.defineProperty(this, "combineDocumentsChain", {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
configurable: true,
|
|
50
|
+
writable: true,
|
|
51
|
+
value: void 0
|
|
52
|
+
});
|
|
53
|
+
Object.defineProperty(this, "questionGeneratorChain", {
|
|
54
|
+
enumerable: true,
|
|
55
|
+
configurable: true,
|
|
56
|
+
writable: true,
|
|
57
|
+
value: void 0
|
|
58
|
+
});
|
|
59
|
+
Object.defineProperty(this, "returnSourceDocuments", {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: true,
|
|
62
|
+
writable: true,
|
|
63
|
+
value: false
|
|
64
|
+
});
|
|
65
|
+
this.retriever = fields.retriever;
|
|
66
|
+
this.combineDocumentsChain = fields.combineDocumentsChain;
|
|
67
|
+
this.questionGeneratorChain = fields.questionGeneratorChain;
|
|
68
|
+
this.inputKey = fields.inputKey ?? this.inputKey;
|
|
69
|
+
this.outputKey = fields.outputKey ?? this.outputKey;
|
|
70
|
+
this.returnSourceDocuments =
|
|
71
|
+
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
72
|
+
}
|
|
73
|
+
async _call(values) {
|
|
74
|
+
if (!(this.inputKey in values)) {
|
|
75
|
+
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
76
|
+
}
|
|
77
|
+
if (!(this.chatHistoryKey in values)) {
|
|
78
|
+
throw new Error(`chat history key ${this.inputKey} not found.`);
|
|
79
|
+
}
|
|
80
|
+
const question = values[this.inputKey];
|
|
81
|
+
const chatHistory = values[this.chatHistoryKey];
|
|
82
|
+
let newQuestion = question;
|
|
83
|
+
if (chatHistory.length > 0) {
|
|
84
|
+
const result = await this.questionGeneratorChain.call({
|
|
85
|
+
question,
|
|
86
|
+
chat_history: chatHistory,
|
|
87
|
+
});
|
|
88
|
+
const keys = Object.keys(result);
|
|
89
|
+
if (keys.length === 1) {
|
|
90
|
+
newQuestion = result[keys[0]];
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
throw new Error("Return from llm chain has multiple values, only single values supported.");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
const docs = await this.retriever.getRelevantDocuments(newQuestion);
|
|
97
|
+
const inputs = {
|
|
98
|
+
question: newQuestion,
|
|
99
|
+
input_documents: docs,
|
|
100
|
+
chat_history: chatHistory,
|
|
101
|
+
};
|
|
102
|
+
const result = await this.combineDocumentsChain.call(inputs);
|
|
103
|
+
if (this.returnSourceDocuments) {
|
|
104
|
+
return {
|
|
105
|
+
...result,
|
|
106
|
+
sourceDocuments: docs,
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
_chainType() {
|
|
112
|
+
return "conversational_retrieval_chain";
|
|
113
|
+
}
|
|
114
|
+
static async deserialize(_data, _values) {
|
|
115
|
+
throw new Error("Not implemented.");
|
|
116
|
+
}
|
|
117
|
+
serialize() {
|
|
118
|
+
throw new Error("Not implemented.");
|
|
119
|
+
}
|
|
120
|
+
static fromLLM(llm, retriever, options = {}) {
|
|
121
|
+
const { questionGeneratorTemplate, qaTemplate, ...rest } = options;
|
|
122
|
+
const question_generator_prompt = PromptTemplate.fromTemplate(questionGeneratorTemplate || question_generator_template);
|
|
123
|
+
const qa_prompt = PromptTemplate.fromTemplate(qaTemplate || qa_template);
|
|
124
|
+
const qaChain = loadQAStuffChain(llm, { prompt: qa_prompt });
|
|
125
|
+
const questionGeneratorChain = new LLMChain({
|
|
126
|
+
prompt: question_generator_prompt,
|
|
127
|
+
llm,
|
|
128
|
+
});
|
|
129
|
+
const instance = new this({
|
|
130
|
+
retriever,
|
|
131
|
+
combineDocumentsChain: qaChain,
|
|
132
|
+
questionGeneratorChain,
|
|
133
|
+
...rest,
|
|
134
|
+
});
|
|
135
|
+
return instance;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
//# sourceMappingURL=conversational_retrieval_chain.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conversational_retrieval_chain.js","sourceRoot":"","sources":["../../src/chains/conversational_retrieval_chain.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,qBAAqB,CAAC;AAIrD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,gBAAgB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AAS;QACP,MAAM,IAAI,KAAK,CAAC,kBAAkB,CAAC,CAAC;IACtC,CAAC;IAED,MAAM,CAAC,OAAO,CACZ,GAAY,EACZ,SAAwB,EACxB,UAMI,EAAE;QAEN,MAAM,EAAE,yBAAyB,EAAE,UAAU,EAAE,GAAG,IAAI,EAAE,GAAG,OAAO,CAAC;QACnE,MAAM,yBAAyB,GAAG,cAAc,CAAC,YAAY,CAC3D,yBAAyB,IAAI,2BAA2B,CACzD,CAAC;QACF,MAAM,SAAS,GAAG,cAAc,CAAC,YAAY,CAAC,UAAU,IAAI,WAAW,CAAC,CAAC;QAEzE,MAAM,OAAO,GAAG,gBAAgB,CAAC,GAAG,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,CAAC,CAAC;QAC7D,MAAM,sBAAsB,GAAG,IAAI,QAAQ,CAAC;YAC1C,MAAM,EAAE,yBAAyB;YACjC,GAAG;SACJ,CAAC,CAAC;QACH,MAAM,QAAQ,GAAG,IAAI,IAAI,CAAC;YACxB,SAAS;YACT,qBAAqB,EAAE,OAAO;YAC9B,sBAAsB;YACtB,GAAG,IAAI;SACR,CAAC,CAAC;QACH,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF"}
|
package/dist/chains/index.d.ts
CHANGED
|
@@ -8,5 +8,6 @@ export { loadChain } from "./load.js";
|
|
|
8
8
|
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, } from "./question_answering/load.js";
|
|
9
9
|
export { loadSummarizationChain } from "./summarization/load.js";
|
|
10
10
|
export { SqlDatabaseChain } from "./sql_db/sql_db_chain.js";
|
|
11
|
+
export { ConversationalRetrievalQAChain } from "./conversational_retrieval_chain.js";
|
|
12
|
+
export { RetrievalQAChain } from "./retrieval_qa.js";
|
|
11
13
|
export { SerializedLLMChain, SerializedSqlDatabaseChain, SerializedAnalyzeDocumentChain, SerializedBaseChain, SerializedChatVectorDBQAChain, SerializedMapReduceDocumentsChain, SerializedStuffDocumentsChain, SerializedVectorDBQAChain, } from "./serde.js";
|
|
12
|
-
export { OutputFixingParser } from "../output_parsers/fix.js";
|
package/dist/chains/index.js
CHANGED
|
@@ -8,5 +8,6 @@ export { loadChain } from "./load.js";
|
|
|
8
8
|
export { loadQAChain, loadQAStuffChain, loadQAMapReduceChain, } from "./question_answering/load.js";
|
|
9
9
|
export { loadSummarizationChain } from "./summarization/load.js";
|
|
10
10
|
export { SqlDatabaseChain } from "./sql_db/sql_db_chain.js";
|
|
11
|
-
export {
|
|
11
|
+
export { ConversationalRetrievalQAChain } from "./conversational_retrieval_chain.js";
|
|
12
|
+
export { RetrievalQAChain } from "./retrieval_qa.js";
|
|
12
13
|
//# sourceMappingURL=index.js.map
|
package/dist/chains/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/chains/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EACL,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/chains/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAe,MAAM,WAAW,CAAC;AACnD,OAAO,EAAE,QAAQ,EAAE,iBAAiB,EAAE,MAAM,gBAAgB,CAAC;AAC7D,OAAO,EACL,mBAAmB,EACnB,uBAAuB,GACxB,MAAM,yBAAyB,CAAC;AACjC,OAAO,EAAE,mBAAmB,EAAE,MAAM,2BAA2B,CAAC;AAChE,OAAO,EAAE,oBAAoB,EAAE,MAAM,8BAA8B,CAAC;AACpE,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,SAAS,EAAE,MAAM,WAAW,CAAC;AACtC,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,oBAAoB,GACrB,MAAM,8BAA8B,CAAC;AACtC,OAAO,EAAE,sBAAsB,EAAE,MAAM,yBAAyB,CAAC;AACjE,OAAO,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,EAAE,8BAA8B,EAAE,MAAM,qCAAqC,CAAC;AACrF,OAAO,EAAE,gBAAgB,EAAE,MAAM,mBAAmB,CAAC"}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { BaseChain } from "./base.js";
|
|
2
|
+
import { BaseLLM } from "../llms/index.js";
|
|
3
|
+
import { SerializedVectorDBQAChain } from "./serde.js";
|
|
4
|
+
import { ChainValues, BaseRetriever } from "../schema/index.js";
|
|
5
|
+
export type LoadValues = Record<string, any>;
|
|
6
|
+
export interface RetrievalQAChainInput {
|
|
7
|
+
retriever: BaseRetriever;
|
|
8
|
+
combineDocumentsChain: BaseChain;
|
|
9
|
+
outputKey: string;
|
|
10
|
+
inputKey: string;
|
|
11
|
+
returnSourceDocuments?: boolean;
|
|
12
|
+
}
|
|
13
|
+
export declare class RetrievalQAChain extends BaseChain implements RetrievalQAChainInput {
|
|
14
|
+
inputKey: string;
|
|
15
|
+
get inputKeys(): string[];
|
|
16
|
+
outputKey: string;
|
|
17
|
+
retriever: BaseRetriever;
|
|
18
|
+
combineDocumentsChain: BaseChain;
|
|
19
|
+
returnSourceDocuments: boolean;
|
|
20
|
+
constructor(fields: {
|
|
21
|
+
retriever: BaseRetriever;
|
|
22
|
+
combineDocumentsChain: BaseChain;
|
|
23
|
+
inputKey?: string;
|
|
24
|
+
outputKey?: string;
|
|
25
|
+
returnSourceDocuments?: boolean;
|
|
26
|
+
});
|
|
27
|
+
_call(values: ChainValues): Promise<ChainValues>;
|
|
28
|
+
_chainType(): "retrieval_qa";
|
|
29
|
+
static deserialize(_data: SerializedVectorDBQAChain, _values: LoadValues): Promise<RetrievalQAChain>;
|
|
30
|
+
serialize(): SerializedVectorDBQAChain;
|
|
31
|
+
static fromLLM(llm: BaseLLM, retriever: BaseRetriever, options?: Partial<Omit<RetrievalQAChainInput, "combineDocumentsChain" | "index">>): RetrievalQAChain;
|
|
32
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { BaseChain } from "./base.js";
|
|
2
|
+
import { loadQAStuffChain } from "./question_answering/load.js";
|
|
3
|
+
export class RetrievalQAChain extends BaseChain {
|
|
4
|
+
get inputKeys() {
|
|
5
|
+
return [this.inputKey];
|
|
6
|
+
}
|
|
7
|
+
constructor(fields) {
|
|
8
|
+
super();
|
|
9
|
+
Object.defineProperty(this, "inputKey", {
|
|
10
|
+
enumerable: true,
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true,
|
|
13
|
+
value: "query"
|
|
14
|
+
});
|
|
15
|
+
Object.defineProperty(this, "outputKey", {
|
|
16
|
+
enumerable: true,
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true,
|
|
19
|
+
value: "result"
|
|
20
|
+
});
|
|
21
|
+
Object.defineProperty(this, "retriever", {
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
writable: true,
|
|
25
|
+
value: void 0
|
|
26
|
+
});
|
|
27
|
+
Object.defineProperty(this, "combineDocumentsChain", {
|
|
28
|
+
enumerable: true,
|
|
29
|
+
configurable: true,
|
|
30
|
+
writable: true,
|
|
31
|
+
value: void 0
|
|
32
|
+
});
|
|
33
|
+
Object.defineProperty(this, "returnSourceDocuments", {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
configurable: true,
|
|
36
|
+
writable: true,
|
|
37
|
+
value: false
|
|
38
|
+
});
|
|
39
|
+
this.retriever = fields.retriever;
|
|
40
|
+
this.combineDocumentsChain = fields.combineDocumentsChain;
|
|
41
|
+
this.inputKey = fields.inputKey ?? this.inputKey;
|
|
42
|
+
this.outputKey = fields.outputKey ?? this.outputKey;
|
|
43
|
+
this.returnSourceDocuments =
|
|
44
|
+
fields.returnSourceDocuments ?? this.returnSourceDocuments;
|
|
45
|
+
}
|
|
46
|
+
async _call(values) {
|
|
47
|
+
if (!(this.inputKey in values)) {
|
|
48
|
+
throw new Error(`Question key ${this.inputKey} not found.`);
|
|
49
|
+
}
|
|
50
|
+
const question = values[this.inputKey];
|
|
51
|
+
const docs = await this.retriever.getRelevantDocuments(question);
|
|
52
|
+
const inputs = { question, input_documents: docs };
|
|
53
|
+
const result = await this.combineDocumentsChain.call(inputs);
|
|
54
|
+
if (this.returnSourceDocuments) {
|
|
55
|
+
return {
|
|
56
|
+
...result,
|
|
57
|
+
sourceDocuments: docs,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
return result;
|
|
61
|
+
}
|
|
62
|
+
_chainType() {
|
|
63
|
+
return "retrieval_qa";
|
|
64
|
+
}
|
|
65
|
+
static async deserialize(_data, _values) {
|
|
66
|
+
throw new Error("Not implemented");
|
|
67
|
+
}
|
|
68
|
+
serialize() {
|
|
69
|
+
throw new Error("Not implemented");
|
|
70
|
+
}
|
|
71
|
+
static fromLLM(llm, retriever, options) {
|
|
72
|
+
const qaChain = loadQAStuffChain(llm);
|
|
73
|
+
return new this({
|
|
74
|
+
retriever,
|
|
75
|
+
combineDocumentsChain: qaChain,
|
|
76
|
+
...options,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
//# sourceMappingURL=retrieval_qa.js.map
|
|
@@ -0,0 +1 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AAQ,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC;QACnD,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC7D,IAAI,IAAI,CAAC,qBAAqB,EAAE;YAC9B,OAAO;gBACL,GAAG,MAAM;gBACT,eAAe,EAAE,IAAI;aACtB,CAAC;SACH;QACD,OAAO,MAAM,CAAC;IAChB,CAAC;IAED,UAAU;QACR,OAAO,cAAuB,CAAC;IACjC,CAAC;IAED,MAAM,CAAC,KAAK,CAAC,WAAW,CACtB,KAAgC,EAChC,OAAmB;QAEnB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAED,SAAS;QACP,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;IAED,MAAM,CAAC,OAAO,CACZ,GAAY,EACZ,SAAwB,EACxB,OAEC;QAED,MAAM,OAAO,GAAG,gBAAgB,CAAC,GAAG,CAAC,CAAC;QACtC,OAAO,IAAI,IAAI,CAAC;YACd,SAAS;YACT,qBAAqB,EAAE,OAAO;YAC9B,GAAG,OAAO;SACX,CAAC,CAAC;IACL,CAAC;CACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cohere.js","sourceRoot":"","sources":["../../src/embeddings/cohere.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
2
|
+
import { BaseRetriever } from "../schema/index.js";
|
|
3
|
+
import { AsyncCaller, AsyncCallerParams } from "../util/async_caller.js";
|
|
4
|
+
interface ChatGPTPluginRetrieverFilter {
|
|
5
|
+
document_id?: string;
|
|
6
|
+
source?: string;
|
|
7
|
+
source_id?: string;
|
|
8
|
+
author?: string;
|
|
9
|
+
start_date?: string;
|
|
10
|
+
end_date?: string;
|
|
11
|
+
}
|
|
12
|
+
type ChatGPTPluginRetrieverAuth = false | {
|
|
13
|
+
bearer: string;
|
|
14
|
+
};
|
|
15
|
+
interface ChatGPTPluginRetrieverParams extends AsyncCallerParams {
|
|
16
|
+
/**
|
|
17
|
+
* The URL of the ChatGPTRetrievalPlugin server
|
|
18
|
+
*/
|
|
19
|
+
url: string;
|
|
20
|
+
/**
|
|
21
|
+
* The authentication method to use, currently implemented is
|
|
22
|
+
* - false: no authentication
|
|
23
|
+
* - { bearer: string }: Bearer token authentication
|
|
24
|
+
*/
|
|
25
|
+
auth: ChatGPTPluginRetrieverAuth;
|
|
26
|
+
/**
|
|
27
|
+
* The number of results to request from the ChatGPTRetrievalPlugin server
|
|
28
|
+
*/
|
|
29
|
+
topK?: number;
|
|
30
|
+
/**
|
|
31
|
+
* The filter to use when querying the ChatGPTRetrievalPlugin server
|
|
32
|
+
*/
|
|
33
|
+
filter?: ChatGPTPluginRetrieverFilter;
|
|
34
|
+
}
|
|
35
|
+
export declare class ChatGPTPluginRetriever extends BaseRetriever implements ChatGPTPluginRetrieverParams {
|
|
36
|
+
url: string;
|
|
37
|
+
auth: ChatGPTPluginRetrieverAuth;
|
|
38
|
+
topK: number;
|
|
39
|
+
filter?: ChatGPTPluginRetrieverFilter;
|
|
40
|
+
asyncCaller: AsyncCaller;
|
|
41
|
+
constructor({ url, auth, topK, filter, ...rest }: ChatGPTPluginRetrieverParams);
|
|
42
|
+
getRelevantDocuments(query: string): Promise<Document[]>;
|
|
43
|
+
}
|
|
44
|
+
export {};
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
2
|
+
import { BaseRetriever } from "../schema/index.js";
|
|
3
|
+
import { AsyncCaller } from "../util/async_caller.js";
|
|
4
|
+
export class ChatGPTPluginRetriever extends BaseRetriever {
|
|
5
|
+
constructor({ url, auth, topK = 4, filter, ...rest }) {
|
|
6
|
+
super();
|
|
7
|
+
Object.defineProperty(this, "url", {
|
|
8
|
+
enumerable: true,
|
|
9
|
+
configurable: true,
|
|
10
|
+
writable: true,
|
|
11
|
+
value: void 0
|
|
12
|
+
});
|
|
13
|
+
Object.defineProperty(this, "auth", {
|
|
14
|
+
enumerable: true,
|
|
15
|
+
configurable: true,
|
|
16
|
+
writable: true,
|
|
17
|
+
value: void 0
|
|
18
|
+
});
|
|
19
|
+
Object.defineProperty(this, "topK", {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
configurable: true,
|
|
22
|
+
writable: true,
|
|
23
|
+
value: void 0
|
|
24
|
+
});
|
|
25
|
+
Object.defineProperty(this, "filter", {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
configurable: true,
|
|
28
|
+
writable: true,
|
|
29
|
+
value: void 0
|
|
30
|
+
});
|
|
31
|
+
Object.defineProperty(this, "asyncCaller", {
|
|
32
|
+
enumerable: true,
|
|
33
|
+
configurable: true,
|
|
34
|
+
writable: true,
|
|
35
|
+
value: void 0
|
|
36
|
+
});
|
|
37
|
+
this.url = url;
|
|
38
|
+
this.auth = auth;
|
|
39
|
+
this.topK = topK;
|
|
40
|
+
this.asyncCaller = new AsyncCaller(rest);
|
|
41
|
+
}
|
|
42
|
+
async getRelevantDocuments(query) {
|
|
43
|
+
const res = await this.asyncCaller.call(fetch, `${this.url}/query`, {
|
|
44
|
+
method: "POST",
|
|
45
|
+
body: JSON.stringify({
|
|
46
|
+
queries: [
|
|
47
|
+
{
|
|
48
|
+
query,
|
|
49
|
+
top_k: this.topK,
|
|
50
|
+
filter: this.filter,
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
}),
|
|
54
|
+
headers: {
|
|
55
|
+
Accept: "application/json",
|
|
56
|
+
"Content-Type": "application/json",
|
|
57
|
+
...(this.auth && this.auth.bearer
|
|
58
|
+
? { Authorization: `Bearer ${this.auth.bearer}` }
|
|
59
|
+
: {}),
|
|
60
|
+
},
|
|
61
|
+
});
|
|
62
|
+
if (!res.ok) {
|
|
63
|
+
throw new Error(`Error calling ChatGPTPluginRetriever: ${res.status}`);
|
|
64
|
+
}
|
|
65
|
+
const body = await res.json();
|
|
66
|
+
const results = body?.results?.[0]?.results;
|
|
67
|
+
if (!results) {
|
|
68
|
+
// Note an empty array of results would not fall into this case
|
|
69
|
+
throw new Error("No results returned from ChatGPTPluginRetriever");
|
|
70
|
+
}
|
|
71
|
+
return results.map(
|
|
72
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
73
|
+
(result) => new Document({
|
|
74
|
+
pageContent: result.text,
|
|
75
|
+
metadata: result.metadata,
|
|
76
|
+
}));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
//# sourceMappingURL=chatgpt-plugin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"chatgpt-plugin.js","sourceRoot":"","sources":["../../src/retrievers/chatgpt-plugin.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,gBAAgB,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AACnD,OAAO,EAAE,WAAW,EAAqB,MAAM,yBAAyB,CAAC;AAqCzE,MAAM,OAAO,sBACX,SAAQ,aAAa;IAarB,YAAY,EACV,GAAG,EACH,IAAI,EACJ,IAAI,GAAG,CAAC,EACR,MAAM,EACN,GAAG,IAAI,EACsB;QAC7B,KAAK,EAAE,CAAC;QAjBV;;;;;WAAY;QAEZ;;;;;WAAiC;QAEjC;;;;;WAAa;QAEb;;;;;WAAsC;QAEtC;;;;;WAAyB;QAWvB,IAAI,CAAC,GAAG,GAAG,GAAG,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,WAAW,GAAG,IAAI,WAAW,CAAC,IAAI,CAAC,CAAC;IAC3C,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,KAAa;QACtC,MAAM,GAAG,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,IAAI,C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+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/retrievers/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC"}
|
package/dist/schema/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Document } from "../document.js";
|
|
1
2
|
export type Example = Record<string, string>;
|
|
2
3
|
export type InputValues = Record<string, any>;
|
|
3
4
|
export type PartialValues = Record<string, string | (() => Promise<string>) | (() => string)>;
|
|
@@ -79,7 +80,12 @@ export type AgentStep = {
|
|
|
79
80
|
};
|
|
80
81
|
export type ChainValues = Record<string, any>;
|
|
81
82
|
/**
|
|
82
|
-
*
|
|
83
|
+
* Base Index class. All indexes should extend this class.
|
|
84
|
+
*/
|
|
85
|
+
export declare abstract class BaseRetriever {
|
|
86
|
+
abstract getRelevantDocuments(query: string): Promise<Document[]>;
|
|
87
|
+
}
|
|
88
|
+
/** Class to parse the output of an LLM call.
|
|
83
89
|
*/
|
|
84
90
|
export declare abstract class BaseOutputParser {
|
|
85
91
|
/**
|
package/dist/schema/index.js
CHANGED
|
@@ -46,7 +46,11 @@ export class ChatMessage extends BaseChatMessage {
|
|
|
46
46
|
export class BasePromptValue {
|
|
47
47
|
}
|
|
48
48
|
/**
|
|
49
|
-
*
|
|
49
|
+
* Base Index class. All indexes should extend this class.
|
|
50
|
+
*/
|
|
51
|
+
export class BaseRetriever {
|
|
52
|
+
}
|
|
53
|
+
/** Class to parse the output of an LLM call.
|
|
50
54
|
*/
|
|
51
55
|
export class BaseOutputParser {
|
|
52
56
|
async parseWithPrompt(text, _prompt) {
|
package/dist/schema/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/schema/index.ts"],"names":[],"mappings":"AA4CA,MAAM,OAAgB,eAAe;IAOnC,YAAY,IAAY;QANxB,+BAA+B;QAC/B;;;;;WAAa;QAMX,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;CACF;AAED,MAAM,OAAO,gBAAiB,SAAQ,eAAe;IACnD,QAAQ;QACN,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAED,MAAM,OAAO,aAAc,SAAQ,eAAe;IAChD,QAAQ;QACN,OAAO,IAAI,CAAC;IACd,CAAC;CACF;AAED,MAAM,OAAO,iBAAkB,SAAQ,eAAe;IACpD,QAAQ;QACN,OAAO,QAAQ,CAAC;IAClB,CAAC;CACF;AAED,MAAM,OAAO,WAAY,SAAQ,eAAe;IAG9C,YAAY,IAAY,EAAE,IAAY;QACpC,KAAK,CAAC,IAAI,CAAC,CAAC;QAHd;;;;;WAAa;QAIX,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IACnB,CAAC;IAED,QAAQ;QACN,OAAO,SAAS,CAAC;IACnB,CAAC;CACF;AAaD;;GAEG;AACH,MAAM,OAAgB,eAAe;CAIpC;AAqBD;;GAEG;AACH,MAAM,OAAgB,aAAa;CAElC;AACD;GACG;AACH,MAAM,OAAgB,gBAAgB;IASpC,KAAK,CAAC,eAAe,CACnB,IAAY,EACZ,OAAwB;QAExB,OAAO,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IAC1B,CAAC;IAcD;;OAEG;IACH,KAAK;QACH,MAAM,IAAI,KAAK,CAAC,uBAAuB,CAAC,CAAC;IAC3C,CAAC;CACF;AAED,MAAM,OAAO,qBAAsB,SAAQ,KAAK;IAC9C,YAAY,OAAe;QACzB,KAAK,CAAC,OAAO,CAAC,CAAC;IACjB,CAAC;CACF"}
|
|
@@ -1,15 +1,26 @@
|
|
|
1
1
|
import { Embeddings } from "../embeddings/base.js";
|
|
2
2
|
import { Document } from "../document.js";
|
|
3
|
+
import { BaseRetriever } from "../schema/index.js";
|
|
4
|
+
export declare class VectorStoreRetriever extends BaseRetriever {
|
|
5
|
+
vectorStore: VectorStore;
|
|
6
|
+
k: number;
|
|
7
|
+
constructor(fields: {
|
|
8
|
+
vectorStore: VectorStore;
|
|
9
|
+
k?: number;
|
|
10
|
+
});
|
|
11
|
+
getRelevantDocuments(query: string): Promise<Document[]>;
|
|
12
|
+
}
|
|
3
13
|
export declare abstract class VectorStore {
|
|
4
14
|
embeddings: Embeddings;
|
|
5
15
|
constructor(embeddings: Embeddings, _dbConfig: Record<string, any>);
|
|
6
16
|
abstract addVectors(vectors: number[][], documents: Document[]): Promise<void>;
|
|
7
17
|
abstract addDocuments(documents: Document[]): Promise<void>;
|
|
8
18
|
abstract similaritySearchVectorWithScore(query: number[], k: number, filter?: object): Promise<[Document, number][]>;
|
|
9
|
-
similaritySearch(query: string, k?: number, filter?: object): Promise<Document[]>;
|
|
10
|
-
similaritySearchWithScore(query: string, k?: number, filter?: object): Promise<[object, number][]>;
|
|
19
|
+
similaritySearch(query: string, k?: number, filter?: object | undefined): Promise<Document[]>;
|
|
20
|
+
similaritySearchWithScore(query: string, k?: number, filter?: object | undefined): Promise<[object, number][]>;
|
|
11
21
|
static fromTexts(_texts: string[], _metadatas: object[], _embeddings: Embeddings, _dbConfig: Record<string, any>): Promise<VectorStore>;
|
|
12
22
|
static fromDocuments(_docs: Document[], _embeddings: Embeddings, _dbConfig: Record<string, any>): Promise<VectorStore>;
|
|
23
|
+
asRetriever(k?: number): BaseRetriever;
|
|
13
24
|
}
|
|
14
25
|
export declare abstract class SaveableVectorStore extends VectorStore {
|
|
15
26
|
abstract save(directory: string): Promise<void>;
|
|
@@ -1,3 +1,27 @@
|
|
|
1
|
+
import { BaseRetriever } from "../schema/index.js";
|
|
2
|
+
export class VectorStoreRetriever extends BaseRetriever {
|
|
3
|
+
constructor(fields) {
|
|
4
|
+
super();
|
|
5
|
+
Object.defineProperty(this, "vectorStore", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
configurable: true,
|
|
8
|
+
writable: true,
|
|
9
|
+
value: void 0
|
|
10
|
+
});
|
|
11
|
+
Object.defineProperty(this, "k", {
|
|
12
|
+
enumerable: true,
|
|
13
|
+
configurable: true,
|
|
14
|
+
writable: true,
|
|
15
|
+
value: 4
|
|
16
|
+
});
|
|
17
|
+
this.vectorStore = fields.vectorStore;
|
|
18
|
+
this.k = fields.k ?? this.k;
|
|
19
|
+
}
|
|
20
|
+
async getRelevantDocuments(query) {
|
|
21
|
+
const results = await this.vectorStore.similaritySearch(query, 4);
|
|
22
|
+
return results;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
1
25
|
export class VectorStore {
|
|
2
26
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
3
27
|
constructor(embeddings, _dbConfig) {
|
|
@@ -9,11 +33,11 @@ export class VectorStore {
|
|
|
9
33
|
});
|
|
10
34
|
this.embeddings = embeddings;
|
|
11
35
|
}
|
|
12
|
-
async similaritySearch(query, k = 4, filter =
|
|
36
|
+
async similaritySearch(query, k = 4, filter = undefined) {
|
|
13
37
|
const results = await this.similaritySearchVectorWithScore(await this.embeddings.embedQuery(query), k, filter);
|
|
14
38
|
return results.map((result) => result[0]);
|
|
15
39
|
}
|
|
16
|
-
async similaritySearchWithScore(query, k = 4, filter =
|
|
40
|
+
async similaritySearchWithScore(query, k = 4, filter = undefined) {
|
|
17
41
|
return this.similaritySearchVectorWithScore(await this.embeddings.embedQuery(query), k, filter);
|
|
18
42
|
}
|
|
19
43
|
static fromTexts(_texts, _metadatas, _embeddings,
|
|
@@ -26,6 +50,9 @@ export class VectorStore {
|
|
|
26
50
|
_dbConfig) {
|
|
27
51
|
throw new Error("the Langchain vectorstore implementation you are using forgot to override this, please report a bug");
|
|
28
52
|
}
|
|
53
|
+
asRetriever(k) {
|
|
54
|
+
return new VectorStoreRetriever({ vectorStore: this, k });
|
|
55
|
+
}
|
|
29
56
|
}
|
|
30
57
|
export class SaveableVectorStore extends VectorStore {
|
|
31
58
|
static load(_directory, _embeddings) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/vectorstores/base.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"base.js","sourceRoot":"","sources":["../../src/vectorstores/base.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAEnD,MAAM,OAAO,oBAAqB,SAAQ,aAAa;IAKrD,YAAY,MAAgD;QAC1D,KAAK,EAAE,CAAC;QALV;;;;;WAAyB;QAEzB;;;;mBAAI,CAAC;WAAC;QAIJ,IAAI,CAAC,WAAW,GAAG,MAAM,CAAC,WAAW,CAAC;QACtC,IAAI,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,CAAC;IAC9B,CAAC;IAED,KAAK,CAAC,oBAAoB,CAAC,KAAa;QACtC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,WAAW,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;QAClE,OAAO,OAAO,CAAC;IACjB,CAAC;CACF;AAED,MAAM,OAAgB,WAAW;IAG/B,8DAA8D;IAC9D,YAAY,UAAsB,EAAE,SAA8B;QAHlE;;;;;WAAuB;QAIrB,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC;IAC/B,CAAC;IAeD,KAAK,CAAC,gBAAgB,CACpB,KAAa,EACb,CAAC,GAAG,CAAC,EACL,SAA6B,SAAS;QAEtC,MAAM,OAAO,GAAG,MAAM,IAAI,CAAC,+BAA+B,CACxD,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,EACvC,CAAC,EACD,MAAM,CACP,CAAC;QAEF,OAAO,OAAO,CAAC,GAAG,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC;IAC5C,CAAC;IAED,KAAK,CAAC,yBAAyB,CAC7B,KAAa,EACb,CAAC,GAAG,CAAC,EACL,SAA6B,SAAS;QAEtC,OAAO,IAAI,CAAC,+BAA+B,CACzC,MAAM,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,KAAK,CAAC,EACvC,CAAC,EACD,MAAM,CACP,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,SAAS,CACd,MAAgB,EAChB,UAAoB,EACpB,WAAuB;IACvB,8DAA8D;IAC9D,SAA8B;QAE9B,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;IACJ,CAAC;IAED,MAAM,CAAC,aAAa,CAClB,KAAiB,EACjB,WAAuB;IACvB,8DAA8D;IAC9D,SAA8B;QAE9B,MAAM,IAAI,KAAK,CACb,qGAAqG,CACtG,CAAC;IACJ,CAAC;IAED,WAAW,CAAC,CAAU;QACpB,OAAO,IAAI,oBAAoB,CAAC,EAAE,WAAW,EAAE,IAAI,EAAE,CAAC,EAAE,CAAC,CAAC;IAC5D,CAAC;CACF;AAED,MAAM,OAAgB,mBAAoB,SAAQ,WAAW;IAG3D,MAAM,CAAC,IAAI,CACT,UAAkB,EAClB,WAAuB;QAEvB,MAAM,IAAI,KAAK,CAAC,iBAAiB,CAAC,CAAC;IACrC,CAAC;CACF"}
|
|
@@ -14,7 +14,7 @@ test("PineconeStore with external ids", async () => {
|
|
|
14
14
|
expect(store).toBeDefined();
|
|
15
15
|
await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }], ["id1"]);
|
|
16
16
|
expect(client.upsert).toHaveBeenCalledTimes(1);
|
|
17
|
-
const results = await store.similaritySearch("hello");
|
|
17
|
+
const results = await store.similaritySearch("hello", 1);
|
|
18
18
|
expect(results).toHaveLength(0);
|
|
19
19
|
});
|
|
20
20
|
test("PineconeStore with generated ids", async () => {
|
|
@@ -29,7 +29,7 @@ test("PineconeStore with generated ids", async () => {
|
|
|
29
29
|
expect(store).toBeDefined();
|
|
30
30
|
await store.addDocuments([{ pageContent: "hello", metadata: { a: 1 } }]);
|
|
31
31
|
expect(client.upsert).toHaveBeenCalledTimes(1);
|
|
32
|
-
const results = await store.similaritySearch("hello");
|
|
32
|
+
const results = await store.similaritySearch("hello", 1);
|
|
33
33
|
expect(results).toHaveLength(0);
|
|
34
34
|
});
|
|
35
35
|
//# sourceMappingURL=pinecone.test.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pinecone.test.js","sourceRoot":"","sources":["../../../src/vectorstores/tests/pinecone.test.ts"],"names":[],"mappings":"AAAA,uDAAuD;AACvD,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAC;AAE1D,OAAO,EAAE,aAAa,EAAE,MAAM,gBAAgB,CAAC;AAE/C,IAAI,CAAC,iCAAiC,EAAE,KAAK,IAAI,EAAE;IACjD,MAAM,MAAM,GAAG;QACb,MAAM,EAAE,IAAI,CAAC,EAAE,EAAE;QACjB,KAAK,EAAE,IAAI,CAAC,EAAE,EAAO,CAAC,iBAAiB,CAAC;YACtC,OAAO,EAAE,EAAE;SACZ,CAAC;KACH,CAAC;IACF,MAAM,UAAU,GAAG,IAAI,cAAc,EAAE,CAAC;IAExC,MAAM,KAAK,GAAG,IAAI,aAAa,CAAC,UAAU,EAAE,EAAE,aAAa,EAAE,MAAa,EAAE,CAAC,CAAC;IAE9E,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAE5B,MAAM,KAAK,CAAC,YAAY,CACtB,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,EAC9C,CAAC,KAAK,CAAC,CACR,CAAC;IAEF,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAE/C,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"pinecone.test.js","sourceRoot":"","sources":["../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EAAO,CAAC,iBAAiB,CAAC;YACtC,OAAO,EAAE,EAAE;SACZ,CAAC;KACH,CAAC;IACF,MAAM,UAAU,GAAG,IAAI,cAAc,EAAE,CAAC;IAExC,MAAM,KAAK,GAAG,IAAI,aAAa,CAAC,UAAU,EAAE,EAAE,aAAa,EAAE,MAAa,EAAE,CAAC,CAAC;IAE9E,MAAM,CAAC,KAAK,CAAC,CAAC,WAAW,EAAE,CAAC;IAE5B,MAAM,KAAK,CAAC,YAAY,CAAC,CAAC,EAAE,WAAW,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;IAEzE,MAAM,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,qBAAqB,CAAC,CAAC,CAAC,CAAC;IAE/C,MAAM,OAAO,GAAG,MAAM,KAAK,CAAC,gBAAgB,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC;IAEzD,MAAM,CAAC,OAAO,CAAC,CAAC,YAAY,CAAC,CAAC,CAAC,CAAC;AAClC,CAAC,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "langchain",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.40",
|
|
4
4
|
"description": "Typescript bindings for langchain",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./index.js",
|
|
@@ -43,6 +43,8 @@
|
|
|
43
43
|
"callbacks.d.ts",
|
|
44
44
|
"output_parsers.js",
|
|
45
45
|
"output_parsers.d.ts",
|
|
46
|
+
"retrievers.js",
|
|
47
|
+
"retrievers.d.ts",
|
|
46
48
|
"index.js",
|
|
47
49
|
"index.d.ts"
|
|
48
50
|
],
|
|
@@ -292,6 +294,10 @@
|
|
|
292
294
|
"./output_parsers": {
|
|
293
295
|
"types": "./output_parsers.d.ts",
|
|
294
296
|
"import": "./output_parsers.js"
|
|
297
|
+
},
|
|
298
|
+
"./retrievers": {
|
|
299
|
+
"types": "./retrievers.d.ts",
|
|
300
|
+
"import": "./retrievers.js"
|
|
295
301
|
}
|
|
296
302
|
}
|
|
297
303
|
}
|
package/retrievers.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/retrievers/index.js'
|
package/retrievers.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/retrievers/index.js'
|