File size: 11,243 Bytes
407b660
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import logging
from typing import Dict, List, Optional
from langchain_groq import ChatGroq
from langchain.callbacks import StreamingStdOutCallbackHandler
from langchain.callbacks.manager import CallbackManager

# Configure Logging
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

class ThemeSynthesizer:
    """A service for extracting themes and insights from document collections using Groq Llama models."""

    # Class constants for different models available on Groq
    DEFAULT_MODELS = {
        "small": "llama3-8b-8192",
        "medium": "llama3-70b-8192",
        "large": "llama3-70b-8192",
        "mixtral": "mixtral-8x7b-32768",
        "gemma": "gemma-7b-it"
    }

    # Template for individual document analysis
    DOCUMENT_ANALYSIS_TEMPLATE = """
    You are an expert document analyst focused on precise extraction.
    Given the following document content, extract relevant information in response to this query:

    Query: {query}

    DOCUMENT CONTENT:
    {content}

    Your task:
    1. Extract the most relevant information that answers the query
    2. Be concise yet complete in your extraction
    3. Include EXACT citations by page number and paragraph (e.g., "Page 3, Page 2")
    4. Only provide information that is directly mentioned in the document
    5. If no relevant information exists, state "No relevant information found"

    Your answer format must be:
    EXTRACTED ANSWER: [Your concise answer here]
    CITATION: [Exact page and paragraph reference]
    """

    # Template for theme synthesis across documents
    THEME_SYNTHESIS_TEMPLATE = """
    You are the expert research analyst synthesizing findings across multiple documents.
    Below are answers extracted from different documents in response to a specific query.

    Query: {query}

    DOCUMENT ANSWERS:
    {document_answers}

    Your task:
    1. Identify 2-6 key themes or patterns across these documents
    2. For each theme, provide a clear title and brief explanation
    3. Be concise yet complete in your extraction
    4. Mention specific document IDs (e.g, DOC001) that support each theme
    5. Format your response as a chat message that synthesizes the information

    Format your response as follows:

    Theme 1 – [Theme Title]:
    Documents ([Document IDs]) [brief explanation with citations]

    Theme 2 – [Theme Title]:
    Documents ([Document IDs]) [brief explanation with citations]

    ...and so on for each theme.
    """

    def __init__(
        self,
        model_size: str = "small",
        custom_model_name: Optional[str] = None,
        temperature: float = 0.2,
        max_tokens: int = 8192,
        streaming: bool = False,
        api_key: Optional[str] = None
    ):
        """Initialize the document analyzer with configurable parameters."""
        # Check for API key
        if not api_key:
            raise ValueError("Groq API key is required. Get one from https://console.groq.com/")

        # Set up callbacks for streaming if enabled
        callbacks = None
        if streaming:
            callbacks = CallbackManager([StreamingStdOutCallbackHandler()])

        # Select model based on size or custom name
        model_name = custom_model_name or self.DEFAULT_MODELS.get(model_size, self.DEFAULT_MODELS["small"])

        # Initialize the Groq language model
        try:
            self.llm = ChatGroq(
                groq_api_key=api_key,
                model_name=model_name,
                temperature=temperature,
                max_tokens=max_tokens,
                streaming=streaming,
                callbacks=callbacks
            )
            logger.info(f"Initialized Groq model: {model_name}")
        except Exception as e:
            logger.error(f"Failed to initialize Groq model: {e}")
            raise ValueError(f"Failed to initialize Groq model: {e}")

        # Store configuration
        self.config = {
            "model": model_name,
            "temperature": temperature,
            "max_tokens": max_tokens,
            "provider": "groq"
        }

    def analyze_single_document(
        self,
        document: Dict,
        query: str
    ) -> Dict:
        """
        Process a single document to extract relevant information for the query.

        Args:
            document: Document dictionary with 'content' and 'metadata'
            query: User query to answer

        Returns:
            Dictionary with extracted answer and citation
        """
        try:
            doc_id = document.get('metadata', {}).get('doc_id', 'Unknown')
            content = document.get('content', '')

            if not content.strip():
                return {
                    "doc_id": doc_id,
                    "extracted_answer": "No content available in document",
                    "citation": "N/A",
                    "status": "error"
                }

            logger.info(f"Analyzing document {doc_id} for query: {query[:50]}...")

            # Format the prompt with query and content
            formatted_prompt = self.DOCUMENT_ANALYSIS_TEMPLATE.format(
                query=query,
                content=content
            )

            # Run document analysis directly with the LLM
            response = self.llm.invoke(formatted_prompt)

            # Extract the result from the response
            if hasattr(response, 'content'):
                response = response.content
            elif isinstance(response, dict) and "text" in response:
                response = response["text"]

            # Parse response to extract answer and citation
            extracted_answer = "No relevant information found"
            citation = "N/A"

            # Simple parsing - can be made more robust
            if "EXTRACTED ANSWER:" in response:
                answer_parts = response.split("EXTRACTED ANSWER:")
                if len(answer_parts) > 1:
                    citation_parts = answer_parts[1].split("CITATION:")
                    extracted_answer = citation_parts[0].strip()
                    if len(citation_parts) > 1:
                        citation = citation_parts[1].strip()

            return {
                "doc_id": doc_id,
                "extracted_answer": extracted_answer,
                "citation": citation,
                "status": "success"
            }

        except Exception as e:
            logger.error(f"Error analyzing document: {e}", exc_info=True)
            return {
                "doc_id": document.get('metadata', {}).get('doc_id', 'Unknown'),
                "extracted_answer": f"Error during analysis: {str(e)}",
                "citation": "N/A",
                "status": "error"
            }

    def batch_analyze_documents(
            self,
            documents: List[Dict],
            query: str
    ) -> List[Dict]:
        """Process a batch of documents to extract relevant information for the query.

        Args:
            documents: List of document dictionaries with 'content' and 'metadata'
            query: User query to answer

        Returns:
            List of dictionaries with extracted answers and citations.

        """
        try:
            results = []
            for doc in documents:
                # Ensure Document has proper Id
                if 'metadata' not in doc:
                    doc['metadata'] = {}
                if 'doc_id' not in doc['metadata']:
                    doc['metadata']['doc_id'] = f"DOC{len(results)+1:03d}"

                # Analyze document
                result = self.analyze_single_document(doc, query)
                results.append(result)
            return results
        except Exception as e:
            logger.error(f"Error batch analyzing documents: {e}", exc_info=True)
            return []

    def synthesize_themes(
        self,
        document_results: List[Dict],
        query: str
    ) -> Dict:
        """
        Synthesize themes from individual document analysis results.

        Args:
            document_results: Results from batch_analyze_documents
            query: Original user query

        Returns:
            Dictionary with synthesized themes and status
        """
        try:
            if not document_results:
                return {
                    "themes": "No documents provided for analysis.",
                    "status": "warning"
                }

            # Format document answers for the synthesis prompt
            formatted_answers = []
            for res in document_results:
                doc_id = res.get("doc_id", "Unknown")
                answer = res.get("extracted_answer", "No answer available")
                citation = res.get("citation", "N/A")

                formatted_answer = f"Document ID: {doc_id}\nExtracted Answer: {answer}\nCitation: {citation}"
                formatted_answers.append(formatted_answer)

            combined_answers = "\n\n".join(formatted_answers)

            # Format the prompt with query and document answers
            formatted_prompt = self.THEME_SYNTHESIS_TEMPLATE.format(
                query=query,
                document_answers=combined_answers
            )

            # Run theme synthesis directly with the LLM
            response = self.llm.invoke(formatted_prompt)

            # Extract the result from the response
            if hasattr(response, 'content'):
                response = response.content
            elif isinstance(response, dict) and "text" in response:
                response = response["text"]

            return {
                "themes": response.strip(),
                "status": "success",
                "stats": {
                    "document_count": len(document_results),
                    "query": query,
                    "model": self.config["model"]
                }
            }

        except Exception as e:
            logger.error(f"Error synthesizing themes: {e}", exc_info=True)
            return {
                "themes": f"Error during theme synthesis: {str(e)}",
                "status": "error",
                "error": str(e)
            }

    def process_query(
        self,
        documents: List[Dict],
        query: str
    ) -> Dict:
        """
        Complete document processing pipeline - analyze documents and synthesize themes.

        Args:
            documents: List of document dictionaries
            query: User query to answer

        Returns:
            Dictionary with individual document results and synthesized themes
        """
        # Step 1: Analyze each document individually
        document_results = self.batch_analyze_documents(documents, query)

        # Step 2: Synthesize themes across documents
        theme_results = self.synthesize_themes(document_results, query)

        # Return complete results
        return {
            "document_results": document_results,
            "themes": theme_results.get("themes", ""),
            "status": "success" if theme_results.get("status") == "success" else "partial",
            "query": query,
            "model_info": self.config
        }