AI知识库

53AI知识库

学习大模型的前沿技术与行业应用场景


AI智能体实践:用Coze复刻吴恩达开源的翻译Agent
发布日期:2024-07-30 08:48:10 浏览次数: 1879 来源:老梁AI视界


前些时候吴恩达开源了一个翻译Agent项目

这是一个python的项目,使用了一个比较新颖的「反思工作流」,让AI自己优化AI翻译出来的内容。用这种方式来解决AI在单次输出时无法发现自己的错误和细节控制不足的问题。

今天就用Coze来复刻一下这个Agent。

先看看效果:

概览

这个项目里代码不多,我们先梳理一下工作流,用流程图表示,大概是这样:

流程不复杂,重点是“让AI对AI的内容进行反思”,这个其实是很多AI工作流里可以加入的一个点。

搭好的完整的Coze工作流如下,对应原项目的每个节点:

注:原项目有对超长文本进行了分块处理,按照约1000个tokens分一块,分块翻译之后再合并。我们这里为了方便理解,就暂时忽略的超长文本分块的问题。

节点详解

下面我们来详细看下每个节点~

开始节点

明确这个工作流需要的几个参数,其中国家语言习惯这个是可选参数,其他的都为必填参数。

AI第一次翻译

“人设与回复逻辑”这个相当于API调用时的system,提示词分别都是来自开源项目源代码里的

原始Prompt 来自原始项目里的one_chunk_initial_translation函数。我这里进行了翻译,因为用国内的大语言模型,可能中文会更适合一点。

system_message = f"You are an expert linguist, specializing in translation from {source_lang} to {target_lang}."
translation_prompt = f"""This is an {source_lang} to {target_lang} translation, please provide the {target_lang} translation for this text. \Do not provide any explanations or text apart from the translation.{source_lang}: {source_text}
{target_lang}:"""

AI反思

AI反思时,会判断前面的国家参数有没有,有和没有在提示词上面一点微小的差异。

原始Prompt,源自项目里的 one_chunk_reflect_on_translation 函数。

system_message = f"You are an expert linguist specializing in translation from {source_lang} to {target_lang}. \You will be provided with a source text and its translation and your goal is to improve the translation."
if country != "":reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticism and helpful suggestions to improve the translation. \The final style and tone of the translation should match the style of {target_lang} colloquially spoken in {country}.
The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:
<SOURCE_TEXT>{source_text}</SOURCE_TEXT>
<TRANSLATION>{translation_1}</TRANSLATION>
When writing suggestions, pay attention to whether there are ways to improve the translation's \n\(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\(iii) style (by ensuring the translations reflect the style of the source text and take into account any cultural context),\n\(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
Write a list of specific, helpful and constructive suggestions for improving the translation.Each suggestion should address one specific part of the translation.Output only the suggestions and nothing else."""
else:reflection_prompt = f"""Your task is to carefully read a source text and a translation from {source_lang} to {target_lang}, and then give constructive criticisms and helpful suggestions to improve the translation. \
The source text and initial translation, delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT> and <TRANSLATION></TRANSLATION>, are as follows:
<SOURCE_TEXT>{source_text}</SOURCE_TEXT>
<TRANSLATION>{translation_1}</TRANSLATION>
When writing suggestions, pay attention to whether there are ways to improve the translation's \n\(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),\n\(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules, and ensuring there are no unnecessary repetitions),\n\(iii) style (by ensuring the translations reflect the style of the source text and take into account any cultural context),\n\(iv) terminology (by ensuring terminology use is consistent and reflects the source text domain; and by only ensuring you use equivalent idioms {target_lang}).\n\
Write a list of specific, helpful and constructive suggestions for improving the translation.Each suggestion should address one specific part of the translation.Output only the suggestions and nothing else."""

AI第二次翻译

第二次翻译,就是让AI结合原文、第一次译文、反思的结果进行再次编辑调整。

原始Prompt来自 one_chunk_improve_translation 函数。

system_message = f"You are an expert linguist, specializing in translation editing from {source_lang} to {target_lang}."
prompt = f"""Your task is to carefully read, then edit, a translation from {source_lang} to {target_lang}, taking intoaccount a list of expert suggestions and constructive criticisms.
The source text, the initial translation, and the expert linguist suggestions are delimited by XML tags <SOURCE_TEXT></SOURCE_TEXT>, <TRANSLATION></TRANSLATION> and <EXPERT_SUGGESTIONS></EXPERT_SUGGESTIONS> \as follows:
<SOURCE_TEXT>{source_text}</SOURCE_TEXT>
<TRANSLATION>{translation_1}</TRANSLATION>
<EXPERT_SUGGESTIONS>{reflection}</EXPERT_SUGGESTIONS>
Please take into account the expert suggestions when editing the translation. Edit the translation by ensuring:
(i) accuracy (by correcting errors of addition, mistranslation, omission, or untranslated text),(ii) fluency (by applying {target_lang} grammar, spelling and punctuation rules and ensuring there are no unnecessary repetitions), \(iii) style (by ensuring the translations reflect the style of the source text)(iv) terminology (inappropriate for context, inconsistent use), or(v) other errors.
Output only the new translation and nothing else."""

结束节点

结束节点,就是直接输出第二次翻译的结果,不要再返回让AI再进行编辑了。

还可以加一点提示,告诉用户翻译完成了。



53AI,企业落地应用大模型首选服务商

产品:大模型应用平台+智能体定制开发+落地咨询服务

承诺:先做场景POC验证,看到效果再签署服务协议。零风险落地应用大模型,已交付160+中大型企业

联系我们

售前咨询
186 6662 7370
预约演示
185 8882 0121

微信扫码

与创始人交个朋友

回到顶部

 
扫码咨询