Skip to content

Commit

Permalink
modified: yuisub/llm.py
Browse files Browse the repository at this point in the history
	modified:   yuisub/prompt.py
  • Loading branch information
NULL204 committed Feb 6, 2025
1 parent e199c12 commit da4b3b4
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 56 deletions.
6 changes: 4 additions & 2 deletions yuisub/llm.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(
api_key=api_key,
base_url=base_url,
)
self.system_prompt = anime_prompt(bangumi_info, summary)
self.system_prompt, self.example_input, self.example_output = anime_prompt(bangumi_info, summary)
self.corner_case = True

@retry(wait=wait_random(min=3, max=5), stop=stop_after_attempt(5))
Expand All @@ -33,6 +33,8 @@ async def ask(self, question: str) -> ZH:

messages = [
{"role": "system", "content": self.system_prompt},
{"role": "user", "content": self.example_input},
{"role": "assistant", "content": self.example_output},
{"role": "user", "content": question},
]

Expand Down Expand Up @@ -63,5 +65,5 @@ async def ask(self, question: str) -> ZH:
class Summarizer(Translator):
def __init__(self, model: str, api_key: str, base_url: str, bangumi_info: Optional[BGM] = None) -> None:
super().__init__(model, api_key, base_url, bangumi_info)
self.system_prompt = summary_prompt(bangumi_info)
self.system_prompt, self.example_input, self.example_output = summary_prompt(bangumi_info)
self.corner_case = False
81 changes: 27 additions & 54 deletions yuisub/prompt.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional
from typing import Optional, Tuple

from pydantic import BaseModel

Expand All @@ -9,68 +9,41 @@ class ZH(BaseModel):
zh: str


def anime_prompt(bangumi_info: Optional[BGM] = None, summary: str = "") -> str:
def anime_prompt(bangumi_info: Optional[BGM] = None, summary: str = "") -> Tuple[str, str, str]:
if bangumi_info is None:
bangumi_info = BGM(introduction="", characters="")

return (
"""
你的目标是把这集新番的台词翻译成中文,要翻译得自然、流畅和地道,使用贴合二次元的表达方式。
字幕可能是通过 AI 生成的,请你在翻译时尽量保持逻辑性和连贯性。
此外,我可能会给你一些动漫相关的信息和本集的剧情总结。请注意,当人名等专有名词出现时,严格按照我提供的信息进行翻译。
system_prompt = (
"你的目标是把这集新番的台词翻译成中文,要翻译得自然、流畅和地道,使用贴合二次元的表达方式。\n"
"字幕可能是通过 AI 生成的,请你在翻译时尽量保持逻辑性和连贯性。\n"
"此外,我可能会给你一些动漫相关的信息和本集的剧情总结。请注意,当人名等专有名词出现时,严格按照我提供的信息进行翻译。\n\n"
"角色列表(日/中):\n"
f"{bangumi_info.characters}\n\n"
"本集简介:\n"
f"{summary}"
).strip()

"""
+ """
角色列表(日/中):
example_input = "止まるんじゃねぇぞ!"
example_output = "不要停下来啊!"

"""
+ bangumi_info.characters
+ """
return system_prompt, example_input, example_output

本集简介:

"""
+ summary
+ """
EXAMPLE INPUT:
止まるんじゃねぇぞ!
EXAMPLE OUTPUT:
不要停下来啊!
"""
)


def summary_prompt(bangumi_info: Optional[BGM] = None) -> str:
def summary_prompt(bangumi_info: Optional[BGM] = None) -> Tuple[str, str, str]:
if bangumi_info is None:
bangumi_info = BGM(introduction="", characters="")

return (
"""
你的目标是用中文总结这集新番的剧情,要求简洁明了,不要遗漏重要的细节。
我会给你一整集新番的台词,你需要根据这段台词总结出剧情的主要内容。
此外,我会提供给你一些动漫相关的信息,帮助你更好地理解剧情。请注意,当人名等专有名词出现时,严格按照我提供的信息总结。
"""
+ """
动漫简介:
"""
+ bangumi_info.introduction
+ """
角色列表(日/中):
"""
+ bangumi_info.characters
+ """
system_prompt = (
"你的目标是用中文总结这集新番的剧情,要求简洁明了,不要遗漏重要的细节。\n"
"我会给你一整集新番的台词,你需要根据这段台词总结出剧情的主要内容。\n"
"此外,我会提供给你一些动漫相关的信息,帮助你更好地理解剧情。请注意,当人名等专有名词出现时,严格按照我提供的信息总结。\n\n"
"动漫简介:\n"
f"{bangumi_info.introduction}\n\n"
"角色列表(日/中):\n"
f"{bangumi_info.characters}"
).strip()

EXAMPLE INPUT:
It is... How do you know that? I think you mentioned it in your introduction when you transferred in. Anyway, that's not important. My birthday is April 9th. I've already turned sixteen. I've already turned sixteen. Anyway, thanks for checking in, Alya. See you tomorrow.
example_input = "It is... How do you know that? I think you mentioned it in your introduction when you transferred in. Anyway, that's not important. My birthday is April 9th. I've already turned sixteen. I've already turned sixteen. Anyway, thanks for checking in, Alya. See you tomorrow."
example_output = "在这部动漫新番中,主角久世政近的邻座是一个名叫艾莉莎的女孩。艾莉莎通常对政近表现出冷漠的态度,但有时她会用俄语小声地向他撒娇。政近实际上拥有母语级别的俄语听力,能够理解艾莉莎的话,但他选择装作听不懂。艾莉莎误以为政近不懂俄语,所以她才会在他面前展示出她真实的一面。两人之间有着一种甜蜜的氛围,但他们都试图隐藏这一点。在对话中,艾莉莎纠正了政近关于她生日的错误认知,告诉他她的生日是4月9日,并且她已经16岁了。这段对话增加了他们之间的亲密感,让人对他们的关系发展充满期待。"

EXAMPLE OUTPUT:
在这部动漫新番中,主角久世政近的邻座是一个名叫艾莉莎的女孩。艾莉莎通常对政近表现出冷漠的态度,但有时她会用俄语小声地向他撒娇。政近实际上拥有母语级别的俄语听力,能够理解艾莉莎的话,但他选择装作听不懂。艾莉莎误以为政近不懂俄语,所以她才会在他面前展示出她真实的一面。两人之间有着一种甜蜜的氛围,但他们都试图隐藏这一点。在对话中,艾莉莎纠正了政近关于她生日的错误认知,告诉他她的生日是4月9日,并且她已经16岁了。这段对话增加了他们之间的亲密感,让人对他们的关系发展充满期待。
"""
)
return system_prompt, example_input, example_output

0 comments on commit da4b3b4

Please sign in to comment.