Spaces:
Runtime error
Runtime error
| import logging | |
| _log = logging.getLogger(__name__) | |
| def run_search(search_tool, query: str, max_results: int = 3, max_content: int = 1500) -> str: | |
| """Invoke a search tool and normalize results to a readable string. | |
| Handles both formats: | |
| - dict with 'results' key (TavilySearch from langchain_tavily) | |
| - list of dicts (older TavilySearchResults / other providers) | |
| """ | |
| try: | |
| raw = search_tool.invoke(query) | |
| if isinstance(raw, dict): | |
| items = raw.get("results", []) | |
| elif isinstance(raw, list): | |
| items = raw | |
| else: | |
| return str(raw)[:max_content * max_results] | |
| lines = [ | |
| f"- {r.get('title', '')}: {r.get('content', '')[:max_content]}" | |
| for r in items[:max_results] | |
| ] | |
| return "\n".join(lines) if lines else "No results found." | |
| except Exception: | |
| _log.exception("search failed for query: %s", query[:120]) | |
| return "Search unavailable." | |