|
def perform_web_search(query: str, max_results: int = 5, include_domains=None, exclude_domains=None) -> str: |
|
"""Perform web search using Tavily with default parameters""" |
|
if not tavily_client: |
|
return "Web search is not available. Please set the TAVILY_API_KEY environment variable." |
|
|
|
try: |
|
|
|
search_params = { |
|
"search_depth": "advanced", |
|
"max_results": min(max(1, max_results), 20) |
|
} |
|
if include_domains is not None: |
|
search_params["include_domains"] = include_domains |
|
if exclude_domains is not None: |
|
search_params["exclude_domains"] = exclude_domains |
|
|
|
response = tavily_client.search(query, **search_params) |
|
|
|
search_results = [] |
|
for result in response.get('results', []): |
|
title = result.get('title', 'No title') |
|
url = result.get('url', 'No URL') |
|
content = result.get('content', 'No content') |
|
search_results.append(f"Title: {title}\nURL: {url}\nContent: {content}\n") |
|
|
|
if search_results: |
|
return "Web Search Results:\n\n" + "\n---\n".join(search_results) |
|
else: |
|
return "No search results found." |
|
|
|
except Exception as e: |
|
return f"Search error: {str(e)}" |
|
|
|
def enhance_query_with_search(query: str, enable_search: bool) -> str: |
|
"""Enhance the query with web search results if search is enabled""" |
|
if not enable_search or not tavily_client: |
|
return query |
|
|
|
|
|
search_results = perform_web_search(query) |
|
|
|
|
|
enhanced_query = f"""Original Query: {query} |
|
{search_results} |
|
Please use the search results above to help create the requested application with the most up-to-date information and best practices.""" |
|
|
|
return enhanced_query |
|
|