Update agents/summarization_agent.py
Browse files- agents/summarization_agent.py +103 -108
agents/summarization_agent.py
CHANGED
|
@@ -1,109 +1,104 @@
|
|
| 1 |
-
from langchain.vectorstores import FAISS
|
| 2 |
-
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 3 |
-
import os
|
| 4 |
-
import streamlit as st
|
| 5 |
-
from agents import SearchAgent
|
| 6 |
-
from config.config import model
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 11 |
-
|
| 12 |
-
class SummarizationAgent:
|
| 13 |
-
def __init__(self):
|
| 14 |
-
self.model = model
|
| 15 |
-
self.prompt = """You are a research assistant tasked with synthesizing findings from multiple academic papers over time. Your goal is to create a comprehensive summary that highlights key trends, thematic developments, and methodological evolution within a given timeframe.
|
| 16 |
-
|
| 17 |
-
Given the following context, analyze the papers to produce a structured summary:
|
| 18 |
-
|
| 19 |
-
Previous conversation:
|
| 20 |
-
{chat_history}
|
| 21 |
-
|
| 22 |
-
Papers context:
|
| 23 |
-
{context}
|
| 24 |
-
|
| 25 |
-
Guidelines for timeline-based summarization:
|
| 26 |
-
|
| 27 |
-
Key Findings and Trends Over Time
|
| 28 |
-
|
| 29 |
-
Identify major discoveries and conclusions, highlighting how they have developed chronologically.
|
| 30 |
-
Note emerging trends, consensus, and any evolving contradictions across papers, especially in response to new technologies or shifts in the field.
|
| 31 |
-
Present statistical evidence and experimental results in relation to time, pointing out any measurable improvements or declines over the years.
|
| 32 |
-
Methodological Evolution
|
| 33 |
-
|
| 34 |
-
Compare and contrast research approaches across different time periods, emphasizing changes or advances in data collection, analysis techniques, or tools.
|
| 35 |
-
Identify and describe innovative methodological contributions and how these may have impacted research outcomes over time.
|
| 36 |
-
Theoretical Progression
|
| 37 |
-
|
| 38 |
-
Outline the theoretical foundations and highlight their chronological development.
|
| 39 |
-
Connect findings to existing theories, noting how interpretations or theoretical perspectives have evolved.
|
| 40 |
-
Identify theoretical advances, challenges, or shifts and their relationship to the timeline.
|
| 41 |
-
Practical Applications and Temporal Shifts
|
| 42 |
-
|
| 43 |
-
Discuss real-world applications over time, noting how findings have influenced industry practices or technology adoption.
|
| 44 |
-
Highlight evolving practical use cases and how implementation considerations have changed with advances in research.
|
| 45 |
-
Research Gaps and Future Directions
|
| 46 |
-
|
| 47 |
-
Identify limitations in studies across time periods, noting any improvement or persistent gaps.
|
| 48 |
-
Point out unexplored areas and suggest specific future research directions informed by chronological developments in the field.
|
| 49 |
-
Formatting and Style:
|
| 50 |
-
|
| 51 |
-
Organize the summary with clear sections that reflect the temporal progression.
|
| 52 |
-
Maintain an academic tone, using specific examples, dates, and quotes where relevant.
|
| 53 |
-
Clearly identify and label sections to enhance readability, and acknowledge any limitations in the available context.
|
| 54 |
-
"""
|
| 55 |
-
|
| 56 |
-
self.papers = None
|
| 57 |
-
self.search_agent_response = ""
|
| 58 |
-
|
| 59 |
-
def solve(self, query):
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
#
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
for paper_id, chunks in paper_chunks.items():
|
| 105 |
-
paper_context = f"\nPaper: {paper_id}\n"
|
| 106 |
-
paper_context += "\n".join(chunks)
|
| 107 |
-
organized_context.append(paper_context)
|
| 108 |
-
|
| 109 |
return "\n\n".join(organized_context)
|
|
|
|
| 1 |
+
from langchain.vectorstores import FAISS
|
| 2 |
+
from langchain_google_genai import GoogleGenerativeAIEmbeddings
|
| 3 |
+
import os
|
| 4 |
+
import streamlit as st
|
| 5 |
+
from agents import SearchAgent
|
| 6 |
+
from config.config import model
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001")
|
| 11 |
+
|
| 12 |
+
class SummarizationAgent:
|
| 13 |
+
def __init__(self):
|
| 14 |
+
self.model = model
|
| 15 |
+
self.prompt = """You are a research assistant tasked with synthesizing findings from multiple academic papers over time. Your goal is to create a comprehensive summary that highlights key trends, thematic developments, and methodological evolution within a given timeframe.
|
| 16 |
+
|
| 17 |
+
Given the following context, analyze the papers to produce a structured summary:
|
| 18 |
+
|
| 19 |
+
Previous conversation:
|
| 20 |
+
{chat_history}
|
| 21 |
+
|
| 22 |
+
Papers context:
|
| 23 |
+
{context}
|
| 24 |
+
|
| 25 |
+
Guidelines for timeline-based summarization:
|
| 26 |
+
|
| 27 |
+
Key Findings and Trends Over Time
|
| 28 |
+
|
| 29 |
+
Identify major discoveries and conclusions, highlighting how they have developed chronologically.
|
| 30 |
+
Note emerging trends, consensus, and any evolving contradictions across papers, especially in response to new technologies or shifts in the field.
|
| 31 |
+
Present statistical evidence and experimental results in relation to time, pointing out any measurable improvements or declines over the years.
|
| 32 |
+
Methodological Evolution
|
| 33 |
+
|
| 34 |
+
Compare and contrast research approaches across different time periods, emphasizing changes or advances in data collection, analysis techniques, or tools.
|
| 35 |
+
Identify and describe innovative methodological contributions and how these may have impacted research outcomes over time.
|
| 36 |
+
Theoretical Progression
|
| 37 |
+
|
| 38 |
+
Outline the theoretical foundations and highlight their chronological development.
|
| 39 |
+
Connect findings to existing theories, noting how interpretations or theoretical perspectives have evolved.
|
| 40 |
+
Identify theoretical advances, challenges, or shifts and their relationship to the timeline.
|
| 41 |
+
Practical Applications and Temporal Shifts
|
| 42 |
+
|
| 43 |
+
Discuss real-world applications over time, noting how findings have influenced industry practices or technology adoption.
|
| 44 |
+
Highlight evolving practical use cases and how implementation considerations have changed with advances in research.
|
| 45 |
+
Research Gaps and Future Directions
|
| 46 |
+
|
| 47 |
+
Identify limitations in studies across time periods, noting any improvement or persistent gaps.
|
| 48 |
+
Point out unexplored areas and suggest specific future research directions informed by chronological developments in the field.
|
| 49 |
+
Formatting and Style:
|
| 50 |
+
|
| 51 |
+
Organize the summary with clear sections that reflect the temporal progression.
|
| 52 |
+
Maintain an academic tone, using specific examples, dates, and quotes where relevant.
|
| 53 |
+
Clearly identify and label sections to enhance readability, and acknowledge any limitations in the available context.
|
| 54 |
+
"""
|
| 55 |
+
|
| 56 |
+
self.papers = None
|
| 57 |
+
self.search_agent_response = ""
|
| 58 |
+
|
| 59 |
+
def solve(self, query):
|
| 60 |
+
|
| 61 |
+
# Load vector store
|
| 62 |
+
vector_db = FAISS.load_local("vector_db", embeddings, index_name="base_and_adjacent", allow_dangerous_deserialization=True)
|
| 63 |
+
|
| 64 |
+
# Get chat history
|
| 65 |
+
chat_history = st.session_state.get("chat_history", [])
|
| 66 |
+
chat_history_text = "\n".join([f"{sender}: {msg}" for sender, msg in chat_history[-5:]])
|
| 67 |
+
|
| 68 |
+
# Get relevant chunks from all papers
|
| 69 |
+
retrieved = vector_db.as_retriever(
|
| 70 |
+
search_kwargs={"k": 10} # Increase number of chunks to get broader context
|
| 71 |
+
).get_relevant_documents(query)
|
| 72 |
+
|
| 73 |
+
# Organize context by paper
|
| 74 |
+
context = self._organize_context(retrieved)
|
| 75 |
+
|
| 76 |
+
# Generate summary
|
| 77 |
+
full_prompt = self.prompt.format(
|
| 78 |
+
chat_history=chat_history_text,
|
| 79 |
+
context=context
|
| 80 |
+
)
|
| 81 |
+
|
| 82 |
+
response = self.model.generate_content(str(self.search_agent_response) + full_prompt)
|
| 83 |
+
return response.text, self.papers
|
| 84 |
+
|
| 85 |
+
def _organize_context(self, documents):
|
| 86 |
+
"""
|
| 87 |
+
Organizes retrieved chunks by paper and creates a structured context.
|
| 88 |
+
"""
|
| 89 |
+
# Group chunks by paper
|
| 90 |
+
paper_chunks = {}
|
| 91 |
+
for doc in documents:
|
| 92 |
+
paper_id = doc.metadata.get('source', 'unknown')
|
| 93 |
+
if paper_id not in paper_chunks:
|
| 94 |
+
paper_chunks[paper_id] = []
|
| 95 |
+
paper_chunks[paper_id].append(doc.page_content)
|
| 96 |
+
|
| 97 |
+
# Create structured context
|
| 98 |
+
organized_context = []
|
| 99 |
+
for paper_id, chunks in paper_chunks.items():
|
| 100 |
+
paper_context = f"\nPaper: {paper_id}\n"
|
| 101 |
+
paper_context += "\n".join(chunks)
|
| 102 |
+
organized_context.append(paper_context)
|
| 103 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 104 |
return "\n\n".join(organized_context)
|