|
@@ -12,6 +12,14 @@ class AIService {
|
|
|
this.apiKey = CONFIG.AI_API.DEFAULT_API_KEY;
|
|
|
this.controller = null; // 用于中断请求的 AbortController
|
|
|
this.currentPageInfo = null; // 保存当前页面信息
|
|
|
+ this.openai = new OpenAI(
|
|
|
+ {
|
|
|
+ // 若没有配置环境变量,请用百炼API Key将下行替换为:apiKey: "sk-xxx",
|
|
|
+ apiKey: 'sk-e9855234f47346049809ce23ed3ebe3f',
|
|
|
+ baseURL: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
|
|
+ dangerouslyAllowBrowser: true,
|
|
|
+ }
|
|
|
+ );
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -48,34 +56,65 @@ class AIService {
|
|
|
try {
|
|
|
// 创建新的 AbortController
|
|
|
this.controller = new AbortController();
|
|
|
+ const a = +new Date()
|
|
|
|
|
|
- const response = await fetch(this.apiEndpoint, {
|
|
|
- method: "POST",
|
|
|
- headers: {
|
|
|
- "Content-Type": "application/json",
|
|
|
- Authorization: `Bearer ${this.apiKey}`,
|
|
|
- },
|
|
|
- body: JSON.stringify({
|
|
|
- model: this.model,
|
|
|
- messages: this.formatMessages(message),
|
|
|
- max_tokens: CONFIG.AI_API.MAX_TOKENS,
|
|
|
- temperature: CONFIG.AI_API.TEMPERATURE,
|
|
|
- }),
|
|
|
- signal: this.controller.signal,
|
|
|
+ const response = await this.openai.chat.completions.create({
|
|
|
+ model: "qwen-plus", //模型列表:https://help.aliyun.com/zh/model-studio/getting-started/models
|
|
|
+ messages: this.formatMessages(message),
|
|
|
+ stream:true
|
|
|
});
|
|
|
-
|
|
|
- if (!response.ok) {
|
|
|
- throw new Error(`API request failed: ${response.status}`);
|
|
|
- }
|
|
|
-
|
|
|
- const data = await response.json();
|
|
|
- const aiResponse = data.choices[0]?.message?.content;
|
|
|
-
|
|
|
- if (!aiResponse) {
|
|
|
- throw new Error("无效的API响应");
|
|
|
- }
|
|
|
-
|
|
|
- return aiResponse;
|
|
|
+ // console.log(completion);
|
|
|
+ const signal = response.controller.signal;
|
|
|
+ let text = ''
|
|
|
+ try {
|
|
|
+ const iterator = response.iterator();
|
|
|
+ for await (const chunk of iterator) {
|
|
|
+ if (chunk) {
|
|
|
+ console.log(chunk);
|
|
|
+
|
|
|
+ const decodedChunk = chunk.choices[0].delta.content;
|
|
|
+ if (decodedChunk) {
|
|
|
+ text += decodedChunk;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log(text);
|
|
|
+
|
|
|
+ } catch (error) {
|
|
|
+ if (signal.aborted) {
|
|
|
+ console.log("Stream reading aborted");
|
|
|
+ } else {
|
|
|
+ console.error("Error reading stream:", error);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ console.log((+new Date() - a) / 1000)
|
|
|
+ // console.log(completion.choices[0].message.content);
|
|
|
+
|
|
|
+ // const response = await fetch(this.apiEndpoint, {
|
|
|
+ // method: "POST",
|
|
|
+ // headers: {
|
|
|
+ // "Content-Type": "application/json",
|
|
|
+ // Authorization: `Bearer ${this.apiKey}`,
|
|
|
+ // },
|
|
|
+ // body: JSON.stringify({
|
|
|
+ // model: this.model,
|
|
|
+ // messages: this.formatMessages(message),
|
|
|
+ // }),
|
|
|
+ // signal: this.controller.signal,
|
|
|
+ // });
|
|
|
+
|
|
|
+ // if (!response.ok) {
|
|
|
+ // throw new Error(`API request failed: ${response.status}`);
|
|
|
+ // }
|
|
|
+
|
|
|
+ // const data = await response.json();
|
|
|
+ // const aiResponse = data.choices[0]?.message?.content;
|
|
|
+ // const aiResponse = completion.choices[0].message.content
|
|
|
+ // if (!aiResponse) {
|
|
|
+ // throw new Error("无效的API响应");
|
|
|
+ // }
|
|
|
+
|
|
|
+ // return aiResponse;
|
|
|
} catch (error) {
|
|
|
if (error.name === "AbortError") {
|
|
|
throw new Error("REQUEST_ABORTED");
|