162 lines
5.6 KiB
Python
162 lines
5.6 KiB
Python
from typing import Callable, Any
|
||
from ..function_tools import judge
|
||
|
||
CHAPTER_FINISH = 0
|
||
CHAPTER_FOCUS = 1
|
||
CHAPTER_LATTER = 2
|
||
CHAPTER_FAILED = 3
|
||
|
||
def tool_name_to_tool(tool_name_list):
|
||
tools = []
|
||
for tool_name in tool_name_list:
|
||
if tool_name == "":
|
||
continue
|
||
if tool_name == "judge":
|
||
tools.append(judge)
|
||
return tools
|
||
|
||
def tool_name_to_tool_with_args(tool_name_list, tool_args_list) -> list[tuple[Callable, dict]]:
|
||
tools = []
|
||
for tool_name, tool_args in zip(tool_name_list, tool_args_list):
|
||
if tool_name == "":
|
||
continue
|
||
if tool_name == "judge":
|
||
tools.append((judge, {"name": tool_args[0]}))
|
||
return tools
|
||
|
||
class Chapter():
|
||
def __init__(self, No = 1, focus = 2,title="", chapter = "", chapter_prompt = "",
|
||
score_prompt = "", append_tools : list[Callable[..., Any], dict] = None) -> None:
|
||
'''
|
||
No : 序号
|
||
focus : 0: Finished, 1: Focus, 2: Latter, 3: Failed
|
||
title : 子章节标题
|
||
chapter : 教案子章节内容
|
||
chapter_prompt : 教案的提示
|
||
score_prompt : 评测标准
|
||
append_tools : 追加工具函数
|
||
'''
|
||
self.No = No
|
||
self.focus = focus
|
||
self.chapter = chapter
|
||
self.chapter_prompt = chapter_prompt
|
||
self.score_prompt = score_prompt
|
||
self.append_tools = append_tools
|
||
self.memory = None
|
||
self.title =title
|
||
|
||
|
||
def GetChapter(self):
|
||
if self.focus == CHAPTER_FINISH: f = "(Finished Chapter)"
|
||
elif self.focus == CHAPTER_FOCUS: f = "(Your Now Chapter)"
|
||
elif self.focus == CHAPTER_LATTER: f = "(Ignore for Latter Chapter)"
|
||
elif self.focus == CHAPTER_FAILED: f = "(Failed Chapter)"
|
||
# des = f"{f} Chapter {self.No}: {self.chapter if self.focus else 'Hidden for further study.'}"
|
||
des = f"{f} Chapter {self.No}: "
|
||
if self.focus == CHAPTER_FINISH or self.focus == CHAPTER_FAILED: des += 'Hidden due to finished.'
|
||
if self.focus == CHAPTER_LATTER: des += 'Hidden for further study.'
|
||
if self.focus == CHAPTER_FOCUS:
|
||
des+=self.chapter
|
||
des+='\n'
|
||
des+='Here are some concrete instructions:\n'
|
||
des+=self.chapter_prompt+'\n'
|
||
|
||
return des
|
||
|
||
|
||
|
||
def Finished(self, successful: bool = True):
|
||
if successful: self.focus = 0
|
||
else: self.focus = 3
|
||
|
||
def Focus(self):
|
||
self.focus = 1
|
||
|
||
def load_chapters(markdown, markdown_prompt, score_prompt):
|
||
'''
|
||
将教案蓝图化解为多个章节(Chapter对象)
|
||
'''
|
||
markdown_list = markdown.split("\n")
|
||
markdown_prompt_list = markdown_prompt.split("\n")
|
||
score_prompt_list = score_prompt.split("\n")
|
||
# 获取 H1 标题
|
||
title = ""
|
||
for line in markdown_list:
|
||
if line.startswith("# "):
|
||
title = line[2:]
|
||
break
|
||
|
||
# 对于 H3 标题 构造每一个 Chapter
|
||
# 首先找到所有的 H3 标题
|
||
chapter_dict = {}
|
||
chapter_sequence = []
|
||
for line in markdown_list:
|
||
if line.startswith("### "):
|
||
chapter_name = line[4:]
|
||
chapter_dict[chapter_name] = {}
|
||
chapter_sequence.append(chapter_name)
|
||
# 将 H3 标题 和 其对应的内容 构造成一个 Chapter
|
||
|
||
h3_name = ""
|
||
content = ""
|
||
for i in range(len(markdown_list)):
|
||
if markdown_list[i].startswith("### "):
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["markdown"] = content
|
||
h3_name = markdown_list[i][4:]
|
||
content = ""
|
||
continue
|
||
if h3_name != "":
|
||
content += markdown_list[i]+"\n"
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["markdown"] = content
|
||
|
||
|
||
h3_name = ""
|
||
content = ""
|
||
require_tools = []
|
||
require_tools_args = []
|
||
for i in range(len(markdown_prompt_list)):
|
||
if markdown_prompt_list[i].startswith("### "):
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["markdown_prompt"] = content
|
||
chapter_dict[h3_name]["require_tools"] = tool_name_to_tool_with_args(require_tools, require_tools_args)
|
||
h3_name = markdown_prompt_list[i][4:]
|
||
content = ""
|
||
require_tools=[]
|
||
require_tools_args = []
|
||
continue
|
||
|
||
if h3_name != "":
|
||
if markdown_prompt_list[i].startswith("_require_tools"):
|
||
require_tools.append(markdown_prompt_list[i].split("=")[1].strip().split(",")[0])
|
||
require_tools_args.append((markdown_prompt_list[i].split("=")[1].strip().split(",")[1:]))
|
||
continue
|
||
content += markdown_prompt_list[i]+"\n"
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["markdown_prompt"] = content
|
||
chapter_dict[h3_name]["require_tools"] = tool_name_to_tool_with_args(require_tools, require_tools_args)
|
||
|
||
|
||
h3_name = ""
|
||
content = ""
|
||
for i in range(len(score_prompt_list)):
|
||
if score_prompt_list[i].startswith("### "):
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["score_prompt"] = content
|
||
h3_name = score_prompt_list[i][4:]
|
||
content = ""
|
||
continue
|
||
|
||
if h3_name != "":
|
||
content += score_prompt_list[i]+"\n"
|
||
if(h3_name != ""):
|
||
chapter_dict[h3_name]["score_prompt"] = content
|
||
|
||
|
||
chapter_chain = []
|
||
No = 1
|
||
for chapter_name in chapter_sequence:
|
||
chapter_chain.append(Chapter( No, CHAPTER_LATTER, chapter_name, chapter_dict[chapter_name]["markdown"], chapter_dict[chapter_name]["markdown_prompt"], chapter_dict[chapter_name]["score_prompt"],chapter_dict[chapter_name]["require_tools"]))
|
||
No+=1
|
||
return chapter_chain |