-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
52 lines (47 loc) · 1.34 KB
/
index.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
import { genkitPlugin } from "@genkit-ai/core";
import { defineFlow } from "@genkit-ai/flow";
import * as z from "zod";
import { saveHNSWIndexer } from "./indexer";
import { FlowOptions, PluginOptions } from "./interfaces";
import { throwError } from "./utilities";
import {
PLUGIN_NAME,
FLOW_NAME,
ERROR_NO_API_KEY,
SCHEMA_INPUT,
SCHEMA_OUTPUT,
SCHEMA_RESULT,
} from "./constants";
const flowConfig = {
name: FLOW_NAME,
inputSchema: z.object({
input: z.string().describe(SCHEMA_INPUT),
output: z.string().describe(SCHEMA_OUTPUT),
}),
outputSchema: z.string().describe(SCHEMA_RESULT),
};
const flowAction = async (
flowOptions: FlowOptions,
pluginOptions: PluginOptions
) => {
try {
await saveHNSWIndexer(flowOptions, pluginOptions);
} catch (error) {
return `Vector saving error ${error}`;
}
return `Vector store saved at ${flowOptions.output}`;
};
const checkApiKey = (pluginOptions: PluginOptions) => {
const { apiKey } = pluginOptions;
const isNoApiKey = !apiKey && !process.env.GOOGLE_API_KEY;
if (isNoApiKey) return throwError("INVALID_ARGUMENT", ERROR_NO_API_KEY);
};
export const HNSWIndexer = genkitPlugin(
PLUGIN_NAME,
async (pluginOptions: PluginOptions) => {
checkApiKey(pluginOptions);
defineFlow(flowConfig, (flowOptions) =>
flowAction(flowOptions, pluginOptions)
);
}
);