|
| 1 | +import { AbstractBot, SendMessageParams } from '../abstract-bot' |
| 2 | +import { requestHostPermission } from '~app/utils/permissions' |
| 3 | +import { ChatError, ErrorCode } from '~utils/errors' |
| 4 | +import { uuid } from '~utils' |
| 5 | +import { generateMessageId, generateSessionId, getUserInfo } from './api' |
| 6 | +import { streamAsyncIterable } from '~utils/stream-async-iterable' |
| 7 | + |
| 8 | +interface Message { |
| 9 | + id: string |
| 10 | + createdAt: number |
| 11 | + data: string |
| 12 | + from: 0 | 1 // human | bot |
| 13 | +} |
| 14 | + |
| 15 | +interface ConversationContext { |
| 16 | + conversationId: string |
| 17 | + historyMessages: Message[] |
| 18 | + userId: number |
| 19 | + lastMessageId?: string |
| 20 | +} |
| 21 | + |
| 22 | +export class BaichuanWebBot extends AbstractBot { |
| 23 | + private conversationContext?: ConversationContext |
| 24 | + |
| 25 | + async doSendMessage(params: SendMessageParams) { |
| 26 | + if (!(await requestHostPermission('https://*.baichuan-ai.com/'))) { |
| 27 | + throw new ChatError('Missing baichuan-ai.com permission', ErrorCode.MISSING_HOST_PERMISSION) |
| 28 | + } |
| 29 | + |
| 30 | + if (!this.conversationContext) { |
| 31 | + const conversationId = generateSessionId() |
| 32 | + const userInfo = await getUserInfo() |
| 33 | + this.conversationContext = { conversationId, historyMessages: [], userId: userInfo.id } |
| 34 | + } |
| 35 | + |
| 36 | + const { conversationId, lastMessageId, historyMessages, userId } = this.conversationContext |
| 37 | + |
| 38 | + const message: Message = { |
| 39 | + id: generateMessageId(), |
| 40 | + createdAt: Date.now(), |
| 41 | + data: params.prompt, |
| 42 | + from: 0, |
| 43 | + } |
| 44 | + |
| 45 | + const resp = await fetch('https://www.baichuan-ai.com/api/chat/v1/chat', { |
| 46 | + method: 'POST', |
| 47 | + signal: params.signal, |
| 48 | + headers: { |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + }, |
| 51 | + body: JSON.stringify({ |
| 52 | + stream: true, |
| 53 | + request_id: uuid(), |
| 54 | + app_info: { id: 10001, name: 'baichuan_web' }, |
| 55 | + user_info: { id: userId, status: 1 }, |
| 56 | + prompt: { |
| 57 | + id: message.id, |
| 58 | + data: message.data, |
| 59 | + from: message.from, |
| 60 | + parent_id: lastMessageId || 0, |
| 61 | + created_at: message.createdAt, |
| 62 | + }, |
| 63 | + session_info: { id: conversationId, name: '新的对话', created_at: Date.now() }, |
| 64 | + parameters: { |
| 65 | + repetition_penalty: -1, |
| 66 | + temperature: -1, |
| 67 | + top_k: -1, |
| 68 | + top_p: -1, |
| 69 | + max_new_tokens: -1, |
| 70 | + do_sample: -1, |
| 71 | + regenerate: 0, |
| 72 | + }, |
| 73 | + history: historyMessages, |
| 74 | + }), |
| 75 | + }) |
| 76 | + |
| 77 | + const decoder = new TextDecoder() |
| 78 | + let result = '' |
| 79 | + let answerMessageId: string | undefined |
| 80 | + |
| 81 | + for await (const uint8Array of streamAsyncIterable(resp.body!)) { |
| 82 | + const str = decoder.decode(uint8Array) |
| 83 | + console.debug('baichuan stream', str) |
| 84 | + const lines = str.split('\n') |
| 85 | + for (const line of lines) { |
| 86 | + if (!line) { |
| 87 | + continue |
| 88 | + } |
| 89 | + const data = JSON.parse(line) |
| 90 | + if (!data.answer) { |
| 91 | + continue |
| 92 | + } |
| 93 | + answerMessageId = data.answer.id |
| 94 | + const text = data.answer.data |
| 95 | + if (text) { |
| 96 | + result += text |
| 97 | + params.onEvent({ type: 'UPDATE_ANSWER', data: { text: result } }) |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + this.conversationContext.historyMessages.push(message) |
| 103 | + if (answerMessageId) { |
| 104 | + this.conversationContext.lastMessageId = answerMessageId |
| 105 | + if (result) { |
| 106 | + this.conversationContext.historyMessages.push({ |
| 107 | + id: answerMessageId, |
| 108 | + data: result, |
| 109 | + createdAt: Date.now(), |
| 110 | + from: 1, |
| 111 | + }) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + params.onEvent({ type: 'DONE' }) |
| 116 | + } |
| 117 | + |
| 118 | + resetConversation() { |
| 119 | + this.conversationContext = undefined |
| 120 | + } |
| 121 | + |
| 122 | + get name() { |
| 123 | + return '百川大模型' |
| 124 | + } |
| 125 | +} |
0 commit comments