-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathllmClient.ts
More file actions
394 lines (350 loc) · 10.3 KB
/
llmClient.ts
File metadata and controls
394 lines (350 loc) · 10.3 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
/**
* Minimal LLM API Client
* Supports OpenAI-compatible / Anthropic-compatible formats
*/
import type { LLMConfig } from './llmModels';
import { logger } from './logger';
import { loadPersistedConfig, savePersistedConfig } from './configPersistence';
const CONFIG_KEY = 'webuiapps-llm-config';
export async function loadConfig(): Promise<LLMConfig | null> {
try {
const persisted = await loadPersistedConfig();
if (persisted?.llm) {
localStorage.setItem(CONFIG_KEY, JSON.stringify(persisted.llm));
return persisted.llm;
}
} catch {
// API not available (production / network error)
}
try {
const raw = localStorage.getItem(CONFIG_KEY);
return raw ? JSON.parse(raw) : null;
} catch {
return null;
}
}
export async function saveConfig(
config: LLMConfig,
imageGenConfig?: import('./imageGenClient').ImageGenConfig | null,
): Promise<void> {
localStorage.setItem(CONFIG_KEY, JSON.stringify(config));
const persisted: import('./configPersistence').PersistedConfig = {
llm: config,
};
if (imageGenConfig) {
persisted.imageGen = imageGenConfig;
}
await savePersistedConfig(persisted);
}
export function loadConfigSync(): LLMConfig | null {
try {
const raw = localStorage.getItem(CONFIG_KEY);
return raw ? JSON.parse(raw) : null;
} catch {
return null;
}
}
export interface ChatMessage {
role: 'system' | 'user' | 'assistant' | 'tool';
content: string;
tool_call_id?: string;
tool_calls?: ToolCall[];
}
export interface ToolCall {
id: string;
type: 'function';
function: {
name: string;
arguments: string;
};
}
export interface ToolDef {
type: 'function';
function: {
name: string;
description: string;
parameters: {
type: 'object';
properties: Record<string, unknown>;
required: string[];
};
};
}
interface LLMResponse {
content: string;
toolCalls: ToolCall[];
}
interface InlineToolParseResult {
content: string;
toolCalls: ToolCall[];
}
function stripThinkTags(content: string): string {
const withoutBlocks = content
.replace(/<think\b[^>]*>[\s\S]*?<\/think>/gi, '')
.replace(/<\/?think\b[^>]*>/gi, '');
return withoutBlocks === content ? content : withoutBlocks.trim();
}
function parseInlineArgValue(rawValue: string): unknown {
const trimmed = rawValue.trim();
if (!trimmed) return '';
try {
return JSON.parse(trimmed);
} catch {
return trimmed;
}
}
function extractInlineToolCalls(rawContent: string): InlineToolParseResult {
const content = stripThinkTags(rawContent);
if (!content.includes('<arg_key>') || !content.includes('<arg_value>')) {
return { content, toolCalls: [] };
}
const blockRegex = /(?:<tool_call>\s*|\()([a-zA-Z0-9_.-]+)\s*([\s\S]*?)<\/tool_call>/g;
const toolCalls: ToolCall[] = [];
let cleanedContent = content;
let matchIndex = 0;
for (const match of content.matchAll(blockRegex)) {
const toolName = match[1]?.trim();
const body = match[2] ?? '';
if (!toolName) continue;
const args: Record<string, unknown> = {};
const pairRegex =
/<arg_key>\s*([\s\S]*?)\s*<\/arg_key>\s*<arg_value>\s*([\s\S]*?)\s*<\/arg_value>/g;
for (const pair of body.matchAll(pairRegex)) {
const key = pair[1]?.trim();
if (!key) continue;
args[key] = parseInlineArgValue(pair[2] ?? '');
}
if (Object.keys(args).length === 0) continue;
toolCalls.push({
id: `inline_tool_${matchIndex++}`,
type: 'function',
function: {
name: toolName,
arguments: JSON.stringify(args),
},
});
cleanedContent = cleanedContent.replace(match[0], '');
}
return {
content: cleanedContent.trim(),
toolCalls,
};
}
function hasVersionSuffix(url: string): boolean {
return /\/v\d+\/?$/.test(url);
}
function joinUrl(baseUrl: string, path: string): string {
return `${baseUrl.replace(/\/+$/, '')}/${path.replace(/^\/+/, '')}`;
}
function getOpenAICompletionsPath(baseUrl: string): string {
return hasVersionSuffix(baseUrl) ? 'chat/completions' : 'v1/chat/completions';
}
function getAnthropicMessagesPath(baseUrl: string): string {
return hasVersionSuffix(baseUrl) ? 'messages' : 'v1/messages';
}
function parseCustomHeaders(raw?: string): Record<string, string> {
if (!raw) return {};
const headers: Record<string, string> = {};
for (const line of raw.split('\n')) {
const trimmed = line.trim();
if (!trimmed) continue;
const idx = trimmed.indexOf(':');
if (idx > 0) {
const key = trimmed.slice(0, idx).trim().toLowerCase();
const val = trimmed.slice(idx + 1).trim();
headers[`x-custom-${key}`] = val;
}
}
return headers;
}
export async function chat(
messages: ChatMessage[],
tools: ToolDef[],
config: LLMConfig,
): Promise<LLMResponse> {
logger.info(
'LLM',
'chat() called, provider:',
config.provider,
'model:',
config.model,
'messages:',
messages.length,
);
if (config.provider === 'anthropic' || config.provider === 'minimax') {
return chatAnthropic(messages, tools, config);
}
return chatOpenAI(messages, tools, config);
}
async function chatOpenAI(
messages: ChatMessage[],
tools: ToolDef[],
config: LLMConfig,
): Promise<LLMResponse> {
const body: Record<string, unknown> = {
model: config.model,
messages,
};
if (tools.length > 0) {
body.tools = tools;
}
const targetUrl = joinUrl(config.baseUrl, getOpenAICompletionsPath(config.baseUrl));
const toolNames = Array.isArray(tools) ? tools.map((t) => t.function?.name).filter(Boolean) : [];
logger.info('ToolLog', 'LLM Request: toolCount=', tools.length, 'toolNames=', toolNames);
logger.info('LLM', 'Request:', {
targetUrl,
model: config.model,
messageCount: messages.length,
toolCount: tools.length,
});
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'X-LLM-Target-URL': targetUrl,
...parseCustomHeaders(config.customHeaders),
};
if (config.apiKey.trim()) {
headers.Authorization = `Bearer ${config.apiKey}`;
}
const res = await fetch('/api/llm-proxy', {
method: 'POST',
headers,
body: JSON.stringify(body),
});
logger.info('LLM', 'Response status:', res.status);
const text = await res.text();
logger.info('LLM', 'Response body:', text.slice(0, 500));
if (!res.ok) {
throw new Error(`LLM API error ${res.status}: ${text}`);
}
const data = JSON.parse(text);
const choice = data.choices?.[0]?.message;
const parsedInline = extractInlineToolCalls(choice?.content || '');
const toolCalls = choice?.tool_calls?.length ? choice.tool_calls : parsedInline.toolCalls;
const calledNames = toolCalls
.map((tc: { function?: { name?: string } }) => tc.function?.name)
.filter(Boolean);
logger.info(
'ToolLog',
'LLM Response: toolCalls count=',
toolCalls.length,
'calledNames=',
calledNames,
);
return {
content: choice?.tool_calls?.length
? stripThinkTags(choice?.content || '')
: parsedInline.content,
toolCalls,
};
}
async function chatAnthropic(
messages: ChatMessage[],
tools: ToolDef[],
config: LLMConfig,
): Promise<LLMResponse> {
const systemMsg = messages.find((m) => m.role === 'system')?.content || '';
const nonSystemMessages = messages.filter((m) => m.role !== 'system');
const anthropicMessages = nonSystemMessages.map((m) => {
if (m.role === 'tool') {
return {
role: 'user' as const,
content: [
{
type: 'tool_result' as const,
tool_use_id: m.tool_call_id,
content: m.content,
},
],
};
}
if (m.role === 'assistant' && m.tool_calls?.length) {
return {
role: 'assistant' as const,
content: [
...(m.content ? [{ type: 'text' as const, text: m.content }] : []),
...m.tool_calls.map((tc) => ({
type: 'tool_use' as const,
id: tc.id,
name: tc.function.name,
input: JSON.parse(tc.function.arguments),
})),
],
};
}
return { role: m.role as 'user' | 'assistant', content: m.content };
});
const anthropicTools = tools.map((t) => ({
name: t.function.name,
description: t.function.description,
input_schema: t.function.parameters,
}));
const body: Record<string, unknown> = {
model: config.model,
max_tokens: 4096,
messages: anthropicMessages,
};
if (systemMsg) body.system = systemMsg;
if (anthropicTools.length > 0) body.tools = anthropicTools;
const anthropicToolNames = anthropicTools.map((t) => t.name).filter(Boolean);
logger.info(
'ToolLog',
'Anthropic Request: toolCount=',
anthropicTools.length,
'toolNames=',
anthropicToolNames,
);
const targetUrl = joinUrl(config.baseUrl, getAnthropicMessagesPath(config.baseUrl));
logger.info('LLM', 'Anthropic Request:', {
targetUrl,
model: config.model,
messageCount: anthropicMessages.length,
toolCount: anthropicTools.length,
});
const headers: Record<string, string> = {
'Content-Type': 'application/json',
'anthropic-version': '2023-06-01',
'X-LLM-Target-URL': targetUrl,
...parseCustomHeaders(config.customHeaders),
};
if (config.apiKey.trim()) {
headers['x-api-key'] = config.apiKey;
}
const res = await fetch('/api/llm-proxy', {
method: 'POST',
headers,
body: JSON.stringify(body),
});
logger.info('LLM', 'Anthropic Response status:', res.status);
if (!res.ok) {
const text = await res.text();
logger.error('LLM', 'Anthropic Error body:', text.slice(0, 500));
throw new Error(`Anthropic API error ${res.status}: ${text}`);
}
const data = await res.json();
logger.info('LLM', 'Anthropic Response data:', JSON.stringify(data).slice(0, 500));
let content = '';
const toolCalls: ToolCall[] = [];
for (const block of data.content || []) {
if (block.type === 'text') {
content += block.text;
} else if (block.type === 'tool_use') {
toolCalls.push({
id: block.id,
type: 'function',
function: {
name: block.name,
arguments: JSON.stringify(block.input),
},
});
}
}
const calledNames = toolCalls.map((tc) => tc.function.name).filter(Boolean);
logger.info(
'ToolLog',
'Anthropic Response: toolCalls count=',
toolCalls.length,
'calledNames=',
calledNames,
);
return { content: stripThinkTags(content), toolCalls };
}