Spaces:
Build error
Build error
| import os | |
| import requests | |
| from agents import function_tool | |
| def brave_web_search(query: str) -> str: | |
| """Search the web using Brave Search API and return formatted results""" | |
| brave_api_key = os.getenv('BRAVE_API_KEY') | |
| url = "https://api.search.brave.com/res/v1/web/search" | |
| headers = { | |
| "Accept": "application/json", | |
| "Accept-Encoding": "gzip", | |
| "X-Subscription-Token": brave_api_key | |
| } | |
| params = { | |
| "q": query, | |
| "count": 10 | |
| } | |
| try: | |
| response = requests.get(url, headers=headers, params=params) | |
| response.raise_for_status() | |
| data = response.json() | |
| # Format results | |
| results = [] | |
| if 'web' in data and 'results' in data['web']: | |
| for item in data['web']['results'][:10]: | |
| title = item.get('title', '') | |
| description = item.get('description', '') | |
| url = item.get('url', '') | |
| results.append(f"**{title}**\n{description}\nURL: {url}\n") | |
| return "\n".join(results) if results else "No results found" | |
| except Exception as e: | |
| return f"Search error: {str(e)}" | |