Spaces:
Sleeping
Sleeping
File size: 2,913 Bytes
65ade69 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 | import os
import requests
from langchain_core.tools import tool
from tavily import TavilyClient
@tool
def web_search(query: str) -> str:
"""
Search the web for information using Tavily API.
Great for finding competitors, market sizing, and general industry trends.
"""
api_key = os.getenv("TAVILY_API_KEY")
if not api_key:
return "Error: TAVILY_API_KEY not set in environment."
try:
client = TavilyClient(api_key=api_key)
# We perform a basic search and ask for answer and raw content
response = client.search(query=query, search_depth="basic", max_results=3)
# Format the response back to a string so the LLM can read it
results_str = []
for result in response.get("results", []):
results_str.append(f"Title: {result.get('title')}\nURL: {result.get('url')}\nContent: {result.get('content')}\n")
return "\n---\n".join(results_str)
except Exception as e:
return f"Error during web search: {e}"
@tool
def hn_search(query: str) -> str:
"""
Search HackerNews to gauge community sentiment, find past discussions, or see similar product launches.
"""
url = f"http://hn.algolia.com/api/v1/search?query={query}&tags=story"
try:
response = requests.get(url)
response.raise_for_status()
data = response.json()
results_str = []
for item in data.get("hits", [])[:5]: # Get top 5 discussions
results_str.append(f"Title: {item.get('title')}\nURL: {item.get('url')}\nPoints: {item.get('points')}\nComments: {item.get('num_comments')}\n")
return "\n---\n".join(results_str) if results_str else "No HackerNews discussions found."
except Exception as e:
return f"Error during HackerNews search: {e}"
@tool
def github_search(query: str) -> str:
"""
Search GitHub repositories to identify open-source alternatives or technical risks.
"""
# Using the GitHub API to search for repositories
url = f"https://api.github.com/search/repositories?q={query}&sort=stars&order=desc"
headers = {"Accept": "application/vnd.github.v3+json"}
token = os.getenv("GITHUB_TOKEN")
if token:
headers["Authorization"] = f"token {token}"
try:
response = requests.get(url, headers=headers)
response.raise_for_status()
data = response.json()
results_str = []
for item in data.get("items", [])[:3]: # Get top 3 repos
results_str.append(f"Repo: {item.get('full_name')}\nDescription: {item.get('description')}\nStars: {item.get('stargazers_count')}\nURL: {item.get('html_url')}\n")
return "\n---\n".join(results_str) if results_str else "No open-source alternatives found on GitHub."
except Exception as e:
return f"Error during GitHub search: {e}"
|