Shreyas094 commited on
Commit
ac6819a
·
verified ·
1 Parent(s): e53822f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -12
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") -> str:
276
- result = DDGS().chat(
277
- keywords=query,
278
- model=model,
279
- timeout=30
280
- )
281
- return result
 
 
 
 
 
 
 
 
 
 
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
- search_results = searcher.search(query, num_results=10)
 
510
 
511
- crawler = TrafilaturaWebCrawler()
512
  context = ""
513
-
514
- for url in search_results:
515
- context += crawler.get_website_content_from_url(url) + "\n"
 
 
 
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}'