jjvelezo commited on
Commit
6232aa7
·
verified ·
1 Parent(s): 7939a0c

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +6 -86
agent.py CHANGED
@@ -1,105 +1,25 @@
1
  import os
2
  import requests
3
- import urllib.parse
4
- from bs4 import BeautifulSoup
5
 
6
  class DuckDuckGoAgent:
7
  def __init__(self):
8
  print("DuckDuckGoAgent initialized.")
9
- self.headers = {
10
- "User-Agent": "Mozilla/5.0"
11
- }
12
-
13
- # Support for multiple model backends
14
- self.supported_models = {
15
- "huggingface": self.call_huggingface_llm,
16
- # You can easily extend this dictionary to support:
17
- # "openai": self.call_openai_model,
18
- # "lite_llm": self.call_litellm_model,
19
- # "custom_server": self.call_custom_model,
20
- }
21
 
22
- self.default_model = "huggingface"
23
  self.model_config = {
24
- "huggingface": {
25
- "api_key": os.getenv("HF_API_TOKEN"),
26
- "model_name": "mistralai/Mistral-7B-Instruct-v0.1"
27
- }
28
  }
29
 
30
  def __call__(self, question: str) -> str:
31
- """
32
- Main method to process a question. It first tries DuckDuckGo,
33
- then scraping, and finally uses a language model if needed.
34
- """
35
  print(f"Agent received question: {question[:50]}...")
36
- answer = self.get_duckduckgo_answer(question)
37
  print(f"Agent returning answer: {answer}")
38
  return answer.strip()
39
 
40
- def get_duckduckgo_answer(self, query: str) -> str:
41
- """
42
- Attempt to get an answer from the DuckDuckGo API.
43
- If no abstract text is found, fall back to scraping.
44
- """
45
- search_query = urllib.parse.quote(query)
46
- url = f"https://api.duckduckgo.com/?q={search_query}&format=json&no_html=1&skip_disambig=1"
47
-
48
- try:
49
- response = requests.get(url, timeout=10)
50
- if response.status_code == 200:
51
- data = response.json()
52
- if 'AbstractText' in data and data['AbstractText']:
53
- return data['AbstractText'][:200]
54
- else:
55
- print("No abstract found, falling back to scraping.")
56
- return self.scrape_duckduckgo(query)
57
- else:
58
- print(f"DuckDuckGo API failed with status: {response.status_code}")
59
- return self.scrape_duckduckgo(query)
60
- except Exception as e:
61
- print(f"Error contacting DuckDuckGo API: {e}")
62
- return self.scrape_duckduckgo(query)
63
-
64
- def scrape_duckduckgo(self, query: str) -> str:
65
- """
66
- Fallback to scraping DuckDuckGo search results if API fails or no abstract found.
67
- """
68
- print("Using fallback: scraping HTML results.")
69
- try:
70
- response = requests.post(
71
- "https://html.duckduckgo.com/html/",
72
- data={"q": query},
73
- headers=self.headers,
74
- timeout=10
75
- )
76
- soup = BeautifulSoup(response.text, "html.parser")
77
- snippets = soup.select(".result__snippet")
78
- for s in snippets:
79
- text = s.get_text().strip()
80
- if text:
81
- return text[:200]
82
- print("No useful snippets found, falling back to language model.")
83
- return self.call_model_backend(query)
84
- except Exception as e:
85
- print(f"Error scraping DuckDuckGo: {e}")
86
- return self.call_model_backend(query)
87
-
88
  def call_model_backend(self, prompt: str) -> str:
89
- """
90
- Dispatch to the selected LLM backend.
91
- """
92
- if self.default_model in self.supported_models:
93
- return self.supported_models[self.default_model](prompt)
94
- return "No valid model backend configured."
95
-
96
- def call_huggingface_llm(self, prompt: str) -> str:
97
- """
98
- Call Hugging Face Inference API as fallback LLM.
99
- """
100
- config = self.model_config.get("huggingface", {})
101
- api_key = config.get("api_key")
102
- model = config.get("model_name")
103
 
104
  if not api_key or not model:
105
  return "Error: Hugging Face API Token or model not configured."
 
1
  import os
2
  import requests
 
 
3
 
4
  class DuckDuckGoAgent:
5
  def __init__(self):
6
  print("DuckDuckGoAgent initialized.")
7
+ self.headers = {"User-Agent": "Mozilla/5.0"}
 
 
 
 
 
 
 
 
 
 
 
8
 
 
9
  self.model_config = {
10
+ "api_key": os.getenv("HF_API_TOKEN"),
11
+ "model_name": "mistralai/Mistral-7B-Instruct-v0.1"
 
 
12
  }
13
 
14
  def __call__(self, question: str) -> str:
 
 
 
 
15
  print(f"Agent received question: {question[:50]}...")
16
+ answer = self.call_model_backend(question)
17
  print(f"Agent returning answer: {answer}")
18
  return answer.strip()
19
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
  def call_model_backend(self, prompt: str) -> str:
21
+ api_key = self.model_config.get("api_key")
22
+ model = self.model_config.get("model_name")
 
 
 
 
 
 
 
 
 
 
 
 
23
 
24
  if not api_key or not model:
25
  return "Error: Hugging Face API Token or model not configured."