Spaces:
Sleeping
Sleeping
File size: 21,537 Bytes
e0aa230 |
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 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
"""
Response Generator Module
This module is responsible for generating coherent responses based on
retrieved knowledge using LangChain RAG.
Technology: LangChain RAG (Retrieval Augmented Generation)
"""
import logging
import time
import os
from typing import Dict, List, Any, Optional
from datetime import datetime
class ResponseGenerator:
"""
Generates coherent responses based on retrieved knowledge.
Features:
- Context-aware response generation
- Source attribution and confidence scoring
- Multiple LLM provider support (Gemini, OpenAI)
- Response quality assessment
- Template-based fallback generation
"""
def __init__(self, config: Optional[Dict[str, Any]] = None):
"""
Initialize the ResponseGenerator with configuration.
Args:
config: Configuration dictionary with generation parameters
"""
self.config = config or {}
self.logger = logging.getLogger(__name__)
# Configuration settings
self.model = self.config.get("model", "gpt-3.5-turbo")
self.max_tokens = self.config.get("max_tokens", 500)
self.temperature = self.config.get("temperature", 0.7)
self.include_sources = self.config.get("include_sources", True)
# Initialize LLM providers
self.llm = None
self.gemini_client = None
self.openai_client = None
self._initialize_llm_providers()
# Response templates with markdown formatting
self.response_templates = {
"no_context": "## ℹ️ No Information Available\n\nI don't have enough information to answer your question. Please try:\n\n- **Uploading relevant documents** using the Upload tab\n- **Adding URLs** using the Add URLs tab\n- **Enabling live search** for real-time web results",
"error": "## ⚠️ Error Occurred\n\nI encountered an error while generating the response. Please try again.\n\nIf the problem persists, check your API keys in the Settings tab.",
"insufficient_confidence": "## 🤔 Limited Confidence\n\nBased on the available information, I found some relevant content, but I'm **not confident enough** to provide a definitive answer.\n\n**Suggestions:**\n- Try rephrasing your question\n- Add more specific documents\n- Enable live search for additional context",
}
self.logger.info("ResponseGenerator initialized with advanced features")
def _initialize_llm_providers(self):
"""Initialize available LLM providers with optimization."""
try:
# Try to initialize Gemini
gemini_api_key = os.getenv("GEMINI_API_KEY")
if gemini_api_key:
try:
import google.generativeai as genai
# Check if settings manager has already initialized Gemini client
# This is an optimization to avoid recreating the client
from utils.settings_manager import SettingsManager
if (
hasattr(SettingsManager, "_gemini_client_cache")
and SettingsManager._gemini_client_cache is not None
and SettingsManager._gemini_client_key == gemini_api_key
):
self.logger.info(
"Reusing existing Gemini client from settings manager"
)
genai_client = SettingsManager._gemini_client_cache
else:
# Configure new client
genai.configure(api_key=gemini_api_key)
genai_client = genai
# Create model instance
self.gemini_client = genai_client.GenerativeModel(
"gemini-2.5-flash-preview-05-20"
)
self.logger.info("Gemini client initialized")
except ImportError:
self.logger.warning("Gemini SDK not available")
except Exception as e:
self.logger.warning(f"Failed to initialize Gemini: {e}")
# Try to initialize OpenAI
openai_api_key = os.getenv("OPENAI_API_KEY")
if openai_api_key:
try:
import openai
self.openai_client = openai.OpenAI(api_key=openai_api_key)
self.logger.info("OpenAI client initialized")
except ImportError:
self.logger.warning("OpenAI SDK not available")
except Exception as e:
self.logger.warning(f"Failed to initialize OpenAI: {e}")
# Try to initialize LangChain
try:
if self.gemini_client:
from langchain_google_genai import ChatGoogleGenerativeAI
self.llm = ChatGoogleGenerativeAI(
model="gemini-2.5-flash-preview-05-20",
temperature=self.temperature,
google_api_key=gemini_api_key,
)
elif self.openai_client:
from langchain_openai import ChatOpenAI
self.llm = ChatOpenAI(
model=self.model,
temperature=self.temperature,
max_tokens=self.max_tokens,
openai_api_key=openai_api_key,
)
if self.llm:
self.logger.info("LangChain LLM initialized")
except ImportError:
self.logger.warning("LangChain not available")
except Exception as e:
self.logger.warning(f"Failed to initialize LangChain: {e}")
except Exception as e:
self.logger.error(f"❌ Error initializing LLM providers: {e}")
def generate_response(
self, query: str, context: List[Dict[str, Any]]
) -> Dict[str, Any]:
"""
Generate a response based on the query and retrieved context.
Args:
query: Original user query
context: List of retrieved context items with text and metadata
Returns:
Dictionary containing the generated response and metadata
"""
if not query:
return {
"response": "I need a question to answer.",
"sources": [],
"confidence": 0.0,
"error": "No query provided",
}
if not context:
return {
"response": self.response_templates["no_context"],
"sources": [],
"confidence": 0.0,
"error": "No context available",
}
self.logger.info(f"Generating response for query: {query[:100]}...")
start_time = time.time()
try:
# Prepare context for generation
formatted_context = self._format_context(context)
# Calculate initial confidence based on context quality
base_confidence = self._calculate_confidence(context)
# Generate response using available LLM
response_result = self._generate_with_llm(query, formatted_context)
if not response_result["success"]:
# Fallback to template-based generation
response_result = self._fallback_generation(query, formatted_context)
# Extract sources from context
sources = self._extract_sources(context) if self.include_sources else []
# Assess response quality
quality_score = self._assess_response_quality(
response_result["response"], query, context
)
# Calculate final confidence
final_confidence = min(base_confidence * quality_score, 1.0)
# Check if confidence is too low
if final_confidence < 0.3:
response_text = self.response_templates["insufficient_confidence"]
final_confidence = 0.2
else:
response_text = response_result["response"]
result = {
"response": response_text,
"sources": sources,
"confidence": final_confidence,
"context_items": len(context),
"generation_time": time.time() - start_time,
"model_used": response_result.get("model", "fallback"),
"quality_score": quality_score,
}
self.logger.info(f"Response generated in {result['generation_time']:.2f}s")
return result
except Exception as e:
self.logger.error(f"❌ Error generating response: {str(e)}")
return {
"response": self.response_templates["error"],
"sources": [],
"confidence": 0.0,
"error": str(e),
"generation_time": time.time() - start_time,
}
def _generate_with_llm(self, query: str, context: str) -> Dict[str, Any]:
"""
Generate response using available LLM providers.
Args:
query: User query
context: Formatted context string
Returns:
Dictionary with generation result
"""
# Create RAG prompt
prompt = self._create_rag_prompt(query, context)
# Try LangChain first
if self.llm:
try:
from langchain.schema import HumanMessage
messages = [HumanMessage(content=prompt)]
response = self.llm.invoke(messages)
return {
"success": True,
"response": response.content,
"model": "langchain",
}
except Exception as e:
self.logger.warning(f"LangChain generation failed: {e}")
# Try Gemini directly
if self.gemini_client:
try:
response = self.gemini_client.generate_content(prompt)
return {
"success": True,
"response": response.text,
"model": "gemini-2.5-flash-preview-05-20",
}
except Exception as e:
self.logger.warning(f"Gemini generation failed: {e}")
# Try OpenAI directly
if self.openai_client:
try:
response = self.openai_client.chat.completions.create(
model=self.model,
messages=[{"role": "user", "content": prompt}],
max_tokens=self.max_tokens,
temperature=self.temperature,
)
return {
"success": True,
"response": response.choices[0].message.content,
"model": self.model,
}
except Exception as e:
self.logger.warning(f"OpenAI generation failed: {e}")
return {"success": False, "response": "", "model": "none"}
def _create_rag_prompt(self, query: str, context: str) -> str:
"""
Create an enhanced prompt template for RAG generation with markdown formatting.
Args:
query: User query
context: Formatted context
Returns:
Formatted prompt string
"""
prompt = f"""You are an AI assistant that answers questions based on provided context. Follow these guidelines:
1. Answer the question using ONLY the information provided in the context
2. If the context doesn't contain enough information, clearly state this
3. Cite specific sources when making claims
4. Be concise but comprehensive
5. If multiple sources provide different information, acknowledge this
6. Use a professional and helpful tone
7. **Format your response in clean, readable Markdown**
Context Information:
{context}
Question: {query}
Instructions:
- Provide a clear, well-structured answer using **Markdown formatting**
- Use headers (##, ###) to organize sections
- Use **bold** for important points
- Use bullet points (-) or numbered lists (1.) for clarity
- Use `code blocks` for technical terms or specific data
- Include relevant details from the context
- If uncertain, express the level of confidence
- Do not make up information not present in the context
Format your response in Markdown with proper structure and formatting.
Answer:"""
return prompt
def _fallback_generation(self, query: str, context: str) -> Dict[str, Any]:
"""
Fallback response generation when LLM is not available.
Args:
query: User query
context: Formatted context
Returns:
Dictionary with generation result
"""
self.logger.info("Using fallback generation")
# Extract key information from context
context_lines = context.split("\n")
relevant_lines = [
line.strip()
for line in context_lines
if line.strip() and not line.startswith("[Source:")
]
if not relevant_lines:
return {
"success": True,
"response": self.response_templates["no_context"],
"model": "fallback",
}
# Create a structured markdown response
response_parts = [
f"## Answer to: {query}",
"",
"Based on the available information:",
"",
]
# Add key information as markdown list
for i, line in enumerate(relevant_lines[:3]): # Limit to 3 most relevant
if len(line) > 50: # Only include substantial content
response_parts.append(f"- {line}")
response_parts.extend(
[
"",
"---",
"",
"**Note:** This response is generated using available context. For more detailed analysis, please ensure proper language model integration.",
]
)
response = "\n".join(response_parts)
return {
"success": True,
"response": response,
"model": "fallback",
}
def _format_context(self, context: List[Dict[str, Any]]) -> str:
"""
Format the retrieved context for use in response generation.
Args:
context: List of context items
Returns:
Formatted context string
"""
formatted_parts = []
for i, item in enumerate(context):
text = item.get("text", "")
source = item.get("source", f"Source {i+1}")
score = item.get("score", 0.0)
# Format each context item with metadata
formatted_part = f"""[Source {i+1}: {source} (Relevance: {score:.2f})]
{text}
---"""
formatted_parts.append(formatted_part)
return "\n\n".join(formatted_parts)
def _extract_sources(self, context: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
"""
Extract source information from context items.
Args:
context: List of context items
Returns:
List of source dictionaries
"""
sources = []
seen_sources = set()
for item in context:
source = item.get("source", "Unknown")
score = item.get("score", 0.0)
final_score = item.get("final_score", score)
if source not in seen_sources:
source_info = {
"source": source,
"relevance_score": round(score, 3),
"final_score": round(final_score, 3),
"metadata": item.get("metadata", {}),
}
# Add source type
if source.endswith(".pdf"):
source_info["type"] = "PDF Document"
elif source.startswith("http"):
source_info["type"] = "Web Page"
elif source.endswith((".docx", ".doc")):
source_info["type"] = "Word Document"
else:
source_info["type"] = "Document"
sources.append(source_info)
seen_sources.add(source)
# Sort by relevance score
sources.sort(key=lambda x: x["final_score"], reverse=True)
return sources
def _calculate_confidence(self, context: List[Dict[str, Any]]) -> float:
"""
Calculate confidence score based on context quality.
Args:
context: List of context items
Returns:
Confidence score between 0.0 and 1.0
"""
if not context:
return 0.0
# Calculate average similarity score
scores = [item.get("final_score", item.get("score", 0.0)) for item in context]
avg_score = sum(scores) / len(scores)
# Factor in the number of context items
context_factor = min(len(context) / 3.0, 1.0) # Normalize to max of 3 items
# Factor in score distribution (prefer consistent scores)
if len(scores) > 1:
score_variance = sum((s - avg_score) ** 2 for s in scores) / len(scores)
consistency_factor = max(0.5, 1.0 - score_variance)
else:
consistency_factor = 1.0
# Combine factors
confidence = (
(avg_score * 0.6) + (context_factor * 0.2) + (consistency_factor * 0.2)
)
return min(confidence, 1.0)
def _assess_response_quality(
self, response: str, query: str, context: List[Dict[str, Any]]
) -> float:
"""
Assess the quality of the generated response.
Args:
response: Generated response
query: Original query
context: Context used for generation
Returns:
Quality score between 0.0 and 1.0
"""
if not response or len(response.strip()) < 10:
return 0.1
quality_score = 0.5 # Base score
# Check response length (not too short, not too long)
response_length = len(response)
if 50 <= response_length <= 1000:
quality_score += 0.2
elif response_length > 1000:
quality_score += 0.1
# Check if response addresses the query
query_words = set(query.lower().split())
response_words = set(response.lower().split())
word_overlap = len(query_words.intersection(response_words))
if word_overlap > 0:
quality_score += min(word_overlap / len(query_words), 0.2)
# Check if response uses context information
context_texts = [item.get("text", "") for item in context]
context_words = set()
for text in context_texts:
context_words.update(text.lower().split())
context_usage = len(response_words.intersection(context_words))
if context_usage > 5: # Uses substantial context
quality_score += 0.1
return min(quality_score, 1.0)
def get_supported_models(self) -> List[str]:
"""
Get list of supported models.
Returns:
List of available model names
"""
models = ["fallback"]
if self.gemini_client:
models.extend(["gemini-2.5-flash-preview-05-20", "gemini-1.5-pro"])
if self.openai_client:
models.extend(["gpt-3.5-turbo", "gpt-4", "gpt-4-turbo"])
return models
def update_model(self, model_name: str) -> bool:
"""
Update the model used for generation.
Args:
model_name: Name of the model to use
Returns:
True if model was updated successfully
"""
try:
if model_name in self.get_supported_models():
self.model = model_name
self.logger.info(f"Model updated to: {model_name}")
return True
else:
self.logger.warning(f"Model {model_name} not supported")
return False
except Exception as e:
self.logger.error(f"❌ Error updating model: {e}")
return False
def get_generation_stats(self) -> Dict[str, Any]:
"""
Get statistics about response generation.
Returns:
Dictionary with generation statistics
"""
return {
"supported_models": self.get_supported_models(),
"current_model": self.model,
"gemini_available": self.gemini_client is not None,
"openai_available": self.openai_client is not None,
"langchain_available": self.llm is not None,
"max_tokens": self.max_tokens,
"temperature": self.temperature,
}
|