| 436 | ) |
| 437 | |
| 438 | def _parse_json(self, response, output_parser): |
| 439 | system_message = ( |
| 440 | "You are an useful assistant expert in materials science, physics, and chemistry " |
| 441 | "that can process text and transform it to JSON." |
| 442 | ) |
| 443 | human_message = """Transform the text between three double quotes in JSON.\n\n\n\n |
| 444 | {format_instructions}\n\nText: \"\"\"{text}\"\"\"""" |
| 445 | |
| 446 | system_message_prompt = SystemMessagePromptTemplate.from_template(system_message) |
| 447 | human_message_prompt = HumanMessagePromptTemplate.from_template(human_message) |
| 448 | |
| 449 | prompt_template = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt]) |
| 450 | |
| 451 | results = self.llm( |
| 452 | prompt_template.format_prompt( |
| 453 | text=response, format_instructions=output_parser.get_format_instructions() |
| 454 | ).to_messages() |
| 455 | ) |
| 456 | parsed_output = output_parser.parse(results.content) |
| 457 | |
| 458 | return parsed_output |
| 459 | |
| 460 | def _run_query(self, doc_id, query, context_size=4) -> tuple[Any, list]: |
| 461 | relevant_documents, relevant_document_coordinates = self._get_context(doc_id, query, context_size) |