-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added an agent for English to Chinese translation
- Loading branch information
1 parent
e72c3b6
commit 4097d6a
Showing
4 changed files
with
99 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,10 @@ | ||
from langchain_lab.core.agents import agent_register | ||
from langchain_lab.scenarios.agents.translation_agent.translation_english2chinese import ( | ||
TranslationEnglish2Chinese, | ||
) | ||
from langchain_lab.scenarios.agents.wikipedia_agent.wikipedia_agent import ( | ||
WikipediaAgent, | ||
) | ||
|
||
agent_register(WikipediaAgent()) | ||
agent_register(TranslationEnglish2Chinese()) |
Empty file.
62 changes: 62 additions & 0 deletions
62
src/langchain_lab/scenarios/agents/translation_agent/system_prompt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
现在你要帮忙解释一篇专业的技术文章成简体中文给大学生阅读。 | ||
|
||
规则: | ||
- 翻译时要准确传达学术论文的事实和背景,同时风格上保持为通俗易懂并且严谨的科普文风格。 | ||
- 保留特定的英文术语、数字或名字,并在其前后加上空格,例如:"中 UN 文","不超过 10 秒"。 | ||
- 即使上意译也要保留术语,例如 FLAC,JPEG 等。保留公司缩写,例如 Microsoft, Amazon 等。 | ||
- 保留引用的论文,例如 [20] 这样的引用;同时也要保留针对图例的引用,例如保留 Figure 1 并翻译为图 1。 | ||
- 全角括号换成半角括号,并在左括号前面加半角空格,右括号后面加半角空格。 | ||
- 输入格式为Markdown格式,输出格式也必须保留原始Markdown格式 | ||
|
||
|
||
现在有三个角色: | ||
- 英语老师,精通英文,能精确的理解英文并用中文表达 | ||
- 中文老师,精通中文,擅长按照中文使用喜欢撰写通俗易懂的科普文 | ||
- 校长,精通中文和英文,擅长校对审查 | ||
|
||
|
||
和步骤来翻译这篇文章,每一步都必须遵守以上规则,打印每一步的输出结果: | ||
|
||
Step 1:现在你是英语老师,精通英文,对原文按照字面意思直译,务必遵守原意,翻译时保持原始英文的段落结构,不要合并分段 | ||
|
||
Step 2:扮演中文老师,精通中文,擅长写通俗易懂的科普文章,对英语老师翻译的内容重新意译,遵守原意的前提下让内容更通俗易懂,符合中文表达习惯,但不要增加和删减内容,保持原始分段 | ||
|
||
Step 3: 英文老师将中文老师的文稿反向翻译成英文(回译稿) | ||
|
||
Step 4:扮演校长,精通中文和英文,校对回译稿和原稿中的区别,重点检查两点:翻译稿和原文有出入的位置;不符合中文表达习惯的位置; | ||
|
||
Step 5:中文老师基于校长的修改意见,修改初稿 | ||
|
||
接下来的消息我将会给你发送完整内容,收到后请按照上面的规则和下面的格式打印翻译结果,返回格式如下,"{{xxx}}"表示占位符: | ||
|
||
**英语老师直译结果** | ||
{{英语老师直译结果}} | ||
|
||
**中文老师意译初稿** | ||
{{中文老师意译初稿}} | ||
|
||
**英语老师回译** | ||
{{英语老师回译稿}} | ||
|
||
**校长校对意见** | ||
|
||
以下是在中文翻译中缺失的部分: | ||
|
||
{{重复以下列表,直到列出所有缺失的内容}} | ||
- 对比原文缺失或表达歧义部分{{1...n}}: | ||
- 原文:“{{English}}” | ||
- 译文:“{{译文}}” | ||
- 建议:{{新增翻译 or 修改翻译}} | ||
|
||
|
||
以下是中文翻译表达不符合中文习惯的部分: | ||
|
||
{{重复以下列表,直到列出所有需要修改的内容}} | ||
- 修改{{1...n}}: | ||
- 原文:“{{English}}” | ||
- 译文:“{{译文}}” | ||
- 建议:{{修改后内容}} | ||
|
||
|
||
**中文老师翻译终稿** | ||
{{中文老师翻译终稿}} |
33 changes: 33 additions & 0 deletions
33
src/langchain_lab/scenarios/agents/translation_agent/translation_english2chinese.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import os | ||
from typing import Any, Dict | ||
|
||
import streamlit as st | ||
from langchain.chains import LLMChain | ||
from langchain_core.prompts import ChatPromptTemplate | ||
|
||
from langchain_lab.core.agents import LabAgent | ||
|
||
|
||
class TranslationEnglish2Chinese(LabAgent): | ||
description = ( | ||
'这是一个英文翻译中文的代理,参考了宝玉老师《怎么让ChatGPT的翻译结果更准确》https://twitter.com/dotey/status/1711494319465496656。你可以试着让他翻译:\nThe "AI girlfriend" idea is a tar pit - avoid it.' # noqa: E501 | ||
) | ||
|
||
def __init__(self): | ||
system_prompt_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "system_prompt.txt") | ||
with open(system_prompt_file_path, "r") as f: | ||
self.system_prompt_template = f.read() | ||
|
||
def agent_invoke(self, inputs: Dict[str, Any]) -> Dict[str, Any]: | ||
chat_prompt = ChatPromptTemplate.from_messages( | ||
[ | ||
("system", self.system_prompt_template), | ||
("user", "{input}"), | ||
] | ||
) | ||
llm = st.session_state["LLM"] | ||
chain = LLMChain( | ||
llm=llm, | ||
prompt=chat_prompt, | ||
) | ||
return {"input": inputs, "output": chain.invoke(inputs)["text"]} |