Skip to content

Commit

Permalink
feat: support claude3 and claude3-5 models and custom prompt
Browse files Browse the repository at this point in the history
  • Loading branch information
jtsang4 committed Aug 17, 2024
1 parent 1158d7b commit 54a8af2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 35 deletions.
68 changes: 34 additions & 34 deletions src/info.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,6 @@
"placeholderText": "sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
},
{
"identifier": "apiUrl",
"type": "text",
"title": "自定义 API Base URL",
"desc": "如果您的网络环境需要代理才能访问 Claude API, 可在这里修改为反代 API 的地址",
"textConfig": {
"type": "visible",
"placeholderText": "https://api.anthropic.com"
}
},
{
"identifier": "apiUrlPath",
"type": "text",
"title": "自定义 API URL Path",
"desc": "发送请求的Path",
"textConfig": {
"type": "visible",
"placeholderText": "/v1/messages"
}
},
{
"identifier": "request_mode",
"type": "menu",
"title": "请求方式",
"defaultValue": "stream",
"menuValues": [
{
"title": "流式请求",
"value": "stream"
}
]
},
{
"identifier": "model",
"type": "menu",
Expand Down Expand Up @@ -85,10 +53,10 @@
"identifier": "temperature",
"type": "text",
"title": "温度",
"desc": "可选项。温度值越高,生成的文本越随机。默认值为 0.2(0~2)。",
"desc": "可选项。温度值越高,生成的文本越随机。默认值为 0.7(0~2)。",
"textConfig": {
"type": "visible",
"placeholderText": "0.2"
"placeholderText": "0.7"
}
},
{
Expand Down Expand Up @@ -122,6 +90,38 @@
"$targetLang"
]
}
},
{
"identifier": "apiUrl",
"type": "text",
"title": "自定义 API Base URL",
"desc": "如果您的网络环境需要代理才能访问 Claude API, 可在这里修改为反代 API 的地址",
"textConfig": {
"type": "visible",
"placeholderText": "https://api.anthropic.com"
}
},
{
"identifier": "apiUrlPath",
"type": "text",
"title": "自定义 API URL Path",
"desc": "发送请求的Path",
"textConfig": {
"type": "visible",
"placeholderText": "/v1/messages"
}
},
{
"identifier": "request_mode",
"type": "menu",
"title": "请求方式",
"defaultValue": "stream",
"menuValues": [
{
"title": "流式请求",
"value": "stream"
}
]
}
]
}
26 changes: 25 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function buildRequestBody(model, query) {
model: model,
messages: [{role: 'user', content: userPrompt}],
system: systemPrompt,
temperature: Number($option.temperature ?? 0.2),
temperature: Number($option.temperature ?? 0.7),
max_tokens: 4096,
stream: true,
};
Expand All @@ -116,6 +116,9 @@ function handleError(query, result) {
const errorMessage =
result.data && result.data.detail ? result.data.detail : '接口响应错误';

// Enhanced error logging
$log.error(`Translation error: ${errorMessage}. Status code: ${statusCode}. Full response: ${JSON.stringify(result)}`);

query.onCompletion({
error: {
type: reason,
Expand Down Expand Up @@ -217,6 +220,27 @@ function handleResponse(query, targetText, responseObj) {
* @type {Bob.Translate}
*/
function translate(query) {
// Input validation
if (!query || typeof query !== 'object') {
return query.onCompletion({
error: {
type: 'param',
message: 'Invalid query object',
addtion: 'Query must be a valid object',
},
});
}

if (!query.text || typeof query.text !== 'string' || query.text.trim() === '') {
return query.onCompletion({
error: {
type: 'param',
message: 'Invalid input text',
addtion: 'Input text must be a non-empty string',
},
});
}

if (!lang.langMap.get(query.detectTo)) {
return query.onCompletion({
error: {
Expand Down

0 comments on commit 54a8af2

Please sign in to comment.