-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.ts
More file actions
432 lines (376 loc) · 11.2 KB
/
util.ts
File metadata and controls
432 lines (376 loc) · 11.2 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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import Cookies from 'js-cookie'
// API请求选项接口
export interface ChatApiOptions {
model: string
temperature: number
top_p: number
top_k: number
frequency_penalty: number
max_tokens: number
prompt_id?: string
request_id?: string
}
// 图像生成选项接口
export interface ImageGenerationOptions {
prompt: string
n?: number
model?: string
size?: string
loading_callback?: (loading: boolean) => void
error_callback?: (error: any) => void
}
// 消息接口
export interface ChatMessage {
role: 'user' | 'assistant' | 'system'
content: string | ChatContent[]
}
// 消息内容接口
export interface ChatContent {
type: 'text' | 'image_url'
text?: string
image_url?: {
url: string
}
}
// 流式回调接口
export interface StreamCallbacks {
onStart?: () => void
onToken?: (token: string) => void
onComplete?: (fullText: string) => void
onError?: (error: any) => void
}
// 默认API选项
export const defaultApiOptions: ChatApiOptions = {
model: 'Qwen/Qwen2.5-VL-72B-Instruct',
temperature: 0.7,
top_p: 0.7,
top_k: 50,
frequency_penalty: 0.5,
max_tokens: 1024
}
// 服务器API路径
const SERVER_MODEL_API_URL = '/bizyair/model/chat'
/**
* 构建聊天请求体
* @param messages 消息数组
* @param options API选项
* @returns 请求体对象
*/
export function buildChatRequestBody(
messages: ChatMessage[],
options: Partial<ChatApiOptions> = {}
): any {
const mergedOptions = { ...defaultApiOptions, ...options }
return {
model: mergedOptions.model,
stream: true,
max_tokens: mergedOptions.max_tokens,
temperature: mergedOptions.temperature,
top_p: mergedOptions.top_p,
top_k: mergedOptions.top_k,
frequency_penalty: mergedOptions.frequency_penalty,
n: 1,
stop: [],
messages: messages,
prompt_id: mergedOptions.prompt_id,
request_id: mergedOptions.request_id
}
}
/**
* 创建带有图片的用户消息
* @param text 文本内容
* @param imageBase64 图片的Base64编码(可以包含或不包含data:前缀)
* @returns 用户消息对象
*/
export function createImageUserMessage(text: string, imageBase64: string): ChatMessage {
// 确保imageBase64有正确的前缀
const imageUrl = imageBase64.startsWith('data:')
? imageBase64
: `data:image/png;base64,${imageBase64}`
return {
role: 'user',
content: [
{
type: 'image_url',
image_url: {
url: imageUrl
}
},
{
type: 'text',
text: text || '请描述一下这张图片'
}
]
}
}
/**
* 创建纯文本用户消息
* @param text 文本内容
* @returns 用户消息对象
*/
export function createTextUserMessage(text: string): ChatMessage {
return {
role: 'user',
content: text
}
}
/**
* 准备包含历史记录的消息数组用于API请求
* @param currentMessage 当前消息
* @returns Promise<ChatMessage[]>
*/
export async function prepareMessagesWithHistory(
currentMessage: ChatMessage
): Promise<ChatMessage[]> {
// 由于不再使用数据库,直接返回包含当前消息的数组
return [currentMessage]
}
/**
* 处理流式响应
* @param body 响应体流
* @param callbacks 流式回调
* @param signal 中止信号
*/
async function processStreamResponse(
body: ReadableStream<Uint8Array>,
callbacks: StreamCallbacks,
signal?: AbortSignal
): Promise<void> {
const reader = body.getReader()
const decoder = new TextDecoder()
let buffer = ''
let fullText = ''
// 如果提供了signal信号,监听中止事件
if (signal) {
signal.addEventListener('abort', () => {
reader.cancel().catch(err => console.error('取消读取流出错:', err))
})
}
try {
callbacks.onStart?.()
let isProcessing = true
while (isProcessing) {
// 检查是否已中止
if (signal?.aborted) {
reader.cancel().catch(err => console.error('取消读取流出错:', err))
break
}
const { done, value } = await reader.read()
if (done) {
isProcessing = false
break
}
buffer += decoder.decode(value, { stream: true })
// 处理缓冲区中的所有完整行
let lineEnd
while ((lineEnd = buffer.indexOf('\n')) !== -1) {
const line = buffer.slice(0, lineEnd).trim()
buffer = buffer.slice(lineEnd + 1)
if (line.startsWith('data: ')) {
const data = line.slice(6)
// 检查是否是结束标记
if (data === '[DONE]') {
// 最终处理文本格式
const formattedText = formatOutputText(fullText)
callbacks.onComplete?.(formattedText)
return
}
try {
const parsed = JSON.parse(data)
if (parsed.choices && parsed.choices[0]?.delta?.content !== undefined) {
const content = parsed.choices[0].delta.content
if (content) {
fullText += content
// 向UI回调发送当前token
callbacks.onToken?.(content)
}
}
} catch (e) {
console.error('解析响应数据出错:', e, data)
}
}
}
}
// 处理最后的缓冲区(如果有剩余内容)
decoder.decode()
const formattedText = formatOutputText(fullText)
callbacks.onComplete?.(formattedText)
} catch (error: any) {
console.error('处理流式响应时出错:', error)
callbacks.onError?.(error)
}
}
/**
* 发送流式聊天请求
* @param messages 消息数组或单条消息
* @param callbacks 流式回调
* @param options API选项
* @returns 用于中止请求的AbortController
*/
export async function sendStreamChatRequest(
messages: ChatMessage[] | ChatMessage,
callbacks: StreamCallbacks,
options: Partial<ChatApiOptions> = {}
): Promise<AbortController> {
// 如果传入的是单条消息,则添加历史记录
let messagesArray: ChatMessage[]
if (!Array.isArray(messages)) {
messagesArray = await prepareMessagesWithHistory(messages)
} else {
messagesArray = messages
}
const requestBody = buildChatRequestBody(messagesArray, options)
// 创建AbortController用于中止请求
const abortController = new AbortController()
try {
// 通知开始
callbacks.onStart?.()
const response = await fetch(SERVER_MODEL_API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: Cookies.get('bizy_token') || '',
...(options as any)?.headers
},
body: JSON.stringify(requestBody),
signal: abortController.signal // 添加中止信号
})
if (!response.ok) {
throw new Error(`API请求失败: ${response.status} ${response.statusText}`)
}
if (!response.body) {
throw new Error('响应体为空')
}
// 处理流式响应
processStreamResponse(response.body, callbacks, abortController.signal)
} catch (error: any) {
// 检查是否是由于中止导致的错误
if (error.name === 'AbortError') {
console.log('请求已中止')
callbacks.onComplete?.('') // 中止时清空并完成
} else {
console.error('API请求错误:', error)
callbacks.onError?.(error)
}
}
return abortController // 返回控制器供外部使用
}
/**
* 将base64数据转换为File对象
* @param base64Data base64数据(可以包含前缀)
* @param fileName 文件名
* @param mimeType MIME类型
* @returns File对象
*/
export function base64ToFile(
base64Data: string,
fileName: string,
mimeType: string = 'image/png'
): File {
// 如果包含前缀,则去除前缀
const base64Content = base64Data.includes('base64,') ? base64Data.split('base64,')[1] : base64Data
// 将base64解码为二进制数据
const byteCharacters = atob(base64Content)
const byteArrays: Uint8Array[] = []
for (let offset = 0; offset < byteCharacters.length; offset += 1024) {
const slice = byteCharacters.slice(offset, offset + 1024)
const byteNumbers = new Array(slice.length)
for (let i = 0; i < slice.length; i++) {
byteNumbers[i] = slice.charCodeAt(i)
}
const byteArray = new Uint8Array(byteNumbers)
byteArrays.push(byteArray)
}
const blob = new Blob(byteArrays, { type: mimeType })
return new File([blob], fileName, { type: mimeType })
}
/**
* 格式化模型输出文本,保留换行和格式
* @param text 原始模型输出文本
* @returns 格式化后的HTML文本
*/
export function formatOutputText(text: string): string {
if (!text) return ''
// 记录处理前的文本用于调试
console.log('格式化前的原始文本:', text)
// 替换井号标记(如 "###","####"等)
text = text.replace(/^(#{1,6})\s*(.+)$/gm, (hashes, content) => {
// 根据井号数量决定标题级别或者样式
const level = Math.min(hashes.length, 6)
// 对于标题内容,应用颜色样式而不是使用h标签
return `<div class="markdown-heading level-${level}">${content}</div>`
})
// 替换加粗文本
text = text.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
// 替换换行符为HTML换行标签
text = text.replace(/\n/g, '<br>')
// 去除连续的<br>标签
text = text.replace(/<br><br><br>/g, '<br><br>')
// 记录处理后的文本用于调试
console.log('格式化后的HTML文本:', text)
return text
}
/**
* 轻量级实时格式化文本函数,用于流式输出时的格式化
* @param text 原始模型输出文本
* @returns 格式化后的HTML文本
*/
export function formatOutputTextLight(text: string): string {
if (!text) return ''
// 基本的Markdown格式转换
let formatted = text
// 替换加粗文本
formatted = formatted.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>')
// 替换换行符为HTML换行标签
formatted = formatted.replace(/\n/g, '<br>')
return formatted
}
/**
* 生成图像
* @param options 图像生成选项
* @returns Promise<string> 返回生成的图像URL
*/
export async function generateImage(options: {
prompt: string
n?: number
size?: string
model?: string
loading_callback?: (loading: boolean) => void
error_callback?: (error: any) => void
}): Promise<string> {
const { prompt, loading_callback, error_callback } = options
try {
loading_callback?.(true)
const response = await fetch('/bizyair/model/images', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: Cookies.get('bizy_token') || '',
...(options as any)?.headers
},
body: JSON.stringify({
prompt: prompt,
n: options.n || 1,
model: options.model || 'Kwai-Kolors/Kolors',
size: options.size || '1024x1024'
})
})
if (!response.ok) {
throw new Error(`图像生成API请求失败: ${response.status} ${response.statusText}`)
}
const data = await response.json()
console.log('图像生成成功:', data)
// 返回生成的图像URL
if (data.data) {
return data.data.images[0].url
} else {
throw new Error('API返回的数据中没有图像URL')
}
} catch (error) {
console.error('生成图像时出错:', error)
error_callback?.(error)
throw error
} finally {
loading_callback?.(false)
}
}