diff --git a/src/translate.ts b/src/translate.ts index 892e9ae..33d6c89 100644 --- a/src/translate.ts +++ b/src/translate.ts @@ -1,9 +1,5 @@ import { ServiceError, TextTranslateQuery } from "@bob-translate/types"; -import { - generatePrompt, - generateSystemPrompt, - handleGeneralError, -} from "./util"; +import { generatePrompt, generateSystemPrompt, handleGeneralError } from "./util"; import { langMap } from "./lang"; export async function translate(query: TextTranslateQuery) { @@ -44,7 +40,7 @@ export async function translate(query: TextTranslateQuery) { messages: [ { role: "system", - content: generateSystemPrompt(), + content: generateSystemPrompt(query), }, { role: "user", @@ -74,8 +70,7 @@ export async function translate(query: TextTranslateQuery) { type: "secretKey", message: "配置错误 - 请确保您在插件配置中填入了正确的 API Keys", addition: "请在插件配置中填写正确的 API Keys", - troubleshootingLink: - "https://bobtranslate.com/service/translate/openai.html", + troubleshootingLink: "https://bobtranslate.com/service/translate/openai.html", }); } else if (streamData.text !== undefined) { // 将新的数据添加到缓冲变量中 @@ -85,11 +80,7 @@ export async function translate(query: TextTranslateQuery) { if (match) { // 如果是一个完整的消息,处理它并从缓冲变量中移除 const textFromResponse = match[1].trim(); - targetText = handleStreamResponse( - query, - targetText, - textFromResponse, - ); + targetText = handleStreamResponse(query, targetText, textFromResponse); buffer = buffer.slice(match[0].length); } } @@ -115,11 +106,7 @@ export async function translate(query: TextTranslateQuery) { } } -const handleStreamResponse = ( - query: TextTranslateQuery, - targetText: string, - textFromResponse: string, -) => { +const handleStreamResponse = (query: TextTranslateQuery, targetText: string, textFromResponse: string) => { if (textFromResponse !== "[DONE]") { try { const dataObj = JSON.parse(textFromResponse); diff --git a/src/util.ts b/src/util.ts index 714a485..c5e71c6 100644 --- a/src/util.ts +++ b/src/util.ts @@ -2,22 +2,50 @@ import { HTTP_ERROR_CODES } from "./constants"; import { HttpResponse, ServiceError, TextTranslateQuery } from "@bob-translate/types"; export function generatePrompt(query: TextTranslateQuery) { - const promptMaps: Record = { - translate: $option.prompt || `Translate the following text to {targetLang}: {sourceText}`, - interpret: `简明扼要地解释:{sourceText}`, - }; - const pattern = $option.pattern || 'translate'; - const promptTemplate = promptMaps[pattern]; - return parseStringTemplate(promptTemplate, { - sourceLang: query.detectFrom, - targetLang: query.detectTo, - sourceText: query.text, - }); + const pattern = $option.pattern || "translate"; + const { text } = query; + + if (pattern === "translate") { + if (isEnglishWord(text.trim())) { + return parseStringTemplate( + ` +请提供关于 {sourceText} 的详细解释,包括以下方面: +1. **词源**: 解释该词的起源、词根,以及它是如何演变而来的。 +2. **发音**: 描述该词的发音,包括任何语音上的细微差别。 +3. **定义**: 提供该词的主要含义以及任何次要含义。 +4. **用法**: 给出使用该词的句子示例,并展示在不同语境下的用法。 +5. **同义词和反义词**: 列出同义词和反义词,并解释它们在意义或用法上的区别。 +6. **相关词汇**: 提及任何相关词汇或衍生词,并解释它们与原词的关联。 +7. **文化或历史背景**: 提供任何相关的文化或历史信息,以帮助理解该词的使用或意义。" +`, + { + sourceLang: query.detectFrom, + targetLang: query.detectTo, + sourceText: query.text, + } + ); + } else { + return parseStringTemplate($option.prompt || `Translate the following text to {targetLang}: {sourceText}`, { + sourceLang: query.detectFrom, + targetLang: query.detectTo, + sourceText: query.text, + }); + } + } else if (pattern === "interpret") { + return parseStringTemplate(`简明扼要地解释:{sourceText}`, { + sourceLang: query.detectFrom, + targetLang: query.detectTo, + sourceText: query.text, + }); + } } -export function generateSystemPrompt() { +export function generateSystemPrompt(query: TextTranslateQuery) { + const { text } = query; const promptMaps: Record = { - translate: "You are a translation engine, translate directly without explanation and any explanatory content", + translate: isEnglishWord(text.trim()) + ? "Your are a English language expert" + : "You are a translation engine, translate directly without explanation and any explanatory content", interpret: "You are now a knowledgeable encyclopedia expert who can provide detailed information and explanations in various fields. Whether it is science, history, technology or culture, you can answer questions in a simple and easy-to-understand way and cite relevant materials and examples to help you understand.", }; @@ -62,3 +90,7 @@ export function handleGeneralError(query: TextTranslateQuery, error: ServiceErro }); } } + +export function isEnglishWord(text: string) { + return text.split(" ").length === 1 && /^[a-zA-Z]+$/.test(text); +}