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

Files changed (63) hide show
  1. webscout/AIauto.py +77 -259
  2. webscout/Agents/Onlinesearcher.py +22 -10
  3. webscout/Agents/functioncall.py +2 -2
  4. webscout/Bard.py +21 -21
  5. webscout/Extra/autollama.py +37 -20
  6. webscout/Local/__init__.py +6 -7
  7. webscout/Local/formats.py +406 -194
  8. webscout/Local/model.py +1074 -477
  9. webscout/Local/samplers.py +108 -144
  10. webscout/Local/thread.py +251 -410
  11. webscout/Local/ui.py +401 -0
  12. webscout/Local/utils.py +338 -136
  13. webscout/Provider/Amigo.py +51 -38
  14. webscout/Provider/Deepseek.py +7 -6
  15. webscout/Provider/EDITEE.py +2 -2
  16. webscout/Provider/GPTWeb.py +1 -1
  17. webscout/Provider/Llama3.py +1 -1
  18. webscout/Provider/NinjaChat.py +200 -0
  19. webscout/Provider/OLLAMA.py +1 -1
  20. webscout/Provider/Perplexity.py +1 -1
  21. webscout/Provider/Reka.py +12 -5
  22. webscout/Provider/TTI/AIuncensored.py +103 -0
  23. webscout/Provider/TTI/Nexra.py +3 -3
  24. webscout/Provider/TTI/__init__.py +4 -2
  25. webscout/Provider/TTI/aiforce.py +2 -2
  26. webscout/Provider/TTI/imgninza.py +136 -0
  27. webscout/Provider/TTI/talkai.py +116 -0
  28. webscout/Provider/TeachAnything.py +0 -3
  29. webscout/Provider/Youchat.py +1 -1
  30. webscout/Provider/__init__.py +16 -12
  31. webscout/Provider/{ChatHub.py → aimathgpt.py} +72 -88
  32. webscout/Provider/cerebras.py +143 -123
  33. webscout/Provider/cleeai.py +1 -1
  34. webscout/Provider/felo_search.py +1 -1
  35. webscout/Provider/gaurish.py +207 -0
  36. webscout/Provider/geminiprorealtime.py +160 -0
  37. webscout/Provider/genspark.py +1 -1
  38. webscout/Provider/julius.py +8 -3
  39. webscout/Provider/learnfastai.py +1 -1
  40. webscout/Provider/{aigames.py → llmchat.py} +74 -84
  41. webscout/Provider/promptrefine.py +3 -1
  42. webscout/Provider/talkai.py +196 -0
  43. webscout/Provider/turboseek.py +3 -8
  44. webscout/Provider/tutorai.py +1 -1
  45. webscout/__init__.py +2 -43
  46. webscout/exceptions.py +5 -1
  47. webscout/tempid.py +4 -73
  48. webscout/utils.py +3 -0
  49. webscout/version.py +1 -1
  50. webscout/webai.py +1 -1
  51. webscout/webscout_search.py +154 -123
  52. {webscout-6.0.dist-info → webscout-6.2.dist-info}/METADATA +164 -245
  53. {webscout-6.0.dist-info → webscout-6.2.dist-info}/RECORD +57 -55
  54. webscout/Local/rawdog.py +0 -946
  55. webscout/Provider/BasedGPT.py +0 -214
  56. webscout/Provider/TTI/amigo.py +0 -148
  57. webscout/Provider/bixin.py +0 -264
  58. webscout/Provider/xdash.py +0 -182
  59. webscout/websx_search.py +0 -19
  60. {webscout-6.0.dist-info → webscout-6.2.dist-info}/LICENSE.md +0 -0
  61. {webscout-6.0.dist-info → webscout-6.2.dist-info}/WHEEL +0 -0
  62. {webscout-6.0.dist-info → webscout-6.2.dist-info}/entry_points.txt +0 -0
  63. {webscout-6.0.dist-info → webscout-6.2.dist-info}/top_level.txt +0 -0
@@ -1,214 +0,0 @@
1
- import requests
2
- import json
3
- from webscout.AIutel import Optimizers
4
- from webscout.AIutel import Conversation
5
- from webscout.AIutel import AwesomePrompts, sanitize_stream
6
- from webscout.AIbase import Provider, AsyncProvider
7
- from webscout import exceptions
8
- from typing import Any, AsyncGenerator, Dict
9
-
10
-
11
- class BasedGPT(Provider):
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 = "gpt-3.5-turbo"
24
- ):
25
- """Instantiates BasedGPT
26
-
27
- Args:
28
- is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
29
- max_tokens (int, optional): Maximum number of tokens to be generated upon completion. Defaults to 600.
30
- timeout (int, optional): Http request timeout. Defaults to 30.
31
- intro (str, optional): Conversation introductory prompt. Defaults to None.
32
- filepath (str, optional): Path to file containing conversation history. Defaults to None.
33
- update_file (bool, optional): Add new prompts and responses to the file. Defaults to True.
34
- proxies (dict, optional): Http request proxies. Defaults to {}.
35
- history_offset (int, optional): Limit conversation history to this number of last texts. Defaults to 10250.
36
- act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
37
- model (str, optional): Model to use for generating text. Defaults to "gpt-3.5-turbo".
38
- """
39
- self.session = requests.Session()
40
- self.is_conversation = is_conversation
41
- self.max_tokens_to_sample = max_tokens
42
- self.chat_endpoint = "https://www.basedgpt.chat/api/chat"
43
- self.timeout = timeout
44
- self.last_response = {}
45
- self.model = model
46
- self.headers = {
47
- "accept": "*/*",
48
- "accept-encoding": "gzip, deflate, br, zstd",
49
- "accept-language": "en-US,en;q=0.9,en-IN;q=0.8",
50
- "content-length": "109",
51
- "content-type": "application/json",
52
- "dnt": "1",
53
- "origin": "https://www.basedgpt.chat",
54
- "priority": "u=1, i",
55
- "referer": "https://www.basedgpt.chat/",
56
- "sec-ch-ua": '"Not)A;Brand";v="99", "Microsoft Edge";v="127", "Chromium";v="127"',
57
- "sec-ch-ua-mobile": "?0",
58
- "sec-ch-ua-platform": '"Windows"',
59
- "sec-fetch-dest": "empty",
60
- "sec-fetch-mode": "cors",
61
- "sec-fetch-site": "same-origin",
62
- "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"
63
- }
64
-
65
- self.__available_optimizers = (
66
- method
67
- for method in dir(Optimizers)
68
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
69
- )
70
- self.session.headers.update(self.headers)
71
- Conversation.intro = (
72
- AwesomePrompts().get_act(
73
- act, raise_not_found=True, default=None, case_insensitive=True
74
- )
75
- if act
76
- else intro or Conversation.intro
77
- )
78
- self.conversation = Conversation(
79
- is_conversation, self.max_tokens_to_sample, filepath, update_file
80
- )
81
- self.conversation.history_offset = history_offset
82
- self.session.proxies = proxies
83
-
84
- def ask(
85
- self,
86
- prompt: str,
87
- stream: bool = False,
88
- raw: bool = False,
89
- optimizer: str = None,
90
- conversationally: bool = False,
91
- ) -> dict:
92
- """Chat with AI
93
-
94
- Args:
95
- prompt (str): Prompt to be send.
96
- stream (bool, optional): Flag for streaming response. Defaults to False.
97
- raw (bool, optional): Stream back raw response as received. Defaults to False.
98
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
99
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
100
- Returns:
101
- dict : {}
102
- ```json
103
- {
104
- "text" : "How may I assist you today?"
105
- }
106
- ```
107
- """
108
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
109
- if optimizer:
110
- if optimizer in self.__available_optimizers:
111
- conversation_prompt = getattr(Optimizers, optimizer)(
112
- conversation_prompt if conversationally else prompt
113
- )
114
- else:
115
- raise Exception(
116
- f"Optimizer is not one of {self.__available_optimizers}"
117
- )
118
-
119
- self.session.headers.update(self.headers)
120
- payload = {
121
- "messages": [
122
- {
123
- "role": "user",
124
- "content": conversation_prompt
125
- }
126
- ]
127
- }
128
-
129
- def for_stream():
130
- response = self.session.post(
131
- self.chat_endpoint, json=payload, stream=True, timeout=self.timeout
132
- )
133
- if not response.ok:
134
- raise Exception(
135
- f"Failed to generate response - ({response.status_code}, {response.reason}) - {response.text}"
136
- )
137
-
138
- streaming_text = ""
139
- for value in response.iter_lines(
140
- decode_unicode=True,
141
- chunk_size=64,
142
- delimiter="\n",
143
- ):
144
- try:
145
- if bool(value):
146
- streaming_text += value + ("\n" if stream else "")
147
- resp = dict(text=streaming_text)
148
- self.last_response.update(resp)
149
- yield value if raw else resp
150
- except json.decoder.JSONDecodeError:
151
- pass
152
- self.conversation.update_chat_history(
153
- prompt, self.get_message(self.last_response)
154
- )
155
-
156
- def for_non_stream():
157
- for _ in for_stream():
158
- pass
159
- return self.last_response
160
-
161
- return for_stream() if stream else for_non_stream()
162
-
163
- def chat(
164
- self,
165
- prompt: str,
166
- stream: bool = False,
167
- optimizer: str = None,
168
- conversationally: bool = False,
169
- ) -> str:
170
- """Generate response `str`
171
- Args:
172
- prompt (str): Prompt to be send.
173
- stream (bool, optional): Flag for streaming response. Defaults to False.
174
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
175
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
176
- Returns:
177
- str: Response generated
178
- """
179
-
180
- def for_stream():
181
- for response in self.ask(
182
- prompt, True, optimizer=optimizer, conversationally=conversationally
183
- ):
184
- yield self.get_message(response)
185
-
186
- def for_non_stream():
187
- return self.get_message(
188
- self.ask(
189
- prompt,
190
- False,
191
- optimizer=optimizer,
192
- conversationally=conversationally,
193
- )
194
- )
195
-
196
- return for_stream() if stream else for_non_stream()
197
-
198
- def get_message(self, response: dict) -> str:
199
- """Retrieves message only from response
200
-
201
- Args:
202
- response (dict): Response generated by `self.ask`
203
-
204
- Returns:
205
- str: Message extracted
206
- """
207
- assert isinstance(response, dict), "Response should be of dict data-type only"
208
- return response["text"]
209
- if __name__ == '__main__':
210
- from rich import print
211
- ai = BasedGPT()
212
- response = ai.chat("tell me about india")
213
- for chunk in response:
214
- print(chunk, end="", flush=True)
@@ -1,148 +0,0 @@
1
- import requests
2
- import json
3
- import uuid
4
- import os
5
- from typing import List
6
-
7
- from webscout.AIbase import ImageProvider
8
-
9
- class AmigoImager(ImageProvider):
10
- """
11
- Image provider for AmigoChat.io.
12
- """
13
- AVAILABLE_MODELS = ["dalle-e-3", "flux-pro", "flux-realism"]
14
- def __init__(self, timeout: int = 60, proxies: dict = {}):
15
- """Initializes the AmigoImager class.
16
-
17
- Args:
18
- timeout (int, optional): HTTP request timeout in seconds. Defaults to 60.
19
- proxies (dict, optional): HTTP request proxies. Defaults to {}.
20
- """
21
- self.url = "https://api.amigochat.io/v1/images/generations"
22
- self.headers = {
23
- "Accept": "*/*",
24
- "Accept-Encoding": "gzip, deflate, br, zstd",
25
- "Accept-Language": "en-US,en;q=0.9,en-IN;q=0.8",
26
- "Authorization": "Bearer ", # Empty
27
- "Content-Type": "application/json; charset=utf-8",
28
- "DNT": "1",
29
- "Origin": "https://amigochat.io",
30
- "Referer": "https://amigochat.io/",
31
- "Sec-CH-UA": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
32
- "Sec-CH-UA-Mobile": "?0",
33
- "Sec-CH-UA-Platform": '"Windows"',
34
- "Sec-Fetch-Dest": "empty",
35
- "Sec-Fetch-Mode": "cors",
36
- "Sec-Fetch-Site": "same-site",
37
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
38
- "X-Device-Language": "en-US",
39
- "X-Device-Platform": "web",
40
- "X-Device-UUID": str(uuid.uuid4()),
41
- "X-Device-Version": "1.0.22"
42
- }
43
- self.session = requests.Session()
44
- self.session.headers.update(self.headers)
45
- self.session.proxies.update(proxies)
46
- self.timeout = timeout
47
- self.prompt: str = "AI-generated image - webscout"
48
- self.image_extension: str = "png"
49
-
50
- def generate(self, prompt: str, amount: int = 1, model: str = "flux-pro") -> List[str]:
51
- """Generate image from prompt
52
-
53
- Args:
54
- prompt (str): Image description.
55
- amount (int, optional): Total images to be generated. Defaults to 1.
56
- model (str, optional): Model to use for generating images. Defaults to "flux-pro".
57
-
58
- Returns:
59
- List[str]: List of generated image URLs.
60
- """
61
- assert bool(prompt), "Prompt cannot be null"
62
- assert isinstance(amount, int), f"Amount should be an integer only not {type(amount)}"
63
- assert amount > 0, "Amount should be greater than 0"
64
- assert model in self.AVAILABLE_MODELS, f"Model should be one of {self.AVAILABLE_MODELS}"
65
-
66
- self.prompt = prompt
67
- response = []
68
-
69
- for _ in range(amount):
70
- # JSON payload for the request
71
- payload = {
72
- "prompt": prompt,
73
- "model": model,
74
- "personaId": "image-generator"
75
- }
76
-
77
- try:
78
- # Sending the POST request
79
- resp = requests.post(self.url, headers=self.headers, data=json.dumps(payload), timeout=self.timeout)
80
- resp.raise_for_status()
81
-
82
- # Process the response
83
- response_data = resp.json()
84
- image_url = response_data['data'][0]['url']
85
- response.append(image_url)
86
-
87
- except requests.exceptions.RequestException as e:
88
- print(f"An error occurred: {e}")
89
- raise
90
-
91
- return response
92
-
93
- def save(
94
- self,
95
- response: List[str], # List of image URLs
96
- name: str = None,
97
- dir: str = os.getcwd(),
98
- filenames_prefix: str = "",
99
- ) -> List[str]:
100
- """Save generated images
101
-
102
- Args:
103
- response (List[str]): List of generated image URLs.
104
- name (str): Filename for the images. Defaults to the last prompt.
105
- dir (str, optional): Directory for saving images. Defaults to os.getcwd().
106
- filenames_prefix (str, optional): String to be prefixed at each filename to be returned.
107
-
108
- Returns:
109
- List[str]: List of saved filenames.
110
- """
111
- assert isinstance(response, list), f"Response should be of {list} not {type(response)}"
112
- name = self.prompt if name is None else name
113
-
114
- filenames = []
115
- count = 0
116
- for img_url in response:
117
- def complete_path():
118
- count_value = "" if count == 0 else f"_{count}"
119
- return os.path.join(dir, name + count_value + "." + self.image_extension)
120
-
121
- while os.path.isfile(complete_path()):
122
- count += 1
123
-
124
- absolute_path_to_file = complete_path()
125
- filenames.append(filenames_prefix + os.path.split(absolute_path_to_file)[1])
126
-
127
- # Download and save the image
128
- try:
129
- img_response = requests.get(img_url, stream=True, timeout=self.timeout)
130
- img_response.raise_for_status()
131
-
132
- with open(absolute_path_to_file, "wb") as fh:
133
- for chunk in img_response.iter_content(chunk_size=8192):
134
- fh.write(chunk)
135
- except requests.exceptions.RequestException as e:
136
- print(f"An error occurred while downloading image from {img_url}: {e}")
137
- raise
138
-
139
- return filenames
140
-
141
- # Example usage
142
- if __name__ == "__main__":
143
- bot = AmigoImager()
144
- try:
145
- resp = bot.generate("A shiny red sports car speeding down a scenic mountain road", 1)
146
- print(bot.save(resp))
147
- except Exception as e:
148
- print(f"An error occurred: {e}")
@@ -1,264 +0,0 @@
1
- import requests
2
- import json
3
- import random
4
- from typing import Any, Dict, Optional, Generator
5
-
6
- from webscout.AIutel import Optimizers
7
- from webscout.AIutel import Conversation
8
- from webscout.AIutel import AwesomePrompts
9
- from webscout.AIbase import Provider
10
- from webscout import exceptions
11
-
12
-
13
- class Bixin(Provider):
14
- """
15
- A class to interact with the Bixin API.
16
- """
17
-
18
- AVAILABLE_MODELS = [
19
- 'gpt-3.5-turbo-0125', 'gpt-3.5-turbo-16k-0613', 'gpt-4-turbo', 'qwen-turbo'
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-4-turbo', # Default model
34
- system_prompt: str = "You are a helpful assistant.",
35
- ):
36
- """
37
- Initializes the Bixin API with given parameters.
38
-
39
- Args:
40
- is_conversation (bool, optional): Flag for chatting conversationally. Defaults to True.
41
- max_tokens (int, optional): Maximum number of tokens to be generated upon completion. 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. Defaults to 10250.
48
- act (str|int, optional): Awesome prompt key or index. (Used as intro). Defaults to None.
49
- model (str, optional): AI model to use. Defaults to "gpt-4-turbo".
50
- system_prompt (str, optional): System prompt for Bixin.
51
- Defaults to "You are a helpful assistant.".
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://chat.bixin123.com/api/chatgpt/chat-process"
60
- self.stream_chunk_size = 1024
61
- self.timeout = timeout
62
- self.last_response = {}
63
- self.model = model
64
- self.system_prompt = system_prompt
65
- self.headers = {
66
- "Accept": "application/json, text/plain, */*",
67
- "Accept-Language": "en-US,en;q=0.9",
68
- "Cache-Control": "no-cache",
69
- "Content-Type": "application/json",
70
- "Fingerprint": self.generate_fingerprint(),
71
- "Origin": "https://chat.bixin123.com",
72
- "Pragma": "no-cache",
73
- "Priority": "u=1, i",
74
- "Referer": "https://chat.bixin123.com/chat",
75
- "Sec-CH-UA": '"Chromium";v="127", "Not)A;Brand";v="99"',
76
- "Sec-CH-UA-Mobile": "?0",
77
- "Sec-CH-UA-Platform": '"Linux"',
78
- "Sec-Fetch-Dest": "empty",
79
- "Sec-Fetch-Mode": "cors",
80
- "Sec-Fetch-Site": "same-origin",
81
- "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
82
- "X-Website-Domain": "chat.bixin123.com",
83
- }
84
-
85
- self.__available_optimizers = (
86
- method
87
- for method in dir(Optimizers)
88
- if callable(getattr(Optimizers, method)) and not method.startswith("__")
89
- )
90
- self.session.headers.update(self.headers)
91
- Conversation.intro = (
92
- AwesomePrompts().get_act(
93
- act, raise_not_found=True, default=None, case_insensitive=True
94
- )
95
- if act
96
- else intro or Conversation.intro
97
- )
98
- self.conversation = Conversation(
99
- is_conversation, self.max_tokens_to_sample, filepath, update_file
100
- )
101
- self.conversation.history_offset = history_offset
102
- self.session.proxies = proxies
103
-
104
- def generate_fingerprint(self) -> str:
105
- """
106
- Generates a random fingerprint number as a string.
107
- """
108
- return str(random.randint(100000000, 999999999))
109
-
110
- def ask(
111
- self,
112
- prompt: str,
113
- stream: bool = False,
114
- raw: bool = False,
115
- optimizer: str = None,
116
- conversationally: bool = False,
117
- ) -> dict:
118
- """Chat with Bixin
119
-
120
- Args:
121
- prompt (str): Prompt to be send.
122
- stream (bool, optional): Flag for streaming response. Defaults to False.
123
- raw (bool, optional): Stream back raw response as received. Defaults to False.
124
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
125
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
126
- Returns:
127
- dict : {}
128
- ```json
129
- {
130
- "text" : "How may I assist you today?"
131
- }
132
- ```
133
- """
134
- conversation_prompt = self.conversation.gen_complete_prompt(prompt)
135
- if optimizer:
136
- if optimizer in self.__available_optimizers:
137
- conversation_prompt = getattr(Optimizers, optimizer)(
138
- conversation_prompt if conversationally else prompt
139
- )
140
- else:
141
- raise Exception(
142
- f"Optimizer is not one of {self.__available_optimizers}"
143
- )
144
-
145
- messages = [
146
- {"role": "system", "content": self.system_prompt},
147
- {"role": "user", "content": conversation_prompt},
148
- ]
149
-
150
- data = {
151
- "prompt": self.format_prompt(messages),
152
- "options": {
153
- "usingNetwork": False,
154
- "file": ""
155
- }
156
- }
157
-
158
- def for_stream():
159
- try:
160
- with requests.post(self.api_endpoint, headers=self.headers, json=data, stream=True, timeout=self.timeout) as response:
161
- response.raise_for_status()
162
-
163
- # Initialize variable to keep track of the last printed text
164
- previous_text = ""
165
-
166
- full_response = ''
167
- for chunk in response.iter_content(chunk_size=self.stream_chunk_size, decode_unicode=True):
168
- if chunk:
169
- try:
170
- json_chunk = json.loads(chunk)
171
- text = json_chunk.get("text", "")
172
-
173
- # Determine the new text to print
174
- if text.startswith(previous_text):
175
- new_text = text[len(previous_text):]
176
- full_response += new_text
177
- yield new_text if raw else dict(text=new_text)
178
- previous_text = text
179
- else:
180
- full_response += text
181
- yield text if raw else dict(text=full_response)
182
- previous_text = text
183
- except json.JSONDecodeError:
184
- # If the chunk isn't a complete JSON object, skip it
185
- continue
186
- self.last_response.update(dict(text=full_response))
187
- self.conversation.update_chat_history(
188
- prompt, self.get_message(self.last_response)
189
- )
190
- except requests.RequestException as e:
191
- raise exceptions.FailedToGenerateResponseError(f"\nRequest failed: {e}")
192
-
193
- def for_non_stream():
194
- for _ in for_stream():
195
- pass
196
- return self.last_response
197
-
198
- return for_stream() if stream else for_non_stream()
199
-
200
- def format_prompt(self, messages: list) -> str:
201
- """
202
- Formats the list of messages into a single prompt string.
203
- """
204
- formatted_messages = []
205
- for message in messages:
206
- role = message.get("role", "")
207
- content = message.get("content", "")
208
- formatted_messages.append(f"{role}: {content}")
209
- return "\n".join(formatted_messages)
210
-
211
- def chat(
212
- self,
213
- prompt: str,
214
- stream: bool = False,
215
- optimizer: str = None,
216
- conversationally: bool = False,
217
- ) -> str:
218
- """Generate response `str`
219
- Args:
220
- prompt (str): Prompt to be send.
221
- stream (bool, optional): Flag for streaming response. Defaults to False.
222
- optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
223
- conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
224
- Returns:
225
- str: Response generated
226
- """
227
-
228
- def for_stream():
229
- for response in self.ask(
230
- prompt, True, optimizer=optimizer, conversationally=conversationally
231
- ):
232
- yield self.get_message(response)
233
-
234
- def for_non_stream():
235
- return self.get_message(
236
- self.ask(
237
- prompt,
238
- False,
239
- optimizer=optimizer,
240
- conversationally=conversationally,
241
- )
242
- )
243
-
244
- return for_stream() if stream else for_non_stream()
245
-
246
- def get_message(self, response: dict) -> str:
247
- """Retrieves message only from response
248
-
249
- Args:
250
- response (dict): Response generated by `self.ask`
251
-
252
- Returns:
253
- str: Message extracted
254
- """
255
- assert isinstance(response, dict), "Response should be of dict data-type only"
256
- return response["text"]
257
-
258
- if __name__ == "__main__":
259
- from rich import print
260
-
261
- ai = Bixin(timeout=5000)
262
- response = ai.chat("write a poem about AI", stream=True)
263
- for chunk in response:
264
- print(chunk, end="", flush=True)