|
| 1 | +/** |
| 2 | + * @fileoverview 火山方舟平台的HTTP调用。 |
| 3 | + * 接口采用SSE请求方式,直接调会跨域,需要配代理,部署在服务器上之后也需要配置代理,详情可以参考本项目的文档 |
| 4 | + */ |
| 5 | +import { fetchEventSource } from "@microsoft/fetch-event-source"; |
| 6 | +import { preProcess } from "@/util/config" |
| 7 | +import store from '../../store'; |
| 8 | + |
| 9 | +// 火山方舟平台的接口地址 在项目内部设置了跨域 所以拼接了字符串"/ali/remote" 对应项目里面的代理配置 vue.config.js |
| 10 | +const API_URLS = { |
| 11 | + llm: "/tt/remote/api/v3/chat/completions", |
| 12 | + vim: "", |
| 13 | + igm: "" // 预留阿里平台的绘图模型 |
| 14 | +}; |
| 15 | + |
| 16 | +/** |
| 17 | + * 调用火山方舟平台的接口 |
| 18 | + * @param {string} prompt - 用户输入的问题 |
| 19 | + * @param {Array} history - 历史对话消息 |
| 20 | + * @param {Array} files - 文件列表 图片 |
| 21 | + * @param {AbortController} controller - 控制请求的取消 |
| 22 | + * @param {Function} onopen - 连接成功回调 |
| 23 | + * @param {Function} onmessage - 接收消息回调 |
| 24 | + * @param {Function} onclose - 连接关闭回调 |
| 25 | + * @param {Function} onerror - 错误处理回调 |
| 26 | + */ |
| 27 | +export async function fetchAPI({ |
| 28 | + prompt, |
| 29 | + history, |
| 30 | + files, |
| 31 | + controller, |
| 32 | + onopen, |
| 33 | + onmessage, |
| 34 | + onclose, |
| 35 | + onerror |
| 36 | +}) { |
| 37 | + const { setting } = store.state; |
| 38 | + const { model_config, web_search_enabled } = setting; |
| 39 | + const { version, pre_method, type, can_web_search } = model_config; |
| 40 | + const api_key = setting.api_key_map[setting.platform]; |
| 41 | + |
| 42 | + const url = API_URLS[type] || API_URLS.llm; |
| 43 | + const is_search = (can_web_search !== undefined && can_web_search) && web_search_enabled; |
| 44 | + |
| 45 | + const requestConfig = { |
| 46 | + method: "POST", |
| 47 | + headers: { |
| 48 | + "Authorization": `Bearer ${api_key}`, |
| 49 | + 'Content-Type': 'application/json', |
| 50 | + 'Accept': 'text/event-stream', |
| 51 | + 'X-DashScope-SSE': 'enable' |
| 52 | + }, |
| 53 | + body: JSON.stringify(preProcess(version, prompt, history, pre_method, files, is_search)), |
| 54 | + signal: controller.signal, |
| 55 | + onopen, |
| 56 | + onmessage, |
| 57 | + onclose, |
| 58 | + onerror, |
| 59 | + openWhenHidden: true |
| 60 | + }; |
| 61 | + |
| 62 | + return await fetchEventSource(url, requestConfig); |
| 63 | +} |
| 64 | + |
0 commit comments