Skip to content

Commit

Permalink
增加moonshot支持
Browse files Browse the repository at this point in the history
  • Loading branch information
unclemcz committed Apr 18, 2024
1 parent f637d43 commit a432032
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,12 @@ npm run make
2. 确认ollama正常运行。
3. 将url填入配置中。url默认为`http://127.0.0.1:11434/`,模型可参考ollama的网站,例如`qwen``qwen:7b`等。

### moonshot大模型

介绍:https://platform.moonshot.cn/docs/intro
1. 注册开通:https://platform.moonshot.cn/console/info
2. 新建秘钥 https://platform.moonshot.cn/console/api-keys
3. 将步骤2中生成的key填入配置中。

## 还未支持的列表

Expand Down
12 changes: 12 additions & 0 deletions html/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,18 @@
<input type="password" class="form-control" id="huawei-key" placeholder="key">
</div>

<div class="col-12">
<nav class="navbar bg-body-tertiary">
<div class="">
<img src="../lib/img/kimi.png" width="24" height="20">
<label id="kimi-name">Kimi大模型</label>
</div>
</nav>
</div>
<div class="col-12">
<input type="password" class="form-control" id="kimi-key" placeholder="key">
</div>

<div class="col-12">
<nav class="navbar bg-body-tertiary">
<div class="">
Expand Down
5 changes: 5 additions & 0 deletions lib/engine/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ let cfg ={
"appid":"",
"key":""
},
"kimi":{
"name":"Kimi大模型",
"model":"",
"key":""
},
"ollama":{
"name":"ollama大模型工具",
"url":"",
Expand Down
108 changes: 108 additions & 0 deletions lib/engine/kimi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
const axios = require("axios");

const apiurl = 'https://api.moonshot.cn/';



async function translate(query,engine,win,controller) {
//console.log(engine);
let model = engine.model;
const key = engine.key;
const name = engine.name;
let result = {};
if (key == '' || key == null || key == undefined) {
result.origintext = 'kimi的key未配置';
result.resulttext = '请配置kimi的key。';
win.webContents.send('update-text', result);
return;
}

query = query.replace(/[\r\n]/g, ' ');


//加一个提醒让用户等待
win.webContents.send('update-text', {"origintext":query,"resulttext":"等待大模型["+name+"]翻译中......","done":false});
const reqdata = {
model: model,
messages: [
{ role: "system", content: "你是 Kimi,由 Moonshot AI 提供的人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。Moonshot AI 为专有名词,不可翻译成其他语言。" },
{ role: "user", content: "你也是一位专业的翻译老师,请将给定的这段文本翻译成简体中文。###"+query+"###", }
],
temperature: 0.3,
stream:true
};

result.origintext = query;
result.resulttext = '';

axios.post(apiurl+'v1/chat/completions', reqdata, {responseType: 'stream',signal: controller.signal,
headers: {
'Content-Type': 'application/json',
'Authorization': 'Bearer ' + key
}
})
.then(response => {
response.data.on('data', chunk => {
chunk.toString().split('\n').forEach(line => {
if (line.length > 0) {
line = line.slice(5);
if (!line.endsWith("[DONE]")) {
let data = JSON.parse(line);
if (data.choices[0].delta.content) {
result.resulttext += data.choices[0].delta.content;
}
}
}
})
result.done = false;
console.log(result)
win.webContents.send('update-text', result);
});
response.data.on('end', () => {
// 完成解析
result.done = true;
result.resulttext += "["+name + ",模型:" + model+"]";
console.log(result)
console.log('Stream ended');
win.webContents.send('update-text', result);
});
})
.catch(error => {
//console.error('Error during request:', JSON.stringify(error));
result = {"origintext":query,"resulttext":error.message,"done":true};
win.webContents.send('update-text', result);
console.log(JSON.stringify(error));
});
}


async function modellist(engine) {
const key = engine.key;
let curmodel = engine.model;
let result = [];

try {
let mlist = await axios.get(apiurl+'v1/models',{
headers: {
'Authorization': 'Bearer ' + key
}
});
mlist = mlist.data.data;
console.log(mlist);
mlist.forEach(m => {
if (m.id == curmodel) {
result.unshift(m.id);
} else {
result.push(m.id);
}
});
} catch (error) {
console.error(error);
//result = error;
}
return result;
}


exports.translate = translate;
exports.modellist = modellist;
6 changes: 6 additions & 0 deletions lib/engine/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const alibase = require('./alibase.js');
const volc = require('./volc.js');
const huawei = require('./huawei.js')
const ollama = require('./ollama.js');
const kimi = require('./kimi.js');


async function translate(text,type,engine,win,controller) {
Expand All @@ -30,6 +31,9 @@ async function translate(text,type,engine,win,controller) {
}else if(type=="ollama"){
result = {"origintext":text,"resulttext":"model-api-stream"};
await ollama.translate(text,engine,win,controller);
}else if(type=="kimi"){
result = {"origintext":text,"resulttext":"model-api-stream"};
await kimi.translate(text,engine,win,controller);
}else{
result = {"origintext":text,"resulttext":"translate.js无["+type+"]判断逻辑,请先增加判断分支。"};
win.webContents.send('update-text', result);
Expand All @@ -42,6 +46,8 @@ async function modellist(type,engine,win) {
let result = {};
if (type=="ollama") {
result = await ollama.modellist(engine);
}else if (type=="kimi") {
result = await kimi.modellist(engine);
} else{
result = [];
}
Expand Down
Binary file added lib/img/kimi.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "wodict",
"version": "0.2.2",
"version": "0.2.4",
"description": "A translator based on Electron",
"main": "main.js",
"scripts": {
Expand Down
7 changes: 7 additions & 0 deletions rendererjs/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ const ollamaurl = document.getElementById('ollama-url');
//const ollamamodel = document.getElementById('ollama-model');
const ollamaname = document.getElementById('ollama-name');

const kimikey = document.getElementById('kimi-key');
const kiminame = document.getElementById('kimi-name');


window.electronAPI.onEngineList((value) => {
for (const key in value) {
Expand Down Expand Up @@ -63,6 +66,9 @@ window.electronAPI.onEngineList((value) => {
ollamaurl.value = value[key].url;
//ollamamodel.value = value[key].model;
break;
case "kimi":
kimikey.value = value[key].key;
break;
default:
break;
}
Expand All @@ -81,6 +87,7 @@ btnsavecfg.addEventListener('click', async () => {
cfg.volc = {"name":volcname.innerText,"appid":volcid.value,"key":volckey.value};
cfg.huawei = {"name":huaweiname.innerText,"appid":huaweiid.value,"key":huaweikey.value};
cfg.ollama = {"name":ollamaname.innerText,"url":ollamaurl.value};
cfg.kimi = {"name":kiminame.innerText,"key":kimikey.value};
//console.log(cfg)
window.electronAPI.onSaveCfg(cfg);
window.close();
Expand Down
25 changes: 25 additions & 0 deletions test/kimi.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

const kimi = require("../lib/engine/kimi.js");
const parse = require('json-stream');

const engine = {
"name":"Kimi大模型",
"key":"sk-Zw129EciC1EbggVxge4iJsfhNGYRBqQpG9H3ZYDWNEsjeJYv",
"model":"moonshot-v1-8k"
};




(async()=>{
await kimi.translate("Coppola grew up watching Francis do battle with movie studios. The success of the “Godfather” films hardly assured him funding equal to his ambitions, and he often went to harrowing lengths to get his projects made independently, driving himself to the brink of bankruptcy or nervous breakdown. ",engine,"win","controller");
})();




// (async()=>{
// const eng = await kimi.modellist(engine);
// console.log(eng);
// })();

0 comments on commit a432032

Please sign in to comment.