-
Notifications
You must be signed in to change notification settings - Fork 363
/
QueryEngineTool.ts
46 lines (38 loc) · 1.29 KB
/
QueryEngineTool.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
import type { JSONSchemaType } from "ajv";
import type { BaseTool, QueryEngine, ToolMetadata } from "../types.js";
const DEFAULT_NAME = "query_engine_tool";
const DEFAULT_DESCRIPTION =
"Useful for running a natural language query against a knowledge base and get back a natural language response.";
const DEFAULT_PARAMETERS: JSONSchemaType<QueryEngineParam> = {
type: "object",
properties: {
query: {
type: "string",
description: "The query to search for",
},
},
required: ["query"],
};
export type QueryEngineToolParams = {
queryEngine: QueryEngine;
metadata: ToolMetadata<JSONSchemaType<QueryEngineParam>>;
};
export type QueryEngineParam = {
query: string;
};
export class QueryEngineTool implements BaseTool<QueryEngineParam> {
private queryEngine: QueryEngine;
metadata: ToolMetadata<JSONSchemaType<QueryEngineParam>>;
constructor({ queryEngine, metadata }: QueryEngineToolParams) {
this.queryEngine = queryEngine;
this.metadata = {
name: metadata?.name ?? DEFAULT_NAME,
description: metadata?.description ?? DEFAULT_DESCRIPTION,
parameters: metadata?.parameters ?? DEFAULT_PARAMETERS,
};
}
async call({ query }: QueryEngineParam) {
const response = await this.queryEngine.query({ query });
return response.response;
}
}