Skip to content

Commit

Permalink
feat: Update Gateway & Official Api
Browse files Browse the repository at this point in the history
  • Loading branch information
WhyMeta committed Apr 22, 2024
1 parent 5c43ccb commit 0d277e4
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
22 changes: 5 additions & 17 deletions src/info.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"identifier": "whymeta.chatglm.translator",
"version": "0.2.0",
"version": "0.3.0",
"category": "translate",
"name": "ChatGLM Translator",
"summary": "GLM-4 powered translator",
Expand All @@ -15,7 +15,7 @@
"type": "text",
"title": "API URL",
"defaultValue": "https://open.bigmodel.cn/api/paas/v4/chat/completions",
"desc": "可选项。如果您的网络环境需要代理才能访问 OpenAI API, 可在这里修改为反代 API 的地址",
"desc": "可选项。如果您的网络环境需要代理才能访问 ZhipuAI ChatGLM API, 可在这里修改为反代 API 的地址",
"textConfig": {
"type": "visible",
"placeholderText": "https://open.bigmodel.cn/api/paas/v4/chat/completions"
Expand Down Expand Up @@ -47,20 +47,8 @@
"value": "glm-3-turbo"
},
{
"title": "glm-3-turbo",
"value": "glm-3-turbo"
},
{
"title": "cogview-3",
"value": "cogview-3"
},
{
"title": "embedding-2",
"value": "embedding-2"
},
{
"title": "glm-4v",
"value": "glm-4v"
"title": "glm-4",
"value": "glm-4"
}
]
},
Expand All @@ -71,7 +59,7 @@
"desc": "可选项。当 Model 选择为 custom 时,此项为必填项。请填写有效的模型名称",
"textConfig": {
"type": "visible",
"placeholderText": "glm-4"
"placeholderText": "659fb5d6e94483d496d62a9b"
}
},
{
Expand Down
57 changes: 39 additions & 18 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ var {


/**
* @param {Bob.TranslateQuery} query
* @param {Bob.TranslateQuery} query 查询参数
* @returns {{
* generatedSystemPrompt: string,
* generatedUserPrompt: string
* }}
* }} 返回包含系统提示语和用户提示语的对象
*/
function generatePrompts(query) {
let generatedSystemPrompt = SYSTEM_PROMPT;
Expand Down Expand Up @@ -60,16 +60,16 @@ function generatePrompts(query) {
}

/**
* @param {string} model
* @param {Bob.TranslateQuery} query
* @param {string} model 模型名称
* @param {Bob.TranslateQuery} query 查询参数
* @returns {{
* model: string;
* messages?: {
* role: "system" | "user";
* content: string;
* }[];
* prompt?: string;
* }}
* }} 返回构建好的请求体
*/
function buildRequestBody(model, query) {
let { customSystemPrompt, customUserPrompt } = $option;
Expand Down Expand Up @@ -101,11 +101,11 @@ function buildRequestBody(model, query) {
};
}

/**
* @param {Bob.TranslateQuery} query
* @param {string} targetText
* @param {string} textFromResponse
* @returns {string}
/**
* @param {Bob.TranslateQuery} query 查询对象
* @param {string} targetText 目标文本
* @param {string} textFromResponse 响应文本
* @returns {string} 返回处理后的文本
*/
function handleStreamResponse(query, targetText, textFromResponse) {
if (textFromResponse !== '[DONE]') {
Expand Down Expand Up @@ -138,6 +138,9 @@ function handleStreamResponse(query, targetText, textFromResponse) {
* @param {Bob.TranslateQuery} query
* @param {Bob.HttpResponse} result
* @returns {void}
* 处理通用响应
* @param query 查询对象
* @param result 查询结果
*/
function handleGeneralResponse(query, result) {
const { choices } = result.data;
Expand Down Expand Up @@ -172,6 +175,8 @@ function handleGeneralResponse(query, result) {

/**
* @type {Bob.Translate}
* 翻译函数
* @param query 翻译请求对象
*/
function translate(query) {
if (!lang.langMap.get(query.detectTo)) {
Expand Down Expand Up @@ -212,9 +217,14 @@ function translate(query) {
const apiKey = getApiKey($option.apiKeys);

const baseUrl = ensureHttpsAndNoTrailingSlash(apiUrl || "https://open.bigmodel.cn");
let apiUrlPath = baseUrl.includes("gateway.ai.cloudflare.com") ? "/api/paas/v4/chat/completions" : "/v1/chat/completions";
let apiUrlPath;
if (baseUrl.includes("open.bigmodel.cn") || baseUrl.includes("gateway.ai.cloudflare.com")) {
apiUrlPath = "/api/paas/v4/chat/completions";
} else {
apiUrlPath = "/v1/chat/completions";
}

const isChatGLMServiceProvider = baseUrl.includes("open.bigmodel.cn");
const isChatGLMServiceProvider = baseUrl.includes("open.bigmodel.cn") || baseUrl.includes("gateway.ai.cloudflare.com");

const header = buildHeader(isChatGLMServiceProvider, apiKey);
const body = buildRequestBody(modelValue, query);
Expand Down Expand Up @@ -291,16 +301,23 @@ function translate(query) {
});
}

/**
* 获取支持的语言列表
*
* @returns 返回一个包含支持语言名称的数组
*/
function supportLanguages() {
return lang.supportLanguages.map(([standardLang]) => standardLang);
}


/**
* @type {Bob.PluginValidate}
* 插件验证函数
* 验证完成后的回调函数
*/
function pluginValidate(completion) {
const { apiKeys, apiUrl, apiVersion, deploymentName } = $option;
const { apiKeys, apiUrl } = $option;
if (!apiKeys) {
handleValidateError(completion, {
type: "secretKey",
Expand All @@ -314,14 +331,18 @@ function pluginValidate(completion) {
const apiKey = getApiKey(apiKeys);

const baseUrl = ensureHttpsAndNoTrailingSlash(apiUrl || "https://open.bigmodel.cn");
let apiUrlPath = baseUrl.includes("gateway.ai.cloudflare.com") ? "/api/paas/v4/chat/completions" : "/v1/chat/completions";

const isChatGLMServiceProvider = baseUrl.includes("open.bigmodel.cn");
let apiUrlPath;
if (baseUrl.includes("open.bigmodel.cn") || baseUrl.includes("gateway.ai.cloudflare.com")) {
apiUrlPath = "/api/paas/v4/chat/completions";
} else {
apiUrlPath = "/v1/chat/completions";
}
const isChatGLMServiceProvider = baseUrl.includes("open.bigmodel.cn") || baseUrl.includes("gateway.ai.cloudflare.com");

const header = buildHeader(isChatGLMServiceProvider, apiKey);
const body = {
"stream": true,
"model": "glm",
"model": "glm-4",
"messages": [{
"content": "You are a helpful assistant.",
"role": "system",
Expand All @@ -338,7 +359,7 @@ function pluginValidate(completion) {
header: header,
body: body,
handler: function (resp) {
$log.info("========: ");
// $log.info("========: ");
if (resp.data.error) {
const { statusCode } = resp.response;
const reason = (statusCode >= 400 && statusCode < 500) ? "param" : "api";
Expand Down
3 changes: 1 addition & 2 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ var HttpErrorCodes = require("./const.js").HttpErrorCodes;
* @param {string} apiKey - The authentication API key.
* @returns {{
* "Content-Type": string;
* "api-key"?: string;
* "Authorization"?: string;
* }} The header object.
*/
function buildHeader(isChatGLMServiceProvider, apiKey) {
return {
"Content-Type": "application/json",
[isChatGLMServiceProvider ? "api-key" : "Authorization"]: isChatGLMServiceProvider ? apiKey : `Bearer ${apiKey}`
"Authorization": isChatGLMServiceProvider ? apiKey : `Bearer ${apiKey}`
};
}

Expand Down

0 comments on commit 0d277e4

Please sign in to comment.