fix: custom api doc
This commit is contained in:
@@ -72,6 +72,39 @@ async (args) => {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
v2.0.2 Request Hook 可以简化为:
|
||||||
|
|
||||||
|
```js
|
||||||
|
async (args) => {
|
||||||
|
const url = args.url;
|
||||||
|
const method = "POST";
|
||||||
|
const headers = { "Content-type": "application/json" };
|
||||||
|
const body = {
|
||||||
|
model: "gemma3",
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "system",
|
||||||
|
content: args.defaultSystemPrompt,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: JSON.stringify({
|
||||||
|
targetLanguage: args.to,
|
||||||
|
segments: args.texts.map((text, id) => ({ id, text })),
|
||||||
|
glossary: {},
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
temperature: 0,
|
||||||
|
max_tokens: 20480,
|
||||||
|
think: false,
|
||||||
|
stream: false,
|
||||||
|
};
|
||||||
|
|
||||||
|
return { url, body, headers, method };
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Response Hook
|
Response Hook
|
||||||
|
|
||||||
```js
|
```js
|
||||||
@@ -108,3 +141,12 @@ async ({ res }) => {
|
|||||||
return { translations };
|
return { translations };
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
v2.0.2 版后内置`parseAIRes`函数,Response Hook 可以简化为:
|
||||||
|
|
||||||
|
```js
|
||||||
|
async ({ res, parseAIRes, }) => {
|
||||||
|
const translations = parseAIRes(res?.choices?.[0]?.message?.content);
|
||||||
|
return { translations };
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|||||||
@@ -26,6 +26,8 @@ import {
|
|||||||
INPUT_PLACE_KEY,
|
INPUT_PLACE_KEY,
|
||||||
INPUT_PLACE_MODEL,
|
INPUT_PLACE_MODEL,
|
||||||
DEFAULT_USER_AGENT,
|
DEFAULT_USER_AGENT,
|
||||||
|
defaultSystemPrompt,
|
||||||
|
defaultSubtitlePrompt,
|
||||||
} from "../config";
|
} from "../config";
|
||||||
import { msAuth } from "../libs/auth";
|
import { msAuth } from "../libs/auth";
|
||||||
import { genDeeplFree } from "./deepl";
|
import { genDeeplFree } from "./deepl";
|
||||||
@@ -678,13 +680,16 @@ export const genTransReq = async ({ reqHook, ...args }) => {
|
|||||||
if (reqHook?.trim() && !events) {
|
if (reqHook?.trim() && !events) {
|
||||||
try {
|
try {
|
||||||
interpreter.run(`exports.reqHook = ${reqHook}`);
|
interpreter.run(`exports.reqHook = ${reqHook}`);
|
||||||
const hookResult = await interpreter.exports.reqHook(args, {
|
const hookResult = await interpreter.exports.reqHook(
|
||||||
url,
|
{ ...args, defaultSystemPrompt, defaultSubtitlePrompt },
|
||||||
body,
|
{
|
||||||
headers,
|
url,
|
||||||
userMsg,
|
body,
|
||||||
method,
|
headers,
|
||||||
});
|
userMsg,
|
||||||
|
method,
|
||||||
|
}
|
||||||
|
);
|
||||||
if (hookResult && hookResult.url) {
|
if (hookResult && hookResult.url) {
|
||||||
return genInit(hookResult);
|
return genInit(hookResult);
|
||||||
}
|
}
|
||||||
@@ -732,6 +737,8 @@ export const parseTransRes = async (
|
|||||||
fromLang,
|
fromLang,
|
||||||
toLang,
|
toLang,
|
||||||
langMap,
|
langMap,
|
||||||
|
extractJson,
|
||||||
|
parseAIRes,
|
||||||
});
|
});
|
||||||
if (hookResult && Array.isArray(hookResult.translations)) {
|
if (hookResult && Array.isArray(hookResult.translations)) {
|
||||||
if (history && userMsg && hookResult.modelMsg) {
|
if (history && userMsg && hookResult.modelMsg) {
|
||||||
|
|||||||
@@ -340,7 +340,7 @@ Object.entries(OPT_LANGS_TO_SPEC).forEach(([t, m]) => {
|
|||||||
OPT_LANGS_TO_CODE[t] = specToCode(m);
|
OPT_LANGS_TO_CODE[t] = specToCode(m);
|
||||||
});
|
});
|
||||||
|
|
||||||
const defaultSystemPrompt = `Act as a translation API. Output a single raw JSON object only. No extra text or fences.
|
export const defaultSystemPrompt = `Act as a translation API. Output a single raw JSON object only. No extra text or fences.
|
||||||
|
|
||||||
Input:
|
Input:
|
||||||
{"targetLanguage":"<lang>","title":"<context>","description":"<context>","segments":[{"id":1,"text":"..."}],"glossary":{"sourceTerm":"targetTerm"},"tone":"<formal|casual>"}
|
{"targetLanguage":"<lang>","title":"<context>","description":"<context>","segments":[{"id":1,"text":"..."}],"glossary":{"sourceTerm":"targetTerm"},"tone":"<formal|casual>"}
|
||||||
@@ -381,7 +381,7 @@ Fail-safe: On any error, return {"translations":[]}.`;
|
|||||||
// 4. **Special Cases**: '[Music]' (and similar cues) are standalone entries. Translate appropriately (e.g., '[音乐]', '[Musique]').
|
// 4. **Special Cases**: '[Music]' (and similar cues) are standalone entries. Translate appropriately (e.g., '[音乐]', '[Musique]').
|
||||||
// `;
|
// `;
|
||||||
|
|
||||||
const defaultSubtitlePrompt = `You are an expert AI for subtitle generation. Convert a JSON array of word-level timestamps into a bilingual VTT file.
|
export const defaultSubtitlePrompt = `You are an expert AI for subtitle generation. Convert a JSON array of word-level timestamps into a bilingual VTT file.
|
||||||
|
|
||||||
**Workflow:**
|
**Workflow:**
|
||||||
1. Merge \`text\` fields into complete sentences; ignore empty text.
|
1. Merge \`text\` fields into complete sentences; ignore empty text.
|
||||||
|
|||||||
Reference in New Issue
Block a user