Skip to content

Commit

Permalink
feat: 添加OpenAI润色、总结、代码解释功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Pylogmon committed Mar 10, 2023
1 parent c430946 commit 62cb361
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/interfaces/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import * as _baidu from './baidu'
import * as _caiyun from './caiyun'
import * as _youdao_free from './youdao_free'
import * as _openai from './openai'
import * as _openai_polish from './openai_polish'
import * as _openai_summary from './openai_summary'
import * as _openai_code from './openai_code'
import * as _tencent from './tencent'

export const baidu = _baidu
export const caiyun = _caiyun
export const youdao_free = _youdao_free
export const openai = _openai
export const openai_polish = _openai_polish
export const openai_summary = _openai_summary
export const openai_code = _openai_code
export const tencent = _tencent
2 changes: 1 addition & 1 deletion src/interfaces/openai.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { fetch } from '@tauri-apps/api/http';
import { get } from '../global/config';

export const info = {
name: "Open AI",
name: "Open AI 翻译",
supportLanguage: {
"zh-cn": "简体中文",
"zh-tw": "繁体中文",
Expand Down
64 changes: 64 additions & 0 deletions src/interfaces/openai_code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fetch } from '@tauri-apps/api/http';
import { get } from '../global/config';

export const info = {
name: "Open AI 代码解释",
supportLanguage: {
"zh-cn": "简体中文",
"zh-tw": "繁体中文",
"yue": '粤语',
"ja": "日本語",
"en": "英语",
"ko": "韩语",
"fr": "法语",
"es": "西班牙语",
"ru": "俄语",
"de": "德语",
},
needs: {}
}

export async function translate(text, from, to) {
const { supportLanguage } = info;
const domain = get('openai_domain', "api.openai.com");
const apikey = get('openai_apikey', "");
if (apikey == "") {
return "请先配置apikey"
}

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${apikey}`,
};

let systemPrompt = "You are a code explanation engine, you can only explain the code, do not interpret or translate it. Also, please report any bugs you find in the code to the author of the code.";
let userPrompt = `用${supportLanguage[to]}解释此段代码、正则表达式或脚本。如果内容不是代码,请返回错误提示。如果代码有明显的错误,请指出:\n\n${text}`

const body = {
model: "gpt-3.5-turbo",
temperature: 0,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
]
};

const res = await fetch(`https://${domain}/v1/chat/completions`, {
method: 'POST',
headers: headers,
body: {
type: 'Json',
payload: body
}
})

const { choices } = res.data;

let target = choices[0].message.content.trim()

return target
}
64 changes: 64 additions & 0 deletions src/interfaces/openai_polish.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fetch } from '@tauri-apps/api/http';
import { get } from '../global/config';

export const info = {
name: "Open AI 润色",
supportLanguage: {
"zh-cn": "简体中文",
"zh-tw": "繁体中文",
"yue": '粤语',
"ja": "日本語",
"en": "英语",
"ko": "韩语",
"fr": "法语",
"es": "西班牙语",
"ru": "俄语",
"de": "德语",
},
needs: {}
}

export async function translate(text, from, to) {
const { supportLanguage } = info;
const domain = get('openai_domain', "api.openai.com");
const apikey = get('openai_apikey', "");
if (apikey == "") {
return "请先配置apikey"
}

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${apikey}`,
};

let systemPrompt = "You are a text embellisher, you can only embellish the text, don't interpret it.";
let userPrompt = `用${supportLanguage[to]}润色此句:\n\n${text}`

const body = {
model: "gpt-3.5-turbo",
temperature: 0,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
]
};

const res = await fetch(`https://${domain}/v1/chat/completions`, {
method: 'POST',
headers: headers,
body: {
type: 'Json',
payload: body
}
})

const { choices } = res.data;

let target = choices[0].message.content.trim()

return target
}
64 changes: 64 additions & 0 deletions src/interfaces/openai_summary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { fetch } from '@tauri-apps/api/http';
import { get } from '../global/config';

export const info = {
name: "Open AI 总结",
supportLanguage: {
"zh-cn": "简体中文",
"zh-tw": "繁体中文",
"yue": '粤语',
"ja": "日本語",
"en": "英语",
"ko": "韩语",
"fr": "法语",
"es": "西班牙语",
"ru": "俄语",
"de": "德语",
},
needs: {}
}

export async function translate(text, from, to) {
const { supportLanguage } = info;
const domain = get('openai_domain', "api.openai.com");
const apikey = get('openai_apikey', "");
if (apikey == "") {
return "请先配置apikey"
}

const headers = {
"Content-Type": "application/json",
"Authorization": `Bearer ${apikey}`,
};

let systemPrompt = "You are a text summarizer, you can only summarize the text, don't interpret it.";
let userPrompt = `用${supportLanguage[to]}总结这段文本:\n\n${text}`

const body = {
model: "gpt-3.5-turbo",
temperature: 0,
max_tokens: 1000,
top_p: 1,
frequency_penalty: 1,
presence_penalty: 1,
messages: [
{ role: "system", content: systemPrompt },
{ role: "user", content: userPrompt },
]
};

const res = await fetch(`https://${domain}/v1/chat/completions`, {
method: 'POST',
headers: headers,
body: {
type: 'Json',
payload: body
}
})

const { choices } = res.data;

let target = choices[0].message.content.trim()

return target
}

0 comments on commit 62cb361

Please sign in to comment.