webscout 6.7__py3-none-any.whl → 6.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.

Files changed (43) hide show
  1. webscout/Extra/YTToolkit/YTdownloader.py +7 -2
  2. webscout/Extra/YTToolkit/ytapi/channel.py +1 -1
  3. webscout/Extra/YTToolkit/ytapi/query.py +3 -0
  4. webscout/Extra/YTToolkit/ytapi/stream.py +3 -0
  5. webscout/Extra/YTToolkit/ytapi/video.py +3 -1
  6. webscout/Provider/Cloudflare.py +2 -1
  7. webscout/Provider/DARKAI.py +2 -2
  8. webscout/Provider/Free2GPT.py +5 -5
  9. webscout/Provider/Marcus.py +3 -3
  10. webscout/Provider/PI.py +113 -47
  11. webscout/Provider/Phind.py +6 -0
  12. webscout/Provider/PizzaGPT.py +62 -53
  13. webscout/Provider/RUBIKSAI.py +93 -38
  14. webscout/Provider/__init__.py +0 -8
  15. webscout/Provider/cerebras.py +3 -3
  16. webscout/Provider/cleeai.py +2 -2
  17. webscout/Provider/elmo.py +2 -2
  18. webscout/Provider/gaurish.py +2 -2
  19. webscout/Provider/geminiprorealtime.py +2 -2
  20. webscout/Provider/lepton.py +2 -2
  21. webscout/Provider/llama3mitril.py +3 -3
  22. webscout/Provider/llamatutor.py +2 -2
  23. webscout/Provider/llmchat.py +3 -2
  24. webscout/Provider/meta.py +2 -2
  25. webscout/Provider/tutorai.py +1 -1
  26. webscout/__init__.py +0 -1
  27. webscout/swiftcli/__init__.py +1 -0
  28. webscout/version.py +1 -1
  29. webscout/webscout_search.py +1140 -1104
  30. webscout/webscout_search_async.py +635 -361
  31. {webscout-6.7.dist-info → webscout-6.9.dist-info}/METADATA +4 -32
  32. {webscout-6.7.dist-info → webscout-6.9.dist-info}/RECORD +36 -43
  33. webscout/Extra/markdownlite/__init__.py +0 -862
  34. webscout/Provider/Deepseek.py +0 -227
  35. webscout/Provider/Farfalle.py +0 -227
  36. webscout/Provider/NinjaChat.py +0 -200
  37. webscout/Provider/mhystical.py +0 -176
  38. webscout/zerodir/__init__.py +0 -225
  39. webstoken/t.py +0 -75
  40. {webscout-6.7.dist-info → webscout-6.9.dist-info}/LICENSE.md +0 -0
  41. {webscout-6.7.dist-info → webscout-6.9.dist-info}/WHEEL +0 -0
  42. {webscout-6.7.dist-info → webscout-6.9.dist-info}/entry_points.txt +0 -0
  43. {webscout-6.7.dist-info → webscout-6.9.dist-info}/top_level.txt +0 -0
@@ -1,227 +0,0 @@
1
- import requests
2
- import json
3
- from typing import Any, AsyncGenerator, Dict
4
-
5
- from webscout.AIutel import Optimizers
6
- from webscout.AIutel import Conversation
7
- from webscout.AIutel import AwesomePrompts, sanitize_stream
8
- from webscout.AIbase import Provider, AsyncProvider
9
- from webscout import exceptions
10
-
11
- class DeepSeek(Provider):
12
- """
13
- A class to interact with the Deepseek API.
14
- """
15
-
16
- AVAILABLE_MODELS = [
17
- "deepseek_chat",
18
- "deepseek_code"
19
- ]
20
-
21
- def __init__(
22
- self,
23
- api_key,
24
- model: str = "deepseek_chat",
25
- temperature: float = 0,
26
- is_conversation: bool = True,
27
- timeout: int = 30,
28
- max_tokens: int = 4000,
29
- intro: str = None,
30
- filepath: str = None,
31
- update_file: bool = True,
32
- proxies: dict = {},
33
- history_offset: int = 10250,
34
- act: str = None,
35
- ) -> None:
36
- """
37
- Initializes the Deepseek API with given parameters.
38
-
39
- Args:
40
- api_key (str): The API token for authentication.
41
- model (str, optional): The AI model to use for text generation.
42
- Defaults to "deepseek_chat".
43
- Options: "deepseek_chat", "deepseek_code".
44
- temperature (float): The temperature parameter for the model.
45
- is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
46
- timeout (int, optional): Http request timeout. Defaults to 30.
47
- intro (str, optional): Conversation introductory prompt. Defaults to None.
48
- filepath (str, optional): Path to file containing conversation history. Defaults to None.
49
- update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
50
- proxies (dict, optional): Http request proxies. Defaults to {}.
51
- history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
52
- act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
53
- """
54
- if model not in self.AVAILABLE_MODELS:
55
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
56
-
57
- self.api_token = api_key
58
- self.api_endpoint = "https://chat.deepseek.com/api/v0/chat/completions"
59
- self.model = model
60
- self.temperature = temperature
61
- self.session = requests.Session()
62
- self.is_conversation = is_conversation
63
- self.max_tokens_to_sample = max_tokens
64
- self.timeout = timeout
65
- self.last_response = {}
66
- self.headers = {
67
- "accept": "*/*",
68
- "accept-encoding": "gzip, deflate, br, zstd",
69
- "accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
70
- "authorization": f"Bearer {self.api_token}",
71
- "content-length": "128",
72
- "content-type": "application/json",
73
- "dnt": "1",
74
- "origin": "https://chat.deepseek.com",
75
- "priority": "u=1, i",
76
- "referer": "https://chat.deepseek.com/",
77
- "sec-ch-ua": '"Chromium";v="130", "Microsoft Edge";v="130", "Not?A_Brand";v="99"',
78
- "sec-ch-ua-mobile": "?0",
79
- "sec-ch-ua-platform": '"Windows"',
80
- "sec-fetch-dest": "empty",
81
- "sec-fetch-mode": "cors",
82
- "sec-fetch-site": "same-origin",
83
- "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36 Edg/130.0.0.0",
84
- "x-app-version": "20241018.0"
85
- }
86
- self.__available_optimizers = (
87
- method
88
- for method in dir(Optimizers)
89
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
90
- )
91
- self.session.headers.update(self.headers)
92
- Conversation.intro = (
93
- AwesomePrompts().get_act(
94
- act, raise_not_found=True, default=None, case_insensitive=True
95
- )
96
- if act
97
- else intro or Conversation.intro
98
- )
99
- self.conversation = Conversation(
100
- is_conversation, self.max_tokens_to_sample, filepath, update_file
101
- )
102
- self.conversation.history_offset = history_offset
103
- self.session.proxies = proxies
104
-
105
- def ask(
106
- self,
107
- prompt: str,
108
- stream: bool = False,
109
- raw: bool = False,
110
- optimizer: str = None,
111
- conversationally: bool = False,
112
- ) -> Dict[str, Any]:
113
- """
114
- Sends a prompt to the Deepseek AI API and returns the response.
115
-
116
- Args:
117
- prompt: The text prompt to generate text from.
118
- stream (bool, optional): Whether to stream the response. Defaults to False.
119
- raw (bool, optional): Whether to return the raw response. Defaults to False.
120
- optimizer (str, optional): The name of the optimizer to use. Defaults to None.
121
- conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
122
-
123
- Returns:
124
- The response from the API.
125
- """
126
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
127
- if optimizer:
128
- if optimizer in self.__available_optimizers:
129
- conversation_prompt = getattr(Optimizers, optimizer)(
130
- conversation_prompt if conversationally else prompt
131
- )
132
- else:
133
- raise Exception(
134
- f"Optimizer is not one of {self.__available_optimizers}"
135
- )
136
-
137
- payload = {
138
- "message": conversation_prompt,
139
- "stream": True,
140
- "model_preference": None,
141
- "model_class": self.model,
142
- "temperature": self.temperature
143
- }
144
-
145
- def for_stream():
146
- response = self.session.post(
147
- self.api_endpoint, json=payload, headers=self.headers, stream=True, timeout=self.timeout
148
- )
149
-
150
- if not response.ok:
151
- raise exceptions.FailedToGenerateResponseError(
152
- f"Failed to generate response - ({response.status_code}, {response.reason})"
153
- )
154
- streaming_response = ""
155
- for line in response.iter_lines():
156
- if line:
157
- json_line = json.loads(line.decode('utf-8').split('data: ')[1])
158
- if 'choices' in json_line and len(json_line['choices']) > 0:
159
- delta_content = json_line['choices'][0].get('delta', {}).get('content')
160
- if delta_content:
161
- streaming_response += delta_content
162
- yield delta_content if raw else dict(text=delta_content)
163
- self.last_response.update(dict(text=streaming_response))
164
- self.conversation.update_chat_history(
165
- prompt, self.get_message(self.last_response)
166
- )
167
-
168
- def for_non_stream():
169
- for _ in for_stream():
170
- pass
171
- return self.last_response
172
-
173
- return for_stream() if stream else for_non_stream()
174
-
175
- def chat(
176
- self,
177
- prompt: str,
178
- stream: bool = False,
179
- optimizer: str = None,
180
- conversationally: bool = False,
181
- ) -> str:
182
- """Generate response `str`
183
- Args:
184
- prompt (str): Prompt to be send.
185
- stream (bool, optional): Flag for streaming response. Defaults to False.
186
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
187
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
188
- Returns:
189
- str: Response generated
190
- """
191
-
192
- def for_stream():
193
- for response in self.ask(
194
- prompt, True, optimizer=optimizer, conversationally=conversationally
195
- ):
196
- yield self.get_message(response)
197
-
198
- def for_non_stream():
199
- return self.get_message(
200
- self.ask(
201
- prompt,
202
- False,
203
- optimizer=optimizer,
204
- conversationally=conversationally,
205
- )
206
- )
207
-
208
- return for_stream() if stream else for_non_stream()
209
-
210
- def get_message(self, response: dict) -> str:
211
- """Retrieves message only from response
212
-
213
- Args:
214
- response (dict): Response generated by `self.ask`
215
-
216
- Returns:
217
- str: Message extracted
218
- """
219
- assert isinstance(response, dict), "Response should be of dict data-type only"
220
- return response["text"]
221
-
222
- if __name__ == '__main__':
223
- from rich import print
224
- ai = DeepSeek(api_key="", timeout=5000)
225
- response = ai.chat("write a poem about AI", stream=True)
226
- for chunk in response:
227
- print(chunk, end="", flush=True)
@@ -1,227 +0,0 @@
1
- import requests
2
- import json
3
- from typing import Any, Dict, Optional
4
-
5
- from webscout.AIutel import Optimizers
6
- from webscout.AIutel import Conversation
7
- from webscout.AIutel import AwesomePrompts
8
- from webscout.AIbase import Provider, AsyncProvider
9
- from webscout import exceptions
10
-
11
-
12
- class Farfalle(Provider):
13
- """
14
- A class to interact with the Farfalle.dev API.
15
- """
16
-
17
- AVAILABLE_MODELS = [
18
- "gpt-3.5-turbo",
19
-
20
- ]
21
-
22
- def __init__(
23
- self,
24
- is_conversation: bool = True,
25
- max_tokens: int = 600,
26
- timeout: int = 30,
27
- intro: str = None,
28
- filepath: str = None,
29
- update_file: bool = True,
30
- proxies: dict = {},
31
- history_offset: int = 10250,
32
- act: str = None,
33
- model: str = "gpt-3.5-turbo",
34
- ) -> None:
35
- """
36
- Initializes the Farfalle.dev API with given parameters.
37
-
38
- Args:
39
- is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
40
- max_tokens (int, optional): Maximum number of tokens to be generated upon completion.
41
- Defaults to 600.
42
- timeout (int, optional): Http request timeout. Defaults to 30.
43
- intro (str, optional): Conversation introductory prompt. Defaults to None.
44
- filepath (str, optional): Path to file containing conversation history. Defaults to None.
45
- update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
46
- proxies (dict, optional): Http request proxies. Defaults to {}.
47
- history_offset (int, optional): Limit conversation history to this number of last texts.
48
- Defaults to 10250.
49
- act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
50
- model (str, optional): AI model to use. Defaults to "gpt-3.5-turbo".
51
- Options: "gpt-3.5-turbo", "gpt-4"
52
- """
53
- if model not in self.AVAILABLE_MODELS:
54
- raise ValueError(f"Invalid model: {model}. Choose from: {self.AVAILABLE_MODELS}")
55
-
56
- self.session = requests.Session()
57
- self.is_conversation = is_conversation
58
- self.max_tokens_to_sample = max_tokens
59
- self.api_endpoint = "https://api.farfalle.dev/chat"
60
- self.stream_chunk_size = 64
61
- self.timeout = timeout
62
- self.last_response = {}
63
- self.model = model
64
- self.headers = {
65
- "accept": "text/event-stream",
66
- "accept-encoding": "gzip, deflate, br, zstd",
67
- "accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
68
- "content-type": "application/json",
69
- "dnt": "1",
70
- "origin": "https://www.farfalle.dev",
71
- "priority": "u=1, i",
72
- "referer": "https://www.farfalle.dev/",
73
- "sec-ch-ua": '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
74
- "sec-ch-ua-mobile": "?0",
75
- "sec-ch-ua-platform": '"Windows"',
76
- "sec-fetch-dest": "empty",
77
- "sec-fetch-mode": "cors",
78
- "sec-fetch-site": "same-site",
79
- "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"
80
- }
81
-
82
- self.__available_optimizers = (
83
- method
84
- for method in dir(Optimizers)
85
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
86
- )
87
- self.session.headers.update(self.headers)
88
- Conversation.intro = (
89
- AwesomePrompts().get_act(
90
- act, raise_not_found=True, default=None, case_insensitive=True
91
- )
92
- if act
93
- else intro or Conversation.intro
94
- )
95
- self.conversation = Conversation(
96
- is_conversation, self.max_tokens_to_sample, filepath, update_file
97
- )
98
- self.conversation.history_offset = history_offset
99
- self.session.proxies = proxies
100
-
101
- def ask(
102
- self,
103
- prompt: str,
104
- stream: bool = False,
105
- raw: bool = False,
106
- optimizer: str = None,
107
- conversationally: bool = False,
108
- ) -> Dict[str, Any]:
109
- """
110
- Sends a prompt to the Farfalle.dev API and returns the response.
111
-
112
- Args:
113
- prompt: The text prompt to generate text from.
114
- stream (bool, optional): Whether to stream the response. Defaults to False.
115
- raw (bool, optional): Whether to return the raw response. Defaults to False.
116
- optimizer (str, optional): The name of the optimizer to use. Defaults to None.
117
- conversationally (bool, optional): Whether to chat conversationally. Defaults to False.
118
-
119
- Returns:
120
- The response from the API.
121
- """
122
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
123
- if optimizer:
124
- if optimizer in self.__available_optimizers:
125
- conversation_prompt = getattr(Optimizers, optimizer)(
126
- conversation_prompt if conversationally else prompt
127
- )
128
- else:
129
- raise Exception(
130
- f"Optimizer is not one of {self.__available_optimizers}"
131
- )
132
-
133
- payload = {
134
- "query": conversation_prompt,
135
- "model": self.model
136
- }
137
-
138
- def for_stream():
139
- response = self.session.post(
140
- self.api_endpoint, json=payload, headers=self.headers, stream=True, timeout=self.timeout
141
- )
142
-
143
- if not response.ok:
144
- raise exceptions.FailedToGenerateResponseError(
145
- f"Failed to generate response - ({response.status_code}, {response.reason})"
146
- )
147
-
148
- streaming_response = ""
149
- for line in response.iter_lines():
150
- if line:
151
- decoded_line = line.decode('utf-8')
152
- if decoded_line.startswith("data:"):
153
- data = decoded_line[len("data:"):].strip()
154
- if data:
155
- try:
156
- event = json.loads(data)
157
- if event.get("event") == "final-response":
158
- message = event['data'].get('message', '')
159
- streaming_response += message
160
- yield message if raw else dict(text=streaming_response)
161
- except json.decoder.JSONDecodeError:
162
- continue
163
- self.last_response.update(dict(text=streaming_response))
164
- self.conversation.update_chat_history(
165
- prompt, self.get_message(self.last_response)
166
- )
167
- def for_non_stream():
168
- for _ in for_stream():
169
- pass
170
- return self.last_response
171
-
172
- return for_stream() if stream else for_non_stream()
173
-
174
- def chat(
175
- self,
176
- prompt: str,
177
- stream: bool = False,
178
- optimizer: str = None,
179
- conversationally: bool = False,
180
- ) -> str:
181
- """Generate response `str`
182
- Args:
183
- prompt (str): Prompt to be send.
184
- stream (bool, optional): Flag for streaming response. Defaults to False.
185
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
186
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
187
- Returns:
188
- str: Response generated
189
- """
190
-
191
- def for_stream():
192
- for response in self.ask(
193
- prompt, True, optimizer=optimizer, conversationally=conversationally
194
- ):
195
- yield self.get_message(response)
196
-
197
- def for_non_stream():
198
- return self.get_message(
199
- self.ask(
200
- prompt,
201
- False,
202
- optimizer=optimizer,
203
- conversationally=conversationally,
204
- )
205
- )
206
-
207
- return for_stream() if stream else for_non_stream()
208
-
209
- def get_message(self, response: dict) -> str:
210
- """Retrieves message only from response
211
-
212
- Args:
213
- response (dict): Response generated by `self.ask`
214
-
215
- Returns:
216
- str: Message extracted
217
- """
218
- assert isinstance(response, dict), "Response should be of dict data-type only"
219
- return response["text"]
220
- if __name__ == "__main__":
221
- from rich import print
222
-
223
- ai = Farfalle()
224
- # Stream the response
225
- response = ai.chat("who is pm of india")
226
- for chunk in response:
227
- print(chunk, end="", flush=True)
@@ -1,200 +0,0 @@
1
- import requests
2
- import json
3
- import os
4
- from typing import Any, Dict, Optional, Generator, List, Union
5
-
6
- from webscout.AIutel import Optimizers
7
- from webscout.AIutel import Conversation
8
- from webscout.AIutel import AwesomePrompts, sanitize_stream
9
- from webscout.AIbase import Provider, AsyncProvider
10
- from webscout import exceptions
11
-
12
-
13
- class NinjaChat(Provider):
14
- """
15
- A class to interact with the NinjaChat API.
16
- """
17
-
18
- AVAILABLE_MODELS = {
19
- "mistral": "https://www.ninjachat.ai/api/mistral",
20
- "perplexity": "https://www.ninjachat.ai/api/perplexity",
21
- "claude-3.5": "https://www.ninjachat.ai/api/claude-pro",
22
- "gemini-1.5-pro": "https://www.ninjachat.ai/api/gemini",
23
- "llama": "https://www.ninjachat.ai/api/llama-pro",
24
- "o1-mini": "https://www.ninjachat.ai/api/o1-mini",
25
- }
26
-
27
- def __init__(
28
- self,
29
- is_conversation: bool = True,
30
- max_tokens: int = 2049,
31
- timeout: int = 30,
32
- intro: str = None, # System message/intro prompt
33
- filepath: str = None,
34
- update_file: bool = True,
35
- proxies: dict = {},
36
- history_offset: int = 10250,
37
- act: str = None,
38
- model: str = "llama", # Default model
39
- system_message: str = "You are a helpful AI assistant.", # Default system message
40
- ):
41
- """Initializes the NinjaChat API client."""
42
-
43
- self.headers = {
44
- "Accept": "*/*",
45
- "Accept-Encoding": "gzip, deflate, br, zstd",
46
- "Accept-Language": "en-US,en;q=0.9,en-IN;q=0.8",
47
- "Content-Type": "application/json",
48
- "Cookie": "_ga=GA1.1.298084589.1727859540; _ga_11N4NZX9WP=GS1.1.1727859539.1.0.1727859552.0.0.0; __stripe_mid=4f63db68-c41d-45b4-9111-2457a6cf1b538696a9; __Host-next-auth.csrf-token=a5cb5a40c73df3e808ebc072dcb116fe7dd4b9b8d39d8002ef7e54153e6aa665%7Cbffe3f934f2db43330d281453af2cd0b4757f439b958f2d1a06a36cea63e9cc8; __stripe_sid=118678d1-403a-43f9-b3b9-d80ed9392a0d2ac131; __Secure-next-auth.callback-url=https%3A%2F%2Fwww.ninjachat.ai%2Fdashboard; __Secure-next-auth.session-token=eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIn0..l34CIFGTJCtstUqU.VjEYgaUUPpgp-49wueXFlFYvbm8csuyX0HichHrPNH45nX4s_LeZX2VhK1ZvwmUpfdlsMD4bi8VzFfQUEgs8FLPhkbKnoZDP939vobV7K_2Q9CA8PgC0oXEsQf_azWmILZ8rOE37uYzTu1evCnOjCucDYrC1ONXzl9NbGNPVa8AQr7hXvatuqtqe-lBUQXWdrw3QLulbqxvh6yLoxJj04gqC-nPudGciU-_-3TZJYr98u8o7KtUGio1ZX9vHNFfv8djWM1NCkji3Kl9eUhiyMj71.6uhUS39UcCVRa6tFzHxz2g; ph_phc_wWUtqcGWqyyochfPvwKlXMkMjIoIQKUwcnHE3KMKm8K_posthog=%7B%22distinct_id%22%3A%2201924c74-2926-7042-a1fb-5b5debdbcd1c%22%2C%22%24sesid%22%3A%5B1727966419499%2C%22019252bb-9de4-75db-9f85-a389fb401670%22%2C1727964880355%5D%7D",
49
- "DNT": "1",
50
- "Origin": "https://www.ninjachat.ai",
51
- "Priority": "u=1, i",
52
- "Referer": "https://www.ninjachat.ai/dashboard",
53
- "Sec-CH-UA": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
54
- "Sec-CH-UA-Mobile": "?0",
55
- "Sec-CH-UA-Platform": '"Windows"',
56
- "Sec-Fetch-Dest": "empty",
57
- "Sec-Fetch-Mode": "cors",
58
- "Sec-Fetch-Site": "same-origin",
59
- "User-Agent": (
60
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
61
- "AppleWebKit/537.36 (KHTML, like Gecko) "
62
- "Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0"
63
- )
64
- }
65
- self.session = requests.Session()
66
- self.session.headers.update(self.headers)
67
- self.session.proxies.update(proxies)
68
- self.timeout = timeout
69
- self.last_response = {}
70
- self.system_message = system_message
71
-
72
- self.is_conversation = is_conversation
73
- self.max_tokens_to_sample = max_tokens
74
- self.__available_optimizers = (
75
- method
76
- for method in dir(Optimizers)
77
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
78
- )
79
-
80
- #Set the intro/system message
81
- Conversation.intro = (
82
- AwesomePrompts().get_act(
83
- act, raise_not_found=True, default=None, case_insensitive=True
84
- )
85
- if act
86
- else intro or system_message or Conversation.intro #Priority: act > intro > system_message > Conversation.intro
87
-
88
- )
89
-
90
-
91
- self.conversation = Conversation(
92
- is_conversation, self.max_tokens_to_sample, filepath, update_file
93
- )
94
- self.conversation.history_offset = history_offset
95
-
96
- if model not in self.AVAILABLE_MODELS:
97
- raise ValueError(f"Invalid model: {model}. Choose from: {', '.join(self.AVAILABLE_MODELS)}")
98
- self.model_url = self.AVAILABLE_MODELS[model]
99
- self.headers["Referer"] = self.model_url # Set initial referer
100
-
101
-
102
-
103
- def ask(
104
- self,
105
- prompt: str,
106
- stream: bool = False,
107
- raw: bool = False,
108
- optimizer: str = None,
109
- conversationally: bool = False,
110
- ) -> Union[Dict, Generator]:
111
-
112
- conversation_prompt = self.conversation.gen_complete_prompt(prompt, intro=Conversation.intro)
113
-
114
- if optimizer:
115
- if optimizer in self.__available_optimizers:
116
- conversation_prompt = getattr(Optimizers, optimizer)(
117
- conversation_prompt if conversationally else prompt
118
- )
119
- else:
120
- raise Exception(f"Optimizer is not one of {self.__available_optimizers}")
121
-
122
- #Include the system message in the payload
123
- payload = {
124
- "messages": [
125
- {"role": "system", "content": self.system_message}, # System message here
126
- {"role": "user", "content": conversation_prompt},
127
- ],
128
- "stream": stream # Now passed dynamically
129
- }
130
-
131
- def for_stream():
132
- try:
133
- with requests.post(self.model_url, headers=self.headers, json=payload, stream=True, timeout=self.timeout) as response:
134
- response.raise_for_status()
135
- streaming_text = ""
136
- for line in response.iter_lines(decode_unicode=True):
137
- if line:
138
- if line.startswith("0:"):
139
- try:
140
- text = json.loads(line[2:]) # Extract streaming text
141
- streaming_text += text #Accumulate for history
142
- resp = dict(text=text)
143
- yield resp if raw else resp
144
- except json.JSONDecodeError:
145
- print("\n[Error] Failed to decode JSON content.")
146
-
147
- elif line.startswith("d:"):
148
- break #End of stream
149
- self.conversation.update_chat_history(prompt, streaming_text)
150
- self.last_response.update({"text": streaming_text})
151
- except requests.exceptions.RequestException as e:
152
- print("An error occurred:", e)
153
-
154
-
155
-
156
- def for_non_stream():
157
-
158
- for _ in for_stream():
159
- pass
160
- return self.last_response
161
-
162
-
163
- return for_stream() if stream else for_non_stream()
164
-
165
- def chat(
166
- self,
167
- prompt: str,
168
- stream: bool = False,
169
- optimizer: str = None,
170
- conversationally: bool = False,
171
- ) -> Union[str, Generator]:
172
-
173
- def for_stream():
174
- for response in self.ask(
175
- prompt, True, optimizer=optimizer, conversationally=conversationally
176
- ):
177
- yield self.get_message(response)
178
-
179
- def for_non_stream():
180
- return self.get_message(
181
- self.ask(
182
- prompt, False, optimizer=optimizer, conversationally=conversationally
183
- )
184
- )
185
- return for_stream() if stream else for_non_stream()
186
-
187
- def get_message(self, response: dict) -> str:
188
- assert isinstance(response, dict), "Response should be of dict data-type only"
189
- return response["text"]
190
-
191
-
192
-
193
- if __name__ == "__main__":
194
- from rich import print
195
- bot = NinjaChat(model="perplexity", system_message="You are a creative writer.")
196
-
197
- response = bot.chat("tell me about Abhay koul, HelpingAI ", stream=True)
198
-
199
- for chunk in response:
200
- print(chunk, end="", flush=True)