WordsStory / StoryWriterAI.py
redstoneleo's picture
Upload 5 files
4946780 verified
import LLMutils
import re # 用于清理输入
import json # 用于格式化打印输出
def compose(phraseList_str):
'''
使用单一 LLM 调用来同时获取故事和(可能不准确的)词典定义。
phraseList_str: 一个包含所有单词的、用逗号分隔的字符串。
'''
messages=[
{"role": "system", "content": '''
用户会给你提供一个由逗号分隔的英语单词或短语列表,请一定使用它们最常用的用法和意思和比它们更简单的常用词汇写一个带点哲理又轻松有趣的英文小故事,注意逻辑一定要严谨!
始终以如下json格式返回结果,不要添加任何额外文本。
{
"en":"英文小故事。如:Use the hair **blower**.其中blower是用户提交的单词或短语之一。用**突出显示目标词汇。这部分不能出现这种形式:he was liable (有责任的)
"zh":"对应的中文翻译。对应的用户提供的英语词汇请在汉语词汇后的括号里注明,如:需要验证 (authenticate)每位访客"。不需要用**突出显示目标词汇。
}
一个完整的示例如下
{
"en": "Once in a small town, there lived a man named Tom. He wasn't from the **nobility**, just an ordinary guy with big dreams. Tom worked at a local factory that produced **proprietary** gadgets. These gadgets were supposed to be the next big thing, but for some reason, the factory's products always seemed to **underperform** in the market. One day, Tom overheard a conversation between two of his colleagues. They were whispering about a **conspiracy** within the factory. They claimed that some higher - ups were deliberately making the products fail so they could sell the factory at a low price and make a huge profit. Tom thought this idea was **insane**, but he couldn't ignore it. As he was thinking about what to do, he accidentally fell into a deep **ditch** near the factory. When he climbed out, he realized he was **liable** to get into trouble if he didn't handle this situation carefully. He decided to gather more evidence before taking any action. In the end, he found out that the conspiracy was just a rumor. The real reason for the underperformance was poor management. Tom learned that sometimes, things aren't as complicated or evil as they seem, and a little common sense can go a long way.",
"zh": "在一个小镇上,住着一个名叫汤姆的人。他并非出身于**贵族(nobility)**,只是一个怀揣着远大梦想的普通人。汤姆在当地一家生产**专利(proprietary)**小玩意的工厂工作。这些小玩意本应成为下一个热门产品,但不知为何,工厂的产品在市场上总是**表现不佳(underperform)**。有一天,汤姆无意中听到两位同事的对话。他们正在小声谈论工厂内部的一个**阴谋(conspiracy)**。他们声称,一些高层故意让产品失败,这样他们就可以低价出售工厂,从而赚取巨额利润。汤姆觉得这个想法简直**疯狂(insane)**,但他不能置之不理。正当他思考该怎么办时,他不小心掉进了工厂附近的一个深**沟渠(ditch)**里。当他爬出来时,他意识到如果自己不谨慎处理这件事,很可能会**惹上麻烦(liable)**。他决定在采取任何行动之前先收集更多证据。最后,他发现这个阴谋只是个谣言。产品表现不佳的真正原因是管理不善。汤姆明白了,有时候事情并不像它们看起来的那么复杂或邪恶,一点常识就能起到很大作用。"
}
'''}, # <- 修正:这里的 f 已经被移除了,因为 system prompt 是静态的。
# "definitionList":[是这些英语单词或短语的释义列表。其中每个item形如
# {
# 'dictKeyText': 'book',
# 'chineseDefinition': 'n. 书; 书籍; 印刷(或电子)出版物; 著作; 本子; 簿子\nv. (向旅馆、饭店、戏院等)预约,预订; 给(某人)预订飞机等座位; 和(歌手等)预约演出日期',
# 'usPhoneticSymbol': 'bʊk',
# 'ukPhoneticSymbol': 'bʊk'
# }
# 其中,dictKeyText是用户提供的英语单词或短语,
# chineseDefinition是它的各种词性及其中文意思,
# usPhoneticSymbol和ukPhoneticSymbol对应的是美式音标和英式音标。
# **重要提示**:请尽你所能提供最准确的中文释义和音标。如果你不确定音标,请返回一个空字符串 "",而不是编造它。
# ]
# (优化) 在 user prompt 中使用 f-string,让指令更清晰
{"role": "user", "content": f"请使用以下词汇为我创作: {phraseList_str}"},
]
# 假设 LLMutils.chat 返回的是一个 dict 或一个 json 字符串
return LLMutils.chat(messages, LLMutils.MODEL_NAME)
if __name__ == "__main__":
text='''
fringe
affiliate
pointless
emphasize
contractor
plumber
contiguity
curriculum vitae
dialect
domestic
nerd
faithfully
quarter
'''
# 1. (重要) 清理输入
# 使用 re.split 来处理各种空白符(换行、空格等)
word_list = [word for word in re.split(r'\s+', text.strip()) if word]
# 将列表转换为逗号分隔的字符串,以匹配 system prompt 中的描述
word_list_str = ", ".join(word_list)
print(f"--- 正在处理的词汇: {word_list_str} ---")
print("-" * 30)
# 2. 一次性调用
translation_result = compose(word_list_str)
# 3. 打印结果 (使用 json 模块格式化输出更易读)
try:
# 检查返回的是字符串还是字典
if isinstance(translation_result, str):
# 如果是字符串,尝试解析为 JSON (字典)
translation_dict = json.loads(translation_result)
else:
# 如果已经是字典
translation_dict = translation_result
# 格式化打印字典
print(json.dumps(translation_dict, indent=4, ensure_ascii=False))
except Exception as e:
# 如果解析失败(例如 LLM 返回了非 JSON 格式的错误信息)
print(f"--- 解析 LLM 输出失败 ---")
print(f"错误详情: {e}")
print("--- 原始输出: ---")
print(translation_result)