webscout 5.6__py3-none-any.whl → 5.8__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.

@@ -1,7 +1,7 @@
1
1
  import requests
2
2
  import os
3
3
  import time
4
- from typing import List
4
+ from typing import List, Optional
5
5
  from string import punctuation
6
6
  from random import choice
7
7
  from requests.exceptions import RequestException
@@ -9,16 +9,29 @@ from requests.exceptions import RequestException
9
9
  from webscout.AIbase import ImageProvider
10
10
 
11
11
  class AiForceimagger(ImageProvider):
12
- """Image provider for pollinations.ai"""
12
+ """Image provider for Airforce API"""
13
+
14
+ AVAILABLE_MODELS = [
15
+ "flux",
16
+ "flux-realism",
17
+ "flux-anime",
18
+ "flux-3d",
19
+ "flux-disney",
20
+ "flux-pixel",
21
+ "flux-4o",
22
+ "any-dark"
23
+ ]
13
24
 
14
25
  def __init__(self, timeout: int = 60, proxies: dict = {}):
15
- """Initializes the PollinationsAI class.
26
+ """Initializes the AiForceimagger class.
16
27
 
17
28
  Args:
29
+ api_token (str, optional): Your Airforce API token. If None, it will use the environment variable "AIRFORCE_API_TOKEN".
30
+ Defaults to None.
18
31
  timeout (int, optional): HTTP request timeout in seconds. Defaults to 60.
19
32
  proxies (dict, optional): HTTP request proxies (socks). Defaults to {}.
20
33
  """
21
- self.image_gen_endpoint = "https://api.airforce/v1/imagine2?prompt={prompt}"
34
+ self.api_endpoint = "https://api.airforce/imagine2"
22
35
  self.headers = {
23
36
  "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
24
37
  "Accept-Language": "en-US,en;q=0.5",
@@ -33,18 +46,28 @@ class AiForceimagger(ImageProvider):
33
46
  self.image_extension: str = "png"
34
47
 
35
48
  def generate(
36
- self, prompt: str, amount: int = 1, additives: bool = True,
37
- max_retries: int = 3, retry_delay: int = 5
49
+ self,
50
+ prompt: str,
51
+ amount: int = 1,
52
+ additives: bool = True,
53
+ model: str = "flux-realism",
54
+ width: int = 768,
55
+ height: int = 768,
56
+ seed: Optional[int] = None,
57
+ max_retries: int = 3,
58
+ retry_delay: int = 5
38
59
  ) -> List[bytes]:
39
60
  """Generate image from prompt
40
61
 
41
62
  Args:
42
63
  prompt (str): Image description.
43
- amount (int): Total images to be generated. Defaults to 1.
64
+ amount (int, optional): Total images to be generated. Defaults to 1.
44
65
  additives (bool, optional): Try to make each prompt unique. Defaults to True.
66
+ model (str, optional): The model to use for image generation.
67
+ Defaults to "flux". Available options: "flux", "flux-realism".
45
68
  width (int, optional): Width of the generated image. Defaults to 768.
46
69
  height (int, optional): Height of the generated image. Defaults to 768.
47
- model (str, optional): The model to use for image generation. Defaults to "flux".
70
+ seed (int, optional): Seed for the random number generator. Defaults to None.
48
71
  max_retries (int, optional): Maximum number of retry attempts. Defaults to 3.
49
72
  retry_delay (int, optional): Delay between retries in seconds. Defaults to 5.
50
73
 
@@ -54,6 +77,7 @@ class AiForceimagger(ImageProvider):
54
77
  assert bool(prompt), "Prompt cannot be null"
55
78
  assert isinstance(amount, int), f"Amount should be an integer only not {type(amount)}"
56
79
  assert amount > 0, "Amount should be greater than 0"
80
+ assert model in self.AVAILABLE_MODELS, f"Model should be one of {self.AVAILABLE_MODELS}"
57
81
 
58
82
  ads = lambda: (
59
83
  ""
@@ -68,9 +92,9 @@ class AiForceimagger(ImageProvider):
68
92
  self.prompt = prompt
69
93
  response = []
70
94
  for _ in range(amount):
71
- url = self.image_gen_endpoint.format(
72
- prompt=prompt
73
- )
95
+ url = f"{self.api_endpoint}?model={model}&prompt={prompt}&size={width}:{height}"
96
+ if seed:
97
+ url += f"&seed={seed}"
74
98
 
75
99
  for attempt in range(max_retries):
76
100
  try:
@@ -127,11 +151,10 @@ class AiForceimagger(ImageProvider):
127
151
 
128
152
  return filenames
129
153
 
130
-
131
154
  if __name__ == "__main__":
132
155
  bot = AiForceimagger()
133
156
  try:
134
- resp = bot.generate("AI-generated image - webscout", 1)
157
+ resp = bot.generate("A shiny red sports car speeding down a scenic mountain road", 1, model="flux-realism")
135
158
  print(bot.save(resp))
136
159
  except Exception as e:
137
160
  print(f"An error occurred: {e}")
@@ -0,0 +1,141 @@
1
+ import cloudscraper
2
+ import os
3
+ import requests
4
+
5
+ from typing import List
6
+
7
+ from webscout.AIbase import ImageProvider
8
+
9
+ class ArtbitImager(ImageProvider):
10
+ """
11
+ Image provider for Artbit.ai.
12
+ """
13
+
14
+ def __init__(self, timeout: int = 60, proxies: dict = {}):
15
+ """Initializes the ArtbitImager 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://artbit.ai/api/generateImage"
22
+ self.scraper = cloudscraper.create_scraper()
23
+ self.scraper.proxies.update(proxies)
24
+ self.timeout = timeout
25
+ self.prompt: str = "AI-generated image - webscout"
26
+ self.image_extension: str = "png"
27
+
28
+ def generate(
29
+ self,
30
+ prompt: str,
31
+ amount: int = 1,
32
+ caption_model: str = "sdxl",
33
+ selected_ratio: str = "1024",
34
+ negative_prompt: str = ""
35
+ ) -> List[str]:
36
+ """Generate image from prompt
37
+
38
+ Args:
39
+ prompt (str): Image description.
40
+ amount (int, optional): Total images to be generated. Defaults to 1.
41
+ caption_model (str, optional): Caption model to use. Defaults to "sdxl".
42
+ selected_ratio (str, optional): Image ratio. Defaults to "1024".
43
+ negative_prompt (str, optional): Negative prompt. Defaults to "".
44
+
45
+ Returns:
46
+ List[str]: List of generated image URLs.
47
+ """
48
+ assert bool(prompt), "Prompt cannot be null"
49
+ assert isinstance(amount, int), f"Amount should be an integer only not {type(amount)}"
50
+ assert amount > 0, "Amount should be greater than 0"
51
+
52
+ self.prompt = prompt
53
+ response: List[str] = []
54
+
55
+ payload = {
56
+ "captionInput": prompt,
57
+ "captionModel": caption_model,
58
+ "selectedRatio": selected_ratio,
59
+ "selectedSamples": str(amount),
60
+ "negative_prompt": negative_prompt
61
+ }
62
+
63
+ try:
64
+ # Sending the POST request using CloudScraper
65
+ resp = self.scraper.post(self.url, json=payload, timeout=self.timeout)
66
+ resp.raise_for_status() # Check for HTTP errors
67
+
68
+ # Parsing the JSON response
69
+ response_data = resp.json()
70
+
71
+ # Extracting image URLs from the response
72
+ imgs = response_data.get("imgs", [])
73
+ if imgs:
74
+ response.extend(imgs)
75
+ else:
76
+ print("No images found in the response.")
77
+
78
+ except requests.RequestException as e:
79
+ print(f"An error occurred while making the request: {e}")
80
+ raise
81
+
82
+ return response
83
+
84
+ def save(
85
+ self,
86
+ response: List[str], # List of image URLs
87
+ name: str = None,
88
+ dir: str = os.getcwd(),
89
+ filenames_prefix: str = "",
90
+ ) -> List[str]:
91
+ """Save generated images
92
+
93
+ Args:
94
+ response (List[str]): List of generated image URLs.
95
+ name (str): Filename for the images. Defaults to the last prompt.
96
+ dir (str, optional): Directory for saving images. Defaults to os.getcwd().
97
+ filenames_prefix (str, optional): String to be prefixed at each filename to be returned.
98
+
99
+ Returns:
100
+ List[str]: List of saved filenames.
101
+ """
102
+ assert isinstance(response, list), f"Response should be of {list} not {type(response)}"
103
+ name = self.prompt if name is None else name
104
+
105
+ filenames = []
106
+ count = 0
107
+ for img_url in response:
108
+ def complete_path():
109
+ count_value = "" if count == 0 else f"_{count}"
110
+ return os.path.join(dir, name + count_value + "." + self.image_extension)
111
+
112
+ while os.path.isfile(complete_path()):
113
+ count += 1
114
+
115
+ absolute_path_to_file = complete_path()
116
+ filenames.append(filenames_prefix + os.path.split(absolute_path_to_file)[1])
117
+
118
+ # Download and save the image
119
+ try:
120
+ img_response = requests.get(img_url, stream=True, timeout=self.timeout)
121
+ img_response.raise_for_status()
122
+
123
+ with open(absolute_path_to_file, "wb") as fh:
124
+ for chunk in img_response.iter_content(chunk_size=8192):
125
+ fh.write(chunk)
126
+ except requests.exceptions.RequestException as e:
127
+ print(f"An error occurred while downloading image from {img_url}: {e}")
128
+ raise
129
+
130
+ return filenames
131
+
132
+ if __name__ == "__main__":
133
+ bot = ArtbitImager()
134
+ try:
135
+ resp = bot.generate(
136
+ "A shiny red sports car speeding down a scenic mountain road with a clear blue sky in the background, surrounded by lush green trees.",
137
+ amount=3
138
+ )
139
+ print(bot.save(resp))
140
+ except Exception as e:
141
+ print(f"An error occurred: {e}")
@@ -0,0 +1,155 @@
1
+ import os
2
+ import requests
3
+ import io
4
+ from PIL import Image
5
+ from typing import Any, List, Optional, Dict
6
+ from webscout.AIbase import ImageProvider
7
+
8
+ class HFimager(ImageProvider):
9
+ """
10
+ Image provider for Hugging Face Inference API.
11
+ """
12
+
13
+ def __init__(
14
+ self,
15
+ api_token: str = None,
16
+ timeout: int = 60,
17
+ proxies: dict = {}
18
+ ):
19
+ """Initializes the HFimager class.
20
+
21
+ Args:
22
+ api_token (str, optional): Hugging Face API token. If None, it will use the environment variable "HUGGINGFACE_API_TOKEN".
23
+ Defaults to None.
24
+ timeout (int, optional): HTTP request timeout in seconds. Defaults to 60.
25
+ proxies (dict, optional): HTTP request proxies. Defaults to {}.
26
+ """
27
+ self.base_url = "https://api-inference.huggingface.co/models/"
28
+ self.api_token = api_token or os.environ["HUGGINGFACE_API_TOKEN"]
29
+ self.headers = {"Authorization": f"Bearer {self.api_token}"}
30
+ self.session = requests.Session()
31
+ self.session.headers.update(self.headers)
32
+ self.session.proxies.update(proxies)
33
+ self.timeout = timeout
34
+ self.prompt: str = "AI-generated image - webscout"
35
+ self.image_extension: str = "jpg"
36
+
37
+ def generate(
38
+ self,
39
+ prompt: str,
40
+ amount: int = 1,
41
+ model: str = "black-forest-labs/FLUX.1-dev",
42
+ guidance_scale: Optional[float] = None,
43
+ negative_prompt: Optional[str] = None,
44
+ num_inference_steps: Optional[int] = None,
45
+ width: Optional[int] = None,
46
+ height: Optional[int] = None,
47
+ scheduler: Optional[str] = None,
48
+ seed: Optional[int] = None,
49
+ ) -> List[bytes]:
50
+ """
51
+ Generate image from prompt.
52
+
53
+ Args:
54
+ prompt (str): Image description.
55
+ amount (int): Total images to be generated. Defaults to 1.
56
+ model (str): Hugging Face model name. Defaults to "black-forest-labs/FLUX.1-dev".
57
+ guidance_scale (float, optional): Guidance scale value. Defaults to None.
58
+ negative_prompt (str, optional): Negative prompt. Defaults to None.
59
+ num_inference_steps (int, optional): Number of inference steps. Defaults to None.
60
+ width (int, optional): Width of the output image. Defaults to None.
61
+ height (int, optional): Height of the output image. Defaults to None.
62
+ scheduler (str, optional): Scheduler to use. Defaults to None.
63
+ seed (int, optional): Seed for random number generator. Defaults to None.
64
+
65
+ Returns:
66
+ List[bytes]: List of generated images as bytes.
67
+ """
68
+ assert bool(prompt), "Prompt cannot be null"
69
+ assert isinstance(amount, int), f"Amount should be an integer only not {type(amount)}"
70
+ assert amount > 0, "Amount should be greater than 0"
71
+
72
+ self.prompt = prompt
73
+ response = []
74
+
75
+ for _ in range(amount):
76
+ url = self.base_url + model
77
+
78
+ # Create the base payload with the prompt
79
+ payload: Dict[str, Any] = {"inputs": prompt}
80
+
81
+ # Add optional parameters to the payload if provided
82
+ parameters = {}
83
+ if guidance_scale is not None:
84
+ parameters["guidance_scale"] = guidance_scale
85
+ if negative_prompt is not None:
86
+ parameters["negative_prompt"] = negative_prompt
87
+ if num_inference_steps is not None:
88
+ parameters["num_inference_steps"] = num_inference_steps
89
+ if width is not None and height is not None:
90
+ parameters["target_size"] = {"width": width, "height": height}
91
+ if scheduler is not None:
92
+ parameters["scheduler"] = scheduler
93
+ if seed is not None:
94
+ parameters["seed"] = seed
95
+
96
+ # Add the parameters to the payload if any are set
97
+ if parameters:
98
+ payload["parameters"] = parameters
99
+
100
+ try:
101
+ resp = self.session.post(url, headers=self.headers, json=payload, timeout=self.timeout)
102
+ resp.raise_for_status()
103
+ response.append(resp.content)
104
+ except requests.RequestException as e:
105
+ print(f"Failed to generate image: {e}")
106
+ raise
107
+
108
+ return response
109
+
110
+ def save(
111
+ self,
112
+ response: List[bytes],
113
+ name: str = None,
114
+ dir: str = os.getcwd(),
115
+ filenames_prefix: str = "",
116
+ ) -> List[str]:
117
+ """Save generated images
118
+
119
+ Args:
120
+ response (List[bytes]): List of generated images as bytes.
121
+ name (str): Filename for the images. Defaults to the last prompt.
122
+ dir (str, optional): Directory for saving images. Defaults to os.getcwd().
123
+ filenames_prefix (str, optional): String to be prefixed at each filename to be returned.
124
+
125
+ Returns:
126
+ List[str]: List of saved filenames.
127
+ """
128
+ assert isinstance(response, list), f"Response should be of {list} not {type(response)}"
129
+ name = self.prompt if name is None else name
130
+
131
+ filenames = []
132
+ count = 0
133
+ for image_bytes in response:
134
+ def complete_path():
135
+ count_value = "" if count == 0 else f"_{count}"
136
+ return os.path.join(dir, name + count_value + "." + self.image_extension)
137
+
138
+ while os.path.isfile(complete_path()):
139
+ count += 1
140
+
141
+ absolute_path_to_file = complete_path()
142
+ filenames.append(filenames_prefix + os.path.split(absolute_path_to_file)[1])
143
+
144
+ with open(absolute_path_to_file, "wb") as fh:
145
+ fh.write(image_bytes)
146
+
147
+ return filenames
148
+
149
+ if __name__ == "__main__":
150
+ bot = HFimager(api_token='your huggingface API')
151
+ try:
152
+ resp = bot.generate("A shiny red sports car speeding down a scenic mountain road with a clear blue sky in the background, surrounded by lush green trees.", 1)
153
+ print(bot.save(resp, name="test"))
154
+ except Exception as e:
155
+ print(f"An error occurred: {e}")
@@ -53,6 +53,12 @@ from .upstage import *
53
53
  from .Bing import *
54
54
  from .GPTWeb import *
55
55
  from .aigames import *
56
+ from .llamatutor import *
57
+ from .promptrefine import *
58
+ from .twitterclone import *
59
+ from .tutorai import *
60
+ from .bixin import *
61
+ from .ChatGPTES import *
56
62
  __all__ = [
57
63
  'Farfalle',
58
64
  'LLAMA',
@@ -110,5 +116,12 @@ __all__ = [
110
116
  'Bing',
111
117
  'GPTWeb',
112
118
  'AIGameIO',
119
+ 'LlamaTutor',
120
+ 'PromptRefine',
121
+ 'AIUncensored',
122
+ 'TutorAI',
123
+ 'Bixin',
124
+ 'ChatGPTES',
125
+
113
126
 
114
127
  ]
@@ -0,0 +1,264 @@
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=full_response)
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()
262
+ response = ai.chat(input(">>> "))
263
+ for chunk in response:
264
+ print(chunk, end="", flush=True)