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+ // 移动云平台的接口地址
10+ const API_URLS = {
11+ llm : "/yidong/remote/inference-api/exp-api/inf-1337014908971847680/v1/chat/completions" , // 暂时需要手动修改此时 未来会重构API KEY管理模块
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 } = setting ;
39+ const { version, pre_method, type } = model_config ;
40+ const api_key = setting . api_key_map [ setting . platform ] ;
41+
42+ const url = API_URLS [ type ] || API_URLS . llm ;
43+
44+ const requestConfig = {
45+ method : "POST" ,
46+ headers : {
47+ "Authorization" : `Bearer ${ api_key } ` ,
48+ 'Content-Type' : 'application/json' ,
49+ 'Accept' : 'text/event-stream'
50+ } ,
51+ body : JSON . stringify ( preProcess ( version , prompt , history , pre_method , files , false ) ) ,
52+ signal : controller . signal ,
53+ onopen,
54+ onmessage,
55+ onclose,
56+ onerror,
57+ openWhenHidden : true
58+ } ;
59+
60+ return await fetchEventSource ( url , requestConfig ) ;
61+ }
0 commit comments