File size: 7,099 Bytes
f866820
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Chain-of-thought reasoning for RAG synthesis.

Performs explicit reasoning over retrieved evidence.
"""

from dataclasses import dataclass
from typing import List, Dict, Any, Optional


@dataclass
class ReasoningResult:
    """Result of reasoning over evidence."""
    answer: str
    reasoning_steps: List[str]
    evidence_used: List[str]
    confidence: float
    reasoning_type: str


# Prompts for different reasoning types
SYNTHESIS_PROMPT = """Based on the evidence below, answer the query.
Show your reasoning step by step, then provide the final answer.

Query: {query}

Evidence:
{evidence}

First, analyze each piece of evidence and its relevance.
Then, synthesize the information to form a complete answer.
Finally, provide your answer with citations [ID:chunk_id].

Reasoning and Answer:"""

COMPARATIVE_PROMPT = """Compare the following based on the evidence provided.

Query: {query}

Evidence:
{evidence}

Structure your response as:
1. Key aspects of the first subject
2. Key aspects of the second subject
3. Similarities
4. Differences
5. Conclusion

Include citations [ID:chunk_id] for each claim.

Comparison:"""

ANALYTICAL_PROMPT = """Analyze and explain based on the evidence provided.

Query: {query}

Evidence:
{evidence}

Structure your response as:
1. Identify the main factors/causes
2. Explain the relationships between them
3. Draw conclusions
4. Note any limitations in the available evidence

Include citations [ID:chunk_id] for each claim.

Analysis:"""


def _format_evidence(chunks: List[Dict[str, Any]]) -> str:
    """Format chunks as numbered evidence."""
    evidence_parts = []
    for i, chunk in enumerate(chunks, 1):
        chunk_id = chunk.get("id", f"chunk_{i}")
        text = chunk.get("text", "")[:800]  # Limit length
        evidence_parts.append(f"[{chunk_id}]\n{text}")
    return "\n\n".join(evidence_parts)


def _extract_reasoning_steps(text: str) -> List[str]:
    """Extract reasoning steps from LLM response."""
    steps = []

    # Look for numbered steps
    import re
    numbered = re.findall(r'\d+\.\s*([^\n]+)', text)
    if numbered:
        steps.extend(numbered)

    # Look for bullet points
    bullets = re.findall(r'[-•]\s*([^\n]+)', text)
    if bullets:
        steps.extend(bullets)

    # If no structure found, split by sentences
    if not steps:
        sentences = re.split(r'(?<=[.!?])\s+', text)
        steps = [s.strip() for s in sentences[:5] if len(s) > 20]

    return steps


def _extract_evidence_ids(text: str) -> List[str]:
    """Extract cited evidence IDs from response."""
    import re
    # Match [ID:...] or ID:...
    ids = re.findall(r'\[?ID:([A-Za-z0-9_\-:.]+)\]?', text)
    return list(set(ids))


def reason_over_evidence(
    query: str,
    chunks: List[Dict[str, Any]],
    query_type: str = "factual",
    use_chain_of_thought: bool = True
) -> ReasoningResult:
    """
    Apply reasoning over retrieved evidence.

    Args:
        query: User query
        chunks: Retrieved and shaped chunks
        query_type: Type of query for prompt selection
        use_chain_of_thought: Whether to request explicit reasoning

    Returns:
        ReasoningResult with answer and reasoning chain
    """
    if not chunks:
        return ReasoningResult(
            answer="I don't have enough information to answer this question.",
            reasoning_steps=["No relevant evidence found"],
            evidence_used=[],
            confidence=0.0,
            reasoning_type="no_evidence"
        )

    try:
        from src.llm_providers import call_llm
    except ImportError:
        return ReasoningResult(
            answer="LLM not available for reasoning.",
            reasoning_steps=[],
            evidence_used=[],
            confidence=0.0,
            reasoning_type="error"
        )

    # Format evidence
    evidence = _format_evidence(chunks)

    # Select prompt based on query type
    if query_type == "comparative":
        prompt = COMPARATIVE_PROMPT.format(query=query, evidence=evidence)
        reasoning_type = "comparative"
    elif query_type == "analytical":
        prompt = ANALYTICAL_PROMPT.format(query=query, evidence=evidence)
        reasoning_type = "analytical"
    else:
        prompt = SYNTHESIS_PROMPT.format(query=query, evidence=evidence)
        reasoning_type = "synthesis"

    try:
        response = call_llm(prompt=prompt, temperature=0.0, max_tokens=800)
        text = response.get("text", "").strip()

        # Extract components
        reasoning_steps = _extract_reasoning_steps(text)
        evidence_ids = _extract_evidence_ids(text)

        # Estimate confidence based on evidence usage
        confidence = min(0.9, 0.3 + 0.1 * len(evidence_ids))

        return ReasoningResult(
            answer=text,
            reasoning_steps=reasoning_steps,
            evidence_used=evidence_ids,
            confidence=confidence,
            reasoning_type=reasoning_type
        )

    except Exception as e:
        return ReasoningResult(
            answer=f"Error during reasoning: {str(e)[:100]}",
            reasoning_steps=[],
            evidence_used=[],
            confidence=0.0,
            reasoning_type="error"
        )


def iterative_retrieve_and_reason(
    query: str,
    initial_chunks: List[Dict[str, Any]],
    retrieve_fn,
    max_iterations: int = 2
) -> ReasoningResult:
    """
    Iteratively retrieve more evidence based on reasoning.

    Args:
        query: Original query
        initial_chunks: First retrieval results
        retrieve_fn: Function to retrieve more chunks (takes query, returns chunks)
        max_iterations: Maximum retrieval iterations

    Returns:
        ReasoningResult after iterative refinement
    """
    all_chunks = list(initial_chunks)
    chunk_ids = {c.get("id") for c in all_chunks}

    try:
        from src.llm_providers import call_llm
    except ImportError:
        return reason_over_evidence(query, all_chunks)

    for i in range(max_iterations):
        # Check if we need more information
        evidence = _format_evidence(all_chunks)

        check_prompt = f"""Given this query and evidence, do we need more information?
If yes, suggest a follow-up search query. If no, respond with "SUFFICIENT".

Query: {query}

Current evidence:
{evidence[:2000]}

Response (either "SUFFICIENT" or a follow-up search query):"""

        response = call_llm(prompt=check_prompt, temperature=0.0, max_tokens=100)
        text = response.get("text", "").strip()

        if "SUFFICIENT" in text.upper():
            break

        # Retrieve more based on suggested query
        follow_up = text.replace("Follow-up query:", "").strip()
        if follow_up and len(follow_up) > 5:
            try:
                new_chunks = retrieve_fn(follow_up)
                for chunk in new_chunks:
                    if chunk.get("id") not in chunk_ids:
                        all_chunks.append(chunk)
                        chunk_ids.add(chunk.get("id"))
            except Exception:
                break

    return reason_over_evidence(query, all_chunks)