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
| import CryptoJS from "crypto-js"; import type { ChatHistory } from "./ai";
const URL = "wss://spark-api.xf-yun.com/v3.1/chat"; const APPID = ""; const API_SECRET = ""; const API_KEY = "";
export function callXinghuo({ text, history, onMessage, }: { text: string; history: ChatHistory; onMessage: (msg: string) => void; }) { return new Promise((resolve, reject) => { let fullMsg = ""; const webSocket = new WebSocket(getWebSocketUrl()); webSocket.onmessage = (e) => { try { const resultData = e.data; const jsonData = JSON.parse(resultData); if (jsonData.header.code === 0) { const msg = getContent(jsonData); console.log("Got message", msg); fullMsg += msg; onMessage(msg); if (jsonData.header.status === 2) { console.log("API response finished", fullMsg); webSocket.close(); resolve(fullMsg); } } else { const error = new Error( `${jsonData.header.code}:${jsonData.header.message}`, ); console.error("API error:", error); reject(error); } } catch (e) { console.error("Handle message exception:", e); reject(e); } }; webSocket.onerror = (e) => { console.error("WebSocket error:", e); reject(e); }; webSocket.onopen = () => { console.log("WebSocket open"); webSocket.send(getParams(text, history)); }; webSocket.onclose = () => { console.log("WebSocket close"); }; }); }
function getWebSocketUrl() { var host = location.host; var date = new Date().toUTCString(); var algorithm = "hmac-sha256"; var headers = "host date request-line"; var signatureOrigin = `host: ${host}\ndate: ${date}\nGET /v3.1/chat HTTP/1.1`; var signatureSha = CryptoJS.HmacSHA256(signatureOrigin, API_SECRET); var signature = CryptoJS.enc.Base64.stringify(signatureSha); var authorizationOrigin = `api_key="${API_KEY}", algorithm="${algorithm}", headers="${headers}", signature="${signature}"`; var authorization = btoa(authorizationOrigin); return `${URL}?authorization=${authorization}&date=${date}&host=${host}`; }
function getParams(message: string, history: ChatHistory) { return JSON.stringify({ header: { app_id: APPID, uid: "fd3f47e40d", }, parameter: { chat: { domain: "generalv3", temperature: 0.5, max_tokens: 1024, }, }, payload: { message: { text: getPayloadText(message, history), }, }, }); }
function getContent(jsonData: any) { let content = ""; try { if (jsonData.header.code === 0) { for (const choice of jsonData.payload.choices.text) { content += choice.content; } } } catch (e) { console.error("Get content error:", e); } return content; }
function getPayloadText(text: string, history: ChatHistory) { const array = []; for (let i = 0; i < history.length - 1; i++) { const chat = history[i]; array.push( { role: "user", content: chat.userMsg, }, { role: "assistant", content: chat.assistantMsg, }, ); } array.push({ role: "user", content: text, }); return array; }
|