-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
vespa.ts
92 lines (83 loc) · 2.49 KB
/
vespa.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
import { Document, type DocumentInterface } from "@langchain/core/documents";
import {
RemoteRetriever,
RemoteRetrieverValues,
RemoteRetrieverParams,
} from "./remote/base.js";
export interface VespaRetrieverParams extends RemoteRetrieverParams {
/**
* The body of the query to send to Vespa
*/
query_body: object;
/**
* The name of the field the content resides in
*/
content_field: string;
}
/**
* Class responsible for retrieving data from Vespa. It extends the
* `RemoteRetriever` class and includes methods for creating the JSON body
* for a query and processing the JSON response from Vespa.
* @example
* ```typescript
* const retriever = new VespaRetriever({
* url: "https:
* auth: false,
* query_body: {
* yql: "select content from paragraph where userQuery()",
* hits: 5,
* ranking: "documentation",
* locale: "en-us",
* },
* content_field: "content",
* });
* const result = await retriever.getRelevantDocuments("what is vespa?");
* ```
*/
export class VespaRetriever extends RemoteRetriever {
static lc_name() {
return "VespaRetriever";
}
lc_namespace = ["langchain", "retrievers", "vespa"];
query_body: object;
content_field: string;
constructor(fields: VespaRetrieverParams) {
super(fields);
this.query_body = fields.query_body;
this.content_field = fields.content_field;
this.url = `${this.url}/search/?`;
}
/**
* Method that takes a query string as input and returns a JSON object
* that includes the query and the original `query_body`.
* @param query The query string to be sent to Vespa.
* @returns A JSON object that includes the query and the original `query_body`.
*/
createJsonBody(query: string): RemoteRetrieverValues {
return {
...this.query_body,
query,
};
}
/**
* Method that processes the JSON response from Vespa into an array of
* `Document` instances. Each `Document` instance includes the content
* from the specified `content_field` and the document's ID.
* @param json The JSON response from Vespa.
* @returns An array of `Document` instances.
*/
processJsonResponse(json: RemoteRetrieverValues): DocumentInterface[] {
return json.root.children.map(
(doc: {
id: string;
relevance: number;
source: string;
fields: Record<string, unknown>;
}) =>
new Document({
pageContent: doc.fields[this.content_field] as string,
metadata: { id: doc.id },
})
);
}
}