NazaninMn commited on
Commit
ceefd46
Β·
unverified Β·
1 Parent(s): e4a5971

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -4
app.py CHANGED
@@ -1,8 +1,10 @@
1
  import streamlit as st
2
  from langchain_groq import ChatGroq
3
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
4
- from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun, DuckDuckGoSearchRun
5
  import traceback
 
 
6
 
7
  ## Arxiv and Wikipedia Tools
8
  arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
@@ -11,7 +13,31 @@ arxiv = ArxivQueryRun(api_wrapper=arxiv_wrapper)
11
  api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
12
  wiki = WikipediaQueryRun(api_wrapper=api_wrapper)
13
 
14
- search = DuckDuckGoSearchRun(name="Search")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
  st.title("πŸ”Ž LangChain - Chat with search")
17
  """
@@ -43,8 +69,8 @@ for msg in st.session_state.messages:
43
  def use_tool(tool_name, query):
44
  """Execute a tool based on its name"""
45
  try:
46
- if "search" in tool_name.lower() or "duckduckgo" in tool_name.lower():
47
- return search.run(query)
48
  elif "arxiv" in tool_name.lower():
49
  return arxiv.run(query)
50
  elif "wiki" in tool_name.lower():
 
1
  import streamlit as st
2
  from langchain_groq import ChatGroq
3
  from langchain_community.utilities import ArxivAPIWrapper, WikipediaAPIWrapper
4
+ from langchain_community.tools import ArxivQueryRun, WikipediaQueryRun
5
  import traceback
6
+ import requests
7
+ from bs4 import BeautifulSoup
8
 
9
  ## Arxiv and Wikipedia Tools
10
  arxiv_wrapper = ArxivAPIWrapper(top_k_results=1, doc_content_chars_max=200)
 
13
  api_wrapper = WikipediaAPIWrapper(top_k_results=1, doc_content_chars_max=200)
14
  wiki = WikipediaQueryRun(api_wrapper=api_wrapper)
15
 
16
+ # Simple web search function using DuckDuckGo Lite (no API needed)
17
+ def simple_web_search(query, num_results=3):
18
+ """Simple web search using DuckDuckGo HTML"""
19
+ try:
20
+ url = f"https://lite.duckduckgo.com/lite/?q={requests.utils.quote(query)}"
21
+ headers = {
22
+ 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36'
23
+ }
24
+ response = requests.get(url, headers=headers, timeout=10)
25
+ soup = BeautifulSoup(response.text, 'html.parser')
26
+
27
+ results = []
28
+ for result in soup.find_all('tr')[:num_results*2]: # Get more rows to filter
29
+ links = result.find_all('a', class_='result-link')
30
+ snippets = result.find_all('td', class_='result-snippet')
31
+
32
+ if links and snippets:
33
+ title = links[0].get_text(strip=True)
34
+ snippet = snippets[0].get_text(strip=True)
35
+ if title and snippet:
36
+ results.append(f"{title}: {snippet}")
37
+
38
+ return "\n\n".join(results[:num_results]) if results else "No results found"
39
+ except Exception as e:
40
+ return f"Search error: {str(e)}"
41
 
42
  st.title("πŸ”Ž LangChain - Chat with search")
43
  """
 
69
  def use_tool(tool_name, query):
70
  """Execute a tool based on its name"""
71
  try:
72
+ if "search" in tool_name.lower() or "web" in tool_name.lower():
73
+ return simple_web_search(query)
74
  elif "arxiv" in tool_name.lower():
75
  return arxiv.run(query)
76
  elif "wiki" in tool_name.lower():