webscout 4.4__tar.gz → 4.5__tar.gz
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.
- {webscout-4.4/webscout.egg-info → webscout-4.5}/PKG-INFO +11 -25
- {webscout-4.4 → webscout-4.5}/README.md +10 -24
- {webscout-4.4 → webscout-4.5}/setup.py +1 -1
- {webscout-4.4 → webscout-4.5}/webscout/AIutel.py +2 -1
- webscout-4.5/webscout/Agents/Onlinesearcher.py +175 -0
- webscout-4.5/webscout/Agents/__init__.py +2 -0
- webscout-4.5/webscout/Agents/functioncall.py +126 -0
- webscout-4.5/webscout/Provider/Andi.py +275 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/__init__.py +3 -1
- {webscout-4.4 → webscout-4.5}/webscout/__init__.py +4 -3
- {webscout-4.4 → webscout-4.5}/webscout/voice.py +8 -1
- {webscout-4.4 → webscout-4.5}/webscout/webai.py +13 -0
- {webscout-4.4 → webscout-4.5}/webscout/webscout_search.py +1 -0
- webscout-4.5/webscout/websx_search.py +19 -0
- {webscout-4.4 → webscout-4.5/webscout.egg-info}/PKG-INFO +11 -25
- {webscout-4.4 → webscout-4.5}/webscout.egg-info/SOURCES.txt +4 -0
- webscout-4.4/webscout/websx_search.py +0 -370
- {webscout-4.4 → webscout-4.5}/LICENSE.md +0 -0
- {webscout-4.4 → webscout-4.5}/setup.cfg +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/AIauto.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/AIbase.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/DWEBS.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Extra/__init__.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Extra/autollama.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Extra/gguf.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Extra/weather.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Extra/weather_ascii.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/GoogleS.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/LLM.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/__init__.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/_version.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/formats.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/model.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/rawdog.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/samplers.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/thread.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Local/utils.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/BasedGPT.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Berlin4h.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Blackboxai.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/ChatGPTUK.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Cohere.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Deepinfra.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Deepseek.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/FreeGemini.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Gemini.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Geminiflash.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Geminipro.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Groq.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Koboldai.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Leo.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Llama.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/OLLAMA.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/OpenGPT.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Openai.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Perplexity.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Phind.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Poe.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Reka.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/ThinkAnyAI.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/VTLchat.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Xjai.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Yepchat.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/Provider/Youchat.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/YTdownloader.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/__main__.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/async_providers.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/cli.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/exceptions.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/g4f.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/models.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/tempid.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/transcriber.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/utils.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/version.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout/webscout_search_async.py +0 -0
- {webscout-4.4 → webscout-4.5}/webscout.egg-info/dependency_links.txt +0 -0
- {webscout-4.4 → webscout-4.5}/webscout.egg-info/entry_points.txt +0 -0
- {webscout-4.4 → webscout-4.5}/webscout.egg-info/requires.txt +0 -0
- {webscout-4.4 → webscout-4.5}/webscout.egg-info/top_level.txt +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: webscout
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.5
|
|
4
4
|
Summary: Search for anything using Google, DuckDuckGo, brave, qwant, phind.com, Contains AI models, can transcribe yt videos, temporary email and phone number generation, has TTS support, webai (terminal gpt and open interpreter) and offline LLMs and more
|
|
5
5
|
Author: OEvortex
|
|
6
6
|
Author-email: helpingai5@gmail.com
|
|
@@ -742,30 +742,11 @@ with WEBS() as WEBS:
|
|
|
742
742
|
## usage of WEBSX -- Another Websearch thing
|
|
743
743
|
```python
|
|
744
744
|
from webscout import WEBSX
|
|
745
|
+
s = "Python development tools"
|
|
745
746
|
|
|
746
|
-
|
|
747
|
-
# Initialize the WEBSX client
|
|
748
|
-
search = WEBSX(
|
|
749
|
-
k=10,
|
|
750
|
-
)
|
|
747
|
+
result = WEBSX(s)
|
|
751
748
|
|
|
752
|
-
|
|
753
|
-
query = "What is the capital of France?"
|
|
754
|
-
answer = search.run(query)
|
|
755
|
-
print(f"Answer: {answer}\n")
|
|
756
|
-
|
|
757
|
-
# Example using `results` method - Get detailed results with metadata
|
|
758
|
-
query = "What is the capital of France?"
|
|
759
|
-
results = search.results(query, num_results=3)
|
|
760
|
-
print("Search Results:")
|
|
761
|
-
for result in results:
|
|
762
|
-
print(f"Title: {result['title']}")
|
|
763
|
-
print(f"Snippet: {result['snippet']}")
|
|
764
|
-
print(f"Link: {result['link']}\n")
|
|
765
|
-
print(f'Engines: {result["engines"]}')
|
|
766
|
-
|
|
767
|
-
if __name__ == "__main__":
|
|
768
|
-
main()
|
|
749
|
+
print(result)
|
|
769
750
|
```
|
|
770
751
|
## ALL acts
|
|
771
752
|
<details>
|
|
@@ -1028,7 +1009,7 @@ ___
|
|
|
1028
1009
|
### 0. `Duckchat` - chat with LLM
|
|
1029
1010
|
```python
|
|
1030
1011
|
from webscout import WEBS as w
|
|
1031
|
-
R = w().chat("
|
|
1012
|
+
R = w().chat("Who are you", model='gpt-4o-mini') # GPT-3.5 Turbo, mixtral-8x7b, llama-3-70b, claude-3-haiku, gpt-4o-mini
|
|
1032
1013
|
print(R)
|
|
1033
1014
|
```
|
|
1034
1015
|
### 1. `PhindSearch` - Search using Phind.com
|
|
@@ -1505,7 +1486,12 @@ llama = LLAMA()
|
|
|
1505
1486
|
r = llama.chat("What is the meaning of life?")
|
|
1506
1487
|
print(r)
|
|
1507
1488
|
```
|
|
1508
|
-
|
|
1489
|
+
### 25. AndiSearch
|
|
1490
|
+
```python
|
|
1491
|
+
from webscout import AndiSearch
|
|
1492
|
+
a = AndiSearch()
|
|
1493
|
+
print(a.chat("HelpingAI-9B"))
|
|
1494
|
+
```
|
|
1509
1495
|
### `LLM`
|
|
1510
1496
|
```python
|
|
1511
1497
|
from webscout.LLM import LLM
|
|
@@ -670,30 +670,11 @@ with WEBS() as WEBS:
|
|
|
670
670
|
## usage of WEBSX -- Another Websearch thing
|
|
671
671
|
```python
|
|
672
672
|
from webscout import WEBSX
|
|
673
|
+
s = "Python development tools"
|
|
673
674
|
|
|
674
|
-
|
|
675
|
-
# Initialize the WEBSX client
|
|
676
|
-
search = WEBSX(
|
|
677
|
-
k=10,
|
|
678
|
-
)
|
|
675
|
+
result = WEBSX(s)
|
|
679
676
|
|
|
680
|
-
|
|
681
|
-
query = "What is the capital of France?"
|
|
682
|
-
answer = search.run(query)
|
|
683
|
-
print(f"Answer: {answer}\n")
|
|
684
|
-
|
|
685
|
-
# Example using `results` method - Get detailed results with metadata
|
|
686
|
-
query = "What is the capital of France?"
|
|
687
|
-
results = search.results(query, num_results=3)
|
|
688
|
-
print("Search Results:")
|
|
689
|
-
for result in results:
|
|
690
|
-
print(f"Title: {result['title']}")
|
|
691
|
-
print(f"Snippet: {result['snippet']}")
|
|
692
|
-
print(f"Link: {result['link']}\n")
|
|
693
|
-
print(f'Engines: {result["engines"]}')
|
|
694
|
-
|
|
695
|
-
if __name__ == "__main__":
|
|
696
|
-
main()
|
|
677
|
+
print(result)
|
|
697
678
|
```
|
|
698
679
|
## ALL acts
|
|
699
680
|
<details>
|
|
@@ -956,7 +937,7 @@ ___
|
|
|
956
937
|
### 0. `Duckchat` - chat with LLM
|
|
957
938
|
```python
|
|
958
939
|
from webscout import WEBS as w
|
|
959
|
-
R = w().chat("
|
|
940
|
+
R = w().chat("Who are you", model='gpt-4o-mini') # GPT-3.5 Turbo, mixtral-8x7b, llama-3-70b, claude-3-haiku, gpt-4o-mini
|
|
960
941
|
print(R)
|
|
961
942
|
```
|
|
962
943
|
### 1. `PhindSearch` - Search using Phind.com
|
|
@@ -1433,7 +1414,12 @@ llama = LLAMA()
|
|
|
1433
1414
|
r = llama.chat("What is the meaning of life?")
|
|
1434
1415
|
print(r)
|
|
1435
1416
|
```
|
|
1436
|
-
|
|
1417
|
+
### 25. AndiSearch
|
|
1418
|
+
```python
|
|
1419
|
+
from webscout import AndiSearch
|
|
1420
|
+
a = AndiSearch()
|
|
1421
|
+
print(a.chat("HelpingAI-9B"))
|
|
1422
|
+
```
|
|
1437
1423
|
### `LLM`
|
|
1438
1424
|
```python
|
|
1439
1425
|
from webscout.LLM import LLM
|
|
@@ -5,7 +5,7 @@ with open("README.md", encoding="utf-8") as f:
|
|
|
5
5
|
|
|
6
6
|
setup(
|
|
7
7
|
name="webscout",
|
|
8
|
-
version="4.
|
|
8
|
+
version="4.5",
|
|
9
9
|
description="Search for anything using Google, DuckDuckGo, brave, qwant, phind.com, Contains AI models, can transcribe yt videos, temporary email and phone number generation, has TTS support, webai (terminal gpt and open interpreter) and offline LLMs and more",
|
|
10
10
|
long_description=README,
|
|
11
11
|
long_description_content_type="text/markdown",
|
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from webscout import WEBS
|
|
3
|
+
import httpx
|
|
4
|
+
from bs4 import BeautifulSoup
|
|
5
|
+
from typing import List, Dict
|
|
6
|
+
|
|
7
|
+
class DeepInfra:
|
|
8
|
+
def __init__(
|
|
9
|
+
self,
|
|
10
|
+
model: str = "meta-llama/Meta-Llama-3.1-70B-Instruct",
|
|
11
|
+
max_tokens: int = 8000,
|
|
12
|
+
timeout: int = 120,
|
|
13
|
+
system_prompt: str = "You are a helpful AI assistant.",
|
|
14
|
+
proxies: dict = {}
|
|
15
|
+
):
|
|
16
|
+
self.model = model
|
|
17
|
+
self.max_tokens = max_tokens
|
|
18
|
+
self.timeout = timeout
|
|
19
|
+
self.system_prompt = system_prompt
|
|
20
|
+
self.chat_endpoint = "https://api.deepinfra.com/v1/openai/chat/completions"
|
|
21
|
+
|
|
22
|
+
self.headers = {
|
|
23
|
+
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36',
|
|
24
|
+
'Accept-Language': 'en,fr-FR;q=0.9,fr;q=0.8,es-ES;q=0.7,es;q=0.6,en-US;q=0.5,am;q=0.4,de;q=0.3',
|
|
25
|
+
'Cache-Control': 'no-cache',
|
|
26
|
+
'Connection': 'keep-alive',
|
|
27
|
+
'Content-Type': 'application/json',
|
|
28
|
+
'Origin': 'https://deepinfra.com',
|
|
29
|
+
'Pragma': 'no-cache',
|
|
30
|
+
'Referer': 'https://deepinfra.com/',
|
|
31
|
+
'Sec-Fetch-Dest': 'empty',
|
|
32
|
+
'Sec-Fetch-Mode': 'cors',
|
|
33
|
+
'Sec-Fetch-Site': 'same-site',
|
|
34
|
+
'X-Deepinfra-Source': 'web-embed',
|
|
35
|
+
'accept': 'text/event-stream',
|
|
36
|
+
'sec-ch-ua': '"Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"',
|
|
37
|
+
'sec-ch-ua-mobile': '?0',
|
|
38
|
+
'sec-ch-ua-platform': '"macOS"'
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
self.client = httpx.Client(proxies=proxies, headers=self.headers)
|
|
42
|
+
|
|
43
|
+
def ask(self, prompt: str, system_prompt: str = None) -> str:
|
|
44
|
+
payload = {
|
|
45
|
+
'model': self.model,
|
|
46
|
+
'messages': [
|
|
47
|
+
{"role": "system", "content": system_prompt or self.system_prompt},
|
|
48
|
+
{"role": "user", "content": prompt},
|
|
49
|
+
],
|
|
50
|
+
'temperature': 0.7,
|
|
51
|
+
'max_tokens': self.max_tokens,
|
|
52
|
+
'stop': []
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
response = self.client.post(self.chat_endpoint, json=payload, timeout=self.timeout)
|
|
56
|
+
if response.status_code != 200:
|
|
57
|
+
raise Exception(f"Failed to generate response - ({response.status_code}, {response.reason_phrase}) - {response.text}")
|
|
58
|
+
|
|
59
|
+
resp = response.json()
|
|
60
|
+
return resp["choices"][0]["message"]["content"]
|
|
61
|
+
|
|
62
|
+
class WebSearchAgent:
|
|
63
|
+
|
|
64
|
+
def __init__(self, model="Qwen/Qwen2-72B-Instruct"):
|
|
65
|
+
self.webs = WEBS()
|
|
66
|
+
self.deepinfra = DeepInfra(model=model)
|
|
67
|
+
|
|
68
|
+
def generate_search_query(self, information):
|
|
69
|
+
prompt = f"""
|
|
70
|
+
Instructions:
|
|
71
|
+
You are a smart online searcher for a large language model.
|
|
72
|
+
Given information, you must create a search query to search the internet for relevant information.
|
|
73
|
+
Your search query must be in the form of a json response.
|
|
74
|
+
Exact json response format must be as follows:
|
|
75
|
+
|
|
76
|
+
{{
|
|
77
|
+
"search_query": "your search query"
|
|
78
|
+
}}
|
|
79
|
+
- You must only provide ONE search query
|
|
80
|
+
- You must provide the BEST search query for the given information
|
|
81
|
+
- The search query must be normal text.
|
|
82
|
+
|
|
83
|
+
Information: {information}
|
|
84
|
+
"""
|
|
85
|
+
|
|
86
|
+
response = self.deepinfra.ask(prompt)
|
|
87
|
+
return json.loads(response)["search_query"]
|
|
88
|
+
|
|
89
|
+
def search(self, information, region='wt-wt', safesearch='off', timelimit='y', max_results=5):
|
|
90
|
+
search_query = self.generate_search_query(information)
|
|
91
|
+
|
|
92
|
+
results = []
|
|
93
|
+
with self.webs as webs:
|
|
94
|
+
for result in webs.text(search_query, region=region, safesearch=safesearch, timelimit=timelimit, max_results=max_results):
|
|
95
|
+
results.append(result)
|
|
96
|
+
|
|
97
|
+
return results
|
|
98
|
+
|
|
99
|
+
def extract_urls(self, results):
|
|
100
|
+
urls = []
|
|
101
|
+
for result in results:
|
|
102
|
+
url = result.get('href')
|
|
103
|
+
if url:
|
|
104
|
+
urls.append(url)
|
|
105
|
+
return list(set(urls)) # Remove duplicates
|
|
106
|
+
|
|
107
|
+
def fetch_webpage(self, url: str) -> str:
|
|
108
|
+
try:
|
|
109
|
+
response = httpx.get(url, timeout=120)
|
|
110
|
+
if response.status_code == 200:
|
|
111
|
+
html = response.text
|
|
112
|
+
soup = BeautifulSoup(html, 'html.parser')
|
|
113
|
+
|
|
114
|
+
# Extract text from <p> tags
|
|
115
|
+
paragraphs = soup.find_all('p')
|
|
116
|
+
text = ' '.join([p.get_text() for p in paragraphs])
|
|
117
|
+
|
|
118
|
+
# Limit the text to around 4000 words
|
|
119
|
+
words = text.split()
|
|
120
|
+
if len(words) > 4000:
|
|
121
|
+
text = ' '.join(words[:4000]) + '...'
|
|
122
|
+
|
|
123
|
+
return text
|
|
124
|
+
else:
|
|
125
|
+
return f"Failed to fetch {url}: HTTP {response.status}"
|
|
126
|
+
except Exception as e:
|
|
127
|
+
return f"Error fetching {url}: {str(e)}"
|
|
128
|
+
|
|
129
|
+
def fetch_all_webpages(self, urls: List[str]) -> List[Dict[str, str]]:
|
|
130
|
+
contents = []
|
|
131
|
+
for url in urls:
|
|
132
|
+
content = self.fetch_webpage(url)
|
|
133
|
+
contents.append({"url": url, "content": content})
|
|
134
|
+
return contents
|
|
135
|
+
|
|
136
|
+
class OnlineSearcher:
|
|
137
|
+
def __init__(self, model="meta-llama/Meta-Llama-3.1-405B-Instruct"):
|
|
138
|
+
self.agent = WebSearchAgent(model)
|
|
139
|
+
self.deepinfra = DeepInfra(model="model")
|
|
140
|
+
|
|
141
|
+
def answer_question(self, question: str) -> str:
|
|
142
|
+
# Perform web search
|
|
143
|
+
search_results = self.agent.search(question)
|
|
144
|
+
|
|
145
|
+
# Extract URLs
|
|
146
|
+
urls = self.agent.extract_urls(search_results)
|
|
147
|
+
|
|
148
|
+
# Fetch webpage contents
|
|
149
|
+
webpage_contents = self.agent.fetch_all_webpages(urls)
|
|
150
|
+
|
|
151
|
+
# Prepare context for AI
|
|
152
|
+
context = "Based on the following search results and webpage contents:\n\n"
|
|
153
|
+
for i, result in enumerate(search_results, 1):
|
|
154
|
+
context += f"{i}. Title: {result['title']}\n URL: {result['href']}\n Snippet: {result['body']}\n\n"
|
|
155
|
+
|
|
156
|
+
context += "Extracted webpage contents:\n"
|
|
157
|
+
for i, webpage in enumerate(webpage_contents):
|
|
158
|
+
context += f"{i}. URL: {webpage['url']}\n Content: {webpage['content'][:4000]}...\n\n"
|
|
159
|
+
|
|
160
|
+
# Generate answer using AI
|
|
161
|
+
prompt = f"{context}\n\nQuestion: {question}\n\nPlease provide a comprehensive answer to the question based on the search results and webpage contents above. Include relevant webpage URLs in your answer when appropriate. If the search results and webpage contents don't contain relevant information, please state that and provide the best answer you can based on your general knowledge. [YOUR RESPONSE WITH SOURCE LINKS ([➊](URL))"
|
|
162
|
+
|
|
163
|
+
answer = self.deepinfra.ask(prompt)
|
|
164
|
+
return answer
|
|
165
|
+
|
|
166
|
+
# Usage example
|
|
167
|
+
if __name__ == "__main__":
|
|
168
|
+
assistant = OnlineSearcher()
|
|
169
|
+
while True:
|
|
170
|
+
question = input(">>> ")
|
|
171
|
+
if question.lower() == 'quit':
|
|
172
|
+
break
|
|
173
|
+
answer = assistant.answer_question(question)
|
|
174
|
+
print(answer)
|
|
175
|
+
print("\n" + "-"*50 + "\n")
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import logging
|
|
3
|
+
from webscout import DeepInfra, WEBS
|
|
4
|
+
|
|
5
|
+
class FunctionCallingAgent:
|
|
6
|
+
def __init__(self, model: str = "Qwen/Qwen2-72B-Instruct", system_prompt: str = 'You are a helpful assistant that will always answer what user wants', tools: list = None):
|
|
7
|
+
self.deepinfra = DeepInfra(model=model, system_prompt=system_prompt)
|
|
8
|
+
self.tools = tools if tools is not None else []
|
|
9
|
+
# logging.basicConfig(level=logging.INFO)
|
|
10
|
+
# self.webs = WEBS() # Initialize a WEBS object for web search
|
|
11
|
+
|
|
12
|
+
def function_call_handler(self, message_text: str):
|
|
13
|
+
"""Handles function calls based on the provided message text
|
|
14
|
+
|
|
15
|
+
Args:
|
|
16
|
+
message_text (str): The input message text from the user.
|
|
17
|
+
|
|
18
|
+
Returns:
|
|
19
|
+
dict: The extracted function call and arguments.
|
|
20
|
+
"""
|
|
21
|
+
system_message = f'[SYSTEM]You are a helpful assistant. You have access to the following functions: \n {str(self.tools)}\n\nTo use these functions respond with:\n<functioncall> {{ "name": "function_name", "arguments": {{ "arg_1": "value_1", "arg_2": "value_2", ... }} }} </functioncall> [USER] {message_text}'
|
|
22
|
+
|
|
23
|
+
response = self.deepinfra.chat(system_message)
|
|
24
|
+
# logging.info(f"Raw response: {response}")
|
|
25
|
+
|
|
26
|
+
try:
|
|
27
|
+
# Extract the JSON-like part of the response
|
|
28
|
+
start_idx = response.find("{")
|
|
29
|
+
end_idx = response.rfind("}") + 1
|
|
30
|
+
if start_idx == -1 or end_idx == -1:
|
|
31
|
+
raise ValueError("JSON-like structure not found in the response")
|
|
32
|
+
|
|
33
|
+
response_json_str = response[start_idx:end_idx]
|
|
34
|
+
# Ensure the JSON string is properly formatted
|
|
35
|
+
response_json_str = response_json_str.replace("'", '"') # Replace single quotes with double quotes
|
|
36
|
+
response_json_str = response_json_str.strip()
|
|
37
|
+
response_data = json.loads(response_json_str)
|
|
38
|
+
except (ValueError, json.JSONDecodeError) as e:
|
|
39
|
+
# logging.error(f"An error occurred while parsing response: {e}")
|
|
40
|
+
return {"error": str(e)}
|
|
41
|
+
|
|
42
|
+
return response_data
|
|
43
|
+
|
|
44
|
+
def execute_function(self, function_call_data: dict) -> str:
|
|
45
|
+
"""Executes the specified function with the provided arguments.
|
|
46
|
+
|
|
47
|
+
Args:
|
|
48
|
+
function_call_data (dict): A dictionary containing the function name and arguments.
|
|
49
|
+
|
|
50
|
+
Returns:
|
|
51
|
+
str: The result of the function execution.
|
|
52
|
+
"""
|
|
53
|
+
function_name = function_call_data.get("name")
|
|
54
|
+
arguments = function_call_data.get("arguments", "{}") # Default to empty dict if not present
|
|
55
|
+
|
|
56
|
+
# Parse the arguments string into a dictionary
|
|
57
|
+
try:
|
|
58
|
+
arguments_dict = json.loads(arguments)
|
|
59
|
+
except json.JSONDecodeError:
|
|
60
|
+
# logging.error("Failed to parse arguments as JSON.")
|
|
61
|
+
return "Invalid arguments format."
|
|
62
|
+
|
|
63
|
+
# logging.info(f"Executing function: {function_name} with arguments: {arguments_dict}")
|
|
64
|
+
|
|
65
|
+
# if function_name == "web_search":
|
|
66
|
+
# query = arguments_dict.get("query")
|
|
67
|
+
# if query:
|
|
68
|
+
# search_results = self.webs.text(query)
|
|
69
|
+
# # You can process the search results here, e.g., extract URLs, summarize, etc.
|
|
70
|
+
# return f"Here's what I found:\n\n{search_results}"
|
|
71
|
+
# else:
|
|
72
|
+
# return "Please provide a search query."
|
|
73
|
+
# else:
|
|
74
|
+
# return f"Function '{function_name}' is not yet implemented."
|
|
75
|
+
|
|
76
|
+
# Example usage
|
|
77
|
+
if __name__ == "__main__":
|
|
78
|
+
tools = [
|
|
79
|
+
{
|
|
80
|
+
"type": "function",
|
|
81
|
+
"function": {
|
|
82
|
+
"name": "UserDetail",
|
|
83
|
+
"parameters": {
|
|
84
|
+
"type": "object",
|
|
85
|
+
"title": "UserDetail",
|
|
86
|
+
"properties": {
|
|
87
|
+
"name": {
|
|
88
|
+
"title": "Name",
|
|
89
|
+
"type": "string"
|
|
90
|
+
},
|
|
91
|
+
"age": {
|
|
92
|
+
"title": "Age",
|
|
93
|
+
"type": "integer"
|
|
94
|
+
}
|
|
95
|
+
},
|
|
96
|
+
"required": ["name", "age"]
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"type": "function",
|
|
102
|
+
"function": {
|
|
103
|
+
"name": "web_search",
|
|
104
|
+
"description": "Search query on google",
|
|
105
|
+
"parameters": {
|
|
106
|
+
"type": "object",
|
|
107
|
+
"properties": {
|
|
108
|
+
"query": {
|
|
109
|
+
"type": "string",
|
|
110
|
+
"description": "web search query"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"required": ["query"]
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
]
|
|
118
|
+
|
|
119
|
+
agent = FunctionCallingAgent(tools=tools)
|
|
120
|
+
message = "tell me about HelpingAI flash"
|
|
121
|
+
function_call_data = agent.function_call_handler(message)
|
|
122
|
+
print(f"Function Call Data: {function_call_data}")
|
|
123
|
+
|
|
124
|
+
if "error" not in function_call_data:
|
|
125
|
+
result = agent.execute_function(function_call_data)
|
|
126
|
+
# print(f"Function Execution Result: {result}")
|