{"repo": "bigwhiteshark/anysearch", "files": {"backend/src/platform/qihu.ts": {"265fc2476943802eee34766144a292162b73d893": {"t": 1785768143.760863, "n": 2651, "h": "388a914265a08a42", "c": "import { TChatInputMessage, TStreamHandler } from '../types';\nimport { httpRequest } from '../utils';\nimport { BaseChat } from './base';\nimport { fetchEventData } from 'fetch-sse';\n\nconst BaseURL = 'https://api.360.cn/v1/';\nconst APIS = {\n chat: 'chat/completions',\n};\n\nexport class QihuChat implements BaseChat {\n private key?: string;\n platform = 'qihu';\n\n constructor() {\n this.key = process.env.QIHU_KEY;\n }\n\n public async chat(\n messages: TChatInputMessage[],\n model: string,\n system?: string\n ) {\n if (system) {\n messages = [\n {\n role: 'system',\n content: system,\n },\n ...messages,\n ];\n }\n const options = {\n input: {\n messages,\n },\n };\n const url = `${BaseURL}${APIS.chat}`;\n const payload = JSON.stringify({\n model,\n input: options.input\n });\n const res = await httpRequest({\n url,\n method: 'POST',\n headers: {\n 'Content-Type': 'application/json',\n 'Authorization': `Bearer ${this.key}`\n },\n data: payload\n });\n const data = await res.json();\n if (data?.message) {\n console.error(data);\n throw new Error(data.message ?? 'bad request.');\n }\n return data.output.text;\n }\n\n public async chatStream(\n messages: TChatInputMessage[],\n onMessage: TStreamHandler,\n model: string,\n system?: string\n ): Promise {\n if (system) {\n messages = [\n {\n role: 'system',\n content: system,\n },\n ...messages,\n ];\n }\n const options = {\n input: {\n messages,\n }\n };\n const url = `${BaseURL}${APIS.chat}`;\n const payload = {\n model,\n input: options.input,\n parameters: {\n incremental_output: true\n }\n };\n const abort = new AbortController();\n const key = this.key;\n try {\n await fetchEventData(url, {\n method: 'POST',\n headers: {\n 'Authorization': `Bearer ${key}`,\n 'Content-Type': 'application/json'\n },\n data: payload,\n signal: abort.signal,\n onMessage: (eventData) => {\n const data = eventData?.data;\n try {\n const result = JSON.parse(data || '{}');\n const msg = result.output?.text ?? '';\n onMessage(msg, false);\n } catch (error) {\n console.error('Aliyun onMessage Error: ', error);\n }\n },\n onClose: () => {\n onMessage(null, false);\n }\n });\n } catch (err) {\n console.error(err);\n abort.abort();\n }\n }\n}\n\nexport const qihu = new QihuChat();\n"}}}}