Spaces:
Runtime error
Runtime error
[feat] merge llm agent kimi to extract question
Browse files- llms/kimi.py +63 -9
- llms/llm_prompt.py +102 -0
- llms/prompt.py +0 -53
- llms/prompts/formula_out_style +0 -18
- llms/prompts/html_question_template +0 -9
- llms/prompts/init_prompt +0 -1
- llms/prompts/question_convert_json_format_prompt +0 -5
- llms/prompts/question_convert_prompt +0 -6
- llms/prompts/question_info_json_format_prompt +0 -10
- llms/prompts/question_info_prompt +0 -1
- main.py +68 -7
- utils.py +8 -0
llms/kimi.py
CHANGED
|
@@ -1,6 +1,8 @@
|
|
| 1 |
import json
|
|
|
|
| 2 |
from openai import OpenAI
|
| 3 |
-
from llms.
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
class KimiAgent:
|
|
@@ -8,6 +10,45 @@ class KimiAgent:
|
|
| 8 |
self.api_key = api_key # 保存 api_key 供初始化 client 使用
|
| 9 |
self.base_url = base_url
|
| 10 |
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
def get_question_info(self) -> dict:
|
| 13 |
completion = self.client.chat.completions.create(
|
|
@@ -15,7 +56,8 @@ class KimiAgent:
|
|
| 15 |
messages=[
|
| 16 |
{"role": "system", "content": init_prompt},
|
| 17 |
{"role": "system", "content": question_info_json_format_prompt},
|
| 18 |
-
{"role": "
|
|
|
|
| 19 |
],
|
| 20 |
temperature=0.3,
|
| 21 |
response_format={"type": "json_object"},
|
|
@@ -24,11 +66,13 @@ class KimiAgent:
|
|
| 24 |
return content
|
| 25 |
|
| 26 |
def extract_question_html(self) -> dict:
|
|
|
|
| 27 |
completion = self.client.chat.completions.create(
|
| 28 |
model="moonshot-v1-auto",
|
| 29 |
messages=[
|
| 30 |
{"role": "system", "content": init_prompt},
|
| 31 |
{"role": "system", "content": question_return_json_format_prompt},
|
|
|
|
| 32 |
{"role": "user", "content": question_convert_prompt},
|
| 33 |
],
|
| 34 |
temperature=0.3,
|
|
@@ -37,13 +81,14 @@ class KimiAgent:
|
|
| 37 |
content = json.loads(completion.choices[0].message.content)
|
| 38 |
return content
|
| 39 |
|
| 40 |
-
def extract_question_jsonl(self) -> dict:
|
| 41 |
completion = self.client.chat.completions.create(
|
| 42 |
model="moonshot-v1-auto",
|
| 43 |
messages=[
|
| 44 |
{"role": "system", "content": init_prompt},
|
| 45 |
-
{"role": "system", "content":
|
| 46 |
-
{"role": "
|
|
|
|
| 47 |
],
|
| 48 |
temperature=0.3,
|
| 49 |
response_format={"type": "json_object"},
|
|
@@ -58,6 +103,10 @@ if __name__ == "__main__":
|
|
| 58 |
|
| 59 |
agent = KimiAgent(api_key=API_KEY)
|
| 60 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
try:
|
| 62 |
print("获取问题信息...")
|
| 63 |
question_info = agent.get_question_info()
|
|
@@ -66,10 +115,15 @@ if __name__ == "__main__":
|
|
| 66 |
except Exception as e:
|
| 67 |
print("获取问题信息出错:", e)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
try:
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
|
|
|
| 74 |
except Exception as e:
|
| 75 |
print("提取问题时出错:", e)
|
|
|
|
| 1 |
import json
|
| 2 |
+
from time import sleep
|
| 3 |
from openai import OpenAI
|
| 4 |
+
from llms.llm_prompt import *
|
| 5 |
+
from pathlib import Path
|
| 6 |
|
| 7 |
|
| 8 |
class KimiAgent:
|
|
|
|
| 10 |
self.api_key = api_key # 保存 api_key 供初始化 client 使用
|
| 11 |
self.base_url = base_url
|
| 12 |
self.client = OpenAI(api_key=self.api_key, base_url=self.base_url)
|
| 13 |
+
self.file_object = None # 初始化 file_object 为 None
|
| 14 |
+
self.file_content = None # 初始化 file_content 为 None
|
| 15 |
+
self.messages = []
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def make_messages(self, input: str, n: int = 20) -> list[dict]:
|
| 19 |
+
"""
|
| 20 |
+
使用 make_messaegs 控制每次请求的消息数量,使其保持在一个合理的范围内,例如默认值是 20。在构建消息列表
|
| 21 |
+
的过程中,我们会先添加 System Prompt,这是因为无论如何对消息进行截断,System Prompt 都是必不可少
|
| 22 |
+
的内容,再获取 messages —— 即历史记录中,最新的 n 条消息作为请求使用的消息,在大部分场景中,这样
|
| 23 |
+
能保证请求的消息所占用的 Tokens 数量不超过模型上下文窗口。
|
| 24 |
+
"""
|
| 25 |
+
# 首先,我们将用户最新的问题构造成一个 message(role=user),并添加到 messages 的尾部
|
| 26 |
+
messages.append({
|
| 27 |
+
"role": "user",
|
| 28 |
+
"content": input,
|
| 29 |
+
})
|
| 30 |
+
|
| 31 |
+
# new_messages 是我们下一次请求使用的消息列表,现在让我们来构建它
|
| 32 |
+
new_messages = []
|
| 33 |
+
|
| 34 |
+
# 每次请求都需要携带 System Messages,因此我们需要先把 system_messages 添加到消息列表中;
|
| 35 |
+
# 注意,即使对消息进行截断,也应该注意保证 System Messages 仍然在 messages 列表中。
|
| 36 |
+
new_messages.extend(system_messages)
|
| 37 |
+
|
| 38 |
+
# 在这里,当历史消息超过 n 条时,我们仅保留最新的 n 条消息
|
| 39 |
+
if len(messages) > n:
|
| 40 |
+
messages = messages[-n:]
|
| 41 |
+
|
| 42 |
+
new_messages.extend(messages)
|
| 43 |
+
return new_messages
|
| 44 |
+
|
| 45 |
+
def get_file_info(self, file_path):
|
| 46 |
+
self.file_object, self.file_content = None, None
|
| 47 |
+
self.file_object = self.client.files.create(
|
| 48 |
+
file=Path(file_path), purpose="file-extract"
|
| 49 |
+
)
|
| 50 |
+
self.file_content = self.client.files.content(file_id=self.file_object.id).text
|
| 51 |
+
print("update question file")
|
| 52 |
|
| 53 |
def get_question_info(self) -> dict:
|
| 54 |
completion = self.client.chat.completions.create(
|
|
|
|
| 56 |
messages=[
|
| 57 |
{"role": "system", "content": init_prompt},
|
| 58 |
{"role": "system", "content": question_info_json_format_prompt},
|
| 59 |
+
{"role": "system", "content": self.file_content},
|
| 60 |
+
{"role": "user", "content": question_info_json_format_prompt},
|
| 61 |
],
|
| 62 |
temperature=0.3,
|
| 63 |
response_format={"type": "json_object"},
|
|
|
|
| 66 |
return content
|
| 67 |
|
| 68 |
def extract_question_html(self) -> dict:
|
| 69 |
+
# need to fix
|
| 70 |
completion = self.client.chat.completions.create(
|
| 71 |
model="moonshot-v1-auto",
|
| 72 |
messages=[
|
| 73 |
{"role": "system", "content": init_prompt},
|
| 74 |
{"role": "system", "content": question_return_json_format_prompt},
|
| 75 |
+
{"role": "system", "content": self.file_content},
|
| 76 |
{"role": "user", "content": question_convert_prompt},
|
| 77 |
],
|
| 78 |
temperature=0.3,
|
|
|
|
| 81 |
content = json.loads(completion.choices[0].message.content)
|
| 82 |
return content
|
| 83 |
|
| 84 |
+
def extract_question_jsonl(self, q) -> dict:
|
| 85 |
completion = self.client.chat.completions.create(
|
| 86 |
model="moonshot-v1-auto",
|
| 87 |
messages=[
|
| 88 |
{"role": "system", "content": init_prompt},
|
| 89 |
+
{"role": "system", "content": jsonl_format_prompt},
|
| 90 |
+
{"role": "system", "content": self.file_content},
|
| 91 |
+
{"role": "user", "content": question_extract_jsonl_prompt.format(q)},
|
| 92 |
],
|
| 93 |
temperature=0.3,
|
| 94 |
response_format={"type": "json_object"},
|
|
|
|
| 103 |
|
| 104 |
agent = KimiAgent(api_key=API_KEY)
|
| 105 |
|
| 106 |
+
agent.get_file_info(
|
| 107 |
+
"D:/Desktop/标注/LLMDataGenerator/temp_files/20250530_235628/1748620588/111/111.md"
|
| 108 |
+
)
|
| 109 |
+
|
| 110 |
try:
|
| 111 |
print("获取问题信息...")
|
| 112 |
question_info = agent.get_question_info()
|
|
|
|
| 115 |
except Exception as e:
|
| 116 |
print("获取问题信息出错:", e)
|
| 117 |
|
| 118 |
+
|
| 119 |
+
# Split questions into groups of 4
|
| 120 |
+
question_groups = [question_info["questions"][i:i + 4] for i in range(0, len(question_info["questions"]), 4)]
|
| 121 |
+
|
| 122 |
try:
|
| 123 |
+
for que in question_groups:
|
| 124 |
+
print("提取问题...")
|
| 125 |
+
question = agent.extract_question_jsonl(str(que))
|
| 126 |
+
print("提取的问题:")
|
| 127 |
+
print(json.dumps(question, indent=2, ensure_ascii=False))
|
| 128 |
except Exception as e:
|
| 129 |
print("提取问题时出错:", e)
|
llms/llm_prompt.py
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# common system prompt
|
| 2 |
+
init_prompt = """
|
| 3 |
+
你是一个人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
question_return_json_format_prompt = """
|
| 7 |
+
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 8 |
+
请使用如下 JSON 格式输出你的回复:
|
| 9 |
+
{
|
| 10 |
+
"text": "文字信息"
|
| 11 |
+
}
|
| 12 |
+
"""
|
| 13 |
+
|
| 14 |
+
# 读取题目基本信息
|
| 15 |
+
question_info_json_format_prompt = """
|
| 16 |
+
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 17 |
+
需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 18 |
+
请使用如下 JSON 格式输出你的回复:
|
| 19 |
+
{
|
| 20 |
+
"questions":[
|
| 21 |
+
"问题全名"
|
| 22 |
+
]
|
| 23 |
+
}
|
| 24 |
+
"""
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
question_info_prompt = f"""
|
| 28 |
+
这是ocr识别出的格式,帮我把文件中的所有题目汇总成json格式。
|
| 29 |
+
题干如果有图片需要包含图片部分内容。
|
| 30 |
+
对于问题全名有图片链接的部分,使用img标签将图片插入对应的位置,<img src=\"对应位置的图片链接\">。
|
| 31 |
+
需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
# html 处理
|
| 35 |
+
html_question_template = """
|
| 36 |
+
<b style="color:blue;">【序号】</b>"序号"<br />
|
| 37 |
+
<b style="color:blue;">【类别】</b>"类别内容"<br />
|
| 38 |
+
<b style="color:blue;">【题干】</b><br />
|
| 39 |
+
<b style="color:blue;"></b>"题干内容"<br />
|
| 40 |
+
<b style="color:blue;">【答案】</b><br />
|
| 41 |
+
<b style="color:blue;"></b>"答案内容"<br />
|
| 42 |
+
<b style="color:blue;">【解析】</b><br />
|
| 43 |
+
<b style="color:blue;"></b>"解析内容"<br />
|
| 44 |
+
<hr style='border:1px solid;' />
|
| 45 |
+
"""
|
| 46 |
+
|
| 47 |
+
question_convert_prompt = """
|
| 48 |
+
这是ocr识别出的格式,帮我把文件中的题目 {}:{} 转换成如下对应模板格式.
|
| 49 |
+
需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 50 |
+
题干中如果有的话需要包含题目所提供的选项。对于序号和类别来说维持一行内。
|
| 51 |
+
对于解析,包括关键思路分析和解答。对于所有段落,不要自行总结和省略,一律使用原文。
|
| 52 |
+
一次性输出所有内容。
|
| 53 |
+
对于每一个有图片链接的部分,使用img标签将图片插入对应的位置。
|
| 54 |
+
如果每个部分中没有原文对应的部分,则联想一下答案内容。
|
| 55 |
+
|
| 56 |
+
{}
|
| 57 |
+
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# jsonl 处理
|
| 61 |
+
|
| 62 |
+
jsonl_format_prompt = """
|
| 63 |
+
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 64 |
+
请严格遵守字段要求,严禁输出无关字段,严禁缺少字段,qlist中的每个对象的属性数量应和模板一致。
|
| 65 |
+
对于需要转换多个题目的情况,直接再后面按照JSON格式叠加到qlist列表。
|
| 66 |
+
请使用如下 JSON 格式输出你的回复:
|
| 67 |
+
{
|
| 68 |
+
"qlist": [
|
| 69 |
+
{
|
| 70 |
+
"text_question": "题干内容",
|
| 71 |
+
"text_answer": "答案内容",
|
| 72 |
+
"answer_analysis": "答案解析内容",
|
| 73 |
+
"is_pic_included": "填充NAN",
|
| 74 |
+
"text_course": "题目学科",
|
| 75 |
+
"text_grade_level": "问题阶段可能对应的年级,例如高中一年级,根据情景判断",
|
| 76 |
+
"text_grade": "问题阶段(小学,初中,高中,大学等,根据情景判断)",
|
| 77 |
+
"text_paper": "填充NAN",
|
| 78 |
+
"language": "问题语言(中文为zh,英文为en等,根据情景判断)",
|
| 79 |
+
"entrance_exam_type": "填充NAN",
|
| 80 |
+
"question_type": "题目类型(选择题,填空题,问答题等,根据情景判断)",
|
| 81 |
+
"static_info": "填充NAN",
|
| 82 |
+
}
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
"""
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
question_extract_jsonl_prompt = """
|
| 90 |
+
请严格遵守字段要求,严禁输出无关字段,严禁缺少字段。
|
| 91 |
+
这是ocr识别出的格式,帮我把文件中如下列表的题目转换成对应 JSONL 模板格式。
|
| 92 |
+
需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 93 |
+
题干中如果有的话需要包含题目所提供的选项。
|
| 94 |
+
对于解析,包括关键思路分析和解答。
|
| 95 |
+
对于所有段落,不要自行总结和省略,一律使用原文。
|
| 96 |
+
一次性输出所有内容。
|
| 97 |
+
题干如果有图片需要包含图片部分内容。
|
| 98 |
+
对于每一个有图片链接的部分,使用img标签将图片插入对应的位置,<img src=\"对应位置的图片链接\">。
|
| 99 |
+
如果每个部分中没有原文对应的部分,则联想一下答案内容。
|
| 100 |
+
|
| 101 |
+
{}
|
| 102 |
+
"""
|
llms/prompt.py
DELETED
|
@@ -1,53 +0,0 @@
|
|
| 1 |
-
from utils import read_all_text
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
init_prompt = """
|
| 5 |
-
你是一个人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。
|
| 6 |
-
"""
|
| 7 |
-
|
| 8 |
-
question_return_json_format_prompt = """
|
| 9 |
-
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 10 |
-
请使用如下 JSON 格式输出你的回复:
|
| 11 |
-
{
|
| 12 |
-
"text": "文字信息"
|
| 13 |
-
}
|
| 14 |
-
"""
|
| 15 |
-
|
| 16 |
-
question_info_json_format_prompt = """
|
| 17 |
-
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 18 |
-
请使用如下 JSON 格式输出你的回复:
|
| 19 |
-
{
|
| 20 |
-
"questions":[
|
| 21 |
-
{
|
| 22 |
-
"id": "问题id",
|
| 23 |
-
"question": "问题全名"
|
| 24 |
-
}
|
| 25 |
-
]
|
| 26 |
-
}
|
| 27 |
-
"""
|
| 28 |
-
|
| 29 |
-
html_question_template = """
|
| 30 |
-
<b style="color:blue;">【序号】</b>[序号]<br />
|
| 31 |
-
<b style="color:blue;">【类别】</b>[类别内容]<br />
|
| 32 |
-
<b style="color:blue;">【题干】</b><br />
|
| 33 |
-
<b style="color:blue;"></b>[题干内容]<br />
|
| 34 |
-
<b style="color:blue;">【答案】</b><br />
|
| 35 |
-
<b style="color:blue;"></b>[答案内容]<br />
|
| 36 |
-
<b style="color:blue;">【解析】</b><br />
|
| 37 |
-
<b style="color:blue;"></b>[解析内容]<br />
|
| 38 |
-
<hr style='border:1px solid;' />
|
| 39 |
-
"""
|
| 40 |
-
|
| 41 |
-
question_info_prompt = f"""
|
| 42 |
-
这是ocr识别出的markdown格式,帮我把文件中的所有题目汇总成json格式,需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
question_convert_prompt = """
|
| 46 |
-
这是ocr识别出的markdown格式,帮我把文件中的题目20题转换成如下对应模板格式,[]符号显示了需要替换你的位置和内容,需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!题干中如果有的话需要包含题目所提供的选项。对于序号和类别来说维持一行内。对于解析,包括关键思路分析和解答。对于所有段落,不要自行总结和省略,一律使用原文。一次性输出所有内容。对于每一个有图片链接的部分,使用img标签将图片插入对应的位置。如果每个部分中没有原文对应的部分,则联想一下答案内容。
|
| 47 |
-
|
| 48 |
-
{}
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
""".format(html_question_template)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/formula_out_style
DELETED
|
@@ -1,18 +0,0 @@
|
|
| 1 |
-
注意:公式渲染使用mathjax,渲染格式如下所示,需要保证能输出可渲染的公式
|
| 2 |
-
|
| 3 |
-
<!-- MathJax 配置 -->
|
| 4 |
-
<script>
|
| 5 |
-
MathJax = {
|
| 6 |
-
tex: {
|
| 7 |
-
packages: ['base', 'ams', 'mhchem'],
|
| 8 |
-
inlineMath: [['$', '$'], ['\\(', '\\)']],
|
| 9 |
-
errorHandler: (id, error) => {
|
| 10 |
-
console.error("MathJax Error:", error.message);
|
| 11 |
-
alert("MathJax Render Error: " + error.message);
|
| 12 |
-
}
|
| 13 |
-
},
|
| 14 |
-
loader: { load: ['[tex]/mhchem'] },
|
| 15 |
-
options: { messageStyle: 'none' }
|
| 16 |
-
};
|
| 17 |
-
</script>
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/html_question_template
DELETED
|
@@ -1,9 +0,0 @@
|
|
| 1 |
-
<b style="color:blue;">【序号】</b>[序号]<br />
|
| 2 |
-
<b style="color:blue;">【类别】</b>[类别内容]<br />
|
| 3 |
-
<b style="color:blue;">【题干】</b><br />
|
| 4 |
-
<b style="color:blue;"></b>[题干内容]<br />
|
| 5 |
-
<b style="color:blue;">【答案】</b><br />
|
| 6 |
-
<b style="color:blue;"></b>[答案内容]<br />
|
| 7 |
-
<b style="color:blue;">【解析】</b><br />
|
| 8 |
-
<b style="color:blue;"></b>[解析内容]<br />
|
| 9 |
-
<hr style='border:1px solid;' />
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/init_prompt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
你是一个人工智能助手,你更擅长中文和英文的对话。你会为用户提供安全,有帮助,准确的回答。同时,你会拒绝一切涉及恐怖主义,种族歧视,黄色暴力等问题的回答。
|
|
|
|
|
|
llms/prompts/question_convert_json_format_prompt
DELETED
|
@@ -1,5 +0,0 @@
|
|
| 1 |
-
你是一个格式转换专家,你负责回答用户提出的各种问题。请参考文档内容回复用户的问题。
|
| 2 |
-
请使用如下 JSON 格式输出你的回复:
|
| 3 |
-
{
|
| 4 |
-
"text": "文字信息"
|
| 5 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/question_convert_prompt
DELETED
|
@@ -1,6 +0,0 @@
|
|
| 1 |
-
这是ocr识别出的markdown格式,帮我把文件中的题目转换成如下对应模板格式。
|
| 2 |
-
[]符号显示了需要替换你的位置和内容,需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
| 3 |
-
题干中如果有的话需要包含题目所提供的选项。对于序号和类别来说维持一行内。
|
| 4 |
-
对于解析,包括关键思路分析和解答。对于所有段落,不要自行总结和省略,一律使用原文。一次性输出所有内容。
|
| 5 |
-
对于每一个有图片链接的部分,使用img标签将图片插入对应的位置。
|
| 6 |
-
如果每个部分中没有原文对应的部分,则联想一下答案内容。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/question_info_json_format_prompt
DELETED
|
@@ -1,10 +0,0 @@
|
|
| 1 |
-
你是一个格式转换专家,你负责回答用户提出的各种问题。问题是一个列表,别表中包含每个问题的id和问题全名。请参考文档内容回复用户的问题。
|
| 2 |
-
请使用如下 JSON 格式输出你的回复:
|
| 3 |
-
{
|
| 4 |
-
"questions":[
|
| 5 |
-
{
|
| 6 |
-
"id": "问题id",
|
| 7 |
-
"question": "问题全名"
|
| 8 |
-
}
|
| 9 |
-
]
|
| 10 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
llms/prompts/question_info_prompt
DELETED
|
@@ -1 +0,0 @@
|
|
| 1 |
-
这是ocr识别出的markdown格式,帮我把文件中的所有题目汇总成json格式,需要对应位置原文复制,严格按照模板输出!!禁止输出无关输出!!
|
|
|
|
|
|
main.py
CHANGED
|
@@ -1,4 +1,3 @@
|
|
| 1 |
-
import json
|
| 2 |
from llms.kimi import KimiAgent
|
| 3 |
from ocr.doc2x import PDFProcessor
|
| 4 |
import os
|
|
@@ -6,12 +5,27 @@ import gradio as gr
|
|
| 6 |
from datetime import datetime
|
| 7 |
import shutil
|
| 8 |
import time
|
| 9 |
-
|
| 10 |
|
| 11 |
def update_ocr_interface(file_type):
|
| 12 |
if file_type == "JSON":
|
| 13 |
-
return gr.update(interactive=False, value="不需要OCR"), "JSON格式不需要OCR识别"
|
| 14 |
-
return gr.update(interactive=True, value="doc2x"), ""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
|
| 17 |
def process_files(json_files, pdf_files, file_type, ocr_choice, llm_choice):
|
|
@@ -19,7 +33,11 @@ def process_files(json_files, pdf_files, file_type, ocr_choice, llm_choice):
|
|
| 19 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 20 |
unix_timestamp = str(int(time.time()))
|
| 21 |
temp_dir = os.path.join("temp_files", timestamp, unix_timestamp)
|
|
|
|
|
|
|
| 22 |
os.makedirs(temp_dir, exist_ok=True)
|
|
|
|
|
|
|
| 23 |
|
| 24 |
log = f"开始新的处理...\n时间戳目录: {temp_dir}\n"
|
| 25 |
|
|
@@ -47,6 +65,49 @@ def process_files(json_files, pdf_files, file_type, ocr_choice, llm_choice):
|
|
| 47 |
|
| 48 |
log += f"使用LLM模型: {llm_choice}\n"
|
| 49 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
except Exception as e:
|
| 51 |
log += f"\n处理出错: {str(e)}\n"
|
| 52 |
|
|
@@ -61,7 +122,6 @@ def update_file_inputs(file_type):
|
|
| 61 |
|
| 62 |
if __name__ == "__main__":
|
| 63 |
os.makedirs("temp_files", exist_ok=True)
|
| 64 |
-
os.makedirs("output", exist_ok=True)
|
| 65 |
|
| 66 |
apikey = "sk-q183d0h5lbz38k3ex46m6pfxbao9p3h1"
|
| 67 |
pdf = PDFProcessor(apikey)
|
|
@@ -98,6 +158,7 @@ if __name__ == "__main__":
|
|
| 98 |
with gr.Column(scale=1):
|
| 99 |
gr.Markdown("### OCR设置")
|
| 100 |
ocr_choice = gr.Radio(choices=["doc2x"], label="OCR接口", value="doc2x")
|
|
|
|
| 101 |
|
| 102 |
# LLM column
|
| 103 |
with gr.Column(scale=1):
|
|
@@ -106,7 +167,7 @@ if __name__ == "__main__":
|
|
| 106 |
|
| 107 |
# Status and control section
|
| 108 |
with gr.Row():
|
| 109 |
-
status_text = gr.Textbox(label="状态")
|
| 110 |
process_btn = gr.Button("开始处理")
|
| 111 |
|
| 112 |
# Log output section
|
|
@@ -116,7 +177,7 @@ if __name__ == "__main__":
|
|
| 116 |
file_type.change(
|
| 117 |
fn=update_ocr_interface,
|
| 118 |
inputs=[file_type],
|
| 119 |
-
outputs=[ocr_choice,
|
| 120 |
)
|
| 121 |
|
| 122 |
file_type.change(
|
|
|
|
|
|
|
| 1 |
from llms.kimi import KimiAgent
|
| 2 |
from ocr.doc2x import PDFProcessor
|
| 3 |
import os
|
|
|
|
| 5 |
from datetime import datetime
|
| 6 |
import shutil
|
| 7 |
import time
|
| 8 |
+
from utils import dump_jsonl
|
| 9 |
|
| 10 |
def update_ocr_interface(file_type):
|
| 11 |
if file_type == "JSON":
|
| 12 |
+
return gr.update(interactive=False, value="不需要OCR"), "JSON格式不需要OCR识别,禁用OCR接口!!!"
|
| 13 |
+
return gr.update(interactive=True, value="doc2x"), "PDF格式需要OCR识别"
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
def llm_extract_question(file_path, llm):
|
| 17 |
+
llm.get_file_info(file_path)
|
| 18 |
+
# 获取有多少个题目以及题目编号
|
| 19 |
+
question_info = agent.get_question_info()
|
| 20 |
+
if question_info["questions"] is not None and len(question_info["questions"]) > 0:
|
| 21 |
+
question_groups = [question_info["questions"][i:i + 4] for i in range(0, len(question_info["questions"]), 4)]
|
| 22 |
+
jsonl_data = []
|
| 23 |
+
for que in question_groups:
|
| 24 |
+
# 处理每个题目
|
| 25 |
+
ex_q= llm.extract_question_jsonl(str(que))
|
| 26 |
+
for q in ex_q["qlist"]:
|
| 27 |
+
jsonl_data.append(q)
|
| 28 |
+
return jsonl_data
|
| 29 |
|
| 30 |
|
| 31 |
def process_files(json_files, pdf_files, file_type, ocr_choice, llm_choice):
|
|
|
|
| 33 |
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
|
| 34 |
unix_timestamp = str(int(time.time()))
|
| 35 |
temp_dir = os.path.join("temp_files", timestamp, unix_timestamp)
|
| 36 |
+
out_dir = os.path.join(temp_dir, "out")
|
| 37 |
+
# 中间流程
|
| 38 |
os.makedirs(temp_dir, exist_ok=True)
|
| 39 |
+
# 交付目录
|
| 40 |
+
os.makedirs(out_dir, exist_ok=True)
|
| 41 |
|
| 42 |
log = f"开始新的处理...\n时间戳目录: {temp_dir}\n"
|
| 43 |
|
|
|
|
| 65 |
|
| 66 |
log += f"使用LLM模型: {llm_choice}\n"
|
| 67 |
|
| 68 |
+
if file_type == "JSON" and json_files is not None:
|
| 69 |
+
for json_file in json_files:
|
| 70 |
+
filename, file_ext = os.path.splitext(os.path.basename(json_file.name))
|
| 71 |
+
# # 解析jsonl
|
| 72 |
+
# js_jsonl = llm_extract_question(json_file, models["llm"][llm_choice])
|
| 73 |
+
# json.dumps(js_jsonl, open(os.path.join(out_dir, f"{filename}.jsonl"), "w", encoding="utf-8"), indent=4)
|
| 74 |
+
# # 移动img到交付目录
|
| 75 |
+
# img_dir = os.path.join(temp_dir, filename, "images")
|
| 76 |
+
# if os.path.exists(img_dir):
|
| 77 |
+
# dest_img_dir = os.path.join(out_dir, "img")
|
| 78 |
+
# os.makedirs(dest_img_dir, exist_ok=True)
|
| 79 |
+
# for img in os.listdir(img_dir):
|
| 80 |
+
# shutil.copy2(
|
| 81 |
+
# os.path.join(img_dir, img),
|
| 82 |
+
# os.path.join(dest_img_dir, img)
|
| 83 |
+
# )
|
| 84 |
+
# log += f"移动图片文件到输出目录\n"
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
elif file_type == "PDF" and pdf_files is not None:
|
| 88 |
+
for pdf_file in pdf_files:
|
| 89 |
+
filename, file_ext = os.path.splitext(os.path.basename(pdf_file.name))
|
| 90 |
+
md_path = os.path.join(temp_dir, filename, f"{filename}.md")
|
| 91 |
+
os.makedirs(os.path.join(out_dir, filename), exist_ok=True)
|
| 92 |
+
# 解析jsonl
|
| 93 |
+
md_jsonl = llm_extract_question(md_path, models["llm"][llm_choice])
|
| 94 |
+
dump_jsonl(os.path.join(out_dir, filename, f"{filename}.jsonl"), md_jsonl)
|
| 95 |
+
# 移动img到交付目录
|
| 96 |
+
img_dir = os.path.join(temp_dir, filename, "images")
|
| 97 |
+
if os.path.exists(img_dir):
|
| 98 |
+
dest_img_dir = os.path.join(out_dir, filename,"images")
|
| 99 |
+
os.makedirs(dest_img_dir, exist_ok=True)
|
| 100 |
+
for img in os.listdir(img_dir):
|
| 101 |
+
shutil.copy2(
|
| 102 |
+
os.path.join(img_dir, img),
|
| 103 |
+
os.path.join(dest_img_dir, img)
|
| 104 |
+
)
|
| 105 |
+
log += f"移动图片文件到输出目录\n"
|
| 106 |
+
|
| 107 |
+
# 上传到阿里云盘 output和tmp
|
| 108 |
+
|
| 109 |
+
# 判断上传完成后删除缓存文件
|
| 110 |
+
|
| 111 |
except Exception as e:
|
| 112 |
log += f"\n处理出错: {str(e)}\n"
|
| 113 |
|
|
|
|
| 122 |
|
| 123 |
if __name__ == "__main__":
|
| 124 |
os.makedirs("temp_files", exist_ok=True)
|
|
|
|
| 125 |
|
| 126 |
apikey = "sk-q183d0h5lbz38k3ex46m6pfxbao9p3h1"
|
| 127 |
pdf = PDFProcessor(apikey)
|
|
|
|
| 158 |
with gr.Column(scale=1):
|
| 159 |
gr.Markdown("### OCR设置")
|
| 160 |
ocr_choice = gr.Radio(choices=["doc2x"], label="OCR接口", value="doc2x")
|
| 161 |
+
status_ocr = gr.Textbox(label="OCR 状态", interactive=False)
|
| 162 |
|
| 163 |
# LLM column
|
| 164 |
with gr.Column(scale=1):
|
|
|
|
| 167 |
|
| 168 |
# Status and control section
|
| 169 |
with gr.Row():
|
| 170 |
+
status_text = gr.Textbox(label="运行状态", interactive=False)
|
| 171 |
process_btn = gr.Button("开始处理")
|
| 172 |
|
| 173 |
# Log output section
|
|
|
|
| 177 |
file_type.change(
|
| 178 |
fn=update_ocr_interface,
|
| 179 |
inputs=[file_type],
|
| 180 |
+
outputs=[ocr_choice, status_ocr],
|
| 181 |
)
|
| 182 |
|
| 183 |
file_type.change(
|
utils.py
CHANGED
|
@@ -1,9 +1,17 @@
|
|
|
|
|
|
|
|
| 1 |
def read_all_text(file_path: str) -> str:
|
| 2 |
with open(file_path, "r", encoding="utf-8") as file:
|
| 3 |
content = file.read()
|
| 4 |
return content
|
| 5 |
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
if __name__ == "__main__":
|
| 8 |
file_path = "D:\Desktop\标注\LLMDataGenerator\llms\prompts\html_question_template" # 请替换为实际的文件路径
|
| 9 |
text = read_all_text(file_path)
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
def read_all_text(file_path: str) -> str:
|
| 4 |
with open(file_path, "r", encoding="utf-8") as file:
|
| 5 |
content = file.read()
|
| 6 |
return content
|
| 7 |
|
| 8 |
|
| 9 |
+
def dump_jsonl(path, data):
|
| 10 |
+
with open(path, 'w') as f:
|
| 11 |
+
for item in data:
|
| 12 |
+
f.write(json.dumps(item, indent=2) + '\n')
|
| 13 |
+
|
| 14 |
+
|
| 15 |
if __name__ == "__main__":
|
| 16 |
file_path = "D:\Desktop\标注\LLMDataGenerator\llms\prompts\html_question_template" # 请替换为实际的文件路径
|
| 17 |
text = read_all_text(file_path)
|