Skip to content

Commit

Permalink
Merge pull request #32 from workfunction/main
Browse files Browse the repository at this point in the history
  • Loading branch information
liby authored Nov 28, 2023
2 parents dbf9d14 + 85ea7af commit 3f4666c
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 60 deletions.
8 changes: 1 addition & 7 deletions global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,7 @@ declare namespace Bob {

// https://ripperhe.gitee.io/bob/#/plugin/api/option
type Option = {
apiKeys: string;
apiUrl: string;
customSystemPrompt: string;
customUserPrompt: string;
deploymentName: string;
model: string;
polishingMode: "simplicity" | "detailed";
[propName: string]: string;
};


Expand Down
35 changes: 26 additions & 9 deletions src/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,22 @@
"identifier": "deploymentName",
"type": "text",
"title": "Dep. Name",
"desc": "可选项。此值为在部署模型时为部署选择的自定义名称,可在 Azure 门户中的 “资源管理>“部署下查看",
"desc": "可选项。此值为在部署 Azure 模型时为部署选择的自定义名称,可在 Azure 门户中的 “资源管理>“部署下查看",
"textConfig": {
"type": "visible"
}
},
{
"identifier": "apiVersion",
"type": "text",
"title": "API Version",
"defaultValue": "2023-03-15-preview",
"desc": "可选项。此值为在使用 Azure 模型时采用的 Chat completions API 版本,不支持 2023-03-15-preview 之前的版本",
"textConfig": {
"type": "visible",
"placeholderText": "2023-03-15-preview"
}
},
{
"identifier": "apiKeys",
"type": "text",
Expand All @@ -47,6 +58,10 @@
"title": "模型",
"defaultValue": "gpt-3.5-turbo-0613",
"menuValues": [
{
"title": "custom",
"value": "custom"
},
{
"title": "gpt-3.5-turbo-0613 (recommended)",
"value": "gpt-3.5-turbo-0613"
Expand Down Expand Up @@ -86,17 +101,19 @@
{
"title": "gpt-4-32k-0613",
"value": "gpt-4-32k-0613"
},
{
"title": "text-davinci-003",
"value": "text-davinci-003"
},
{
"title": "text-davinci-002",
"value": "text-davinci-002"
}
]
},
{
"identifier": "customModel",
"type": "text",
"title": "自定义模型",
"desc": "可选项。当 Model 选择为 custom 时,此项为必填项。请填写有效的模型名称",
"textConfig": {
"type": "visible",
"placeholderText": "gpt-3.5-turbo"
}
},
{
"identifier": "customSystemPrompt",
"type": "text",
Expand Down
82 changes: 38 additions & 44 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,6 @@
//@ts-check

var lang = require("./lang.js");
var ChatGPTModels = [
"gpt-3.5-turbo",
"gpt-3.5-turbo-16k",
"gpt-3.5-turbo-0301",
"gpt-3.5-turbo-0613",
"gpt-4",
"gpt-4-0314",
"gpt-4-0613",
"gpt-4-32k",
"gpt-4-32k-0314",
"gpt-4-32k-0613",
];
var HttpErrorCodes = {
"400": "Bad Request",
"401": "Unauthorized",
Expand Down Expand Up @@ -85,7 +73,7 @@ function buildHeader(isAzureServiceProvider, apiKey) {

/**
* @param {string} basePrompt
* @param {"simplicity" | "detailed"} polishingMode
* @param {string} polishingMode
* @param {Bob.TranslateQuery} query
* @returns {string}
*/
Expand Down Expand Up @@ -150,11 +138,10 @@ function replacePromptKeywords(prompt, query) {
}

/**
* @param {typeof ChatGPTModels[number]} model
* @param {boolean} isChatGPTModel
* @param {string} model
* @param {Bob.TranslateQuery} query
* @returns {{
* model: typeof ChatGPTModels[number];
* model: string;
* temperature: number;
* max_tokens: number;
* top_p: number;
Expand All @@ -167,14 +154,14 @@ function replacePromptKeywords(prompt, query) {
* prompt?: string;
* }}
*/
function buildRequestBody(model, isChatGPTModel, query) {
function buildRequestBody(model, query) {
const { customSystemPrompt, customUserPrompt, polishingMode } = $option;

const systemPrompt = generateSystemPrompt(replacePromptKeywords(customSystemPrompt, query), polishingMode, query);
const userPrompt = customUserPrompt ? `${replacePromptKeywords(customUserPrompt, query)}:\n\n"${query.text}"` : query.text;

const standardBody = {
model,
model: model,
stream: true,
temperature: 0.2,
max_tokens: 1000,
Expand All @@ -183,24 +170,19 @@ function buildRequestBody(model, isChatGPTModel, query) {
presence_penalty: 1,
};

if (isChatGPTModel) {
return {
...standardBody,
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: userPrompt,
},
],
};
}
return {
...standardBody,
prompt: `${systemPrompt}\n\n${userPrompt}`,
model: model,
messages: [
{
role: "system",
content: systemPrompt,
},
{
role: "user",
content: userPrompt,
},
],
};
}

Expand All @@ -223,12 +205,11 @@ function handleError(query, result) {

/**
* @param {Bob.TranslateQuery} query
* @param {boolean} isChatGPTModel
* @param {string} targetText
* @param {string} textFromResponse
* @returns {string}
*/
function handleResponse(query, isChatGPTModel, targetText, textFromResponse) {
function handleResponse(query, targetText, textFromResponse) {
if (textFromResponse !== '[DONE]') {
try {
const dataObj = JSON.parse(textFromResponse);
Expand All @@ -244,7 +225,7 @@ function handleResponse(query, isChatGPTModel, targetText, textFromResponse) {
return targetText;
}

const content = isChatGPTModel ? choices[0].delta.content : choices[0].text;
const content = choices[0].delta.content;
if (content !== undefined) {
targetText += content;
query.onStream({
Expand Down Expand Up @@ -282,7 +263,18 @@ function translate(query, completion) {
});
}

const { model, apiKeys, apiUrl, deploymentName } = $option;
const { model, customModel, apiKeys, apiVersion, apiUrl, deploymentName } = $option;

const isCustomModelRequired = model === "custom";
if (isCustomModelRequired && !customModel) {
query.onCompletion({
error: {
type: "param",
message: "配置错误 - 请确保您在插件配置中填入了正确的自定义模型名称",
addtion: "请在插件配置中填写自定义模型名称",
},
});
}

if (!apiKeys) {
completion({
Expand All @@ -293,20 +285,22 @@ function translate(query, completion) {
},
});
}

const modelValue = isCustomModelRequired ? customModel : model;

const trimmedApiKeys = apiKeys.endsWith(",") ? apiKeys.slice(0, -1) : apiKeys;
const apiKeySelection = trimmedApiKeys.split(",").map(key => key.trim());
const apiKey = apiKeySelection[Math.floor(Math.random() * apiKeySelection.length)];

const modifiedApiUrl = ensureHttpsAndNoTrailingSlash(apiUrl || "https://api.openai.com");

const isChatGPTModel = ChatGPTModels.includes(model);
const isAzureServiceProvider = modifiedApiUrl.includes("openai.azure.com");
let apiUrlPath = isChatGPTModel ? "/v1/chat/completions" : "/v1/completions";
let apiUrlPath = "/v1/chat/completions";
const apiVersionQuery = apiVersion ? `?api-version=${apiVersion}` : "?api-version=2023-03-15-preview";

if (isAzureServiceProvider) {
if (deploymentName) {
apiUrlPath = `/openai/deployments/${deploymentName}`;
apiUrlPath += isChatGPTModel ? "/chat/completions?api-version=2023-03-15-preview" : "/completions?api-version=2022-12-01";
apiUrlPath = `/openai/deployments/${deploymentName}/chat/completions${apiVersionQuery}`;
} else {
completion({
error: {
Expand All @@ -319,7 +313,7 @@ function translate(query, completion) {
}

const header = buildHeader(isAzureServiceProvider, apiKey);
const body = buildRequestBody(model, isChatGPTModel, query);
const body = buildRequestBody(modelValue, query);

let targetText = ""; // 初始化拼接结果变量
let buffer = ""; // 新增 buffer 变量
Expand Down Expand Up @@ -348,7 +342,7 @@ function translate(query, completion) {
if (match) {
// 如果是一个完整的消息,处理它并从缓冲变量中移除
const textFromResponse = match[1].trim();
targetText = handleResponse(query, isChatGPTModel, targetText, textFromResponse);
targetText = handleResponse(query, targetText, textFromResponse);
buffer = buffer.slice(match[0].length);
} else {
// 如果没有完整的消息,等待更多的数据
Expand Down

0 comments on commit 3f4666c

Please sign in to comment.