Spaces:
Running
Running
Wire chatbot search chunker and summy
Browse files- api/chatbot.py +14 -2
api/chatbot.py
CHANGED
|
@@ -8,6 +8,7 @@ from .retrieval import retrieval_engine
|
|
| 8 |
from memory import MemoryManager
|
| 9 |
from utils import translate_query, process_medical_image
|
| 10 |
from search import search_web
|
|
|
|
| 11 |
from models import process_search_query
|
| 12 |
|
| 13 |
logger = logging.getLogger("medical-chatbot")
|
|
@@ -60,8 +61,19 @@ class RAGMedicalChatbot:
|
|
| 60 |
search_results = search_web(user_query, num_results=10)
|
| 61 |
if search_results:
|
| 62 |
logger.info(f"[SEARCH] Retrieved {len(search_results)} web resources")
|
| 63 |
-
#
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
logger.info(f"[SEARCH] Processed with Llama, generated {len(url_mapping)} URL mappings")
|
| 66 |
else:
|
| 67 |
logger.warning("[SEARCH] No search results found")
|
|
|
|
| 8 |
from memory import MemoryManager
|
| 9 |
from utils import translate_query, process_medical_image
|
| 10 |
from search import search_web
|
| 11 |
+
from models import summarizer
|
| 12 |
from models import process_search_query
|
| 13 |
|
| 14 |
logger = logging.getLogger("medical-chatbot")
|
|
|
|
| 61 |
search_results = search_web(user_query, num_results=10)
|
| 62 |
if search_results:
|
| 63 |
logger.info(f"[SEARCH] Retrieved {len(search_results)} web resources")
|
| 64 |
+
# Compose a compact context strictly from query-relevant snippets
|
| 65 |
+
# Also build URL mapping for citations
|
| 66 |
+
url_mapping = {doc['id']: doc['url'] for doc in search_results if doc.get('id') and doc.get('url')}
|
| 67 |
+
# Create per-doc short summaries (already relevant snippets), further compress
|
| 68 |
+
summaries = []
|
| 69 |
+
for doc in search_results:
|
| 70 |
+
content = (doc.get('content') or '').strip()
|
| 71 |
+
if not content:
|
| 72 |
+
continue
|
| 73 |
+
concise = summarizer.summarize_for_query(content, user_query, max_length=320)
|
| 74 |
+
if concise:
|
| 75 |
+
summaries.append(f"Document {doc['id']}: {concise}")
|
| 76 |
+
search_context = "\n\n".join(summaries)
|
| 77 |
logger.info(f"[SEARCH] Processed with Llama, generated {len(url_mapping)} URL mappings")
|
| 78 |
else:
|
| 79 |
logger.warning("[SEARCH] No search results found")
|