Spaces:
Sleeping
Sleeping
updoot
Browse files- app_hf.py +20 -5
- queryrun.py +29 -5
app_hf.py
CHANGED
|
@@ -57,19 +57,34 @@ def handle_query():
|
|
| 57 |
# Add web search results to sources
|
| 58 |
for i, web_result in enumerate(web_search_results, len(sources_for_response) + 1):
|
| 59 |
urls = web_result.get('urls', [])
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
"id": i,
|
| 62 |
"score": 0.0, # Web search results don't have similarity scores
|
| 63 |
-
"url": urls[0] if urls else None, # Primary URL for easy access
|
| 64 |
-
"urls": urls, # All URLs at top level for easy access
|
| 65 |
"metadata": {
|
| 66 |
"source": web_result.get('source', 'Web Search'),
|
| 67 |
"type": web_result.get('type', 'web_search'),
|
| 68 |
"query": web_result.get('query', 'Unknown'),
|
| 69 |
"content": web_result.get('content', '')[:500], # Preview
|
| 70 |
-
"urls": urls # Also in metadata for consistency
|
| 71 |
}
|
| 72 |
-
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
return jsonify({
|
| 75 |
"response": response_text,
|
|
|
|
| 57 |
# Add web search results to sources
|
| 58 |
for i, web_result in enumerate(web_search_results, len(sources_for_response) + 1):
|
| 59 |
urls = web_result.get('urls', [])
|
| 60 |
+
# Ensure URLs is a list (not None or empty)
|
| 61 |
+
if not urls:
|
| 62 |
+
urls = []
|
| 63 |
+
|
| 64 |
+
source_entry = {
|
| 65 |
"id": i,
|
| 66 |
"score": 0.0, # Web search results don't have similarity scores
|
|
|
|
|
|
|
| 67 |
"metadata": {
|
| 68 |
"source": web_result.get('source', 'Web Search'),
|
| 69 |
"type": web_result.get('type', 'web_search'),
|
| 70 |
"query": web_result.get('query', 'Unknown'),
|
| 71 |
"content": web_result.get('content', '')[:500], # Preview
|
|
|
|
| 72 |
}
|
| 73 |
+
}
|
| 74 |
+
|
| 75 |
+
# Add URLs at multiple levels for maximum compatibility
|
| 76 |
+
if urls:
|
| 77 |
+
source_entry["url"] = urls[0] # Primary URL for easy access
|
| 78 |
+
source_entry["urls"] = urls # All URLs at top level
|
| 79 |
+
source_entry["metadata"]["url"] = urls[0] # Primary URL in metadata
|
| 80 |
+
source_entry["metadata"]["urls"] = urls # All URLs in metadata
|
| 81 |
+
else:
|
| 82 |
+
source_entry["url"] = None
|
| 83 |
+
source_entry["urls"] = []
|
| 84 |
+
source_entry["metadata"]["url"] = None
|
| 85 |
+
source_entry["metadata"]["urls"] = []
|
| 86 |
+
|
| 87 |
+
sources_for_response.append(source_entry)
|
| 88 |
|
| 89 |
return jsonify({
|
| 90 |
"response": response_text,
|
queryrun.py
CHANGED
|
@@ -566,8 +566,8 @@ Guidelines:
|
|
| 566 |
formatted_results.append(f"Source {i}: {doc['content'][:500]}...")
|
| 567 |
return "\n".join(formatted_results)
|
| 568 |
|
| 569 |
-
def get_context_summary() -> str:
|
| 570 |
-
"""Get a summary of available context documents"""
|
| 571 |
if not context_docs:
|
| 572 |
return "No context documents available."
|
| 573 |
summary = f"Found {len(context_docs)} relevant documents:\n"
|
|
@@ -586,31 +586,55 @@ Guidelines:
|
|
| 586 |
# Get search results with URLs
|
| 587 |
ddg_results = list(ddgs.text(search_query, max_results=5))
|
| 588 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 589 |
# Format results
|
| 590 |
formatted_results = []
|
| 591 |
urls = []
|
| 592 |
for r in ddg_results:
|
|
|
|
|
|
|
| 593 |
title = r.get('title', '')
|
| 594 |
body = r.get('body', '')
|
| 595 |
-
|
|
|
|
| 596 |
if title or body:
|
| 597 |
formatted_results.append(f"{title}: {body}")
|
| 598 |
if href:
|
| 599 |
urls.append(href)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 600 |
|
| 601 |
results = "\n".join(formatted_results) if formatted_results else "No results found."
|
| 602 |
|
| 603 |
print(f"β
[AGENT] Web search completed. Found {len(ddg_results)} results with {len(urls)} URLs.")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 604 |
|
| 605 |
# Store search results with metadata for sources
|
|
|
|
| 606 |
web_search_results.append({
|
| 607 |
'query': search_query,
|
| 608 |
'content': results,
|
| 609 |
'type': 'web_search',
|
| 610 |
'source': f'Web Search: {search_query}',
|
| 611 |
-
'urls': urls[:5] if urls else [] # Store up to 5 URLs
|
| 612 |
})
|
| 613 |
|
|
|
|
|
|
|
|
|
|
| 614 |
# Format results with URLs if found
|
| 615 |
if urls:
|
| 616 |
url_list = "\n".join([f" - {url}" for url in urls[:5]])
|
|
@@ -642,7 +666,7 @@ Guidelines:
|
|
| 642 |
Tool(
|
| 643 |
name="get_context_summary",
|
| 644 |
func=get_context_summary,
|
| 645 |
-
description="Get a summary of the currently available context documents. Use this to understand what information is available."
|
| 646 |
)
|
| 647 |
]
|
| 648 |
|
|
|
|
| 566 |
formatted_results.append(f"Source {i}: {doc['content'][:500]}...")
|
| 567 |
return "\n".join(formatted_results)
|
| 568 |
|
| 569 |
+
def get_context_summary(_=None) -> str:
|
| 570 |
+
"""Get a summary of available context documents. Takes no arguments (ignores any provided)."""
|
| 571 |
if not context_docs:
|
| 572 |
return "No context documents available."
|
| 573 |
summary = f"Found {len(context_docs)} relevant documents:\n"
|
|
|
|
| 586 |
# Get search results with URLs
|
| 587 |
ddg_results = list(ddgs.text(search_query, max_results=5))
|
| 588 |
|
| 589 |
+
# Debug: Print first result structure to understand format
|
| 590 |
+
if ddg_results and len(ddg_results) > 0:
|
| 591 |
+
print(f"π [DEBUG] First result keys: {list(ddg_results[0].keys()) if isinstance(ddg_results[0], dict) else 'Not a dict'}")
|
| 592 |
+
|
| 593 |
# Format results
|
| 594 |
formatted_results = []
|
| 595 |
urls = []
|
| 596 |
for r in ddg_results:
|
| 597 |
+
if not isinstance(r, dict):
|
| 598 |
+
continue
|
| 599 |
title = r.get('title', '')
|
| 600 |
body = r.get('body', '')
|
| 601 |
+
# Try different possible field names for URL
|
| 602 |
+
href = r.get('href', '') or r.get('url', '') or r.get('link', '') or r.get('href', '')
|
| 603 |
if title or body:
|
| 604 |
formatted_results.append(f"{title}: {body}")
|
| 605 |
if href:
|
| 606 |
urls.append(href)
|
| 607 |
+
# Also check if the result itself is a dict with URL info
|
| 608 |
+
if not href:
|
| 609 |
+
# Try to find any URL-like value in all fields
|
| 610 |
+
for key, value in r.items():
|
| 611 |
+
if isinstance(value, str) and ('http://' in value or 'https://' in value):
|
| 612 |
+
if value not in urls:
|
| 613 |
+
urls.append(value)
|
| 614 |
+
print(f"π [DEBUG] Found URL in field '{key}': {value}")
|
| 615 |
+
break
|
| 616 |
|
| 617 |
results = "\n".join(formatted_results) if formatted_results else "No results found."
|
| 618 |
|
| 619 |
print(f"β
[AGENT] Web search completed. Found {len(ddg_results)} results with {len(urls)} URLs.")
|
| 620 |
+
if urls:
|
| 621 |
+
print(f"π [DEBUG] URLs found: {urls[:3]}") # Show first 3 URLs for debugging
|
| 622 |
+
else:
|
| 623 |
+
print(f"β οΈ [DEBUG] No URLs found in search results!")
|
| 624 |
|
| 625 |
# Store search results with metadata for sources
|
| 626 |
+
# Always include urls field, even if empty
|
| 627 |
web_search_results.append({
|
| 628 |
'query': search_query,
|
| 629 |
'content': results,
|
| 630 |
'type': 'web_search',
|
| 631 |
'source': f'Web Search: {search_query}',
|
| 632 |
+
'urls': urls[:5] if urls else [] # Store up to 5 URLs, empty list if none
|
| 633 |
})
|
| 634 |
|
| 635 |
+
# Debug: Verify URLs are stored
|
| 636 |
+
print(f"π [DEBUG] Stored web_search_result with {len(web_search_results[-1].get('urls', []))} URLs")
|
| 637 |
+
|
| 638 |
# Format results with URLs if found
|
| 639 |
if urls:
|
| 640 |
url_list = "\n".join([f" - {url}" for url in urls[:5]])
|
|
|
|
| 666 |
Tool(
|
| 667 |
name="get_context_summary",
|
| 668 |
func=get_context_summary,
|
| 669 |
+
description="Get a summary of the currently available context documents. Takes no arguments. Use this to understand what information is available."
|
| 670 |
)
|
| 671 |
]
|
| 672 |
|