Skip to content

Commit

Permalink
🚀feat(translate) add detailed prompt for English words
Browse files Browse the repository at this point in the history
🛠️refactor(translate) pass query to generateSystemPrompt
⚙️chore(format) optimize import and format code
  • Loading branch information
CaicoLeung committed Aug 16, 2024
1 parent d5d51f2 commit 837ffa8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 31 deletions.
23 changes: 5 additions & 18 deletions src/translate.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -44,7 +40,7 @@ export async function translate(query: TextTranslateQuery) {
messages: [
{
role: "system",
content: generateSystemPrompt(),
content: generateSystemPrompt(query),
},
{
role: "user",
Expand Down Expand Up @@ -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) {
// 将新的数据添加到缓冲变量中
Expand All @@ -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);
}
}
Expand All @@ -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);
Expand Down
58 changes: 45 additions & 13 deletions src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> = {
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<string, string> = {
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.",
};
Expand Down Expand Up @@ -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);
}

0 comments on commit 837ffa8

Please sign in to comment.