forked from mongodb/chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TypeChatJsonTranslateFunc.ts
71 lines (59 loc) · 1.64 KB
/
TypeChatJsonTranslateFunc.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
import { createAzureOpenAILanguageModel, createJsonTranslator } from "typechat";
import { backOff } from "exponential-backoff";
export interface AzureOpenAiServiceConfig {
apiKey: string;
baseUrl: string;
deployment: string;
version: string;
}
export type MakeTypeChatJsonTranslateFuncArgs = {
/**
The name of the TypeChat schema or interface.
*/
schemaName: string;
/**
The text of a .d.ts that would inform the schema.
*/
schema: string;
/**
Settings for using the Azure service.
*/
azureOpenAiServiceConfig: AzureOpenAiServiceConfig;
/**
Number of times to retry the query preprocessor if it fails.
*/
numRetries?: number;
/**
Delay between retries in milliseconds.
*/
retryDelayMs?: number;
};
export function makeTypeChatJsonTranslateFunc<SchemaType extends object>({
azureOpenAiServiceConfig,
schema,
schemaName,
numRetries = 1,
retryDelayMs = 1000,
}: MakeTypeChatJsonTranslateFuncArgs): (prompt: string) => Promise<SchemaType> {
const { apiKey, baseUrl, deployment, version } = azureOpenAiServiceConfig;
const model = createAzureOpenAILanguageModel(
apiKey,
`${baseUrl}openai/deployments/${deployment}/chat/completions?api-version=${version}`
);
// LLM function
const translator = createJsonTranslator<SchemaType>(
model,
schema,
schemaName
);
return async (prompt: string) => {
const response = await backOff(() => translator.translate(prompt), {
numOfAttempts: numRetries,
startingDelay: retryDelayMs,
});
if (!response.success) {
throw response; // Response is `Error`
}
return response.data; // Success
};
}