File size: 7,713 Bytes
1a27ebc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# ============================================================================

import asyncio
import logging
import time
from typing import List, Dict, Any, Optional
import openai
import os
from openai import AsyncOpenAI

logger = logging.getLogger(__name__)

class LLMService:
    """OpenAI GPT service for intelligent question answering"""
    
    def __init__(self):
        api_key = os.getenv('OPENAI_API_KEY')
        if not api_key:
            logger.error("❌ OPENAI_API_KEY not found in environment")
            raise ValueError("OpenAI API key is required")
        
        self.client = AsyncOpenAI(api_key=api_key)
        self.model = "gpt-4"
        self.fallback_model = "gpt-3.5-turbo"
        self.max_tokens = 500
        self.temperature = 0.1  # Low temperature for consistent, factual responses
        
        logger.info("✅ LLM service initialized")
    
    async def answer_question(
        self, 
        question: str, 
        context_chunks: List[Dict], 
        document_url: str
    ) -> Dict[str, Any]:
        """Generate answer using GPT with context"""
        start_time = time.time()
        
        try:
            # Prepare context from relevant chunks
            context = self._prepare_context(context_chunks)
            
            # Create prompt
            prompt = self._create_prompt(question, context, document_url)
            
            # Try GPT-4 first, fallback to GPT-3.5
            try:
                response = await self._make_llm_request(prompt, self.model)
            except Exception as e:
                logger.warning(f"⚠️ GPT-4 failed, trying fallback: {e}")
                response = await self._make_llm_request(prompt, self.fallback_model)
            
            processing_time = time.time() - start_time
            
            # Extract answer and validate
            answer = response.choices[0].message.content.strip()
            
            # Calculate confidence based on response quality
            confidence = self._calculate_confidence(
                answer, context_chunks, response.usage
            )
            
            return {
                "answer": answer,
                "confidence": confidence,
                "model": response.model,
                "processing_time": processing_time,
                "token_usage": {
                    "prompt_tokens": response.usage.prompt_tokens,
                    "completion_tokens": response.usage.completion_tokens,
                    "total_tokens": response.usage.total_tokens
                },
                "context_chunks_used": len(context_chunks)
            }
            
        except Exception as e:
            logger.error(f"❌ LLM request failed: {e}")
            return {
                "answer": f"I apologize, but I encountered an error while processing your question: {str(e)}",
                "confidence": 0.0,
                "model": "error",
                "processing_time": time.time() - start_time,
                "error": str(e)
            }
    
    async def _make_llm_request(self, prompt: str, model: str) -> Any:
        """Make request to OpenAI API with retries"""
        max_retries = 3
        base_delay = 1
        
        for attempt in range(max_retries):
            try:
                response = await self.client.chat.completions.create(
                    model=model,
                    messages=[
                        {
                            "role": "system",
                            "content": "You are a helpful AI assistant that answers questions based on provided context. Always provide accurate, specific answers based on the given information. If the information is not in the context, clearly state that."
                        },
                        {
                            "role": "user", 
                            "content": prompt
                        }
                    ],
                    max_tokens=self.max_tokens,
                    temperature=self.temperature,
                    top_p=0.9,
                    frequency_penalty=0.0,
                    presence_penalty=0.0
                )
                return response
                
            except openai.RateLimitError as e:
                if attempt < max_retries - 1:
                    delay = base_delay * (2 ** attempt)
                    logger.warning(f"⚠️ Rate limit hit, waiting {delay}s...")
                    await asyncio.sleep(delay)
                    continue
                raise e
                
            except Exception as e:
                if attempt < max_retries - 1:
                    delay = base_delay * (2 ** attempt)
                    logger.warning(f"⚠️ Request failed (attempt {attempt + 1}), retrying in {delay}s: {e}")
                    await asyncio.sleep(delay)
                    continue
                raise e
        
        raise Exception("All retry attempts failed")
    
    def _prepare_context(self, context_chunks: List[Dict]) -> str:
        """Prepare context from relevant chunks"""
        if not context_chunks:
            return "No relevant context found."
        
        context_parts = []
        for i, chunk in enumerate(context_chunks[:5], 1):  # Limit to top 5 chunks
            similarity = chunk.get('similarity', 0.0)
            content = chunk.get('content', '').strip()
            
            if content:
                context_parts.append(f"[Context {i}] (Relevance: {similarity:.2f})\n{content}\n")
        
        return "\n".join(context_parts)
    
    def _create_prompt(self, question: str, context: str, document_url: str) -> str:
        """Create optimized prompt for question answering"""
        prompt = f"""Based on the following context from a document, please answer the question accurately and specifically.

CONTEXT:
{context}

QUESTION: {question}

INSTRUCTIONS:
1. Answer based ONLY on the provided context
2. Be specific and include relevant details from the context
3. If the answer is not in the context, clearly state "The provided context does not contain information to answer this question"
4. For numerical values, dates, or specific terms, quote them exactly as they appear in the context
5. Keep your answer concise but complete
6. If there are conditions or exceptions mentioned in the context, include them in your answer

ANSWER:"""
        
        return prompt
    
    def _calculate_confidence(
        self, 
        answer: str, 
        context_chunks: List[Dict], 
        usage: Any
    ) -> float:
        """Calculate confidence score for the answer"""
        base_confidence = 0.5
        
        # Boost confidence if we have good context
        if context_chunks:
            avg_similarity = sum(chunk.get('similarity', 0) for chunk in context_chunks) / len(context_chunks)
            base_confidence += avg_similarity * 0.3
        
        # Boost confidence for longer, more detailed answers
        if len(answer) > 50:
            base_confidence += 0.1
        if len(answer) > 100:
            base_confidence += 0.1
        
        # Reduce confidence for uncertain phrases
        uncertain_phrases = [
            "not sure", "don't know", "unclear", "might be", "could be",
            "not contain information", "not in the context"
        ]
        
        for phrase in uncertain_phrases:
            if phrase in answer.lower():
                base_confidence -= 0.3
                break
        
        # Ensure confidence is within bounds
        return max(0.0, min(1.0, base_confidence))

# ============================================================================