webscout 4.8__py3-none-any.whl → 5.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of webscout might be problematic. Click here for more details.

@@ -1,72 +1,44 @@
1
1
  import json
2
2
  import logging
3
- from webscout import DeepInfra, WEBS
3
+ from webscout import LLAMA3, WEBS
4
4
 
5
5
  class FunctionCallingAgent:
6
- def __init__(self, model: str = "Qwen/Qwen2-72B-Instruct",
6
+ def __init__(self, model: str = "llama3-8b",
7
7
  system_prompt: str = 'You are a helpful assistant that will always answer what the user wants',
8
8
  tools: list = None):
9
- """
10
- Initialize the FunctionCallingAgent with the model, system prompt, and tools.
11
-
12
- Args:
13
- model (str): The model to use for deepinfra chat.
14
- system_prompt (str): The system prompt to initialize the model.
15
- tools (list): A list of tools the agent can use.
16
- """
17
- self.deepinfra = DeepInfra(model=model, system_prompt=system_prompt, timeout=300)
9
+ self.LLAMA3 = LLAMA3(model=model, system=system_prompt, timeout=300)
18
10
  self.tools = tools if tools is not None else []
19
-
11
+ self.webs = WEBS()
20
12
 
21
13
  def function_call_handler(self, message_text: str) -> dict:
22
- """
23
- Handles function calls based on the provided message text.
24
-
25
- Args:
26
- message_text (str): The input message text from the user.
27
-
28
- Returns:
29
- dict: The extracted function call and arguments.
30
- """
31
14
  system_message = self._generate_system_message(message_text)
32
- response = self.deepinfra.chat(system_message)
15
+ response = self.LLAMA3.chat(system_message)
33
16
  # logging.info(f"Raw response: {response}")
34
-
35
17
  return self._parse_function_call(response)
36
18
 
37
19
  def _generate_system_message(self, user_message: str) -> str:
38
- """
39
- Generates a system message incorporating the user message and available tools.
40
-
41
- Args:
42
- user_message (str): The input message from the user.
43
-
44
- Returns:
45
- str: The formatted system message.
46
- """
47
- tools_description = '\n'.join([f"{tool['function']['name']}: {tool['function'].get('description', '')}" for tool in self.tools])
20
+ tools_description = '\n'.join([f"- {tool['function']['name']}: {tool['function'].get('description', '')}" for tool in self.tools])
48
21
  return (
49
- f"[SYSTEM] You are a helpful and capable AI assistant. "
50
- "Your goal is to understand the user's request and provide accurate and relevant information. "
51
- "You have access to the following tools:\n\n"
52
- f"{tools_description}\n\n"
53
- "To use a tool, please follow this format:\n\n"
54
- "```json\n"
55
- "{{ 'tool_name': 'tool_name', 'tool_input': {{ 'arg_1': 'value_1', 'arg_2': 'value_2', ... }} }}\n"
56
- "```\n\n"
57
- f"[USER] {user_message}"
22
+ "You are an AI assistant capable of understanding user requests and using tools to fulfill them. "
23
+ "Always respond using the JSON format specified below, even if you're not sure about the answer. "
24
+ f"Available tools:\n{tools_description}\n\n"
25
+ "Instructions:\n"
26
+ "1. Analyze the user's request.\n"
27
+ "2. Choose the most appropriate tool based on the request.\n"
28
+ "3. Respond ONLY with a JSON object in this exact format:\n"
29
+ "{\n"
30
+ ' "tool_name": "name_of_the_tool",\n'
31
+ ' "tool_input": {\n'
32
+ ' "param1": "value1",\n'
33
+ ' "param2": "value2"\n'
34
+ " }\n"
35
+ "}\n\n"
36
+ "If you can't determine a suitable tool, use the 'general_ai' tool with the user's message as the 'question' parameter.\n\n"
37
+ f"User request: {user_message}\n\n"
38
+ "Your response (in JSON format):"
58
39
  )
59
40
 
60
41
  def _parse_function_call(self, response: str) -> dict:
61
- """
62
- Parses the response from the model to extract the function call.
63
-
64
- Args:
65
- response (str): The raw response from the model.
66
-
67
- Returns:
68
- dict: A dictionary containing the function name and arguments.
69
- """
70
42
  try:
71
43
  # Find the JSON-like part of the response
72
44
  start_idx = response.find("{")
@@ -76,14 +48,7 @@ class FunctionCallingAgent:
76
48
  raise ValueError("No valid JSON structure found in the response.")
77
49
 
78
50
  response_json_str = response[start_idx:end_idx]
79
-
80
- # Replace single quotes with double quotes and remove extra braces
81
- response_json_str = response_json_str.replace("'", '"')
82
- response_json_str = response_json_str.replace("{{", "{").replace("}}", "}")
83
51
 
84
- # Remove any leading or trailing whitespace
85
- response_json_str = response_json_str.strip()
86
-
87
52
  # Attempt to load the JSON string
88
53
  return json.loads(response_json_str)
89
54
 
@@ -92,17 +57,8 @@ class FunctionCallingAgent:
92
57
  return {"error": str(e)}
93
58
 
94
59
  def execute_function(self, function_call_data: dict) -> str:
95
- """
96
- Executes the specified function with the provided arguments.
97
-
98
- Args:
99
- function_call_data (dict): A dictionary containing the function name and arguments.
100
-
101
- Returns:
102
- str: The result of the function execution.
103
- """
104
- function_name = function_call_data.get("tool_name") # Use 'tool_name' instead of 'name'
105
- arguments = function_call_data.get("tool_input", {}) # Use 'tool_input' instead of 'arguments'
60
+ function_name = function_call_data.get("tool_name")
61
+ arguments = function_call_data.get("tool_input", {})
106
62
 
107
63
  if not isinstance(arguments, dict):
108
64
  logging.error("Invalid arguments format.")
@@ -110,28 +66,32 @@ class FunctionCallingAgent:
110
66
 
111
67
  logging.info(f"Executing function: {function_name} with arguments: {arguments}")
112
68
 
113
- if function_name == "web_search":
114
- return self._handle_web_search(arguments)
115
- else:
116
- return f"Function '{function_name}' is not implemented."
69
+ # if function_name == "web_search":
70
+ # return self._handle_web_search(arguments)
71
+ # elif function_name == "general_ai":
72
+ # return self._handle_general_ai(arguments)
73
+ # else:
74
+ # return f"Function '{function_name}' is not implemented."
117
75
 
118
76
  # def _handle_web_search(self, arguments: dict) -> str:
119
- # """
120
- # Handles web search queries using the WEBS tool.
121
-
122
- # Args:
123
- # arguments (dict): A dictionary containing the query argument.
124
-
125
- # Returns:
126
- # str: The result of the web search.
127
- # """
128
77
  # query = arguments.get("query")
129
78
  # if not query:
130
79
  # return "Please provide a search query."
131
80
 
132
- # search_results = self.webs.text(query)
133
- # # Additional processing of search results can be done here if needed.
134
- # return f"Here's what I found:\n\n{search_results}"
81
+ # search_results = self.webs.text(query, max_results=3)
82
+ # formatted_results = "\n\n".join(
83
+ # f"{i+1}. {result['title']}\n{result['body']}\nURL: {result['href']}"
84
+ # for i, result in enumerate(search_results)
85
+ # )
86
+ # return f"Here's what I found:\n\n{formatted_results}"
87
+
88
+ # def _handle_general_ai(self, arguments: dict) -> str:
89
+ # question = arguments.get("question")
90
+ # if not question:
91
+ # return "Please provide a question for the AI to answer."
92
+
93
+ # response = self.LLAMA3.chat(question)
94
+ # return response
135
95
 
136
96
  # Example usage
137
97
  if __name__ == "__main__":
@@ -139,48 +99,44 @@ if __name__ == "__main__":
139
99
  {
140
100
  "type": "function",
141
101
  "function": {
142
- "name": "UserDetail",
102
+ "name": "web_search",
103
+ "description": "Search query on Google",
143
104
  "parameters": {
144
105
  "type": "object",
145
- "title": "UserDetail",
146
106
  "properties": {
147
- "name": {
148
- "title": "Name",
149
- "type": "string"
150
- },
151
- "age": {
152
- "title": "Age",
153
- "type": "integer"
107
+ "query": {
108
+ "type": "string",
109
+ "description": "web search query"
154
110
  }
155
111
  },
156
- "required": ["name", "age"]
112
+ "required": ["query"]
157
113
  }
158
114
  }
159
115
  },
160
116
  {
161
117
  "type": "function",
162
118
  "function": {
163
- "name": "web_search",
164
- "description": "Search query on google",
119
+ "name": "general_ai",
120
+ "description": "Use AI to answer a general question",
165
121
  "parameters": {
166
122
  "type": "object",
167
123
  "properties": {
168
- "query": {
124
+ "question": {
169
125
  "type": "string",
170
- "description": "web search query"
126
+ "description": "The question to be answered by the AI"
171
127
  }
172
128
  },
173
- "required": ["query"]
129
+ "required": ["question"]
174
130
  }
175
131
  }
176
132
  }
177
133
  ]
178
134
 
179
135
  agent = FunctionCallingAgent(tools=tools)
180
- message = "websearch about helpingai-9b"
136
+ message = "open yt"
181
137
  function_call_data = agent.function_call_handler(message)
182
138
  print(f"Function Call Data: {function_call_data}")
183
139
 
184
140
  if "error" not in function_call_data:
185
141
  result = agent.execute_function(function_call_data)
186
- print(f"Function Execution Result: {result}")
142
+ print(f"Function Execution Result: {result}")
@@ -0,0 +1,124 @@
1
+ from bs4 import BeautifulSoup
2
+ import requests
3
+ from typing import Dict, List, Optional, Union
4
+ from concurrent.futures import ThreadPoolExecutor, as_completed
5
+ from urllib.parse import urlparse
6
+ from termcolor import colored
7
+ import time
8
+ import random
9
+
10
+ class BingS:
11
+ """Bing search class to get search results from bing.com."""
12
+
13
+ _executor: ThreadPoolExecutor = ThreadPoolExecutor(max_workers=10)
14
+
15
+ def __init__(
16
+ self,
17
+ headers: Optional[Dict[str, str]] = None,
18
+ proxy: Optional[str] = None,
19
+ timeout: Optional[int] = 10,
20
+ ) -> None:
21
+ """Initialize the BingS object."""
22
+ self.proxy: Optional[str] = proxy
23
+ self.headers = headers if headers else {
24
+ "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.62"
25
+ }
26
+ self.headers["Referer"] = "https://www.bing.com/"
27
+ self.client = requests.Session()
28
+ self.client.headers.update(self.headers)
29
+ self.client.proxies.update({"http": self.proxy, "https": self.proxy})
30
+ self.timeout = timeout
31
+
32
+ def __enter__(self) -> "BingS":
33
+ return self
34
+
35
+ def __exit__(self, exc_type, exc_val, exc_tb):
36
+ self.client.close()
37
+
38
+ def _get_url(
39
+ self,
40
+ method: str,
41
+ url: str,
42
+ params: Optional[Dict[str, str]] = None,
43
+ data: Optional[Union[Dict[str, str], bytes]] = None,
44
+ ) -> bytes:
45
+ try:
46
+ resp = self.client.request(method, url, params=params, data=data, timeout=self.timeout)
47
+ except Exception as ex:
48
+ raise Exception(f"{url} {type(ex).__name__}: {ex}") from ex
49
+ if resp.status_code == 200:
50
+ return resp.content
51
+ raise Exception(f"{resp.url} returned status code {resp.status_code}. {params=} {data=}")
52
+
53
+ def search(
54
+ self,
55
+ keywords: str,
56
+ region: str = "us-EN", # Bing uses us-EN
57
+ lang: str = "en",
58
+ safe: str = "off",
59
+ timelimit: Optional[str] = None, # Not directly supported by Bing
60
+ max_results: Optional[int] = None,
61
+ ) -> List[Dict[str, str]]:
62
+ """Bing text search."""
63
+ assert keywords, "keywords is mandatory"
64
+
65
+ results = []
66
+ start = 1 # Bing uses 1-based indexing for pages
67
+ while len(results) < (max_results or float('inf')):
68
+ params = {
69
+ "q": keywords,
70
+ "count": 10, # Number of results per page
71
+ "mkt": region,
72
+ "setlang": lang,
73
+ "safeSearch": safe,
74
+ "first": start, # Bing uses 'first' for pagination
75
+ }
76
+
77
+ try:
78
+ resp_content = self._get_url("GET", "https://www.bing.com/search", params=params)
79
+ soup = BeautifulSoup(resp_content, "html.parser")
80
+ result_block = soup.find_all("li", class_="b_algo")
81
+
82
+ if not result_block:
83
+ break
84
+
85
+ for result in result_block:
86
+ try:
87
+ link = result.find("a", href=True)
88
+ if link:
89
+ initial_url = link["href"]
90
+
91
+ title = result.find("h2").text if result.find("h2") else ""
92
+ description = result.find("p").text.strip() if result.find("p") else "" # Strip whitespace
93
+
94
+ # Remove 'WEB' prefix if present
95
+ if description.startswith("WEB"):
96
+ description = description[4:] # Skip the first 4 characters ('WEB ')
97
+
98
+ results.append({
99
+ "title": title,
100
+ "href": initial_url,
101
+ "abstract": description,
102
+ "index": len(results),
103
+ "type": "web",
104
+ })
105
+
106
+ if len(results) >= max_results:
107
+ return results
108
+
109
+ except Exception as e:
110
+ print(f"Error extracting result: {e}")
111
+
112
+ except Exception as e:
113
+ print(f"Error fetching URL: {e}")
114
+
115
+ start += 10
116
+
117
+ return results
118
+
119
+ if __name__ == "__main__":
120
+ from rich import print
121
+ searcher = BingS()
122
+ results = searcher.search("Python development tools", max_results=30)
123
+ for result in results:
124
+ print(result)