Skip to content

Commit ba6ebd6

Browse files
committed
feat: add gitee ai model support, use the serverless api
1 parent 76283cd commit ba6ebd6

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

src/ai/AiModelManager.ts

+4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {WenXinAiModel} from "./wenxin/WenXinAiModel.ts";
55
import {CustomAiModel} from "./custom/CustomAiModel.ts";
66
import {OpenaiAiModel} from "./openai/OpenaiAiModel.ts";
77
import {InnerEditor} from "../core/AiEditor.ts";
8+
import {GiteeAiModel} from "./gitee/GiteeAiModel.ts";
89

910
export class AiModelManager {
1011

@@ -23,6 +24,9 @@ export class AiModelManager {
2324
case "openai":
2425
this.set(key, new OpenaiAiModel(editor, globalConfig))
2526
break;
27+
case "gitee":
28+
this.set(key, new GiteeAiModel(editor, globalConfig))
29+
break;
2630
case "custom":
2731
this.set(key, new CustomAiModel(editor, globalConfig))
2832
break;

src/ai/gitee/GiteeAiModel.ts

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import {AiClient} from "../core/AiClient.ts";
2+
import {AiMessageListener} from "../core/AiMessageListener.ts";
3+
import {AiModel} from "../core/AiModel.ts";
4+
import {AiGlobalConfig} from "../AiGlobalConfig.ts";
5+
import {SseClient} from "../core/client/sse/SseClient.ts";
6+
import {InnerEditor} from "../../core/AiEditor.ts";
7+
import {GiteeModelConfig} from "./GiteeModelConfig.ts";
8+
9+
10+
export class GiteeAiModel extends AiModel {
11+
12+
constructor(editor: InnerEditor, globalConfig: AiGlobalConfig) {
13+
super(editor, globalConfig, "gitee");
14+
this.aiModelConfig = {
15+
max_tokens: 512,
16+
temperature: 0.7,
17+
top_p: 0.7,
18+
top_k: 50,
19+
...globalConfig.models["gitee"]
20+
} as GiteeModelConfig;
21+
}
22+
23+
createAiClient(url: string, listener: AiMessageListener): AiClient {
24+
const config = this.aiModelConfig as GiteeModelConfig;
25+
return new SseClient({
26+
url,
27+
method: "post",
28+
headers: {
29+
"Content-Type": "application/json",
30+
"Authorization": `Bearer ${config.apiKey}`,
31+
}
32+
}, {
33+
onStart: listener.onStart,
34+
onStop: listener.onStop,
35+
onMessage: (bodyString: string) => {
36+
console.log("onMessage: ",bodyString)
37+
38+
const message = JSON.parse(bodyString) as any;
39+
40+
listener.onMessage({
41+
status: message.choices[0].finish_reason === "stop" ? 2 : 1,
42+
role: "assistant",
43+
content: message.choices[0].delta?.content || "",
44+
index: message.choices[0].index,
45+
})
46+
//通知 ai 消费情况
47+
if (this.globalConfig.onTokenConsume && message.usage?.["total_tokens"]) {
48+
this.globalConfig.onTokenConsume(this.aiModelName, this.aiModelConfig!, message.usage["total_tokens"])
49+
}
50+
}
51+
});
52+
}
53+
54+
wrapPayload(prompt: string) {
55+
const config = this.aiModelConfig as GiteeModelConfig;
56+
const payload = {
57+
"messages": [
58+
{
59+
"role": "user",
60+
"content": prompt,
61+
}
62+
],
63+
stream: true,
64+
max_tokens: config.max_tokens,
65+
temperature: config.temperature,
66+
top_p: config.top_p,
67+
top_k: config.top_k,
68+
}
69+
return JSON.stringify(payload);
70+
}
71+
72+
createAiClientUrl(): string {
73+
const {endpoint} = this.aiModelConfig as GiteeModelConfig;
74+
return endpoint;
75+
}
76+
77+
78+
}

src/ai/gitee/GiteeModelConfig.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {AiModelConfig} from "../core/AiModelConfig.ts";
2+
3+
export interface GiteeModelConfig extends AiModelConfig {
4+
endpoint: string,
5+
apiKey: string,
6+
max_tokens: number,
7+
temperature: number,
8+
top_p: number,
9+
top_k: number
10+
}

src/main.ts

+4
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ window.aiEditor = new AiEditor({
5151
// endpoint: "https://api.moonshot.cn",
5252
// model: "moonshot-v1-8k",
5353
// apiKey: "sk-alQ96zb******"
54+
// },
55+
// gitee:{
56+
// endpoint:"https://ai.gitee.com/api/inference/serverless/KGHCVOPBV7GY/chat/completions",
57+
// apiKey:"***",
5458
// }
5559
},
5660
// bubblePanelEnable:false,

0 commit comments

Comments
 (0)