Update app.py
Browse files
app.py
CHANGED
|
@@ -272,13 +272,23 @@ def generate_chunked_response(prompt, model, max_tokens=10000, num_calls=3, temp
|
|
| 272 |
return final_response
|
| 273 |
|
| 274 |
class SimpleDDGSearch:
|
| 275 |
-
def search(self, query: str, model: str = "claude-3-haiku"
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
|
| 283 |
class TrafilaturaWebCrawler:
|
| 284 |
def get_website_content_from_url(self, url: str) -> str:
|
|
@@ -506,13 +516,16 @@ def create_web_search_vectors(search_results):
|
|
| 506 |
|
| 507 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
| 508 |
searcher = SimpleDDGSearch()
|
| 509 |
-
|
|
|
|
| 510 |
|
| 511 |
-
crawler = TrafilaturaWebCrawler()
|
| 512 |
context = ""
|
| 513 |
-
|
| 514 |
-
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
| 516 |
|
| 517 |
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:
|
| 518 |
{context} that fulfills the following user request: '{query}'
|
|
|
|
| 272 |
return final_response
|
| 273 |
|
| 274 |
class SimpleDDGSearch:
|
| 275 |
+
def search(self, query: str, use_chat: bool = False, model: str = "claude-3-haiku", num_results: int = 10):
|
| 276 |
+
if use_chat:
|
| 277 |
+
result = DDGS().chat(
|
| 278 |
+
keywords=query,
|
| 279 |
+
model=model,
|
| 280 |
+
timeout=30
|
| 281 |
+
)
|
| 282 |
+
return [{"content": result, "url": "AI-generated response"}]
|
| 283 |
+
else:
|
| 284 |
+
results = DDGS().news(
|
| 285 |
+
keywords=query,
|
| 286 |
+
region='wt-wt',
|
| 287 |
+
safesearch='off',
|
| 288 |
+
timelimit='m',
|
| 289 |
+
max_results=num_results
|
| 290 |
+
)
|
| 291 |
+
return [{"content": res["body"], "url": res["url"]} for res in results]
|
| 292 |
|
| 293 |
class TrafilaturaWebCrawler:
|
| 294 |
def get_website_content_from_url(self, url: str) -> str:
|
|
|
|
| 516 |
|
| 517 |
def get_response_with_search(query, model, num_calls=3, temperature=0.2):
|
| 518 |
searcher = SimpleDDGSearch()
|
| 519 |
+
use_chat = model == "@cf/meta/llama-3.1-8b-instruct" # Or however you want to determine when to use chat
|
| 520 |
+
search_results = searcher.search(query, use_chat=use_chat, model="claude-3-haiku" if use_chat else None, num_results=10)
|
| 521 |
|
|
|
|
| 522 |
context = ""
|
| 523 |
+
for result in search_results:
|
| 524 |
+
if use_chat:
|
| 525 |
+
context += result['content']
|
| 526 |
+
else:
|
| 527 |
+
crawler = TrafilaturaWebCrawler()
|
| 528 |
+
context += crawler.get_website_content_from_url(result['url']) + "\n"
|
| 529 |
|
| 530 |
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:
|
| 531 |
{context} that fulfills the following user request: '{query}'
|