-
Notifications
You must be signed in to change notification settings - Fork 2k
/
remote-retriever.ts
93 lines (84 loc) 路 2.56 KB
/
remote-retriever.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
import {
RemoteRetriever,
RemoteRetrieverParams,
RemoteRetrieverValues,
} from "@langchain/community/retrievers/remote";
import { Document } from "@langchain/core/documents";
/** @deprecated */
export interface RemoteLangChainRetrieverParams extends RemoteRetrieverParams {
/**
* The key in the JSON body to put the query in
*/
inputKey?: string;
/**
* The key in the JSON response to get the response from
*/
responseKey?: string;
/**
* The key in the JSON response to get the page content from
*/
pageContentKey?: string;
/**
* The key in the JSON response to get the metadata from
*/
metadataKey?: string;
}
/**
* @deprecated Use RemoteRetriever instead.
* Specific implementation of the `RemoteRetriever` class designed to
* retrieve documents from a remote source using a JSON-based API. It
* implements the `RemoteLangChainRetrieverParams` interface which defines
* the keys used to interact with the JSON API.
*/
export class RemoteLangChainRetriever
extends RemoteRetriever
implements RemoteLangChainRetrieverParams
{
lc_namespace = ["langchain", "retrievers", "remote", "remote-retriever"];
inputKey: string;
responseKey: string;
pageContentKey: string;
metadataKey: string;
constructor({
inputKey = "message",
responseKey = "response",
pageContentKey = "page_content",
metadataKey = "metadata",
...rest
}: RemoteLangChainRetrieverParams) {
super(rest);
this.inputKey = inputKey;
this.responseKey = responseKey;
this.pageContentKey = pageContentKey;
this.metadataKey = metadataKey;
}
/**
* Creates the JSON body of the request sent to the API. The `inputKey` is
* set to the query.
* @param query Query string to be sent to the API.
* @returns An object with the `inputKey` set to the query.
*/
createJsonBody(query: string): RemoteRetrieverValues {
return {
[this.inputKey]: query,
};
}
/**
* Processes the JSON response from the API. It returns an array of
* `Document` objects, each created with the page content and metadata
* extracted from the response using the `pageContentKey` and
* `metadataKey`, respectively.
* @param json JSON response from the API.
* @returns An array of `Document` objects.
*/
processJsonResponse(json: RemoteRetrieverValues): Document[] {
return json[this.responseKey].map(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(r: any) =>
new Document({
pageContent: r[this.pageContentKey],
metadata: r[this.metadataKey],
})
);
}
}