File size: 11,522 Bytes
d520909
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Grounded Answer Generator

Generates answers from retrieved context with citations.
Uses local LLMs (Ollama) or cloud APIs.
"""

from typing import List, Optional, Dict, Any, Tuple
from pydantic import BaseModel, Field
from loguru import logger
import json
import re

from .retriever import RetrievedChunk, DocumentRetriever, get_document_retriever

try:
    import httpx
    HTTPX_AVAILABLE = True
except ImportError:
    HTTPX_AVAILABLE = False


class GeneratorConfig(BaseModel):
    """Configuration for grounded generator."""
    # LLM settings
    llm_provider: str = Field(
        default="ollama",
        description="LLM provider: ollama, openai"
    )
    ollama_base_url: str = Field(
        default="http://localhost:11434",
        description="Ollama API base URL"
    )
    ollama_model: str = Field(
        default="llama3.2:3b",
        description="Ollama model for generation"
    )

    # OpenAI settings
    openai_model: str = Field(
        default="gpt-4o-mini",
        description="OpenAI model for generation"
    )
    openai_api_key: Optional[str] = Field(
        default=None,
        description="OpenAI API key"
    )

    # Generation settings
    temperature: float = Field(default=0.1, ge=0.0, le=2.0)
    max_tokens: int = Field(default=1024, ge=1)
    timeout: float = Field(default=120.0, ge=1.0)

    # Citation settings
    require_citations: bool = Field(
        default=True,
        description="Require citations in answers"
    )
    citation_format: str = Field(
        default="[{index}]",
        description="Citation format template"
    )
    abstain_on_low_confidence: bool = Field(
        default=True,
        description="Abstain when confidence is low"
    )
    confidence_threshold: float = Field(
        default=0.6,
        ge=0.0,
        le=1.0,
        description="Minimum confidence threshold"
    )


class Citation(BaseModel):
    """A citation reference."""
    index: int
    chunk_id: str
    page: Optional[int] = None
    text_snippet: str
    confidence: float


class GeneratedAnswer(BaseModel):
    """Generated answer with citations."""
    answer: str
    citations: List[Citation]
    confidence: float
    abstained: bool = False
    abstain_reason: Optional[str] = None

    # Source information
    num_chunks_used: int
    query: str


class GroundedGenerator:
    """
    Generates grounded answers with citations.

    Features:
    - Uses retrieved chunks as context
    - Generates answers with inline citations
    - Confidence-based abstention
    - Support for Ollama and OpenAI
    """

    SYSTEM_PROMPT = """You are a precise document question-answering assistant.
Your task is to answer questions based ONLY on the provided context from documents.

Rules:
1. Only use information from the provided context
2. Cite your sources using [N] notation where N is the chunk number
3. If the context doesn't contain enough information, say "I cannot answer this based on the available context"
4. Be precise and concise
5. If information is uncertain or partial, indicate this clearly

Context format: Each chunk is numbered [1], [2], etc. with page numbers and content.
"""

    def __init__(
        self,
        config: Optional[GeneratorConfig] = None,
        retriever: Optional[DocumentRetriever] = None,
    ):
        """
        Initialize generator.

        Args:
            config: Generator configuration
            retriever: Document retriever instance
        """
        self.config = config or GeneratorConfig()
        self._retriever = retriever

    @property
    def retriever(self) -> DocumentRetriever:
        """Get retriever (lazy initialization)."""
        if self._retriever is None:
            self._retriever = get_document_retriever()
        return self._retriever

    def generate(
        self,
        query: str,
        chunks: List[RetrievedChunk],
        additional_context: Optional[str] = None,
    ) -> GeneratedAnswer:
        """
        Generate an answer from retrieved chunks.

        Args:
            query: User question
            chunks: Retrieved context chunks
            additional_context: Optional additional context

        Returns:
            GeneratedAnswer with citations
        """
        # Check if we should abstain
        if self.config.abstain_on_low_confidence and chunks:
            avg_confidence = sum(c.similarity for c in chunks) / len(chunks)
            if avg_confidence < self.config.confidence_threshold:
                return GeneratedAnswer(
                    answer="I cannot provide a confident answer based on the available context.",
                    citations=[],
                    confidence=avg_confidence,
                    abstained=True,
                    abstain_reason=f"Average confidence ({avg_confidence:.2f}) below threshold ({self.config.confidence_threshold})",
                    num_chunks_used=len(chunks),
                    query=query,
                )

        # Build context
        context = self._build_context(chunks, additional_context)

        # Build prompt
        prompt = self._build_prompt(query, context)

        # Generate answer
        if self.config.llm_provider == "ollama":
            raw_answer = self._generate_ollama(prompt)
        elif self.config.llm_provider == "openai":
            raw_answer = self._generate_openai(prompt)
        else:
            raise ValueError(f"Unknown LLM provider: {self.config.llm_provider}")

        # Parse citations from answer
        citations = self._extract_citations(raw_answer, chunks)

        # Calculate confidence
        if citations:
            confidence = sum(c.confidence for c in citations) / len(citations)
        elif chunks:
            confidence = sum(c.similarity for c in chunks) / len(chunks)
        else:
            confidence = 0.0

        return GeneratedAnswer(
            answer=raw_answer,
            citations=citations,
            confidence=confidence,
            abstained=False,
            num_chunks_used=len(chunks),
            query=query,
        )

    def answer_question(
        self,
        query: str,
        top_k: int = 5,
        filters: Optional[Dict[str, Any]] = None,
    ) -> GeneratedAnswer:
        """
        Retrieve context and generate answer.

        Args:
            query: User question
            top_k: Number of chunks to retrieve
            filters: Optional retrieval filters

        Returns:
            GeneratedAnswer with citations
        """
        # Retrieve relevant chunks
        chunks = self.retriever.retrieve(query, top_k=top_k, filters=filters)

        if not chunks:
            return GeneratedAnswer(
                answer="I could not find any relevant information in the documents to answer this question.",
                citations=[],
                confidence=0.0,
                abstained=True,
                abstain_reason="No relevant chunks found",
                num_chunks_used=0,
                query=query,
            )

        return self.generate(query, chunks)

    def _build_context(
        self,
        chunks: List[RetrievedChunk],
        additional_context: Optional[str] = None,
    ) -> str:
        """Build context string from chunks."""
        parts = []

        if additional_context:
            parts.append(f"Additional context:\n{additional_context}\n")

        parts.append("Document excerpts:")

        for i, chunk in enumerate(chunks, 1):
            header = f"\n[{i}]"
            if chunk.page is not None:
                header += f" (Page {chunk.page + 1}"
                if chunk.chunk_type:
                    header += f", {chunk.chunk_type}"
                header += ")"

            parts.append(f"{header}:")
            parts.append(chunk.text)

        return "\n".join(parts)

    def _build_prompt(self, query: str, context: str) -> str:
        """Build the full prompt."""
        return f"""Based on the following context, answer the question.

{context}

Question: {query}

Answer (cite sources using [N] notation):"""

    def _generate_ollama(self, prompt: str) -> str:
        """Generate using Ollama."""
        if not HTTPX_AVAILABLE:
            raise ImportError("httpx required for Ollama")

        with httpx.Client(timeout=self.config.timeout) as client:
            response = client.post(
                f"{self.config.ollama_base_url}/api/generate",
                json={
                    "model": self.config.ollama_model,
                    "prompt": prompt,
                    "system": self.SYSTEM_PROMPT,
                    "stream": False,
                    "options": {
                        "temperature": self.config.temperature,
                        "num_predict": self.config.max_tokens,
                    },
                },
            )
            response.raise_for_status()
            result = response.json()

        return result.get("response", "").strip()

    def _generate_openai(self, prompt: str) -> str:
        """Generate using OpenAI."""
        try:
            import openai
        except ImportError:
            raise ImportError("openai package required")

        client = openai.OpenAI(api_key=self.config.openai_api_key)

        response = client.chat.completions.create(
            model=self.config.openai_model,
            messages=[
                {"role": "system", "content": self.SYSTEM_PROMPT},
                {"role": "user", "content": prompt},
            ],
            temperature=self.config.temperature,
            max_tokens=self.config.max_tokens,
        )

        return response.choices[0].message.content.strip()

    def _extract_citations(
        self,
        answer: str,
        chunks: List[RetrievedChunk],
    ) -> List[Citation]:
        """Extract citations from answer text."""
        citations = []
        seen_indices = set()

        # Find citation patterns like [1], [2], etc.
        pattern = r'\[(\d+)\]'
        matches = re.findall(pattern, answer)

        for match in matches:
            index = int(match)
            if index in seen_indices:
                continue
            if index < 1 or index > len(chunks):
                continue

            seen_indices.add(index)
            chunk = chunks[index - 1]

            citation = Citation(
                index=index,
                chunk_id=chunk.chunk_id,
                page=chunk.page,
                text_snippet=chunk.text[:150] + ("..." if len(chunk.text) > 150 else ""),
                confidence=chunk.similarity,
            )
            citations.append(citation)

        return sorted(citations, key=lambda c: c.index)


# Global instance and factory
_grounded_generator: Optional[GroundedGenerator] = None


def get_grounded_generator(
    config: Optional[GeneratorConfig] = None,
    retriever: Optional[DocumentRetriever] = None,
) -> GroundedGenerator:
    """
    Get or create singleton grounded generator.

    Args:
        config: Generator configuration
        retriever: Optional retriever instance

    Returns:
        GroundedGenerator instance
    """
    global _grounded_generator

    if _grounded_generator is None:
        _grounded_generator = GroundedGenerator(
            config=config,
            retriever=retriever,
        )

    return _grounded_generator


def reset_grounded_generator():
    """Reset the global generator instance."""
    global _grounded_generator
    _grounded_generator = None