Update app.py
Browse files272 unmodified lines273274275276277276277278279280221 unmodified lines502503504505506505506507508509510511512513514509510511512513514272 unmodified lines
class SimpleDDGSearch: def search(self, query: str, num_results: int = 10): results = DDGS().news(query, region='wt-wt', safesearch='off', max_results=num_results) return [res["url"] for res in results] results = DDGS().text(query, region='wt-wt', safesearch='off', max_results=num_results) return [res["href"] for res in results]
class TrafilaturaWebCrawler: def get_website_content_from_url(self, url: str) -> str:221 unmodified lines
def get_response_with_search(query, model, num_calls=3, temperature=0.2): searcher = SimpleDDGSearch() use_chat = model == "@cf/meta/llama-3.1-8b-instruct" # Or however you want to determine when to use chat search_results = searcher.search(query, use_chat=use_chat, model="claude-3-haiku" if use_chat else None, num_results=10) search_results = searcher.search(query, num_results=10) crawler = TrafilaturaWebCrawler() context = "" for result in search_results: if use_chat: context += result['content'] else: crawler = TrafilaturaWebCrawler() context += crawler.get_website_content_from_url(result['url']) + "\n" for url in search_results: context += crawler.get_website_content_from_url(url) + "\n" prompt = f"""You are an expert AI named Sentinel and have been given a task to create a detailed and complete research article using the following context from web search results:{context} that fulfills the following user request: '{query}'app.py+7-10