webscout 4.7__py3-none-any.whl → 4.9__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/Agents/functioncall.py +97 -37
- webscout/Bard.py +365 -0
- webscout/Bing_search.py +124 -0
- webscout/DWEBS.py +141 -777
- webscout/Local/_version.py +1 -1
- webscout/Provider/Andi.py +7 -1
- webscout/Provider/BasedGPT.py +11 -5
- webscout/Provider/Berlin4h.py +11 -5
- webscout/Provider/Blackboxai.py +10 -4
- webscout/Provider/Cloudflare.py +286 -0
- webscout/Provider/Cohere.py +11 -5
- webscout/Provider/DARKAI.py +25 -7
- webscout/Provider/Deepinfra.py +2 -1
- webscout/Provider/Deepseek.py +25 -9
- webscout/Provider/DiscordRocks.py +389 -0
- webscout/Provider/Farfalle.py +227 -0
- webscout/Provider/Gemini.py +1 -1
- webscout/Provider/Groq.py +244 -110
- webscout/Provider/Llama.py +13 -5
- webscout/Provider/Llama3.py +15 -2
- webscout/Provider/OLLAMA.py +8 -7
- webscout/Provider/{Geminiflash.py → PI.py} +96 -40
- webscout/Provider/Perplexity.py +422 -52
- webscout/Provider/Phind.py +6 -5
- webscout/Provider/PizzaGPT.py +7 -1
- webscout/Provider/Youchat.py +98 -76
- webscout/Provider/__init__.py +26 -31
- webscout/Provider/ai4chat.py +193 -0
- webscout/Provider/{VTLchat.py → felo_search.py} +62 -76
- webscout/Provider/julius.py +263 -0
- webscout/Provider/koala.py +11 -5
- webscout/Provider/liaobots.py +268 -0
- webscout/Provider/meta.py +2 -1
- webscout/Provider/{ChatGPTUK.py → turboseek.py} +79 -56
- webscout/Provider/{FreeGemini.py → xdash.py} +51 -18
- webscout/Provider/yep.py +258 -0
- webscout/__init__.py +1 -59
- webscout/version.py +1 -1
- webscout/webai.py +2 -64
- webscout/webscout_search.py +1 -1
- {webscout-4.7.dist-info → webscout-4.9.dist-info}/METADATA +249 -323
- webscout-4.9.dist-info/RECORD +83 -0
- webscout/GoogleS.py +0 -342
- webscout/Provider/Geminipro.py +0 -152
- webscout/Provider/Leo.py +0 -469
- webscout/Provider/OpenGPT.py +0 -867
- webscout/Provider/Xjai.py +0 -230
- webscout/Provider/Yepchat.py +0 -478
- webscout-4.7.dist-info/RECORD +0 -80
- {webscout-4.7.dist-info → webscout-4.9.dist-info}/LICENSE.md +0 -0
- {webscout-4.7.dist-info → webscout-4.9.dist-info}/WHEEL +0 -0
- {webscout-4.7.dist-info → webscout-4.9.dist-info}/entry_points.txt +0 -0
- {webscout-4.7.dist-info → webscout-4.9.dist-info}/top_level.txt +0 -0
webscout/Provider/Youchat.py
CHANGED
|
@@ -19,18 +19,22 @@ import io
|
|
|
19
19
|
import re
|
|
20
20
|
import json
|
|
21
21
|
import yaml
|
|
22
|
-
from
|
|
23
|
-
from
|
|
24
|
-
from
|
|
25
|
-
from
|
|
26
|
-
from Helpingai_T2 import Perplexity
|
|
22
|
+
from webscout.AIutel import Optimizers
|
|
23
|
+
from webscout.AIutel import Conversation
|
|
24
|
+
from webscout.AIutel import AwesomePrompts, sanitize_stream
|
|
25
|
+
from webscout.AIbase import Provider, AsyncProvider
|
|
27
26
|
from webscout import exceptions
|
|
28
27
|
from typing import Any, AsyncGenerator, Dict
|
|
29
28
|
import logging
|
|
30
29
|
import httpx
|
|
30
|
+
import cloudscraper
|
|
31
|
+
|
|
31
32
|
|
|
32
|
-
#-------------------------------------------------------youchat--------------------------------------------------------
|
|
33
33
|
class YouChat(Provider):
|
|
34
|
+
"""
|
|
35
|
+
This class provides methods for interacting with the You.com chat API in a consistent provider structure.
|
|
36
|
+
"""
|
|
37
|
+
|
|
34
38
|
def __init__(
|
|
35
39
|
self,
|
|
36
40
|
is_conversation: bool = True,
|
|
@@ -43,34 +47,42 @@ class YouChat(Provider):
|
|
|
43
47
|
history_offset: int = 10250,
|
|
44
48
|
act: str = None,
|
|
45
49
|
):
|
|
46
|
-
|
|
50
|
+
"""Instantiates YouChat
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
54
|
+
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
55
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
56
|
+
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
57
|
+
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
58
|
+
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
59
|
+
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
60
|
+
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
61
|
+
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
62
|
+
"""
|
|
63
|
+
self.session = cloudscraper.create_scraper() # Create a Cloudscraper session
|
|
47
64
|
self.is_conversation = is_conversation
|
|
48
65
|
self.max_tokens_to_sample = max_tokens
|
|
49
66
|
self.chat_endpoint = "https://you.com/api/streamingSearch"
|
|
50
67
|
self.stream_chunk_size = 64
|
|
51
68
|
self.timeout = timeout
|
|
52
69
|
self.last_response = {}
|
|
53
|
-
|
|
54
|
-
self.payload = {
|
|
55
|
-
"q": "",
|
|
56
|
-
"page": 1,
|
|
57
|
-
"count": 10,
|
|
58
|
-
"safeSearch": "Off",
|
|
59
|
-
"onShoppingPage": False,
|
|
60
|
-
"mkt": "",
|
|
61
|
-
"responseFilter": "WebPages,Translations,TimeZone,Computation,RelatedSearches",
|
|
62
|
-
"domain": "youchat",
|
|
63
|
-
"queryTraceId": uuid.uuid4(),
|
|
64
|
-
"conversationTurnId": uuid.uuid4(),
|
|
65
|
-
"pastChatLength": 0,
|
|
66
|
-
"selectedChatMode": "default",
|
|
67
|
-
"chat": "[]",
|
|
68
|
-
}
|
|
69
|
-
|
|
70
70
|
self.headers = {
|
|
71
|
-
"
|
|
72
|
-
|
|
73
|
-
|
|
71
|
+
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0",
|
|
72
|
+
"Accept": "text/event-stream",
|
|
73
|
+
"Accept-Language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
74
|
+
"Referer": "https://you.com/search?q=hi&fromSearchBar=true&tbm=youchat",
|
|
75
|
+
"Connection": "keep-alive",
|
|
76
|
+
"DNT": "1",
|
|
77
|
+
}
|
|
78
|
+
self.cookies = {
|
|
79
|
+
"uuid_guest_backup": uuid4().hex,
|
|
80
|
+
"youchat_personalization": "true",
|
|
81
|
+
"youchat_smart_learn": "true",
|
|
82
|
+
"youpro_subscription": "false",
|
|
83
|
+
"ydc_stytch_session": uuid4().hex,
|
|
84
|
+
"ydc_stytch_session_jwt": uuid4().hex,
|
|
85
|
+
"__cf_bm": uuid4().hex,
|
|
74
86
|
}
|
|
75
87
|
|
|
76
88
|
self.__available_optimizers = (
|
|
@@ -78,7 +90,6 @@ class YouChat(Provider):
|
|
|
78
90
|
for method in dir(Optimizers)
|
|
79
91
|
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
80
92
|
)
|
|
81
|
-
self.session.headers.update(self.headers)
|
|
82
93
|
Conversation.intro = (
|
|
83
94
|
AwesomePrompts().get_act(
|
|
84
95
|
act, raise_not_found=True, default=None, case_insensitive=True
|
|
@@ -100,6 +111,22 @@ class YouChat(Provider):
|
|
|
100
111
|
optimizer: str = None,
|
|
101
112
|
conversationally: bool = False,
|
|
102
113
|
) -> dict:
|
|
114
|
+
"""Chat with AI
|
|
115
|
+
|
|
116
|
+
Args:
|
|
117
|
+
prompt (str): Prompt to be send.
|
|
118
|
+
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
119
|
+
raw (bool, optional): Stream back raw response as received. Defaults to False.
|
|
120
|
+
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
121
|
+
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
122
|
+
Returns:
|
|
123
|
+
dict : {}
|
|
124
|
+
```json
|
|
125
|
+
{
|
|
126
|
+
"text" : "How may I assist you today?"
|
|
127
|
+
}
|
|
128
|
+
```
|
|
129
|
+
"""
|
|
103
130
|
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
104
131
|
if optimizer:
|
|
105
132
|
if optimizer in self.__available_optimizers:
|
|
@@ -110,44 +137,54 @@ class YouChat(Provider):
|
|
|
110
137
|
raise Exception(
|
|
111
138
|
f"Optimizer is not one of {self.__available_optimizers}"
|
|
112
139
|
)
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
140
|
+
|
|
141
|
+
payload = {
|
|
142
|
+
"q": conversation_prompt,
|
|
143
|
+
"page": 1,
|
|
144
|
+
"count": 10,
|
|
145
|
+
"safeSearch": "Moderate",
|
|
146
|
+
"mkt": "en-IN",
|
|
147
|
+
"domain": "youchat",
|
|
148
|
+
"use_personalization_extraction": "true",
|
|
149
|
+
"queryTraceId": str(uuid4()),
|
|
150
|
+
"chatId": str(uuid4()),
|
|
151
|
+
"conversationTurnId": str(uuid4()),
|
|
152
|
+
"pastChatLength": 0,
|
|
153
|
+
"isSmallMediumDevice": "true",
|
|
154
|
+
"selectedChatMode": "default",
|
|
155
|
+
"traceId": str(uuid4()),
|
|
156
|
+
"chat": "[]"
|
|
157
|
+
}
|
|
120
158
|
|
|
121
159
|
def for_stream():
|
|
122
160
|
response = self.session.get(
|
|
123
|
-
self.chat_endpoint,
|
|
124
|
-
params=self.payload,
|
|
125
|
-
stream=True,
|
|
126
|
-
timeout=self.timeout,
|
|
161
|
+
self.chat_endpoint, headers=self.headers, cookies=self.cookies, params=payload, stream=True, timeout=self.timeout
|
|
127
162
|
)
|
|
128
|
-
|
|
129
163
|
if not response.ok:
|
|
130
164
|
raise exceptions.FailedToGenerateResponseError(
|
|
131
|
-
f"Failed to generate response - ({response.status_code}, {response.reason})"
|
|
165
|
+
f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
|
|
132
166
|
)
|
|
133
167
|
|
|
134
|
-
|
|
135
|
-
for
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
168
|
+
streaming_text = ""
|
|
169
|
+
for value in response.iter_lines(
|
|
170
|
+
decode_unicode=True,
|
|
171
|
+
chunk_size=self.stream_chunk_size,
|
|
172
|
+
delimiter="\n",
|
|
173
|
+
):
|
|
174
|
+
try:
|
|
175
|
+
if bool(value) and value.startswith('data: ') and 'youChatToken' in value:
|
|
176
|
+
data = json.loads(value[6:])
|
|
177
|
+
token = data.get('youChatToken', '')
|
|
178
|
+
if token:
|
|
179
|
+
streaming_text += token
|
|
180
|
+
resp = dict(text=streaming_text)
|
|
181
|
+
self.last_response.update(resp)
|
|
182
|
+
yield value if raw else resp
|
|
183
|
+
except json.decoder.JSONDecodeError:
|
|
184
|
+
pass
|
|
147
185
|
self.conversation.update_chat_history(
|
|
148
186
|
prompt, self.get_message(self.last_response)
|
|
149
187
|
)
|
|
150
|
-
return streaming_response
|
|
151
188
|
|
|
152
189
|
def for_non_stream():
|
|
153
190
|
for _ in for_stream():
|
|
@@ -156,23 +193,6 @@ class YouChat(Provider):
|
|
|
156
193
|
|
|
157
194
|
return for_stream() if stream else for_non_stream()
|
|
158
195
|
|
|
159
|
-
def chat(
|
|
160
|
-
self,
|
|
161
|
-
prompt: str,
|
|
162
|
-
stream: bool = False,
|
|
163
|
-
optimizer: str = None,
|
|
164
|
-
conversationally: bool = False,
|
|
165
|
-
) -> str:
|
|
166
|
-
"""Generate response `str`
|
|
167
|
-
Args:
|
|
168
|
-
prompt (str): Prompt to be send.
|
|
169
|
-
stream (bool, optional): Flag for streaming response. Defaults to False.
|
|
170
|
-
optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
|
|
171
|
-
conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
|
|
172
|
-
Returns:
|
|
173
|
-
str: Response generated
|
|
174
|
-
"""
|
|
175
|
-
|
|
176
196
|
def chat(
|
|
177
197
|
self,
|
|
178
198
|
prompt: str,
|
|
@@ -219,7 +239,9 @@ class YouChat(Provider):
|
|
|
219
239
|
"""
|
|
220
240
|
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
221
241
|
return response["text"]
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
242
|
+
if __name__ == '__main__':
|
|
243
|
+
from rich import print
|
|
244
|
+
ai = YouChat()
|
|
245
|
+
response = ai.chat(input(">>> "))
|
|
246
|
+
for chunk in response:
|
|
247
|
+
print(chunk, end="", flush=True)
|
webscout/Provider/__init__.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# webscout/providers/__init__.py
|
|
2
2
|
|
|
3
3
|
from .ThinkAnyAI import ThinkAnyAI
|
|
4
|
-
from .
|
|
4
|
+
from .PI import *
|
|
5
5
|
from .Llama import LLAMA
|
|
6
6
|
from .Cohere import Cohere
|
|
7
7
|
from .Reka import REKA
|
|
@@ -9,35 +9,24 @@ from .Groq import GROQ
|
|
|
9
9
|
from .Groq import AsyncGROQ
|
|
10
10
|
from .Openai import OPENAI
|
|
11
11
|
from .Openai import AsyncOPENAI
|
|
12
|
-
from .Leo import LEO
|
|
13
|
-
from .Leo import AsyncLEO
|
|
14
12
|
from .Koboldai import KOBOLDAI
|
|
15
13
|
from .Koboldai import AsyncKOBOLDAI
|
|
16
|
-
from .
|
|
17
|
-
from .OpenGPT import OPENGPTv2
|
|
18
|
-
from .OpenGPT import AsyncOPENGPT
|
|
19
|
-
from .Perplexity import PERPLEXITY
|
|
14
|
+
from .Perplexity import *
|
|
20
15
|
from .Blackboxai import BLACKBOXAI
|
|
21
16
|
from .Blackboxai import AsyncBLACKBOXAI
|
|
22
17
|
from .Phind import PhindSearch
|
|
23
18
|
from .Phind import AsyncPhindSearch
|
|
24
19
|
from .Phind import Phindv2
|
|
25
20
|
from .Phind import AsyncPhindv2
|
|
26
|
-
from .
|
|
27
|
-
from .Yepchat import AsyncYEPCHAT
|
|
28
|
-
from .Youchat import YouChat
|
|
21
|
+
from .ai4chat import *
|
|
29
22
|
from .Gemini import GEMINI
|
|
30
23
|
from .Berlin4h import Berlin4h
|
|
31
|
-
from .ChatGPTUK import ChatGPTUK
|
|
32
24
|
from .Poe import POE
|
|
33
25
|
from .BasedGPT import BasedGPT
|
|
34
26
|
from .Deepseek import DeepSeek
|
|
35
27
|
from .Deepinfra import DeepInfra, VLM, AsyncDeepInfra
|
|
36
|
-
from .
|
|
37
|
-
from .Geminipro import GEMINIPRO
|
|
38
|
-
from .Geminiflash import GEMINIFLASH
|
|
28
|
+
from .Farfalle import *
|
|
39
29
|
from .OLLAMA import OLLAMA
|
|
40
|
-
from .FreeGemini import FreeGemini
|
|
41
30
|
from .Andi import AndiSearch
|
|
42
31
|
from .PizzaGPT import *
|
|
43
32
|
from .Llama3 import *
|
|
@@ -45,10 +34,18 @@ from .DARKAI import *
|
|
|
45
34
|
from .koala import *
|
|
46
35
|
from .RUBIKSAI import *
|
|
47
36
|
from .meta import *
|
|
48
|
-
|
|
37
|
+
from .liaobots import *
|
|
38
|
+
from .DiscordRocks import *
|
|
39
|
+
from .felo_search import *
|
|
40
|
+
from .xdash import *
|
|
41
|
+
from .julius import *
|
|
42
|
+
from .Youchat import *
|
|
43
|
+
from .yep import *
|
|
44
|
+
from .Cloudflare import *
|
|
45
|
+
from .turboseek import *
|
|
49
46
|
__all__ = [
|
|
50
47
|
'ThinkAnyAI',
|
|
51
|
-
'
|
|
48
|
+
'Farfalle',
|
|
52
49
|
'LLAMA',
|
|
53
50
|
'Cohere',
|
|
54
51
|
'REKA',
|
|
@@ -56,37 +53,26 @@ __all__ = [
|
|
|
56
53
|
'AsyncGROQ',
|
|
57
54
|
'OPENAI',
|
|
58
55
|
'AsyncOPENAI',
|
|
59
|
-
'LEO',
|
|
60
|
-
'AsyncLEO',
|
|
61
56
|
'KOBOLDAI',
|
|
62
57
|
'AsyncKOBOLDAI',
|
|
63
|
-
'
|
|
64
|
-
'AsyncOPENGPT',
|
|
65
|
-
'PERPLEXITY',
|
|
58
|
+
'Perplexity',
|
|
66
59
|
'BLACKBOXAI',
|
|
67
60
|
'AsyncBLACKBOXAI',
|
|
68
61
|
'PhindSearch',
|
|
69
62
|
'AsyncPhindSearch',
|
|
70
|
-
'
|
|
71
|
-
'AsyncYEPCHAT',
|
|
72
|
-
'YouChat',
|
|
63
|
+
'Felo',
|
|
73
64
|
'GEMINI',
|
|
74
65
|
'Berlin4h',
|
|
75
|
-
'ChatGPTUK',
|
|
76
66
|
'POE',
|
|
77
67
|
'BasedGPT',
|
|
78
68
|
'DeepSeek',
|
|
79
69
|
'DeepInfra',
|
|
80
70
|
'VLM',
|
|
81
71
|
'AsyncDeepInfra',
|
|
82
|
-
'
|
|
72
|
+
'AI4Chat',
|
|
83
73
|
'AsyncPhindv2',
|
|
84
74
|
'Phindv2',
|
|
85
|
-
'OPENGPTv2',
|
|
86
|
-
'GEMINIPRO',
|
|
87
|
-
'GEMINIFLASH',
|
|
88
75
|
'OLLAMA',
|
|
89
|
-
'FreeGemini',
|
|
90
76
|
'AndiSearch',
|
|
91
77
|
'PIZZAGPT',
|
|
92
78
|
'LLAMA3',
|
|
@@ -94,4 +80,13 @@ __all__ = [
|
|
|
94
80
|
'KOALA',
|
|
95
81
|
'RUBIKSAI',
|
|
96
82
|
'Meta',
|
|
83
|
+
'LiaoBots',
|
|
84
|
+
'DiscordRocks',
|
|
85
|
+
'PiAI',
|
|
86
|
+
'XDASH',
|
|
87
|
+
'Julius',
|
|
88
|
+
'YouChat',
|
|
89
|
+
'YEPCHAT',
|
|
90
|
+
'Cloudflare',
|
|
91
|
+
'TurboSeek',
|
|
97
92
|
]
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
import requests
|
|
2
|
+
import json
|
|
3
|
+
import html
|
|
4
|
+
from re import sub
|
|
5
|
+
from typing import Any, Dict
|
|
6
|
+
|
|
7
|
+
from webscout.AIutel import Optimizers
|
|
8
|
+
from webscout.AIutel import Conversation
|
|
9
|
+
from webscout.AIutel import AwesomePrompts
|
|
10
|
+
from webscout.AIbase import Provider
|
|
11
|
+
|
|
12
|
+
class AI4Chat(Provider):
|
|
13
|
+
"""
|
|
14
|
+
A class to interact with the AI4Chat API.
|
|
15
|
+
"""
|
|
16
|
+
|
|
17
|
+
def __init__(
|
|
18
|
+
self,
|
|
19
|
+
is_conversation: bool = True,
|
|
20
|
+
max_tokens: int = 600,
|
|
21
|
+
timeout: int = 30,
|
|
22
|
+
intro: str = None,
|
|
23
|
+
filepath: str = None,
|
|
24
|
+
update_file: bool = True,
|
|
25
|
+
proxies: dict = {},
|
|
26
|
+
history_offset: int = 10250,
|
|
27
|
+
act: str = None,
|
|
28
|
+
system_prompt: str = "You are a helpful and informative AI assistant.",
|
|
29
|
+
) -> None:
|
|
30
|
+
"""
|
|
31
|
+
Initializes the AI4Chat API with given parameters.
|
|
32
|
+
|
|
33
|
+
Args:
|
|
34
|
+
is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
|
|
35
|
+
max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
|
|
36
|
+
timeout (int, optional): Http request timeout. Defaults to 30.
|
|
37
|
+
intro (str, optional): Conversation introductory prompt. Defaults to None.
|
|
38
|
+
filepath (str, optional): Path to file containing conversation history. Defaults to None.
|
|
39
|
+
update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
|
|
40
|
+
proxies (dict, optional): Http request proxies. Defaults to {}.
|
|
41
|
+
history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
|
|
42
|
+
act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
|
|
43
|
+
system_prompt (str, optional): System prompt to guide the AI's behavior. Defaults to "You are a helpful and informative AI assistant.".
|
|
44
|
+
"""
|
|
45
|
+
self.session = requests.Session()
|
|
46
|
+
self.is_conversation = is_conversation
|
|
47
|
+
self.max_tokens_to_sample = max_tokens
|
|
48
|
+
self.api_endpoint = "https://www.ai4chat.co/generate-response"
|
|
49
|
+
self.timeout = timeout
|
|
50
|
+
self.last_response = {}
|
|
51
|
+
self.headers = {
|
|
52
|
+
"authority": "www.ai4chat.co",
|
|
53
|
+
"method": "POST",
|
|
54
|
+
"path": "/generate-response",
|
|
55
|
+
"scheme": "https",
|
|
56
|
+
"accept": "*/*",
|
|
57
|
+
"accept-encoding": "gzip, deflate, br, zstd",
|
|
58
|
+
"accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
|
|
59
|
+
"content-type": "application/json",
|
|
60
|
+
"cookie": "messageCount=1",
|
|
61
|
+
"dnt": "1",
|
|
62
|
+
"origin": "https://www.ai4chat.co",
|
|
63
|
+
"priority": "u=1, i",
|
|
64
|
+
"referer": "https://www.ai4chat.co/gpt/talkdirtytome",
|
|
65
|
+
"sec-ch-ua": '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
|
|
66
|
+
"sec-ch-ua-mobile": "?0",
|
|
67
|
+
"sec-ch-ua-platform": '"Windows"',
|
|
68
|
+
"sec-fetch-dest": "empty",
|
|
69
|
+
"sec-fetch-mode": "cors",
|
|
70
|
+
"sec-fetch-site": "same-origin",
|
|
71
|
+
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0"
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
self.__available_optimizers = (
|
|
75
|
+
method
|
|
76
|
+
for method in dir(Optimizers)
|
|
77
|
+
if callable(getattr(Optimizers, method)) and not method.startswith("__")
|
|
78
|
+
)
|
|
79
|
+
self.session.headers.update(self.headers)
|
|
80
|
+
Conversation.intro = (
|
|
81
|
+
AwesomePrompts().get_act(
|
|
82
|
+
act, raise_not_found=True, default=None, case_insensitive=True
|
|
83
|
+
)
|
|
84
|
+
if act
|
|
85
|
+
else intro or Conversation.intro
|
|
86
|
+
)
|
|
87
|
+
self.conversation = Conversation(
|
|
88
|
+
is_conversation, self.max_tokens_to_sample, filepath, update_file
|
|
89
|
+
)
|
|
90
|
+
self.conversation.history_offset = history_offset
|
|
91
|
+
self.session.proxies = proxies
|
|
92
|
+
self.system_prompt = system_prompt
|
|
93
|
+
|
|
94
|
+
def ask(
|
|
95
|
+
self,
|
|
96
|
+
prompt: str,
|
|
97
|
+
stream: bool = False, # Streaming is not supported by AI4Chat
|
|
98
|
+
raw: bool = False,
|
|
99
|
+
optimizer: str = None,
|
|
100
|
+
conversationally: bool = False,
|
|
101
|
+
) -> Dict[str, Any]:
|
|
102
|
+
"""
|
|
103
|
+
Sends a prompt to the AI4Chat API and returns the response.
|
|
104
|
+
|
|
105
|
+
Args:
|
|
106
|
+
prompt: The text prompt to generate text from.
|
|
107
|
+
stream (bool, optional): Not used (AI4Chat doesn't support streaming).
|
|
108
|
+
raw (bool, optional): Whether to return the raw response. Defaults to False.
|
|
109
|
+
optimizer (str, optional): The name of the optimizer to use. Defaults to None.
|
|
110
|
+
conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
|
|
111
|
+
|
|
112
|
+
Returns:
|
|
113
|
+
dict: A dictionary containing the AI's response.
|
|
114
|
+
"""
|
|
115
|
+
conversation_prompt = self.conversation.gen_complete_prompt(prompt)
|
|
116
|
+
if optimizer:
|
|
117
|
+
if optimizer in self.__available_optimizers:
|
|
118
|
+
conversation_prompt = getattr(Optimizers, optimizer)(
|
|
119
|
+
conversation_prompt if conversationally else prompt
|
|
120
|
+
)
|
|
121
|
+
else:
|
|
122
|
+
raise Exception(
|
|
123
|
+
f"Optimizer is not one of {self.__available_optimizers}"
|
|
124
|
+
)
|
|
125
|
+
|
|
126
|
+
payload = {
|
|
127
|
+
"messages": [
|
|
128
|
+
{"role": "system", "content": self.system_prompt},
|
|
129
|
+
{"role": "user", "content": conversation_prompt}
|
|
130
|
+
]
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
response = self.session.post(self.api_endpoint, headers=self.headers, json=payload, timeout=self.timeout)
|
|
134
|
+
if not response.ok:
|
|
135
|
+
raise Exception(f"Failed to generate response: {response.status_code} - {response.reason}")
|
|
136
|
+
|
|
137
|
+
response_data = response.json()
|
|
138
|
+
message_content = response_data.get('message', 'No message found')
|
|
139
|
+
|
|
140
|
+
# Decode HTML entities and remove HTML tags
|
|
141
|
+
decoded_message = html.unescape(message_content)
|
|
142
|
+
cleaned_text = sub('<[^<]+?>', '', decoded_message)
|
|
143
|
+
|
|
144
|
+
self.last_response.update(dict(text=cleaned_text))
|
|
145
|
+
self.conversation.update_chat_history(prompt, cleaned_text)
|
|
146
|
+
return self.last_response
|
|
147
|
+
|
|
148
|
+
def chat(
|
|
149
|
+
self,
|
|
150
|
+
prompt: str,
|
|
151
|
+
stream: bool = False, # Streaming is not supported by AI4Chat
|
|
152
|
+
optimizer: str = None,
|
|
153
|
+
conversationally: bool = False,
|
|
154
|
+
) -> str:
|
|
155
|
+
"""
|
|
156
|
+
Generates a response from the AI4Chat API.
|
|
157
|
+
|
|
158
|
+
Args:
|
|
159
|
+
prompt (str): The prompt to send to the AI.
|
|
160
|
+
stream (bool, optional): Not used (AI4Chat doesn't support streaming).
|
|
161
|
+
optimizer (str, optional): The name of the optimizer to use. Defaults to None.
|
|
162
|
+
conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
|
|
163
|
+
|
|
164
|
+
Returns:
|
|
165
|
+
str: The response generated by the AI.
|
|
166
|
+
"""
|
|
167
|
+
return self.get_message(
|
|
168
|
+
self.ask(
|
|
169
|
+
prompt,
|
|
170
|
+
optimizer=optimizer,
|
|
171
|
+
conversationally=conversationally,
|
|
172
|
+
)
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
def get_message(self, response: dict) -> str:
|
|
176
|
+
"""Retrieves message only from response
|
|
177
|
+
|
|
178
|
+
Args:
|
|
179
|
+
response (dict): Response generated by `self.ask`
|
|
180
|
+
|
|
181
|
+
Returns:
|
|
182
|
+
str: Message extracted
|
|
183
|
+
"""
|
|
184
|
+
assert isinstance(response, dict), "Response should be of dict data-type only"
|
|
185
|
+
return response["text"]
|
|
186
|
+
if __name__ == "__main__":
|
|
187
|
+
from rich import print
|
|
188
|
+
|
|
189
|
+
ai = AI4Chat()
|
|
190
|
+
# Stream the response
|
|
191
|
+
response = ai.chat(input(">>> "))
|
|
192
|
+
for chunk in response:
|
|
193
|
+
print(chunk, end="", flush=True)
|