forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm-gateway.ts
More file actions
68 lines (62 loc) · 2.04 KB
/
Copy pathllm-gateway.ts
File metadata and controls
68 lines (62 loc) · 2.04 KB
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
export const ADMIN_LLM_LANES = {
chatLab: "omi:auto:persona-chat-premium",
notificationRegeneration: "omi:auto:proactive-notification",
} as const;
type GatewayMessage = {
role: "system" | "user" | "assistant";
content: string;
};
type GatewayRequest = {
lane: (typeof ADMIN_LLM_LANES)[keyof typeof ADMIN_LLM_LANES];
feature: string;
messages: GatewayMessage[];
responseFormat?: Record<string, unknown>;
temperature?: number;
};
export class AdminLlmGatewayUnavailableError extends Error {
constructor() {
super("The LLM gateway is unavailable");
this.name = "AdminLlmGatewayUnavailableError";
}
}
export async function invokeAdminLlmGateway(
request: GatewayRequest,
fetchImpl: typeof fetch = fetch,
): Promise<string> {
const gatewayUrl = process.env.OMI_LLM_GATEWAY_URL?.trim();
const gatewayToken = process.env.OMI_LLM_GATEWAY_SERVICE_TOKEN?.trim();
if (!gatewayUrl || !gatewayToken) throw new AdminLlmGatewayUnavailableError();
let response: Response;
try {
response = await fetchImpl(
`${gatewayUrl.replace(/\/+$/, "")}/v1/chat/completions`,
{
method: "POST",
headers: {
Authorization: `Bearer ${gatewayToken}`,
"Content-Type": "application/json",
"X-Omi-Service-Caller": "omi-admin-dashboard",
"X-Omi-LLM-Feature": request.feature,
},
body: JSON.stringify({
model: request.lane,
messages: request.messages,
...(request.responseFormat
? { response_format: request.responseFormat }
: {}),
temperature: request.temperature ?? 0.3,
}),
},
);
} catch {
throw new AdminLlmGatewayUnavailableError();
}
if (!response.ok) throw new AdminLlmGatewayUnavailableError();
const payload = (await response.json()) as {
choices?: Array<{ message?: { content?: unknown } }>;
};
const content = payload.choices?.[0]?.message?.content;
if (typeof content !== "string" || !content.trim())
throw new AdminLlmGatewayUnavailableError();
return content;
}