Skip to content

Commit 147eab2

Browse files
committed
修改为提示词使用模型
1 parent 4a2c7da commit 147eab2

2 files changed

Lines changed: 54 additions & 6 deletions

File tree

ai/enhance.py

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,32 @@ def load_existing_ids(output_file):
4343
print(f"读取现有文件时出错: {e}", file=sys.stderr)
4444
return existing_ids
4545

46+
def parse_llm_response(response_text):
47+
"""解析LLM返回的文本为结构化数据"""
48+
try:
49+
# 尝试直接解析整个响应为JSON
50+
return json.loads(response_text)
51+
except json.JSONDecodeError:
52+
# 如果失败,尝试提取JSON部分
53+
try:
54+
# 查找可能的JSON开始和结束位置
55+
start_idx = response_text.find('{')
56+
end_idx = response_text.rfind('}') + 1
57+
if start_idx >= 0 and end_idx > start_idx:
58+
json_str = response_text[start_idx:end_idx]
59+
return json.loads(json_str)
60+
except (json.JSONDecodeError, ValueError):
61+
pass
62+
63+
# 如果仍然失败,返回错误结构
64+
return {
65+
"tldr": "解析错误",
66+
"motivation": "解析错误",
67+
"method": "解析错误",
68+
"result": "解析错误",
69+
"conclusion": "解析错误"
70+
}
71+
4672
def main():
4773
args = parse_args()
4874
model_name = os.environ.get("MODEL_NAME", 'deepseek-chat')
@@ -80,8 +106,8 @@ def main():
80106

81107
print('Open:', args.data, file=sys.stderr)
82108

83-
# 修改这里,使用structured_output而不是function_calling
84-
llm = ChatOpenAI(model=model_name).with_structured_output(Structure)
109+
# 不使用function_calling,直接使用LLM
110+
llm = ChatOpenAI(model=model_name)
85111
print('Connect to:', model_name, file=sys.stderr)
86112
prompt_template = ChatPromptTemplate.from_messages([
87113
SystemMessagePromptTemplate.from_template(system),
@@ -92,12 +118,25 @@ def main():
92118

93119
for idx, d in enumerate(data):
94120
try:
95-
response: Structure = chain.invoke({
121+
# 获取文本响应
122+
response = chain.invoke({
96123
"language": language,
97124
"content": d['summary']
98125
})
99-
d['AI'] = response.model_dump()
100-
except langchain_core.exceptions.OutputParserException as e:
126+
127+
# 解析响应为结构化数据
128+
response_text = response.content
129+
structured_data = parse_llm_response(response_text)
130+
131+
# 确保所有必要字段都存在
132+
required_fields = ["tldr", "motivation", "method", "result", "conclusion"]
133+
for field in required_fields:
134+
if field not in structured_data:
135+
structured_data[field] = "未提供"
136+
137+
d['AI'] = structured_data
138+
139+
except Exception as e:
101140
print(f"{d['id']} has an error: {e}", file=sys.stderr)
102141
d['AI'] = {
103142
"tldr": "Error",

ai/system.txt

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
11
You are a professional paper analyst.
22
You should not respond too long output.
3-
Your output should in {language}.
3+
Your output should in {language}.
4+
IMPORTANT: You must respond with a valid JSON object containing the following fields:
5+
{
6+
"tldr": "brief summary of the paper",
7+
"motivation": "what motivated this research",
8+
"method": "methods used in this paper",
9+
"result": "main results of the paper",
10+
"conclusion": "conclusions of the paper"
11+
}
12+
Do not include any text outside of this JSON structure. The JSON must be properly formatted and parseable.

0 commit comments

Comments
 (0)