|
22 | 22 | TEMPLATE_TEXT_EXAMPLE_PROMPT, |
23 | 23 | TEMPLATE_SUBSECTION_INSTRUCTION_PROMPT, |
24 | 24 | TEMPLATE_PREVIOUS_SUBSECITON_PROMPT, |
| 25 | + CYPHER_QUERY_EXAMPLE, |
| 26 | + TEMPLATE_EVOLVE_SUBSECTION_INSTRUCTION_PROMPT, |
| 27 | + TEMPLATE_EVOLVE_TEXT_PROMPT, |
| 28 | + TEMPLATE_EVOLVE_PREVIOUS_SUBSECITON_PROMPT, |
25 | 29 | ) |
26 | 30 |
|
27 | 31 | logger = logging.getLogger() |
@@ -62,8 +66,12 @@ def generate_prop_slot(self, prefix, info): |
62 | 66 | return output_str |
63 | 67 |
|
64 | 68 | def get_fetch_query(self, query, schema, lang="cypher"): |
| 69 | + example = "" |
| 70 | + if lang == "cypher": |
| 71 | + example = CYPHER_QUERY_EXAMPLE |
| 72 | + |
65 | 73 | fetch_prompt = TEMPLATE_QUERY_GENERATOR.format( |
66 | | - user_query=query, schema=schema, language=lang |
| 74 | + user_query=query, schema=schema, language=lang, example=example |
67 | 75 | ) |
68 | 76 |
|
69 | 77 | queries = self.generate("fetch_query", fetch_prompt) |
@@ -192,3 +200,61 @@ def append_bib_text(self, text, id2bib): |
192 | 200 | bib_text += "\n" + f"bib of {paper_id}" + "\n" |
193 | 201 |
|
194 | 202 | return bib_text |
| 203 | + |
| 204 | + def generate_evolve_texts(self, query, mind_map, max_token_per_subsection): |
| 205 | + section_prompts = [] |
| 206 | + |
| 207 | + subsection_id = 0 |
| 208 | + for category in mind_map.get("data", []): |
| 209 | + subsection_id += 1 |
| 210 | + prop_slot = str(category) |
| 211 | + |
| 212 | + generated_instruction = "" |
| 213 | + |
| 214 | + generated_instruction += ( |
| 215 | + TEMPLATE_EVOLVE_SUBSECTION_INSTRUCTION_PROMPT.format( |
| 216 | + subsection_id=str(subsection_id), |
| 217 | + max_token_per_subsection=str(max_token_per_subsection), |
| 218 | + ) |
| 219 | + ) |
| 220 | + |
| 221 | + if subsection_id > 1: |
| 222 | + generated_instruction += TEMPLATE_EVOLVE_PREVIOUS_SUBSECITON_PROMPT |
| 223 | + |
| 224 | + paper_memories = TEMPLATE_EVOLVE_TEXT_PROMPT.format( |
| 225 | + user_query=query, |
| 226 | + prop_slot=prop_slot, |
| 227 | + generate_instruction=generated_instruction, |
| 228 | + ) |
| 229 | + |
| 230 | + section_prompts.append(paper_memories) |
| 231 | + |
| 232 | + return section_prompts |
| 233 | + |
| 234 | + def write_evolve_sec_by_sec( |
| 235 | + self, query, mind_map, max_token_per_subsection, bib2id={} |
| 236 | + ): |
| 237 | + section_prompts = self.generate_evolve_texts( |
| 238 | + query=query, |
| 239 | + mind_map=mind_map, |
| 240 | + max_token_per_subsection=max_token_per_subsection, |
| 241 | + ) |
| 242 | + |
| 243 | + # for sec in section_prompts: |
| 244 | + # print("########## SECTION PROMPT ###############") |
| 245 | + # print(sec) |
| 246 | + |
| 247 | + section_text = "" |
| 248 | + for i in range(len(section_prompts)): |
| 249 | + text_prompt = section_prompts[i] |
| 250 | + if "<PREVIOUS></PREVIOUS>" in text_prompt: |
| 251 | + text_prompt = text_prompt.replace("<PREVIOUS></PREVIOUS>", section_text) |
| 252 | + new_section_text = self.generate("query_report_text", text_prompt) |
| 253 | + section_text += "\n" + new_section_text + "\n" |
| 254 | + |
| 255 | + final_section = section_text |
| 256 | + bib_text = self.append_bib_text(final_section, bib2id) |
| 257 | + |
| 258 | + final_section += bib_text |
| 259 | + |
| 260 | + return final_section |
0 commit comments