| 92 | |
| 93 | |
| 94 | def generate_summaries(self,llm, parent_documents, documentId, embeddings, driver): |
| 95 | # Code for generating summaries |
| 96 | |
| 97 | # Ingest summaries |
| 98 | |
| 99 | summary_prompt = ChatPromptTemplate.from_messages( |
| 100 | [ |
| 101 | ( |
| 102 | "system", |
| 103 | ( |
| 104 | "You are generating concise and accurate summaries based on the " |
| 105 | "information found in the text." |
| 106 | ), |
| 107 | ), |
| 108 | ( |
| 109 | "human", |
| 110 | ("Generate a summary of the following input: {question}\n" "Summary:"), |
| 111 | ), |
| 112 | ] |
| 113 | ) |
| 114 | |
| 115 | summary_chain = summary_prompt | llm |
| 116 | |
| 117 | for i, parent in enumerate(parent_documents): |
| 118 | self.update_state(state=AppConfig.PROCESSING_SUMMARY, meta={"page": i+1, "total_pages": len(parent_documents), "documentId": documentId}) |
| 119 | logging.info(f"Generating summary for page {i+1} of {len(parent_documents)} for document {documentId}") |
| 120 | |
| 121 | summary = summary_chain.invoke({"question": parent.page_content}).content |
| 122 | params = { |
| 123 | "parent_id": f"Page {i+1}", |
| 124 | "uuid": str(uuid.uuid4()), |
| 125 | "summary": summary, |
| 126 | "embedding": embeddings.embed_query(summary), |
| 127 | "document_uuid": documentId |
| 128 | } |
| 129 | with driver.session() as session : |
| 130 | session.run( |
| 131 | """ |
| 132 | match (d:Document)-[]-(p:Page) where d.uuid=$document_uuid and p.name=$parent_id |
| 133 | with p |
| 134 | MERGE (p)-[:HAS_SUMMARY]->(s:Summary) |
| 135 | SET s.text = $summary, s.datecreated= datetime(), s.uuid= $uuid, s.source=p.uuid |
| 136 | WITH s |
| 137 | CALL db.create.setVectorProperty(s, 'embedding', $embedding) |
| 138 | YIELD node |
| 139 | RETURN count(*) |
| 140 | """, |
| 141 | params, |
| 142 | ) |