mirror of
https://github.com/yihong0618/bilingual_book_maker.git
synced 2025-06-05 19:15:34 +00:00
reformat python script
This commit is contained in:
parent
cf992aef70
commit
b83ac10e88
@ -17,6 +17,7 @@ def parse_prompt_arg(prompt_arg):
|
|||||||
if prompt_arg.endswith(".md") and os.path.exists(prompt_arg):
|
if prompt_arg.endswith(".md") and os.path.exists(prompt_arg):
|
||||||
try:
|
try:
|
||||||
from promptdown import StructuredPrompt
|
from promptdown import StructuredPrompt
|
||||||
|
|
||||||
structured_prompt = StructuredPrompt.from_promptdown_file(prompt_arg)
|
structured_prompt = StructuredPrompt.from_promptdown_file(prompt_arg)
|
||||||
|
|
||||||
# Initialize our prompt structure
|
# Initialize our prompt structure
|
||||||
@ -24,27 +25,40 @@ def parse_prompt_arg(prompt_arg):
|
|||||||
|
|
||||||
# Handle developer_message or system_message
|
# Handle developer_message or system_message
|
||||||
# Developer message takes precedence if both are present
|
# Developer message takes precedence if both are present
|
||||||
if hasattr(structured_prompt, 'developer_message') and structured_prompt.developer_message:
|
if (
|
||||||
prompt['system'] = structured_prompt.developer_message
|
hasattr(structured_prompt, "developer_message")
|
||||||
elif hasattr(structured_prompt, 'system_message') and structured_prompt.system_message:
|
and structured_prompt.developer_message
|
||||||
prompt['system'] = structured_prompt.system_message
|
):
|
||||||
|
prompt["system"] = structured_prompt.developer_message
|
||||||
|
elif (
|
||||||
|
hasattr(structured_prompt, "system_message")
|
||||||
|
and structured_prompt.system_message
|
||||||
|
):
|
||||||
|
prompt["system"] = structured_prompt.system_message
|
||||||
|
|
||||||
# Extract user message from conversation
|
# Extract user message from conversation
|
||||||
if hasattr(structured_prompt, 'conversation') and structured_prompt.conversation:
|
if (
|
||||||
|
hasattr(structured_prompt, "conversation")
|
||||||
|
and structured_prompt.conversation
|
||||||
|
):
|
||||||
for message in structured_prompt.conversation:
|
for message in structured_prompt.conversation:
|
||||||
if message.role.lower() == 'user':
|
if message.role.lower() == "user":
|
||||||
prompt['user'] = message.content
|
prompt["user"] = message.content
|
||||||
break
|
break
|
||||||
|
|
||||||
# Ensure we found a user message
|
# Ensure we found a user message
|
||||||
if 'user' not in prompt or not prompt['user']:
|
if "user" not in prompt or not prompt["user"]:
|
||||||
raise ValueError("PromptDown file must contain at least one user message")
|
raise ValueError(
|
||||||
|
"PromptDown file must contain at least one user message"
|
||||||
|
)
|
||||||
|
|
||||||
print(f"Successfully loaded PromptDown file: {prompt_arg}")
|
print(f"Successfully loaded PromptDown file: {prompt_arg}")
|
||||||
|
|
||||||
# Validate required placeholders
|
# Validate required placeholders
|
||||||
if any(c not in prompt["user"] for c in ["{text}"]):
|
if any(c not in prompt["user"] for c in ["{text}"]):
|
||||||
raise ValueError("User message in PromptDown must contain `{text}` placeholder")
|
raise ValueError(
|
||||||
|
"User message in PromptDown must contain `{text}` placeholder"
|
||||||
|
)
|
||||||
|
|
||||||
return prompt
|
return prompt
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
@ -388,7 +402,17 @@ So you are close to reaching the limit. You have to choose your own value, there
|
|||||||
translate_model = MODEL_DICT.get(options.model)
|
translate_model = MODEL_DICT.get(options.model)
|
||||||
assert translate_model is not None, "unsupported model"
|
assert translate_model is not None, "unsupported model"
|
||||||
API_KEY = ""
|
API_KEY = ""
|
||||||
if options.model in ["openai", "chatgptapi", "gpt4", "gpt4omini", "gpt4o", "o1preview", "o1", "o1mini", "o3mini"]:
|
if options.model in [
|
||||||
|
"openai",
|
||||||
|
"chatgptapi",
|
||||||
|
"gpt4",
|
||||||
|
"gpt4omini",
|
||||||
|
"gpt4o",
|
||||||
|
"o1preview",
|
||||||
|
"o1",
|
||||||
|
"o1mini",
|
||||||
|
"o3mini",
|
||||||
|
]:
|
||||||
if OPENAI_API_KEY := (
|
if OPENAI_API_KEY := (
|
||||||
options.openai_key
|
options.openai_key
|
||||||
or env.get(
|
or env.get(
|
||||||
|
@ -64,6 +64,7 @@ O3MINI_MODEL_LIST = [
|
|||||||
"o3-mini",
|
"o3-mini",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
class ChatGPTAPI(Base):
|
class ChatGPTAPI(Base):
|
||||||
DEFAULT_PROMPT = "Please help me to translate,`{text}` to {language}, please return only translated content not include the origin text"
|
DEFAULT_PROMPT = "Please help me to translate,`{text}` to {language}, please return only translated content not include the origin text"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user