webscout 5.1__py3-none-any.whl → 5.2__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.
- webscout/AIauto.py +83 -277
- webscout/AIbase.py +106 -4
- webscout/AIutel.py +31 -0
- webscout/Agents/Onlinesearcher.py +91 -104
- webscout/Agents/__init__.py +2 -1
- webscout/Agents/ai.py +186 -0
- webscout/Agents/functioncall.py +57 -27
- webscout/Bing_search.py +73 -43
- webscout/Local/_version.py +1 -1
- webscout/Provider/AI21.py +177 -0
- webscout/Provider/Cloudflare.py +0 -4
- webscout/Provider/EDITEE.py +215 -0
- webscout/Provider/NetFly.py +256 -0
- webscout/Provider/TTI/PollinationsAI.py +138 -0
- webscout/Provider/TTI/__init__.py +2 -0
- webscout/Provider/TTI/deepinfra.py +148 -0
- webscout/Provider/TTS/__init__.py +2 -0
- webscout/Provider/TTS/streamElements.py +296 -0
- webscout/Provider/TTS/voicepod.py +114 -0
- webscout/Provider/TeachAnything.py +177 -0
- webscout/Provider/__init__.py +8 -0
- webscout/__init__.py +2 -0
- webscout/version.py +1 -1
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/METADATA +32 -12
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/RECORD +29 -19
- webscout/async_providers.py +0 -21
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/LICENSE.md +0 -0
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/WHEEL +0 -0
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/entry_points.txt +0 -0
- {webscout-5.1.dist-info → webscout-5.2.dist-info}/top_level.txt +0 -0
|
@@ -1,100 +1,81 @@
|
|
|
1
1
|
import json
|
|
2
|
+
import colorlog
|
|
2
3
|
from webscout import WEBS
|
|
4
|
+
from webscout.Agents.ai import LLAMA3
|
|
3
5
|
import httpx
|
|
4
6
|
from bs4 import BeautifulSoup
|
|
5
7
|
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"]
|
|
8
|
+
import logging
|
|
61
9
|
|
|
62
10
|
class WebSearchAgent:
|
|
63
|
-
|
|
64
|
-
def __init__(self, model="Qwen/Qwen2-72B-Instruct"):
|
|
11
|
+
def __init__(self):
|
|
65
12
|
self.webs = WEBS()
|
|
66
|
-
self.
|
|
13
|
+
self.ai = LLAMA3(system="You are an advanced AI assistant specialized in generating optimal search queries and providing comprehensive answers based on web search results.")
|
|
67
14
|
|
|
68
|
-
def
|
|
15
|
+
def generate_search_queries(self, information, num_queries=3):
|
|
69
16
|
prompt = f"""
|
|
17
|
+
Task: Generate {num_queries} optimal search queries based on the given information.
|
|
18
|
+
|
|
70
19
|
Instructions:
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
20
|
+
1. Analyze the provided information carefully.
|
|
21
|
+
2. Identify key concepts, entities, and relationships.
|
|
22
|
+
3. Formulate {num_queries} concise and specific search queries that will yield relevant results.
|
|
23
|
+
4. Each query should focus on a different aspect or angle of the information.
|
|
24
|
+
5. The queries should be in natural language, not in the form of keywords.
|
|
25
|
+
6. Avoid unnecessary words or phrases that might limit the search results.
|
|
26
|
+
|
|
27
|
+
Your response must be in the following JSON format:
|
|
76
28
|
{{
|
|
77
|
-
"
|
|
29
|
+
"search_queries": [
|
|
30
|
+
"Your first search query here",
|
|
31
|
+
"Your second search query here",
|
|
32
|
+
"Your third search query here"
|
|
33
|
+
]
|
|
78
34
|
}}
|
|
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
35
|
|
|
83
|
-
|
|
36
|
+
Ensure that:
|
|
37
|
+
- You provide exactly {num_queries} search queries.
|
|
38
|
+
- Each query is unique and focuses on a different aspect of the information.
|
|
39
|
+
- The queries are in plain text, suitable for a web search engine.
|
|
40
|
+
|
|
41
|
+
Information to base the search queries on:
|
|
42
|
+
{information}
|
|
43
|
+
|
|
44
|
+
Now, generate the optimal search queries:
|
|
84
45
|
"""
|
|
85
46
|
|
|
86
|
-
response =
|
|
87
|
-
|
|
47
|
+
response = ""
|
|
48
|
+
for chunk in self.ai.chat(prompt):
|
|
49
|
+
response += chunk
|
|
50
|
+
|
|
51
|
+
try:
|
|
52
|
+
json_response = json.loads(response)
|
|
53
|
+
return json_response["search_queries"]
|
|
54
|
+
except json.JSONDecodeError:
|
|
55
|
+
print(f"Warning: Failed to parse JSON. Raw response: {response}")
|
|
56
|
+
# Fallback: try to extract queries manually
|
|
57
|
+
queries = []
|
|
58
|
+
for line in response.split('\n'):
|
|
59
|
+
if line.strip().startswith('"') and line.strip().endswith('"'):
|
|
60
|
+
queries.append(line.strip(' "'))
|
|
61
|
+
if queries:
|
|
62
|
+
return queries[:num_queries]
|
|
63
|
+
else:
|
|
64
|
+
print(f"Warning: Using original information as search query.")
|
|
65
|
+
return [information]
|
|
88
66
|
|
|
89
67
|
def search(self, information, region='wt-wt', safesearch='off', timelimit='y', max_results=5):
|
|
90
|
-
|
|
68
|
+
search_queries = self.generate_search_queries(information)
|
|
69
|
+
all_results = []
|
|
91
70
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
71
|
+
for query in search_queries:
|
|
72
|
+
results = []
|
|
73
|
+
with self.webs as webs:
|
|
74
|
+
for result in webs.text(query, region=region, safesearch=safesearch, timelimit=timelimit, max_results=max_results):
|
|
75
|
+
results.append(result)
|
|
76
|
+
all_results.extend(results)
|
|
96
77
|
|
|
97
|
-
return
|
|
78
|
+
return all_results
|
|
98
79
|
|
|
99
80
|
def extract_urls(self, results):
|
|
100
81
|
urls = []
|
|
@@ -102,7 +83,7 @@ class WebSearchAgent:
|
|
|
102
83
|
url = result.get('href')
|
|
103
84
|
if url:
|
|
104
85
|
urls.append(url)
|
|
105
|
-
return list(set(urls))
|
|
86
|
+
return list(set(urls))
|
|
106
87
|
|
|
107
88
|
def fetch_webpage(self, url: str) -> str:
|
|
108
89
|
try:
|
|
@@ -110,16 +91,11 @@ class WebSearchAgent:
|
|
|
110
91
|
if response.status_code == 200:
|
|
111
92
|
html = response.text
|
|
112
93
|
soup = BeautifulSoup(html, 'html.parser')
|
|
113
|
-
|
|
114
|
-
# Extract text from <p> tags
|
|
115
94
|
paragraphs = soup.find_all('p')
|
|
116
95
|
text = ' '.join([p.get_text() for p in paragraphs])
|
|
117
|
-
|
|
118
|
-
# Limit the text to around 4000 words
|
|
119
96
|
words = text.split()
|
|
120
|
-
if len(words) >
|
|
121
|
-
text = ' '.join(words[:
|
|
122
|
-
|
|
97
|
+
if len(words) > 150:
|
|
98
|
+
text = ' '.join(words[:150]) + '...'
|
|
123
99
|
return text
|
|
124
100
|
else:
|
|
125
101
|
return f"Failed to fetch {url}: HTTP {response.status}"
|
|
@@ -134,34 +110,44 @@ class WebSearchAgent:
|
|
|
134
110
|
return contents
|
|
135
111
|
|
|
136
112
|
class OnlineSearcher:
|
|
137
|
-
def __init__(self
|
|
138
|
-
self.agent = WebSearchAgent(
|
|
139
|
-
self.
|
|
113
|
+
def __init__(self):
|
|
114
|
+
self.agent = WebSearchAgent()
|
|
115
|
+
self.ai = LLAMA3(system="You are an advanced AI assistant specialized in providing comprehensive and accurate answers based on web search results and your general knowledge.")
|
|
140
116
|
|
|
141
|
-
def answer_question(self, question: str)
|
|
142
|
-
# Perform web search
|
|
117
|
+
def answer_question(self, question: str):
|
|
143
118
|
search_results = self.agent.search(question)
|
|
144
|
-
|
|
145
|
-
# Extract URLs
|
|
146
119
|
urls = self.agent.extract_urls(search_results)
|
|
147
|
-
|
|
148
|
-
# Fetch webpage contents
|
|
149
120
|
webpage_contents = self.agent.fetch_all_webpages(urls)
|
|
150
121
|
|
|
151
|
-
|
|
152
|
-
context = "Based on the following search results and webpage contents:\n\n"
|
|
122
|
+
context = "Web search results and extracted content:\n\n"
|
|
153
123
|
for i, result in enumerate(search_results, 1):
|
|
154
124
|
context += f"{i}. Title: {result['title']}\n URL: {result['href']}\n Snippet: {result['body']}\n\n"
|
|
155
125
|
|
|
156
126
|
context += "Extracted webpage contents:\n"
|
|
157
127
|
for i, webpage in enumerate(webpage_contents):
|
|
158
|
-
context += f"{i}. URL: {webpage['url']}\n Content: {webpage['content'][:
|
|
128
|
+
context += f"{i}. URL: {webpage['url']}\n Content: {webpage['content'][:150]}...\n\n"
|
|
129
|
+
|
|
130
|
+
prompt = f"""
|
|
131
|
+
Task: Provide a comprehensive and accurate answer to the given question based on the provided web search results and your general knowledge.
|
|
159
132
|
|
|
160
|
-
|
|
161
|
-
|
|
133
|
+
Question: {question}
|
|
134
|
+
|
|
135
|
+
Context:
|
|
136
|
+
{context}
|
|
137
|
+
|
|
138
|
+
Instructions:
|
|
139
|
+
1. Carefully analyze the provided web search results and extracted content.
|
|
140
|
+
2. Synthesize the information to form a coherent and comprehensive answer.
|
|
141
|
+
3. If the search results contain relevant information, incorporate it into your answer.
|
|
142
|
+
4. Don't provide irrelevant information, and don't say "according to web page".
|
|
143
|
+
5. If the search results don't contain sufficient information, clearly state this and provide the best answer based on your general knowledge.
|
|
144
|
+
6. Ensure your answer is well-structured, factual, and directly addresses the question.
|
|
145
|
+
7. If appropriate, provide additional context or related information that might be helpful.
|
|
146
|
+
|
|
147
|
+
Your response should be informative, accurate, and properly sourced when possible. Begin your answer now:
|
|
148
|
+
"""
|
|
162
149
|
|
|
163
|
-
|
|
164
|
-
return answer
|
|
150
|
+
return self.ai.chat(prompt)
|
|
165
151
|
|
|
166
152
|
# Usage example
|
|
167
153
|
if __name__ == "__main__":
|
|
@@ -170,6 +156,7 @@ if __name__ == "__main__":
|
|
|
170
156
|
question = input(">>> ")
|
|
171
157
|
if question.lower() == 'quit':
|
|
172
158
|
break
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
159
|
+
print("\n" + "="*50)
|
|
160
|
+
for chunk in assistant.answer_question(question):
|
|
161
|
+
print(chunk, end="", flush=True)
|
|
162
|
+
print("\n" + "="*50)
|
webscout/Agents/__init__.py
CHANGED
webscout/Agents/ai.py
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
from webscout.AIutel import Optimizers
|
|
4
|
+
from webscout.AIutel import Conversation
|
|
5
|
+
from webscout.AIutel import AwesomePrompts
|
|
6
|
+
from webscout.AIbase import Provider
|
|
7
|
+
|
|
8
|
+
class LLAMA3(Provider):
|
|
9
|
+
|
|
10
|
+
AVAILABLE_MODELS = ["llama3-70b", "llama3-8b", "llama3-405b"]
|
|
11
|
+
|
|
12
|
+
def __init__(
|
|
13
|
+
self,
|
|
14
|
+
is_conversation: bool = True,
|
|
15
|
+
max_tokens: int = 600,
|
|
16
|
+
timeout: int = 30,
|
|
17
|
+
intro: str = None,
|
|
18
|
+
filepath: str = None,
|
|
19
|
+
update_file: bool = True,
|
|
20
|
+
proxies: dict = {},
|
|
21
|
+
history_offset: int = 10250,
|
|
22
|
+
act: str = None,
|
|
23
|
+
model: str = "llama3-8b",
|
|
24
|
+
system: str = "GPT syle",
|
|
25
|
+
):
|
|
26
|
+
"""Instantiates Snova
|
|
27
|
+
|
|
28
|
+
Args:
|
|
29
|
+
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
30
|
+
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
31
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
32
|
+
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
33
|
+
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
34
|
+
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
35
|
+
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
36
|
+
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
37
|
+
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
38
|
+
model (str, optional): Snova model name. Defaults to "llama3-70b".
|
|
39
|
+
system (str, optional): System prompt for Snova. Defaults to "Answer as concisely as possible.".
|
|
40
|
+
"""
|
|
41
|
+
if model not in self.AVAILABLE_MODELS:
|
|
42
|
+
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
43
|
+
|
|
44
|
+
self.session = requests.Session()
|
|
45
|
+
self.is_conversation = is_conversation
|
|
46
|
+
self.max_tokens_to_sample = max_tokens
|
|
47
|
+
self.timeout = timeout
|
|
48
|
+
self.model = model
|
|
49
|
+
self.system = system
|
|
50
|
+
self.last_response = {}
|
|
51
|
+
self.env_type = "tp16405b" if "405b" in model else "tp16"
|
|
52
|
+
self.headers = {'content-type': 'application/json'}
|
|
53
|
+
|
|
54
|
+
self.__available_optimizers = (
|
|
55
|
+
method
|
|
56
|
+
for method in dir(Optimizers)
|
|
57
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
58
|
+
)
|
|
59
|
+
self.session.headers.update(self.headers)
|
|
60
|
+
Conversation.intro = (
|
|
61
|
+
AwesomePrompts().get_act(
|
|
62
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
63
|
+
)
|
|
64
|
+
if act
|
|
65
|
+
else intro or Conversation.intro
|
|
66
|
+
)
|
|
67
|
+
self.conversation = Conversation(
|
|
68
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
69
|
+
)
|
|
70
|
+
self.conversation.history_offset = history_offset
|
|
71
|
+
self.session.proxies = proxies
|
|
72
|
+
|
|
73
|
+
def ask(
|
|
74
|
+
self,
|
|
75
|
+
prompt: str,
|
|
76
|
+
stream: bool = False,
|
|
77
|
+
raw: bool = False,
|
|
78
|
+
optimizer: str = None,
|
|
79
|
+
conversationally: bool = False,
|
|
80
|
+
) -> dict:
|
|
81
|
+
"""Chat with AI
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
prompt (str): Prompt to be send.
|
|
85
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
86
|
+
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
87
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
88
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
89
|
+
Returns:
|
|
90
|
+
dict : {}
|
|
91
|
+
```json
|
|
92
|
+
{
|
|
93
|
+
"text" : "How may I assist you today?"
|
|
94
|
+
}
|
|
95
|
+
```
|
|
96
|
+
"""
|
|
97
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
98
|
+
if optimizer:
|
|
99
|
+
if optimizer in self.__available_optimizers:
|
|
100
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
101
|
+
conversation_prompt if conversationally else prompt
|
|
102
|
+
)
|
|
103
|
+
else:
|
|
104
|
+
raise Exception(
|
|
105
|
+
f"Optimizer is not one of {self.__available_optimizers}"
|
|
106
|
+
)
|
|
107
|
+
data = {'body': {'messages': [{'role': 'system', 'content': self.system}, {'role': 'user', 'content': conversation_prompt}], 'stream': True, 'model': self.model}, 'env_type': self.env_type}
|
|
108
|
+
|
|
109
|
+
def for_stream(data=data): # Pass data as a default argument
|
|
110
|
+
response = self.session.post('https://fast.snova.ai/api/completion', headers=self.headers, json=data, stream=True, timeout=self.timeout)
|
|
111
|
+
output = ''
|
|
112
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
113
|
+
if line.startswith('data:'):
|
|
114
|
+
try:
|
|
115
|
+
data = json.loads(line[len('data: '):])
|
|
116
|
+
output += data.get("choices", [{}])[0].get("delta", {}).get("content", '')
|
|
117
|
+
self.last_response.update(dict(text=output))
|
|
118
|
+
yield data if raw else dict(text=output)
|
|
119
|
+
except json.JSONDecodeError:
|
|
120
|
+
if line[len('data: '):] == '[DONE]':
|
|
121
|
+
break
|
|
122
|
+
self.conversation.update_chat_history(
|
|
123
|
+
prompt, self.get_message(self.last_response)
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
def for_non_stream():
|
|
127
|
+
for _ in for_stream():
|
|
128
|
+
pass
|
|
129
|
+
return self.last_response
|
|
130
|
+
|
|
131
|
+
return for_stream() if stream else for_non_stream()
|
|
132
|
+
|
|
133
|
+
def chat(
|
|
134
|
+
self,
|
|
135
|
+
prompt: str,
|
|
136
|
+
stream: bool = False,
|
|
137
|
+
optimizer: str = None,
|
|
138
|
+
conversationally: bool = False,
|
|
139
|
+
) -> str:
|
|
140
|
+
"""Generate response `str`
|
|
141
|
+
Args:
|
|
142
|
+
prompt (str): Prompt to be send.
|
|
143
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
144
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
145
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
146
|
+
Returns:
|
|
147
|
+
str: Response generated
|
|
148
|
+
"""
|
|
149
|
+
|
|
150
|
+
def for_stream():
|
|
151
|
+
for response in self.ask(
|
|
152
|
+
prompt, True, optimizer=optimizer, conversationally=conversationally
|
|
153
|
+
):
|
|
154
|
+
yield self.get_message(response)
|
|
155
|
+
|
|
156
|
+
def for_non_stream():
|
|
157
|
+
return self.get_message(
|
|
158
|
+
self.ask(
|
|
159
|
+
prompt,
|
|
160
|
+
False,
|
|
161
|
+
optimizer=optimizer,
|
|
162
|
+
conversationally=conversationally,
|
|
163
|
+
)
|
|
164
|
+
)
|
|
165
|
+
|
|
166
|
+
return for_stream() if stream else for_non_stream()
|
|
167
|
+
|
|
168
|
+
def get_message(self, response: dict) -> str:
|
|
169
|
+
"""Retrieves message only from response
|
|
170
|
+
|
|
171
|
+
Args:
|
|
172
|
+
response (dict): Response generated by `self.ask`
|
|
173
|
+
|
|
174
|
+
Returns:
|
|
175
|
+
str: Message extracted
|
|
176
|
+
"""
|
|
177
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
178
|
+
return response["text"]
|
|
179
|
+
if __name__ == "__main__":
|
|
180
|
+
from rich import print
|
|
181
|
+
|
|
182
|
+
ai = LLAMA3()
|
|
183
|
+
# Stream the response
|
|
184
|
+
response = ai.chat(input(">>> "))
|
|
185
|
+
for chunk in response:
|
|
186
|
+
print(chunk, end="", flush=True)
|
webscout/Agents/functioncall.py
CHANGED
|
@@ -1,6 +1,62 @@
|
|
|
1
1
|
import json
|
|
2
2
|
import logging
|
|
3
|
-
|
|
3
|
+
import time
|
|
4
|
+
from typing import Any, Dict, Optional
|
|
5
|
+
|
|
6
|
+
import requests
|
|
7
|
+
from webscout import WEBS # Import only WEBS from webscout
|
|
8
|
+
|
|
9
|
+
class LLAMA3:
|
|
10
|
+
|
|
11
|
+
AVAILABLE_MODELS = ["llama3-70b", "llama3-8b", "llama3-405b"]
|
|
12
|
+
|
|
13
|
+
def __init__(
|
|
14
|
+
self,
|
|
15
|
+
is_conversation: bool = True,
|
|
16
|
+
max_tokens: int = 600,
|
|
17
|
+
timeout: int = 30,
|
|
18
|
+
model: str = "llama3-8b",
|
|
19
|
+
system: str = "GPT syle",
|
|
20
|
+
proxies: dict = {}, # Add proxies parameter
|
|
21
|
+
):
|
|
22
|
+
"""Instantiates Snova
|
|
23
|
+
|
|
24
|
+
Args:
|
|
25
|
+
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
26
|
+
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
27
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
28
|
+
model (str, optional): Snova model name. Defaults to "llama3-70b".
|
|
29
|
+
system (str, optional): System prompt for Snova. Defaults to "Answer as concisely as possible.".
|
|
30
|
+
proxies (dict, optional): Proxy settings for requests. Defaults to an empty dictionary.
|
|
31
|
+
"""
|
|
32
|
+
if model not in self.AVAILABLE_MODELS:
|
|
33
|
+
raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
|
|
34
|
+
|
|
35
|
+
self.session = requests.Session()
|
|
36
|
+
self.is_conversation = is_conversation
|
|
37
|
+
self.max_tokens_to_sample = max_tokens
|
|
38
|
+
self.timeout = timeout
|
|
39
|
+
self.model = model
|
|
40
|
+
self.system = system
|
|
41
|
+
self.last_response = {}
|
|
42
|
+
self.env_type = "tp16405b" if "405b" in model else "tp16"
|
|
43
|
+
self.headers = {'content-type': 'application/json'}
|
|
44
|
+
self.session.headers.update(self.headers)
|
|
45
|
+
self.session.proxies = proxies
|
|
46
|
+
|
|
47
|
+
def chat(self, prompt: str) -> str:
|
|
48
|
+
data = {'body': {'messages': [{'role': 'system', 'content': self.system}, {'role': 'user', 'content': prompt}], 'stream': True, 'model': self.model}, 'env_type': self.env_type}
|
|
49
|
+
response = self.session.post('https://fast.snova.ai/api/completion', headers=self.headers, json=data, stream=True, timeout=self.timeout)
|
|
50
|
+
output = ''
|
|
51
|
+
for line in response.iter_lines(decode_unicode=True):
|
|
52
|
+
if line.startswith('data:'):
|
|
53
|
+
try:
|
|
54
|
+
data = json.loads(line[len('data: '):])
|
|
55
|
+
output += data.get("choices", [{}])[0].get("delta", {}).get("content", '')
|
|
56
|
+
except json.JSONDecodeError:
|
|
57
|
+
if line[len('data: '):] == '[DONE]':
|
|
58
|
+
break
|
|
59
|
+
return output
|
|
4
60
|
|
|
5
61
|
class FunctionCallingAgent:
|
|
6
62
|
def __init__(self, model: str = "llama3-8b",
|
|
@@ -66,32 +122,6 @@ class FunctionCallingAgent:
|
|
|
66
122
|
|
|
67
123
|
logging.info(f"Executing function: {function_name} with arguments: {arguments}")
|
|
68
124
|
|
|
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."
|
|
75
|
-
|
|
76
|
-
# def _handle_web_search(self, arguments: dict) -> str:
|
|
77
|
-
# query = arguments.get("query")
|
|
78
|
-
# if not query:
|
|
79
|
-
# return "Please provide a search query."
|
|
80
|
-
|
|
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
|
|
95
125
|
|
|
96
126
|
# Example usage
|
|
97
127
|
if __name__ == "__main__":
|