AgentConfig的字段和AiTranslatorConfig使用相同写法
This commit is contained in:
@@ -41,8 +41,8 @@ class PartialAgentResultError(ValueError):
|
|||||||
@dataclass(kw_only=True)
|
@dataclass(kw_only=True)
|
||||||
class AgentConfig:
|
class AgentConfig:
|
||||||
logger: logging.Logger
|
logger: logging.Logger
|
||||||
baseurl: str
|
base_url: str
|
||||||
key: str
|
api_key: str
|
||||||
model_id: str
|
model_id: str
|
||||||
temperature: float = 0.7
|
temperature: float = 0.7
|
||||||
max_concurrent: int = 30
|
max_concurrent: int = 30
|
||||||
@@ -110,11 +110,11 @@ class Agent:
|
|||||||
|
|
||||||
def __init__(self, config: AgentConfig):
|
def __init__(self, config: AgentConfig):
|
||||||
|
|
||||||
self.baseurl = config.baseurl.strip()
|
self.baseurl = config.base_url.strip()
|
||||||
if self.baseurl.endswith("/"):
|
if self.baseurl.endswith("/"):
|
||||||
self.baseurl = self.baseurl[:-1]
|
self.baseurl = self.baseurl[:-1]
|
||||||
self.domain = urlparse(self.baseurl).netloc
|
self.domain = urlparse(self.baseurl).netloc
|
||||||
self.key = config.key.strip() or "xx"
|
self.key = config.api_key.strip() or "xx"
|
||||||
self.model_id = config.model_id.strip()
|
self.model_id = config.model_id.strip()
|
||||||
self.system_prompt = ""
|
self.system_prompt = ""
|
||||||
self.temperature = config.temperature
|
self.temperature = config.temperature
|
||||||
|
|||||||
@@ -33,19 +33,20 @@ You need to extract person names and location names from these paragraphs and tr
|
|||||||
Finally, output a glossary of original terms:translated terms
|
Finally, output a glossary of original terms:translated terms
|
||||||
|
|
||||||
# Requirements
|
# Requirements
|
||||||
|
- The original language is identified based on the context.The target language is {self.to_lang}
|
||||||
|
- The src in the output glossary must exactly match the original term in original language, while dst is the {self.to_lang} translation of the term
|
||||||
- Do not include special tags or tags formatted as `<ph-xxxxxx>` in the glossary
|
- Do not include special tags or tags formatted as `<ph-xxxxxx>` in the glossary
|
||||||
- The src in the output glossary must exactly match the original term, while dst is the {self.to_lang} translation of the term
|
|
||||||
- The same src should only appear once in the glossary without repetition
|
- The same src should only appear once in the glossary without repetition
|
||||||
-Do not include common nouns in the glossary.
|
- Do not include common nouns in the glossary.
|
||||||
|
|
||||||
# Output
|
# Output
|
||||||
The output format should be plain JSON text in a list format
|
The output format should be plain JSON text in a list format
|
||||||
{[{"src": "<Original Term>", "dst": "<Translated Term>"}]}
|
{[{"src": "<Original Term>", "dst": "<Translated Term>"}]}
|
||||||
|
|
||||||
# Example
|
# Example1(Assuming the source language is English and the target language is Chinese in the example)
|
||||||
## Input
|
## Input
|
||||||
{{"0":"Jobs likes apples","1":"Bill Gates is sunbathing in Shanghai."}}
|
{{"0":"Jobs likes apples","1":"Bill Gates is sunbathing in Shanghai."}}
|
||||||
## Output(Assuming the target language is Chinese)
|
## Output
|
||||||
{r'[{"src": "Jobs", "dst": "乔布斯"}, {"src": "Bill Gates", "dst": "比尔盖茨"}, {"src": "Shanghai", "dst": "上海"}]'}
|
{r'[{"src": "Jobs", "dst": "乔布斯"}, {"src": "Bill Gates", "dst": "比尔盖茨"}, {"src": "Shanghai", "dst": "上海"}]'}
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|||||||
@@ -40,8 +40,7 @@ Target language: {config.to_lang}
|
|||||||
# Output
|
# Output
|
||||||
The translated markdown text as plain text (not in a markdown code block, with no extraneous text).
|
The translated markdown text as plain text (not in a markdown code block, with no extraneous text).
|
||||||
|
|
||||||
# Example
|
# Example(Assuming the target language is Chinese in the example, {config.to_lang} is the actual target language)
|
||||||
## Target language is Chinese
|
|
||||||
Input:
|
Input:
|
||||||
hello, what's your nam*@e?
|
hello, what's your nam*@e?
|
||||||

|

|
||||||
|
|||||||
@@ -43,10 +43,10 @@ class SegmentsTranslateAgent(Agent):
|
|||||||
- The translated sequence of segments, represented as JSON text (note: not a code block). The keys are the segment IDs, and the values are the translated segments.
|
- The translated sequence of segments, represented as JSON text (note: not a code block). The keys are the segment IDs, and the values are the translated segments.
|
||||||
- The returned JSON text must be a dictionary of the form {{<segment_id>: <translation>}}.
|
- The returned JSON text must be a dictionary of the form {{<segment_id>: <translation>}}.
|
||||||
- The segment IDs in the output must **exactly** match those in the input. And all segment IDs in input must appear in the output.
|
- The segment IDs in the output must **exactly** match those in the input. And all segment IDs in input must appear in the output.
|
||||||
# Example
|
# Example(Assuming the target language is Chinese in the example, {config.to_lang} is the actual target language)
|
||||||
## Input
|
## Input
|
||||||
{r'{"10":"hello","11":"apple","12":true,"13":"false","14":null}'}
|
{r'{"10":"hello","11":"apple","12":true,"13":"false","14":null}'}
|
||||||
## Output(Assuming the target language is Chinese)
|
## Output
|
||||||
{r'{"10":"你好","11":"苹果","12":true,"13":"错误","14":null}'}
|
{r'{"10":"你好","11":"苹果","12":true,"13":"错误","14":null}'}
|
||||||
> Warning: Never wrap the JSON text in ```.
|
> Warning: Never wrap the JSON text in ```.
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -273,15 +273,6 @@ class BaseWorkflowParams(BaseModel):
|
|||||||
# 如果跳过翻译,则不进行任何检查,允许 base_url 等字段为空
|
# 如果跳过翻译,则不进行任何检查,允许 base_url 等字段为空
|
||||||
return values
|
return values
|
||||||
|
|
||||||
@model_validator(mode='after')
|
|
||||||
def check_glossary_config(self) -> 'BaseWorkflowParams':
|
|
||||||
"""
|
|
||||||
在所有字段验证后,检查术语表相关配置的逻辑一致性。
|
|
||||||
"""
|
|
||||||
if self.glossary_generate_enable and not self.glossary_agent_config:
|
|
||||||
raise ValueError("当 `glossary_generate_enable` 为 `True` 时, `glossary_agent_config` 字段是必须的。")
|
|
||||||
return self
|
|
||||||
|
|
||||||
|
|
||||||
# 2. 为每个工作流创建独立的参数模型
|
# 2. 为每个工作流创建独立的参数模型
|
||||||
class MarkdownWorkflowParams(BaseWorkflowParams):
|
class MarkdownWorkflowParams(BaseWorkflowParams):
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
@@ -50,8 +50,8 @@ class AiTranslator(Translator[T]):
|
|||||||
else:
|
else:
|
||||||
glossary_agent_config = GlossaryAgentConfig(
|
glossary_agent_config = GlossaryAgentConfig(
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -44,8 +44,8 @@ class DocxTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -35,8 +35,8 @@ class EpubTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -92,8 +92,8 @@ class HtmlTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ class JsonTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -25,8 +25,8 @@ class MDTranslator(AiTranslator):
|
|||||||
if not self.skip_translate:
|
if not self.skip_translate:
|
||||||
agent_config = MDTranslateAgentConfig(custom_prompt=config.custom_prompt,
|
agent_config = MDTranslateAgentConfig(custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -31,8 +31,8 @@ class SrtTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -49,8 +49,8 @@ class TXTTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
@@ -33,8 +33,8 @@ class XlsxTranslator(AiTranslator):
|
|||||||
agent_config = SegmentsTranslateAgentConfig(
|
agent_config = SegmentsTranslateAgentConfig(
|
||||||
custom_prompt=config.custom_prompt,
|
custom_prompt=config.custom_prompt,
|
||||||
to_lang=config.to_lang,
|
to_lang=config.to_lang,
|
||||||
baseurl=config.base_url,
|
base_url=config.base_url,
|
||||||
key=config.api_key,
|
api_key=config.api_key,
|
||||||
model_id=config.model_id,
|
model_id=config.model_id,
|
||||||
temperature=config.temperature,
|
temperature=config.temperature,
|
||||||
thinking=config.thinking,
|
thinking=config.thinking,
|
||||||
|
|||||||
Reference in New Issue
Block a user