优化词典生成效果
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
"use server";
|
||||
|
||||
import { parseAIGeneratedJSON } from "@/lib/utils";
|
||||
import { getAnswer } from "./zhipu";
|
||||
import { executeDictionaryLookup } from "./dictionary";
|
||||
import { createLookUp, createPhrase, createWord, selectLastLookUp } from "../services/dictionaryService";
|
||||
import { DictLookUpRequest, DictWordResponse, isDictErrorResponse, isDictPhraseResponse, isDictWordResponse, type DictLookUpResponse } from "@/lib/shared";
|
||||
|
||||
@@ -56,6 +55,18 @@ const saveResult = async (req: DictLookUpRequest, res: DictLookUpResponse) => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 查询单词或短语
|
||||
*
|
||||
* 使用模块化的词典查询系统,将提示词拆分为6个阶段:
|
||||
* - 阶段0:基础系统提示
|
||||
* - 阶段1:输入解析与语言识别
|
||||
* - 阶段2:跨语言语义映射决策
|
||||
* - 阶段3:standardForm 生成与规范化
|
||||
* - 阶段4:释义与词条生成
|
||||
* - 阶段5:错误处理
|
||||
* - 阶段6:最终输出封装
|
||||
*/
|
||||
export const lookUp = async ({
|
||||
text,
|
||||
queryLang,
|
||||
@@ -69,61 +80,15 @@ export const lookUp = async ({
|
||||
queryLang,
|
||||
definitionLang
|
||||
});
|
||||
|
||||
if (forceRelook || !lastLookUp) {
|
||||
const response = await getAnswer([
|
||||
{
|
||||
role: "system",
|
||||
content: `
|
||||
你是一个词典工具,返回单词或短语的 JSON 解释结果。
|
||||
// 使用新的模块化查询系统
|
||||
const response = await executeDictionaryLookup(
|
||||
text,
|
||||
queryLang,
|
||||
definitionLang
|
||||
);
|
||||
|
||||
查询语言:${queryLang}
|
||||
释义语言:${definitionLang}
|
||||
|
||||
用户输入在 <text> 标签内,判断是单词还是短语。
|
||||
|
||||
语言规则:
|
||||
|
||||
若输入语言与查询语言一致,直接查询。
|
||||
|
||||
若不一致但语义清晰(如“吃”“跑”“睡觉”),先理解其语义,再映射到查询语言中最常见、最标准的对应词或短语(如 查询语言=意大利语,输入“吃” → mangiare)。
|
||||
|
||||
若语义不清晰或存在明显歧义,视为无效输入。
|
||||
|
||||
standardForm 规则:
|
||||
返回查询语言下的标准形式(英语动词原形、日语基本形、罗曼语族不定式等)。如无法确定,则与输入相同。
|
||||
|
||||
有效输入时返回:
|
||||
{
|
||||
"standardForm": "标准形式",
|
||||
"entries": [...]
|
||||
}
|
||||
|
||||
单词条目格式:
|
||||
{
|
||||
"ipa": "音标(如适用)",
|
||||
"definition": "释义(使用 ${definitionLang})",
|
||||
"partOfSpeech": "词性",
|
||||
"example": "例句(使用 ${queryLang})"
|
||||
}
|
||||
|
||||
短语条目格式:
|
||||
{
|
||||
"definition": "释义(使用 ${definitionLang})",
|
||||
"example": "例句(使用 ${queryLang})"
|
||||
}
|
||||
|
||||
无效输入返回:
|
||||
{
|
||||
"error": "错误信息(使用 ${definitionLang})"
|
||||
}
|
||||
|
||||
只输出 JSON,不附加任何解释性文字。
|
||||
`.trim()
|
||||
}, {
|
||||
role: "user",
|
||||
content: `<text>${text}</text>请处理text标签内的内容后返回给我json`
|
||||
}
|
||||
]).then(parseAIGeneratedJSON<DictLookUpResponse>);
|
||||
saveResult({
|
||||
text,
|
||||
queryLang,
|
||||
@@ -131,8 +96,10 @@ standardForm 规则:
|
||||
userId,
|
||||
forceRelook
|
||||
}, response);
|
||||
|
||||
return response;
|
||||
} else {
|
||||
// 从数据库返回缓存的结果
|
||||
if (lastLookUp.dictionaryWordId) {
|
||||
createLookUp({
|
||||
user: userId ? {
|
||||
|
||||
Reference in New Issue
Block a user