File size: 7,588 Bytes
40e5eae
 
5fd4bb2
40e5eae
5fd4bb2
40e5eae
 
5fd4bb2
40e5eae
 
5fd4bb2
 
40e5eae
 
 
 
 
 
 
 
 
 
 
5fd4bb2
40e5eae
 
5fd4bb2
40e5eae
 
 
 
5fd4bb2
 
40e5eae
 
5fd4bb2
40e5eae
5fd4bb2
 
 
 
40e5eae
5fd4bb2
 
 
40e5eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd4bb2
 
40e5eae
 
 
5fd4bb2
 
 
 
 
 
 
 
40e5eae
5fd4bb2
 
 
 
 
 
 
 
 
 
40e5eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5fd4bb2
 
40e5eae
 
5fd4bb2
 
40e5eae
5ab05da
5fd4bb2
 
40e5eae
5fd4bb2
40e5eae
 
5ab05da
 
5fd4bb2
5ab05da
 
 
 
 
 
 
40e5eae
5ab05da
 
 
40e5eae
 
5ab05da
40e5eae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
LLM Handler Module
Manages interaction with Hugging Face Inference API for answer generation
"""
import os
from typing import Generator, Dict, List
import logging
from huggingface_hub import InferenceClient

from config import (
    HF_MODEL,
    HF_TOKEN,
    SYSTEM_PROMPT,
    PROMPT_TEMPLATE,
)

# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)


class LLMHandler:
    """
    Handles LLM interactions using Hugging Face Inference API
    """
    
    def __init__(self, model: str = HF_MODEL, token: str = None):
        """
        Initialize the LLM handler
        
        Args:
            model: Name of the Hugging Face model to use
            token: HF API token (if not provided, will use HF_TOKEN from config)
        """
        self.model = model
        self.token = token or HF_TOKEN
        
        if not self.token:
            raise ValueError(
                "Hugging Face token not found. Please set HF_TOKEN environment variable "
                "or pass it to the constructor."
            )
        
        self.client = InferenceClient(token=self.token)
        logger.info(f"Initialized LLM handler with model: {model}")
    
    def generate_answer(
        self,
        question: str,
        context: str,
        stream: bool = False
    ) -> str:
        """
        Generate an answer based on the question and context
        
        Args:
            question: User's question
            context: Retrieved context from documents
            stream: Whether to stream the response
            
        Returns:
            Generated answer
        """
        # Format the prompt with system prompt, context, and question
        full_prompt = f"{SYSTEM_PROMPT}\n\n{PROMPT_TEMPLATE.format(context=context, question=question)}"
        
        try:
            if not stream:
                response = self.client.text_generation(
                    prompt=full_prompt,
                    model=self.model,
                    max_new_tokens=1024,
                    temperature=0.7,
                    top_p=0.95,
                    stream=False,
                )
                return response
            else:
                # Return generator for streaming
                return self.client.text_generation(
                    prompt=full_prompt,
                    model=self.model,
                    max_new_tokens=1024,
                    temperature=0.7,
                    top_p=0.95,
                    stream=True,
                )
        
        except Exception as e:
            logger.error(f"Error generating answer: {e}")
            raise
    
    def stream_answer(
        self,
        question: str,
        context: str
    ) -> Generator[str, None, None]:
        """
        Stream the answer generation token by token
        
        Args:
            question: User's question
            context: Retrieved context from documents
            
        Yields:
            Generated text tokens
        """
        # Format the prompt with system prompt, context, and question
        full_prompt = f"{SYSTEM_PROMPT}\n\n{PROMPT_TEMPLATE.format(context=context, question=question)}"
        
        try:
            stream = self.client.text_generation(
                prompt=full_prompt,
                model=self.model,
                max_new_tokens=512,
                temperature=0.7,
                top_p=0.95,
                stream=True,
                details=False,
            )
            
            # Collect tokens and yield them
            has_content = False
            for token in stream:
                if token:  # Only yield non-empty tokens
                    has_content = True
                    yield token
            
            # If no content was generated, yield a fallback message
            if not has_content:
                yield "I apologize, but I couldn't generate an answer based on the provided context. Please try rephrasing your question."
        
        except StopIteration:
            # Handle empty generator
            yield "I apologize, but I couldn't generate an answer. The model returned an empty response."
        except Exception as e:
            logger.error(f"Error streaming answer: {e}")
            yield f"\n\n❌ Error: {str(e)}"


def format_response(
    question: str,
    answer: str,
    sources: List[Dict]
) -> str:
    """
    Format the final response with question, answer, and sources
    
    Args:
        question: User's question
        answer: Generated answer
        sources: List of source documents
        
    Returns:
        Formatted response in markdown
    """
    # Create response header
    response_parts = [
        f"**Question:** {question}\n",
        f"**Answer:** {answer}\n",
    ]
    
    # Add sources section
    if sources:
        response_parts.append("\n**Sources:**\n")
        
        # Group sources by document
        sources_by_doc = {}
        for source in sources:
            doc_name = source["source"]
            if doc_name not in sources_by_doc:
                sources_by_doc[doc_name] = []
            sources_by_doc[doc_name].append(source)
        
        # Format sources
        for doc_name, doc_sources in sources_by_doc.items():
            chunks = ", ".join([s["chunk_id"] for s in doc_sources])
            avg_similarity = sum(s["similarity"] for s in doc_sources) / len(doc_sources)
            response_parts.append(
                f"- {doc_name} (chunks: {chunks}, "
                f"relevance: {avg_similarity:.2%})\n"
            )
    
    return "".join(response_parts)


def stream_llm_answer(
    question: str,
    context: str
) -> Generator[str, None, None]:
    """
    Stream answer generation for a question with context
    
    Args:
        question: User's question
        context: Retrieved context
        
    Yields:
        Generated text tokens
    """
    llm = LLMHandler()
    
    try:
        for token in llm.stream_answer(question, context):
            yield token
    except Exception as e:
        logger.error(f"Error in stream_llm_answer: {e}")
        yield f"\n\n❌ Error generating answer: {str(e)}"


def generate_answer(
    question: str,
    context: str
) -> str:
    """
    Generate a complete answer for a question with context
    
    Args:
        question: User's question
        context: Retrieved context
        
    Returns:
        Generated answer
    """
    llm = LLMHandler()
    
    try:
        answer = llm.generate_answer(question, context, stream=False)
        return answer
    except Exception as e:
        logger.error(f"Error generating answer: {e}")
        return f"❌ Error generating answer: {str(e)}"


if __name__ == "__main__":
    # Test the LLM handler
    logger.info("Testing LLM handler...")
    
    # Create LLM instance
    llm = LLMHandler()
    
    # Test answer generation
    test_question = "What is Python?"
    test_context = "Python is a high-level programming language known for its simplicity and readability."
    
    logger.info(f"\nTest Question: {test_question}")
    logger.info(f"Context: {test_context}\n")
    
    # Test streaming
    logger.info("Streaming answer:")
    for token in llm.stream_answer(test_question, test_context):
        print(token, end='', flush=True)
    print("\n")
    
    # Test non-streaming
    logger.info("\nGenerating complete answer:")
    answer = llm.generate_answer(test_question, test_context)
    logger.info(f"Answer: {answer}")