webscout 1.3.6__py3-none-any.whl → 1.3.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/webai.py CHANGED
@@ -1,2361 +1,2438 @@
1
- import webscout
2
- import click
3
- import cmd
4
- import logging
5
- import os
6
- import sys
7
- import clipman
8
- import re
9
- import rich
10
- import getpass
11
- import json
12
- import re
13
- import sys
14
- import datetime
15
- import time
16
- import subprocess
17
- from threading import Thread as thr
18
- from functools import wraps
19
- from rich.panel import Panel
20
- from rich.style import Style
21
- from rich.markdown import Markdown
22
- from rich.console import Console
23
- from rich.live import Live
24
- from rich.table import Table
25
- from rich.prompt import Prompt
26
- from rich.progress import Progress
27
- from typing import Iterator
28
- from webscout.AIutel import Optimizers
29
- from webscout.AIutel import default_path
30
- from webscout.AIutel import AwesomePrompts
31
- from webscout.AIutel import RawDog
32
- from webscout import available_providers
33
- from colorama import Fore
34
- from colorama import init as init_colorama
35
- from dotenv import load_dotenv
36
- import g4f
37
-
38
- import webscout.AIutel
39
-
40
- init_colorama(autoreset=True)
41
-
42
- load_dotenv() # loads .env variables
43
-
44
- logging.basicConfig(
45
- format="%(asctime)s - %(levelname)s : %(message)s ",
46
- datefmt="%H:%M:%S",
47
- level=logging.INFO,
48
- )
49
-
50
- try:
51
- clipman.init()
52
- except Exception as e:
53
- logging.debug(f"Dropping clipman in favor of pyperclip - {(e)}")
54
- import pyperclip
55
-
56
- clipman.set = pyperclip.copy
57
- clipman.get = pyperclip.paste
58
-
59
-
60
- class this:
61
- """Console's common variables"""
62
-
63
- rich_code_themes = ["monokai", "paraiso-dark", "igor", "vs", "fruity", "xcode"]
64
-
65
- default_provider = "phind"
66
-
67
- getExc = lambda e: e.args[1] if len(e.args) > 1 else str(e)
68
-
69
- context_settings = dict(auto_envvar_prefix="Webscout")
70
-
71
- """Console utils"""
72
-
73
- @staticmethod
74
- def run_system_command(
75
- command: str, exit_on_error: bool = True, stdout_error: bool = True
76
- ):
77
- """Run commands against system
78
- Args:
79
- command (str): shell command
80
- exit_on_error (bool, optional): Exit on error. Defaults to True.
81
- stdout_error (bool, optional): Print out the error. Defaults to True.
82
-
83
- Returns:
84
- tuple : (is_successfull, object[Exception|Subprocess.run])
85
- """
86
- try:
87
- # Run the command and capture the output
88
- result = subprocess.run(
89
- command,
90
- shell=True,
91
- check=True,
92
- text=True,
93
- stdout=subprocess.PIPE,
94
- stderr=subprocess.PIPE,
95
- )
96
- return (True, result)
97
- except subprocess.CalledProcessError as e:
98
- # Handle error if the command returns a non-zero exit code
99
- if stdout_error:
100
- click.secho(f"Error Occurred: while running '{command}'", fg="yellow")
101
- click.secho(e.stderr, fg="red")
102
- sys.exit(e.returncode) if exit_on_error else None
103
- return (False, e)
104
-
105
- def g4f_providers_in_dict(
106
- url=True,
107
- working=True,
108
- stream=False,
109
- context=False,
110
- gpt35=False,
111
- gpt4=False,
112
- selenium=False,
113
- ):
114
- from webscout.g4f import GPT4FREE
115
- import g4f.Provider.selenium as selenium_based
116
-
117
- selenium_based_providers: list = dir(selenium_based)
118
- hunted_providers = []
119
- required_attrs = (
120
- "url",
121
- "working",
122
- "supports_gpt_35_turbo",
123
- "supports_gpt_4",
124
- "supports_stream",
125
- "supports_message_history",
126
- )
127
-
128
- def sanitize_provider(provider: object):
129
- for attr in required_attrs:
130
- if not hasattr(provider, attr):
131
- setattr(provider, attr, False)
132
-
133
- return provider
134
-
135
- for provider_name, provider_class in g4f.Provider.__map__.items():
136
- provider = sanitize_provider(provider_class)
137
- provider_meta = dict(name=provider_name)
138
- if url:
139
- provider_meta["url"] = provider.url
140
- if working:
141
- provider_meta["working"] = provider.working
142
- if stream:
143
- provider_meta["stream"] = provider.supports_stream
144
- if context:
145
- provider_meta["context"] = provider.supports_message_history
146
- if gpt35:
147
- provider_meta["gpt35_turbo"] = provider.supports_gpt_35_turbo
148
- if gpt4:
149
- provider_meta["gpt4"] = provider.supports_gpt_4
150
- if selenium:
151
- try:
152
- selenium_based_providers.index(provider_meta["name"])
153
- value = True
154
- except ValueError:
155
- value = False
156
- provider_meta["non_selenium"] = value
157
-
158
- hunted_providers.append(provider_meta)
159
-
160
- return hunted_providers
161
-
162
- @staticmethod
163
- def stream_output(
164
- iterable: Iterator,
165
- title: str = "",
166
- is_markdown: bool = True,
167
- style: object = Style(),
168
- transient: bool = False,
169
- title_generator: object = None,
170
- title_generator_params: dict = {},
171
- code_theme: str = "monokai",
172
- vertical_overflow: str = "ellipsis",
173
- ) -> None:
174
- """Stdout streaming response
175
-
176
- Args:
177
- iterable (Iterator): Iterator containing contents to be stdout
178
- title (str, optional): Content title. Defaults to ''.
179
- is_markdown (bool, optional): Flag for markdown content. Defaults to True.
180
- style (object, optional): `rich.style` instance. Defaults to Style().
181
- transient (bool, optional): Flag for transient. Defaults to False.
182
- title_generator (object, optional): Function for generating title. Defaults to None.
183
- title_generator_params (dict, optional): Kwargs for `title_generator` function. Defaults to {}.
184
- code_theme (str, optional): Theme for styling codes. Defaults to `monokai`
185
- vertical_overflow (str, optional): Vertical overflow behaviour on content display. Defaultss to ellipsis.
186
- """
187
- render_this = ""
188
- with Live(
189
- render_this,
190
- transient=transient,
191
- refresh_per_second=8,
192
- vertical_overflow=vertical_overflow,
193
- ) as live:
194
- for entry in iterable:
195
- render_this += entry
196
- live.update(
197
- Panel(
198
- (
199
- Markdown(entry, code_theme=code_theme)
200
- if is_markdown
201
- else entry
202
- ),
203
- title=title,
204
- style=style,
205
- )
206
- )
207
- if title_generator:
208
- title = title_generator(**title_generator_params)
209
- live.update(
210
- Panel(
211
- Markdown(entry, code_theme=code_theme) if is_markdown else entry,
212
- title=title,
213
- style=style,
214
- )
215
- )
216
-
217
- @staticmethod
218
- def clear_history_file(file_path, is_true):
219
- """When --new flag is True"""
220
- if is_true and os.path.isfile(file_path):
221
- try:
222
- os.remove(file_path)
223
- except Exception as e:
224
- logging.error(
225
- f"Failed to clear previous chat history - {this.getExc(e)}"
226
- )
227
-
228
- @staticmethod
229
- def handle_exception(func):
230
- """Safely handles cli-based exceptions and exit status-codes"""
231
-
232
- @wraps(func)
233
- def decorator(*args, **kwargs):
234
- try:
235
- exit_status = func(*args, **kwargs)
236
- except Exception as e:
237
- exit_status = False
238
- logging.error(this.getExc(e))
239
- finally:
240
- sys.exit(0 if exit_status not in (False, "") else 1)
241
-
242
- return decorator
243
-
244
-
245
- class busy_bar:
246
- querying = None
247
- __spinner = (
248
- (),
249
- ("-", "\\", "|", "/"),
250
- (
251
- "█■■■■",
252
- "■█■■■",
253
- "■■█■■",
254
- "■■■█■",
255
- "■■■■█",
256
- ),
257
- ("⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "),
258
- )
259
- spin_index = 0
260
- sleep_time = 0.1
261
-
262
- @classmethod
263
- def __action(
264
- cls,
265
- ):
266
- while cls.querying:
267
- for spin in cls.__spinner[cls.spin_index]:
268
- print(" " + spin, end="\r", flush=True)
269
- if not cls.querying:
270
- break
271
- time.sleep(cls.sleep_time)
272
-
273
- @classmethod
274
- def start_spinning(
275
- cls,
276
- ):
277
- try:
278
- cls.querying = True
279
- t1 = thr(
280
- target=cls.__action,
281
- args=(),
282
- )
283
- t1.start()
284
- except Exception as e:
285
- cls.querying = False
286
- logging.debug(this.getExc(e))
287
- t1.join()
288
-
289
- @classmethod
290
- def stop_spinning(cls):
291
- """Stop displaying busy-bar"""
292
- if cls.querying:
293
- cls.querying = False
294
- time.sleep(cls.sleep_time)
295
-
296
- @classmethod
297
- def run(cls, help: str = "Exception", index: int = None, immediate: bool = False):
298
- """Handle function exceptions safely why showing busy bar
299
-
300
- Args:
301
- help (str, optional): Message to be shown incase of an exception. Defaults to ''.
302
- index (int, optional): Busy bars spin index. Defaults to `default`.
303
- immediate (bool, optional): Start the spinning immediately. Defaults to False.
304
- """
305
- if isinstance(index, int):
306
- cls.spin_index = index
307
-
308
- def decorator(func):
309
- @wraps(func) # Preserves function metadata
310
- def main(*args, **kwargs):
311
- try:
312
- if immediate:
313
- cls.start_spinning()
314
- return func(*args, **kwargs)
315
- except KeyboardInterrupt:
316
- cls.stop_spinning()
317
- return
318
- except EOFError:
319
- cls.querying = False
320
- sys.exit(logging.info("Stopping program"))
321
- except Exception as e:
322
- logging.error(f"{help} - {this.getExc(e)}")
323
- finally:
324
- cls.stop_spinning()
325
-
326
- return main
327
-
328
- return decorator
329
-
330
-
331
- class Main(cmd.Cmd):
332
- intro = (
333
- "Welcome to webai Chat in terminal. "
334
- "Type 'help' or 'h' for usage info.\n"
335
- )
336
-
337
- def __init__(
338
- self,
339
- max_tokens,
340
- temperature,
341
- top_k,
342
- top_p,
343
- model,
344
- auth,
345
- timeout,
346
- disable_conversation,
347
- filepath,
348
- update_file,
349
- intro,
350
- history_offset,
351
- awesome_prompt,
352
- proxy_path,
353
- provider,
354
- quiet=False,
355
- chat_completion=False,
356
- ignore_working=False,
357
- rawdog=False,
358
- internal_exec=False,
359
- confirm_script=False,
360
- interpreter="python",
361
- *args,
362
- **kwargs,
363
- ):
364
- super().__init__(*args, **kwargs)
365
- if proxy_path:
366
- with open(proxy_path) as fh:
367
- proxies = json.load(fh)
368
- else:
369
- proxies = {}
370
-
371
- try:
372
- getOr = lambda option, default: option if option else default
373
-
374
- if rawdog:
375
-
376
- self.RawDog = RawDog(
377
- quiet=quiet,
378
- internal_exec=internal_exec,
379
- confirm_script=confirm_script,
380
- interpreter=interpreter,
381
- prettify=True,
382
- )
383
- intro = self.RawDog.intro_prompt
384
- getpass.getuser = lambda: "RawDog"
385
-
386
- if provider == "g4fauto":
387
- from webscout.g4f import TestProviders
388
-
389
- test = TestProviders(quiet=quiet, timeout=timeout)
390
- g4fauto = test.best if ignore_working else test.auto
391
- if isinstance(g4fauto, str):
392
- provider = "g4fauto+" + g4fauto
393
- from webscout.g4f import GPT4FREE
394
-
395
- self.bot = GPT4FREE(
396
- provider=g4fauto,
397
- auth=auth,
398
- max_tokens=max_tokens,
399
- model=model,
400
- chat_completion=chat_completion,
401
- ignore_working=ignore_working,
402
- timeout=timeout,
403
- intro=intro,
404
- filepath=filepath,
405
- update_file=update_file,
406
- proxies=proxies,
407
- history_offset=history_offset,
408
- act=awesome_prompt,
409
- )
410
- else:
411
- raise Exception(
412
- "No working g4f provider found. "
413
- "Consider running 'webscout gpt4free test -y' first"
414
- )
415
-
416
- elif provider == "leo":
417
- from webscout.AI import LEO
418
-
419
- self.bot = LEO(
420
- is_conversation=disable_conversation,
421
- max_tokens=max_tokens,
422
- temperature=temperature,
423
- top_k=top_k,
424
- top_p=top_p,
425
- model=getOr(model, "llama-2-13b-chat"),
426
- brave_key=getOr(auth, "qztbjzBqJueQZLFkwTTJrieu8Vw3789u"),
427
- timeout=timeout,
428
- intro=intro,
429
- filepath=filepath,
430
- update_file=update_file,
431
- proxies=proxies,
432
- history_offset=history_offset,
433
- act=awesome_prompt,
434
- )
435
-
436
- elif provider == "openai":
437
- assert auth, (
438
- "OpenAI's API-key is required. " "Use the flag `--key` or `-k`"
439
- )
440
- from webscout.AI import OPENAI
441
-
442
- self.bot = OPENAI(
443
- api_key=auth,
444
- is_conversation=disable_conversation,
445
- max_tokens=max_tokens,
446
- temperature=temperature,
447
- presence_penalty=top_p,
448
- frequency_penalty=top_k,
449
- top_p=top_p,
450
- model=getOr(model, model),
451
- timeout=timeout,
452
- intro=intro,
453
- filepath=filepath,
454
- update_file=update_file,
455
- proxies=proxies,
456
- history_offset=history_offset,
457
- act=awesome_prompt,
458
- )
459
-
460
- elif provider == "opengpt":
461
- from webscout.AI import OPENGPT
462
-
463
- self.bot = OPENGPT(
464
- is_conversation=disable_conversation,
465
- max_tokens=max_tokens,
466
- timeout=timeout,
467
- intro=intro,
468
- filepath=filepath,
469
- update_file=update_file,
470
- proxies=proxies,
471
- history_offset=history_offset,
472
- act=awesome_prompt,
473
- )
474
- elif provider == "groq":
475
- assert auth, (
476
- "GROQ's API-key is required. " "Use the flag `--key` or `-k`"
477
- )
478
- from webscout.AI import GROQ
479
-
480
-
481
- self.bot = GROQ(
482
- api_key=auth,
483
- is_conversation=disable_conversation,
484
- max_tokens=max_tokens,
485
- temperature=temperature,
486
- presence_penalty=top_p,
487
- frequency_penalty=top_k,
488
- top_p=top_p,
489
- model=getOr(model, "mixtral-8x7b-32768"),
490
- timeout=timeout,
491
- intro=intro,
492
- filepath=filepath,
493
- update_file=update_file,
494
- proxies=proxies,
495
- history_offset=history_offset,
496
- act=awesome_prompt,
497
- )
498
- elif provider == "sean":
499
- from webscout.AI import Sean
500
-
501
- self.bot = Sean(
502
- is_conversation=disable_conversation,
503
- max_tokens=max_tokens,
504
- timeout=timeout,
505
- intro=intro,
506
- filepath=filepath,
507
- update_file=update_file,
508
- proxies=proxies,
509
- history_offset=history_offset,
510
- act=awesome_prompt,
511
- )
512
- elif provider == "cohere":
513
- assert auth, (
514
- "Cohere's API-key is required. Use the flag `--key` or `-k`"
515
- )
516
- from webscout.AI import Cohere
517
- self.bot = Cohere(
518
- api_key=auth,
519
- is_conversation=disable_conversation,
520
- max_tokens=max_tokens,
521
- temperature=temperature,
522
- top_k=top_k,
523
- top_p=top_p,
524
- model=getOr(model, "command-r-plus"),
525
- timeout=timeout,
526
- intro=intro,
527
- filepath=filepath,
528
- update_file=update_file,
529
- proxies=proxies,
530
- history_offset=history_offset,
531
- act=awesome_prompt,
532
- )
533
- elif provider == "reka":
534
- from webscout.AI import REKA
535
-
536
- self.bot = REKA(
537
- api_key=auth,
538
- is_conversation=disable_conversation,
539
- max_tokens=max_tokens,
540
- timeout=timeout,
541
- intro=intro,
542
- filepath=filepath,
543
- update_file=update_file,
544
- proxies=proxies,
545
- history_offset=history_offset,
546
- act=awesome_prompt,
547
- model=getOr(model, "reka-core"),
548
- # quiet=quiet,
549
- )
550
-
551
- elif provider == "koboldai":
552
- from webscout.AI import KOBOLDAI
553
-
554
- self.bot = KOBOLDAI(
555
- is_conversation=disable_conversation,
556
- max_tokens=max_tokens,
557
- temperature=temperature,
558
- top_p=top_p,
559
- timeout=timeout,
560
- intro=intro,
561
- filepath=filepath,
562
- update_file=update_file,
563
- proxies=proxies,
564
- history_offset=history_offset,
565
- act=awesome_prompt,
566
- )
567
-
568
- elif provider == "gemini":
569
- from webscout.AI import GEMINI
570
-
571
- assert auth, (
572
- "Path to gemini.google.com.cookies.json file is required. "
573
- "Use the flag `--key` or `-k`"
574
- )
575
- self.bot = GEMINI(
576
- cookie_file=auth,
577
- proxy=proxies,
578
- timeout=timeout,
579
- )
580
-
581
- elif provider == "phind":
582
- from webscout.AI import PhindSearch
583
-
584
- self.bot = PhindSearch(
585
- is_conversation=disable_conversation,
586
- max_tokens=max_tokens,
587
- timeout=timeout,
588
- intro=intro,
589
- filepath=filepath,
590
- update_file=update_file,
591
- proxies=proxies,
592
- history_offset=history_offset,
593
- act=awesome_prompt,
594
- model=getOr(model, "Phind Model"),
595
- quiet=quiet,
596
- )
597
-
598
- elif provider == "blackboxai":
599
-
600
- from webscout.AI import BLACKBOXAI
601
-
602
- self.bot = BLACKBOXAI(
603
- is_conversation=disable_conversation,
604
- max_tokens=max_tokens,
605
- timeout=timeout,
606
- intro=intro,
607
- filepath=filepath,
608
- update_file=update_file,
609
- proxies=proxies,
610
- history_offset=history_offset,
611
- act=awesome_prompt,
612
- )
613
-
614
-
615
- elif provider in webscout.gpt4free_providers:
616
- from webscout.g4f import GPT4FREE
617
-
618
- self.bot = GPT4FREE(
619
- provider=provider,
620
- is_conversation=disable_conversation,
621
- auth=auth,
622
- max_tokens=max_tokens,
623
- model=model,
624
- chat_completion=chat_completion,
625
- ignore_working=ignore_working,
626
- timeout=timeout,
627
- intro=intro,
628
- filepath=filepath,
629
- update_file=update_file,
630
- proxies=proxies,
631
- history_offset=history_offset,
632
- act=awesome_prompt,
633
- )
634
-
635
-
636
- elif provider == "perplexity":
637
- from webscout.AI import PERPLEXITY
638
-
639
- self.bot = PERPLEXITY(
640
- is_conversation=disable_conversation,
641
- max_tokens=max_tokens,
642
- timeout=timeout,
643
- intro=intro,
644
- filepath=filepath,
645
- update_file=update_file,
646
- proxies=proxies,
647
- history_offset=history_offset,
648
- act=awesome_prompt,
649
- quiet=quiet,
650
- )
651
-
652
- else:
653
- raise NotImplementedError(
654
- f"The provider `{provider}` is not yet implemented."
655
- )
656
-
657
- except Exception as e:
658
- logging.error(this.getExc(e))
659
- click.secho("Quitting", fg="red")
660
- sys.exit(1)
661
- self.prettify = True
662
- self.color = "cyan"
663
- self.code_theme = "monokai"
664
- self.quiet = quiet
665
- self.vertical_overflow = "ellipsis"
666
- self.disable_stream = False
667
- self.provider = provider
668
- self.disable_coloring = False
669
- self.internal_exec = internal_exec
670
- self.confirm_script = confirm_script
671
- self.interpreter = interpreter
672
- self.rawdog = rawdog
673
- self.__init_time = time.time()
674
- self.__start_time = time.time()
675
- self.__end_time = time.time()
676
-
677
- @property
678
- def prompt(self):
679
- current_time = datetime.datetime.now().strftime("%H:%M:%S")
680
-
681
- def find_range(start, end, hms: bool = False):
682
- in_seconds = round(end - start, 1)
683
- return (
684
- str(datetime.timedelta(seconds=in_seconds)).split(".")[0].zfill(8)
685
- if hms
686
- else in_seconds
687
- )
688
- if not self.disable_coloring:
689
- cmd_prompt = (
690
- f"╭─[`{Fore.GREEN}{getpass.getuser().capitalize()}@webai]`"
691
- f"(`{Fore.YELLOW}{self.provider})`"
692
- f"~[`{Fore.LIGHTWHITE_EX}⏰{Fore.MAGENTA}{current_time}-`"
693
- f"{Fore.LIGHTWHITE_EX}💻{Fore.BLUE}{find_range(self.__init_time, time.time(), True)}-`"
694
- f"{Fore.LIGHTWHITE_EX}⚡️{Fore.RED}{find_range(self.__start_time, self.__end_time)}s]`"
695
- f"\n╰─>"
696
- )
697
- whitelist = ["[", "]", "~", "-", "(", ")"]
698
- for character in whitelist:
699
- cmd_prompt = cmd_prompt.replace(character + "`", Fore.RESET + character)
700
- return cmd_prompt
701
-
702
- else:
703
- return (
704
- f"╭─[{getpass.getuser().capitalize()}@webscout]({self.provider})"
705
- f"~[⏰{current_time}"
706
- f"-💻{find_range(self.__init_time, time.time(), True)}"
707
- f"-⚡️{find_range(self.__start_time, self.__end_time)}s]"
708
- f"~[⏰{current_time}"
709
- f"-💻{find_range(self.__init_time, time.time(), True)}"
710
- f"-⚡️{find_range(self.__start_time, self.__end_time)}s]"
711
- "\n╰─>"
712
- )
713
-
714
- def output_bond(
715
- self,
716
- title: str,
717
- text: str,
718
- color: str = "cyan",
719
- frame: bool = True,
720
- is_json: bool = False,
721
- ):
722
- """Print prettified output
723
-
724
- Args:
725
- title (str): Title
726
- text (str): Info to be printed
727
- color (str, optional): Output color. Defaults to "cyan".
728
- frame (bool, optional): Add frame. Defaults to True.
729
- """
730
- if is_json:
731
- text = f"""
732
- ```json
733
- {json.dumps(text,indent=4)}
734
- ```
735
- """
736
- rich.print(
737
- Panel(
738
- Markdown(text, code_theme=self.code_theme),
739
- title=title.title(),
740
- style=Style(
741
- color=color,
742
- frame=frame,
743
- ),
744
- ),
745
- )
746
- if is_json and click.confirm("Do you wish to save this"):
747
- default_path = title + ".json"
748
- save_to = click.prompt(
749
- "Enter path to save to", default=default_path, type=click.STRING
750
- )
751
- with open(save_to, "a") as fh:
752
- json.dump(text, fh, indent=4)
753
- click.secho(f"Successfuly saved to `{save_to}`", fg="green")
754
-
755
- def do_h(self, line):
756
- """Show help info in tabular form"""
757
- table = Table(
758
- title="Help info",
759
- show_lines=True,
760
- )
761
- table.add_column("No.", style="white", justify="center")
762
- table.add_column("Command", style="yellow", justify="left")
763
- table.add_column("Function", style="cyan")
764
- command_methods = [
765
- getattr(self, method)
766
- for method in dir(self)
767
- if callable(getattr(self, method)) and method.startswith("do_")
768
- ]
769
- command_methods.append(self.default)
770
- command_methods.reverse()
771
- for no, method in enumerate(command_methods):
772
- table.add_row(
773
- str(no + 1),
774
- method.__name__[3:] if not method == self.default else method.__name__,
775
- method.__doc__,
776
- )
777
- Console().print(table)
778
-
779
- @busy_bar.run("Settings saved")
780
- def do_settings(self, line):
781
- """Configure settings"""
782
- self.prettify = click.confirm(
783
- "\nPrettify markdown response", default=self.prettify
784
- )
785
- busy_bar.spin_index = click.prompt(
786
- "Spin bar index [0: None, 1:/, 2:■█■■■, 3:⣻]",
787
- default=busy_bar.spin_index,
788
- type=click.IntRange(0, 3),
789
- )
790
- self.color = click.prompt(
791
- "Response stdout font color", default=self.color or "white"
792
- )
793
- self.code_theme = Prompt.ask(
794
- "Enter code_theme", choices=this.rich_code_themes, default=self.code_theme
795
- )
796
- self.vertical_overflow = Prompt.ask(
797
- "\nVertical overflow behaviour",
798
- choices=["ellipsis", "visible", "crop"],
799
- default=self.vertical_overflow,
800
- )
801
- self.bot.max_tokens_to_sample = click.prompt(
802
- "\nMaximum tokens to sample",
803
- type=click.INT,
804
- default=self.bot.max_tokens_to_sample,
805
- )
806
- self.bot.temperature = click.prompt(
807
- "Temperature", type=click.FLOAT, default=self.bot.temperature
808
- )
809
- self.bot.top_k = click.prompt(
810
- "Chance of topic being repeated, top_k",
811
- type=click.FLOAT,
812
- default=self.bot.top_k,
813
- )
814
- self.bot.top_p = click.prompt(
815
- "Sampling threshold during inference time, top_p",
816
- type=click.FLOAT,
817
- default=self.bot.top_p,
818
- )
819
- self.bot.model = click.prompt(
820
- "Model name", type=click.STRING, default=self.bot.model
821
- )
822
-
823
- @busy_bar.run(help="System error")
824
- def do_copy_this(self, line):
825
- """Copy last response
826
- Usage:
827
- copy_this:
828
- text-copied = {whole last-response}
829
- copy_this code:
830
- text-copied = {All codes in last response}
831
- """
832
- if self.bot.last_response:
833
- global last_response
834
- last_response = self.bot.get_message(self.bot.last_response)
835
- if not "code" in line:
836
- clipman.set(last_response)
837
- click.secho("Last response copied successfully!", fg="cyan")
838
- return
839
-
840
- # Copies just code
841
- sanitized_codes = []
842
- code_blocks = re.findall(r"```.*?```", last_response, re.DOTALL)
843
- for code_block in code_blocks:
844
- new_code_block = re.sub(
845
- "^```.*$", "", code_block.strip(), flags=re.MULTILINE
846
- )
847
- if bool(new_code_block.strip()):
848
- sanitized_codes.append(new_code_block)
849
- if sanitized_codes:
850
- if len(sanitized_codes) > 1:
851
- if not click.confirm("Do you wish to copy all codes"):
852
- for index, code in enumerate(sanitized_codes):
853
- rich.print(
854
- Panel(
855
- Markdown(
856
- code_blocks[index], code_theme=self.code_theme
857
- ),
858
- title=f"Index : {index}",
859
- title_align="left",
860
- )
861
- )
862
-
863
- clipman.set(
864
- sanitized_codes[
865
- click.prompt(
866
- "Enter code index",
867
- type=click.IntRange(0, len(sanitized_codes) - 1),
868
- )
869
- ]
870
- )
871
- click.secho("Code copied successfully", fg="cyan")
872
- else:
873
- clipman.set("\n\n".join(sanitized_codes))
874
- click.secho(
875
- f"All {len(sanitized_codes)} codes copied successfully!",
876
- fg="cyan",
877
- )
878
- else:
879
- clipman.set(sanitized_codes[0])
880
- click.secho("Code copied successfully!", fg="cyan")
881
- else:
882
- click.secho("No code found in the last response!", fg="red")
883
- else:
884
- click.secho("Chat with AI first.", fg="yellow")
885
-
886
- @busy_bar.run()
887
- def do_with_copied(self, line):
888
- """Attach last copied text to the prompt
889
- Usage:
890
- from_copied:
891
- prompt = {text-copied}
892
- from_copied Debug this code:
893
- prompt = Debug this code {newline} {text-copied}
894
- """
895
- issued_prompt = (
896
- f"{line}\n{clipman.get()}" if bool(line.strip()) else clipman.get()
897
- )
898
- click.secho(issued_prompt, fg="yellow")
899
- if click.confirm("Do you wish to proceed"):
900
- self.default(issued_prompt)
901
-
902
- @busy_bar.run()
903
- def do_code(self, line):
904
- """Enhance prompt for code generation
905
- usage :
906
- code <Code description>
907
- """
908
- self.default(Optimizers.code(line))
909
-
910
- @busy_bar.run()
911
- def do_shell(self, line):
912
- """Enhance prompt for system command (shell) generation
913
- Usage:
914
- shell <Action to be accomplished>
915
- """
916
- self.default(Optimizers.shell_command(line))
917
- if click.confirm("Do you wish to run the command(s) generated in your system"):
918
- self.do_sys(self.bot.get_message(self.bot.last_response))
919
-
920
- @busy_bar.run("While changing directory")
921
- def do_cd(self, line):
922
- """Change directory
923
- Usage :
924
- cd <path-to-directory>
925
- """
926
- assert line, "File path is required"
927
- os.chdir(line)
928
-
929
- def do_clear(self, line):
930
- """Clear console"""
931
- sys.stdout.write("\u001b[2J\u001b[H")
932
- sys.stdout.flush()
933
-
934
- @busy_bar.run("While handling history")
935
- def do_history(self, line):
936
- """Show current conversation history"""
937
- history = self.bot.conversation.chat_history
938
- formatted_history = re.sub(
939
- "\nLLM :",
940
- "\n\n**LLM** :",
941
- re.sub("\nUser :", "\n\n**User** :", history),
942
- )
943
- self.output_bond("Chat History", formatted_history, self.color)
944
- if click.confirm("Do you wish to save this chat"):
945
- save_to = click.prompt(
946
- "Enter path/file-name", default="llama-conversation.txt"
947
- )
948
- with open(save_to, "a") as fh:
949
- fh.write(history)
950
- click.secho(f"Conversation saved successfully to '{save_to}'", fg="cyan")
951
-
952
- @busy_bar.run("while resetting conversation")
953
- def do_reset(self, line):
954
- """Start new conversation thread"""
955
- self.bot.conversation.chat_history = click.prompt(
956
- "Introductory prompt", default=self.bot.conversation.intro
957
- )
958
- if hasattr(self.bot, "reset"):
959
- self.bot.reset()
960
- click.secho("Conversation reset successfully. New one created.", fg="cyan")
961
-
962
- @busy_bar.run("while loading conversation")
963
- def do_load(self, line):
964
- """Load conversation history from file"""
965
- history_file = click.prompt("Enter path to history path", default=line)
966
- if not os.path.isfile(history_file):
967
- click.secho(f"Path `{history_file}` does not exist!", fg="red")
968
- return
969
- with open(history_file) as fh:
970
- self.bot.conversation.chat_history = fh.read()
971
- click.secho("Conversation loaded successfully.", fg="cyan")
972
-
973
- def do_last_response(self, line):
974
- """Show whole last response in json format"""
975
- self.output_bond(
976
- "Last Response",
977
- self.bot.last_response,
978
- is_json=True,
979
- )
980
-
981
- @busy_bar.run()
982
- def do_exec(self, line):
983
- """Exec python code in last response with RawDog"""
984
- last_response = self.bot.get_message(self.bot.last_response)
985
- assert last_response, "Last response is null"
986
- assert "```python" in last_response, "Last response has no python code"
987
- if self.rawdog:
988
- self.RawDog.main(last_response)
989
- else:
990
- rawdog = RawDog(
991
- quiet=self.quiet,
992
- internal_exec=self.internal_exec,
993
- confirm_script=self.confirm_script,
994
- interpreter=self.interpreter,
995
- prettify=self.prettify,
996
- )
997
- rawdog.main(last_response)
998
-
999
- @busy_bar.run()
1000
- def do_rawdog(self, line):
1001
- """Repeat executing last rawdog's python code"""
1002
- assert self.rawdog, "Session not in rawdog mode. Restart with --rawdog"
1003
- self.default(self.bot.get_message(self.bot.last_response))
1004
-
1005
- @busy_bar.run()
1006
- def default(self, line, exit_on_error: bool = False, normal_stdout: bool = False):
1007
- """Chat with LLM"""
1008
- if not bool(line):
1009
- return
1010
- if line.startswith("./"):
1011
- os.system(line[2:])
1012
-
1013
- elif self.rawdog:
1014
- self.__start_time = time.time()
1015
- busy_bar.start_spinning()
1016
- ai_response = self.bot.chat(line, stream=False)
1017
- busy_bar.stop_spinning()
1018
- is_feedback = self.RawDog.main(ai_response)
1019
- if is_feedback:
1020
- return self.default(is_feedback)
1021
- self.__end_time = time.time()
1022
-
1023
- else:
1024
- self.__start_time = time.time()
1025
- try:
1026
-
1027
- def generate_response():
1028
- # Ensure response is yielded
1029
- def for_stream():
1030
- return self.bot.chat(line, stream=True)
1031
-
1032
- def for_non_stream():
1033
- yield self.bot.chat(line, stream=False)
1034
-
1035
- return for_non_stream() if self.disable_stream else for_stream()
1036
-
1037
- busy_bar.start_spinning()
1038
- generated_response = generate_response()
1039
-
1040
- if normal_stdout or not self.prettify and not self.disable_stream:
1041
- cached_response: str = ""
1042
- if not normal_stdout:
1043
- busy_bar.stop_spinning()
1044
- for response in generated_response:
1045
- offset = len(cached_response)
1046
- print(response[offset:], end="")
1047
- cached_response = response
1048
- if not normal_stdout:
1049
- print("")
1050
- return
1051
-
1052
- if self.quiet:
1053
- busy_bar.stop_spinning()
1054
- console_ = Console()
1055
- with Live(
1056
- console=console_,
1057
- refresh_per_second=16,
1058
- vertical_overflow=self.vertical_overflow,
1059
- ) as live:
1060
- for response in generated_response:
1061
- live.update(
1062
- Markdown(response, code_theme=self.code_theme)
1063
- if self.prettify
1064
- else response
1065
- )
1066
- else:
1067
- busy_bar.stop_spinning()
1068
- this.stream_output(
1069
- generated_response,
1070
- title="AI Response",
1071
- is_markdown=self.prettify,
1072
- style=Style(
1073
- color=self.color,
1074
- ),
1075
- code_theme=self.code_theme,
1076
- vertical_overflow=self.vertical_overflow,
1077
- )
1078
- except (KeyboardInterrupt, EOFError):
1079
- busy_bar.stop_spinning()
1080
- print("")
1081
- return False # Exit cmd
1082
-
1083
- except Exception as e:
1084
- # logging.exception(e)
1085
- busy_bar.stop_spinning()
1086
- logging.error(this.getExc(e))
1087
- if exit_on_error:
1088
- sys.exit(1)
1089
- finally:
1090
- self.__end_time = time.time()
1091
-
1092
- def do_sys(self, line):
1093
- """Execute system commands
1094
- shortcut [./<command>]
1095
- Usage:
1096
- sys <System command>
1097
- or
1098
- ./<System command>
1099
- """
1100
- os.system(line)
1101
-
1102
- def do_exit(self, line):
1103
- """Quit this program"""
1104
- if click.confirm("Are you sure to exit"):
1105
- click.secho("Okay Goodbye!", fg="yellow")
1106
- return True
1107
-
1108
-
1109
- class EntryGroup:
1110
- """Entry commands"""
1111
-
1112
- # @staticmethod
1113
- @click.group()
1114
- @click.version_option(
1115
- webscout.__version__, "-v", "--version", package_name="Webscout"
1116
- )
1117
- @click.help_option("-h", "--help")
1118
- def webai_():
1119
- pass
1120
-
1121
- @staticmethod
1122
- @webai_.group()
1123
- @click.help_option("-h", "--help")
1124
- def utils():
1125
- """Utility endpoint for webscout"""
1126
- pass
1127
-
1128
- @staticmethod
1129
- @webai_.group()
1130
- @click.help_option("-h", "--help")
1131
- def gpt4free():
1132
- """Discover gpt4free models, providers etc"""
1133
- pass
1134
-
1135
- @staticmethod
1136
- @webai_.group()
1137
- @click.help_option("-h", "--help")
1138
- def awesome():
1139
- """Perform CRUD operations on awesome-prompts"""
1140
- pass
1141
-
1142
-
1143
- import webscout
1144
- class Chatwebai:
1145
- """webai command"""
1146
-
1147
- @staticmethod
1148
- @click.command(context_settings=this.context_settings)
1149
- @click.option(
1150
- "-m",
1151
- "--model",
1152
- help="Model name for text-generation", # default="llama-2-13b-chat"
1153
- )
1154
- @click.option(
1155
- "-t",
1156
- "--temperature",
1157
- help="Charge of the generated text's randomness",
1158
- type=click.FloatRange(0, 1),
1159
- default=0.2,
1160
- )
1161
- @click.option(
1162
- "-mt",
1163
- "--max-tokens",
1164
- help="Maximum number of tokens to be generated upon completion",
1165
- type=click.INT,
1166
- default=600,
1167
- )
1168
- @click.option(
1169
- "-tp",
1170
- "--top-p",
1171
- help="Sampling threshold during inference time",
1172
- type=click.FLOAT,
1173
- default=0.999,
1174
- )
1175
- @click.option(
1176
- "-tk",
1177
- "--top-k",
1178
- help="Chance of topic being repeated",
1179
- type=click.FLOAT,
1180
- default=0,
1181
- )
1182
- @click.option(
1183
- "-k",
1184
- "--key",
1185
- help="LLM API access key or auth value or path to LLM with provider.",
1186
- )
1187
- @click.option(
1188
- "-ct",
1189
- "--code-theme",
1190
- help="Theme for displaying codes in response",
1191
- type=click.Choice(this.rich_code_themes),
1192
- default="monokai",
1193
- )
1194
- @click.option(
1195
- "-bi",
1196
- "--busy-bar-index",
1197
- help="Index of busy bar icon : [0: None, 1:/, 2:■█■■■, 3:⣻]",
1198
- type=click.IntRange(0, 3),
1199
- default=3,
1200
- )
1201
- @click.option("-fc", "--font-color", help="Stdout font color")
1202
- @click.option(
1203
- "-to", "--timeout", help="Http requesting timeout", type=click.INT, default=30
1204
- )
1205
- @click.argument("prompt", required=False)
1206
- @click.option(
1207
- "--prettify/--raw",
1208
- help="Flag for prettifying markdowned response",
1209
- default=True,
1210
- )
1211
- @click.option(
1212
- "-dc",
1213
- "--disable-conversation",
1214
- is_flag=True,
1215
- default=True, # is_conversation = True
1216
- help="Disable chatting conversationally (Stable)",
1217
- )
1218
- @click.option(
1219
- "-fp",
1220
- "--filepath",
1221
- type=click.Path(),
1222
- default=os.path.join(default_path, "chat-history.txt"),
1223
- help="Path to chat history - new will be created incase doesn't exist",
1224
- )
1225
- @click.option(
1226
- "--update-file/--retain-file",
1227
- help="Controls updating chat history in file",
1228
- default=True,
1229
- )
1230
- @click.option(
1231
- "-i",
1232
- "--intro",
1233
- help="Conversation introductory prompt",
1234
- )
1235
- @click.option(
1236
- "-ho",
1237
- "--history-offset",
1238
- help="Limit conversation history to this number of last texts",
1239
- type=click.IntRange(100, 16000),
1240
- default=10250,
1241
- )
1242
- @click.option(
1243
- "-ap",
1244
- "--awesome-prompt",
1245
- default="0",
1246
- callback=lambda ctx, param, value: (
1247
- int(value) if str(value).isdigit() else value
1248
- ),
1249
- help="Awesome prompt key or index. Alt. to intro",
1250
- )
1251
- @click.option(
1252
- "-pp",
1253
- "--proxy-path",
1254
- type=click.Path(exists=True),
1255
- help="Path to .json file containing proxies",
1256
- )
1257
- @click.option(
1258
- "-p",
1259
- "--provider",
1260
- type=click.Choice(available_providers),
1261
- default=this.default_provider,
1262
- help="Name of LLM provider.",
1263
- metavar=(
1264
- f"[{'|'.join(webscout.webai)}] etc, "
1265
- "run 'webscout gpt4free list providers -w' to "
1266
- "view more providers and 'webscout gpt4free test -y' "
1267
- "for advanced g4f providers test"
1268
- ),
1269
- )
1270
- @click.option(
1271
- "-vo",
1272
- "--vertical-overflow",
1273
- help="Vertical overflow behaviour on content display",
1274
- type=click.Choice(["visible", "crop", "ellipsis"]),
1275
- default="ellipsis",
1276
- )
1277
- @click.option(
1278
- "-w",
1279
- "--whole",
1280
- is_flag=True,
1281
- default=False,
1282
- help="Disable streaming response",
1283
- )
1284
- @click.option(
1285
- "-q",
1286
- "--quiet",
1287
- is_flag=True,
1288
- help="Flag for controlling response-framing and response verbosity",
1289
- default=False,
1290
- )
1291
- @click.option(
1292
- "-n",
1293
- "--new",
1294
- help="Overwrite the filepath contents",
1295
- is_flag=True,
1296
- )
1297
- @click.option(
1298
- "-wc",
1299
- "--with-copied",
1300
- is_flag=True,
1301
- help="Postfix prompt with last copied text",
1302
- )
1303
- @click.option(
1304
- "-nc", "--no-coloring", is_flag=True, help="Disable intro prompt font-coloring"
1305
- )
1306
- @click.option(
1307
- "-cc",
1308
- "--chat-completion",
1309
- is_flag=True,
1310
- help="Provide native context for gpt4free providers",
1311
- )
1312
- @click.option(
1313
- "-iw",
1314
- "--ignore-working",
1315
- is_flag=True,
1316
- help="Ignore working status of the provider",
1317
- )
1318
- @click.option(
1319
- "-rd",
1320
- "--rawdog",
1321
- is_flag=True,
1322
- help="Generate and auto-execute Python scripts - (experimental)",
1323
- )
1324
- @click.option(
1325
- "-ix",
1326
- "--internal-exec",
1327
- is_flag=True,
1328
- help="RawDog : Execute scripts with exec function instead of out-of-script interpreter",
1329
- )
1330
- @click.option(
1331
- "-cs",
1332
- "--confirm-script",
1333
- is_flag=True,
1334
- help="RawDog : Give consent to generated scripts prior to execution",
1335
- )
1336
- @click.option(
1337
- "-int",
1338
- "--interpreter",
1339
- default="python",
1340
- help="RawDog : Python's interpreter name",
1341
- )
1342
- @click.help_option("-h", "--help")
1343
- def webai(
1344
- model,
1345
- temperature,
1346
- max_tokens,
1347
- top_p,
1348
- top_k,
1349
- key,
1350
- code_theme,
1351
- busy_bar_index,
1352
- font_color,
1353
- timeout,
1354
- prompt,
1355
- prettify,
1356
- disable_conversation,
1357
- filepath,
1358
- update_file,
1359
- intro,
1360
- history_offset,
1361
- awesome_prompt,
1362
- proxy_path,
1363
- provider,
1364
- vertical_overflow,
1365
- whole,
1366
- quiet,
1367
- new,
1368
- with_copied,
1369
- no_coloring,
1370
- chat_completion,
1371
- ignore_working,
1372
- rawdog,
1373
- internal_exec,
1374
- confirm_script,
1375
- interpreter,
1376
- ):
1377
- """Chat with AI webaily (Default)"""
1378
- this.clear_history_file(filepath, new)
1379
- bot = Main(
1380
- max_tokens,
1381
- temperature,
1382
- top_k,
1383
- top_p,
1384
- model,
1385
- key,
1386
- timeout,
1387
- disable_conversation,
1388
- filepath,
1389
- update_file,
1390
- intro,
1391
- history_offset,
1392
- awesome_prompt,
1393
- proxy_path,
1394
- provider,
1395
- quiet,
1396
- chat_completion,
1397
- ignore_working,
1398
- rawdog=rawdog,
1399
- internal_exec=internal_exec,
1400
- confirm_script=confirm_script,
1401
- interpreter=interpreter,
1402
- )
1403
- busy_bar.spin_index = busy_bar_index
1404
- bot.code_theme = code_theme
1405
- bot.color = font_color
1406
- bot.disable_coloring = no_coloring
1407
- bot.prettify = prettify
1408
- bot.vertical_overflow = vertical_overflow
1409
- bot.disable_stream = whole
1410
- if prompt:
1411
- if with_copied:
1412
- prompt = prompt + "\n" + clipman.get()
1413
- bot.default(prompt)
1414
- bot.cmdloop()
1415
-
1416
-
1417
- class ChatGenerate:
1418
- """Generate command"""
1419
-
1420
- @staticmethod
1421
- @click.command(context_settings=this.context_settings)
1422
- @click.option(
1423
- "-m",
1424
- "--model",
1425
- help="Model name for text-generation",
1426
- )
1427
- @click.option(
1428
- "-t",
1429
- "--temperature",
1430
- help="Charge of the generated text's randomness",
1431
- type=click.FloatRange(0, 1),
1432
- default=0.2,
1433
- )
1434
- @click.option(
1435
- "-mt",
1436
- "--max-tokens",
1437
- help="Maximum number of tokens to be generated upon completion",
1438
- type=click.INT,
1439
- default=600,
1440
- )
1441
- @click.option(
1442
- "-tp",
1443
- "--top-p",
1444
- help="Sampling threshold during inference time",
1445
- type=click.FLOAT,
1446
- default=0.999,
1447
- )
1448
- @click.option(
1449
- "-tk",
1450
- "--top-k",
1451
- help="Chance of topic being repeated",
1452
- type=click.FLOAT,
1453
- default=0,
1454
- )
1455
- @click.option(
1456
- "-k",
1457
- "--key",
1458
- help="LLM API access key or auth value or path to LLM with provider.",
1459
- )
1460
- @click.option(
1461
- "-ct",
1462
- "--code-theme",
1463
- help="Theme for displaying codes in response",
1464
- type=click.Choice(this.rich_code_themes),
1465
- default="monokai",
1466
- )
1467
- @click.option(
1468
- "-bi",
1469
- "--busy-bar-index",
1470
- help="Index of busy bar icon : [0: None, 1:/, 2:■█■■■, 3:⣻]",
1471
- type=click.IntRange(0, 3),
1472
- default=3,
1473
- )
1474
- @click.option(
1475
- "-fc",
1476
- "--font-color",
1477
- help="Stdout font color",
1478
- )
1479
- @click.option(
1480
- "-to", "--timeout", help="Http requesting timeout", type=click.INT, default=30
1481
- )
1482
- @click.argument("prompt", required=False)
1483
- @click.option(
1484
- "--prettify/--raw",
1485
- help="Flag for prettifying markdowned response",
1486
- default=True,
1487
- )
1488
- @click.option(
1489
- "-w",
1490
- "--whole",
1491
- is_flag=True,
1492
- default=False,
1493
- help="Disable streaming response",
1494
- )
1495
- @click.option(
1496
- "-c",
1497
- "--code",
1498
- is_flag=True,
1499
- default=False,
1500
- help="Optimize prompt for code generation",
1501
- )
1502
- @click.option(
1503
- "-s",
1504
- "--shell",
1505
- is_flag=True,
1506
- default=False,
1507
- help="Optimize prompt for shell command generation",
1508
- )
1509
- @click.option(
1510
- "-dc",
1511
- "--disable-conversation",
1512
- is_flag=True,
1513
- default=True, # is_conversation = True
1514
- help="Disable chatting conversationally (Stable)",
1515
- )
1516
- @click.option(
1517
- "-fp",
1518
- "--filepath",
1519
- type=click.Path(),
1520
- default=os.path.join(default_path, "chat-history.txt"),
1521
- help="Path to chat history - new will be created incase doesn't exist",
1522
- )
1523
- @click.option(
1524
- "--update-file/--retain-file",
1525
- help="Controls updating chat history in file",
1526
- default=True,
1527
- )
1528
- @click.option(
1529
- "-i",
1530
- "--intro",
1531
- help="Conversation introductory prompt",
1532
- )
1533
- @click.option(
1534
- "-ho",
1535
- "--history-offset",
1536
- help="Limit conversation history to this number of last texts",
1537
- type=click.IntRange(100, 16000),
1538
- default=10250,
1539
- )
1540
- @click.option(
1541
- "-ap",
1542
- "--awesome-prompt",
1543
- default="0",
1544
- callback=lambda ctx, param, value: (
1545
- int(value) if str(value).isdigit() else value
1546
- ),
1547
- help="Awesome prompt key or index. Alt. to intro",
1548
- )
1549
- @click.option(
1550
- "-pp",
1551
- "--proxy-path",
1552
- type=click.Path(exists=True),
1553
- help="Path to .json file containing proxies",
1554
- )
1555
- @click.option(
1556
- "-p",
1557
- "--provider",
1558
- type=click.Choice(webscout.available_providers),
1559
- default=this.default_provider,
1560
- help="Name of LLM provider.",
1561
- metavar=(
1562
- f"[{'|'.join(webscout.webai)}] etc, "
1563
- "run 'webscout gpt4free list providers -w' to "
1564
- "view more providers and 'webscout gpt4free test -y' "
1565
- "for advanced g4f providers test"
1566
- ),
1567
- )
1568
- @click.option(
1569
- "-vo",
1570
- "--vertical-overflow",
1571
- help="Vertical overflow behaviour on content display",
1572
- type=click.Choice(["visible", "crop", "ellipsis"]),
1573
- default="ellipsis",
1574
- )
1575
- @click.option(
1576
- "-q",
1577
- "--quiet",
1578
- is_flag=True,
1579
- help="Flag for controlling response-framing and response verbosity",
1580
- default=False,
1581
- )
1582
- @click.option(
1583
- "-n",
1584
- "--new",
1585
- help="Override the filepath contents",
1586
- is_flag=True,
1587
- )
1588
- @click.option(
1589
- "-wc",
1590
- "--with-copied",
1591
- is_flag=True,
1592
- help="Postfix prompt with last copied text",
1593
- )
1594
- @click.option(
1595
- "-iw",
1596
- "--ignore-working",
1597
- is_flag=True,
1598
- help="Ignore working status of the provider",
1599
- )
1600
- @click.option(
1601
- "-rd",
1602
- "--rawdog",
1603
- is_flag=True,
1604
- help="Generate and auto-execute Python scripts - (experimental)",
1605
- )
1606
- @click.option(
1607
- "-ix",
1608
- "--internal-exec",
1609
- is_flag=True,
1610
- help="RawDog : Execute scripts with exec function instead of out-of-script interpreter",
1611
- )
1612
- @click.option(
1613
- "-cs",
1614
- "--confirm-script",
1615
- is_flag=True,
1616
- help="RawDog : Give consent to generated scripts prior to execution",
1617
- )
1618
- @click.option(
1619
- "-int",
1620
- "--interpreter",
1621
- default="python",
1622
- help="RawDog : Python's interpreter name",
1623
- )
1624
- @click.help_option("-h", "--help")
1625
- def generate(
1626
- model,
1627
- temperature,
1628
- max_tokens,
1629
- top_p,
1630
- top_k,
1631
- key,
1632
- code_theme,
1633
- busy_bar_index,
1634
- font_color,
1635
- timeout,
1636
- prompt,
1637
- prettify,
1638
- whole,
1639
- code,
1640
- shell,
1641
- disable_conversation,
1642
- filepath,
1643
- update_file,
1644
- intro,
1645
- history_offset,
1646
- awesome_prompt,
1647
- proxy_path,
1648
- provider,
1649
- vertical_overflow,
1650
- quiet,
1651
- new,
1652
- with_copied,
1653
- ignore_working,
1654
- rawdog,
1655
- internal_exec,
1656
- confirm_script,
1657
- interpreter,
1658
- ):
1659
- """Generate a quick response with AI"""
1660
- this.clear_history_file(filepath, new)
1661
- bot = Main(
1662
- max_tokens,
1663
- temperature,
1664
- top_k,
1665
- top_p,
1666
- model,
1667
- key,
1668
- timeout,
1669
- disable_conversation,
1670
- filepath,
1671
- update_file,
1672
- intro,
1673
- history_offset,
1674
- awesome_prompt,
1675
- proxy_path,
1676
- provider,
1677
- quiet,
1678
- ignore_working=ignore_working,
1679
- rawdog=rawdog,
1680
- internal_exec=internal_exec,
1681
- confirm_script=confirm_script,
1682
- interpreter=interpreter,
1683
- )
1684
- prompt = prompt if prompt else ""
1685
- copied_placeholder = "{{copied}}"
1686
- stream_placeholder = "{{stream}}"
1687
-
1688
- if with_copied or copied_placeholder in prompt:
1689
- last_copied_text = clipman.get()
1690
- assert last_copied_text, "No copied text found, issue prompt"
1691
-
1692
- if copied_placeholder in prompt:
1693
- prompt = prompt.replace(copied_placeholder, last_copied_text)
1694
-
1695
- else:
1696
- sep = "\n" if prompt else ""
1697
- prompt = prompt + sep + last_copied_text
1698
-
1699
- if not prompt and sys.stdin.isatty(): # No prompt issued and no piped input
1700
- help_info = (
1701
- "Usage: webscout generate [OPTIONS] PROMPT\n"
1702
- "Try 'webscout generate --help' for help.\n"
1703
- "Error: Missing argument 'PROMPT'."
1704
- )
1705
- click.secho(
1706
- help_info
1707
- ) # Let's try to mimic the click's missing argument help info
1708
- sys.exit(1)
1709
-
1710
- if not sys.stdin.isatty(): # Piped input detected - True
1711
- # Let's try to read piped input
1712
- stream_text = click.get_text_stream("stdin").read()
1713
- if stream_placeholder in prompt:
1714
- prompt = prompt.replace(stream_placeholder, stream_text)
1715
- else:
1716
- prompt = prompt + "\n" + stream_text if prompt else stream_text
1717
-
1718
- assert stream_placeholder not in prompt, (
1719
- "No piped input detected ~ " + stream_placeholder
1720
- )
1721
- assert copied_placeholder not in prompt, (
1722
- "No copied text found ~ " + copied_placeholder
1723
- )
1724
-
1725
- prompt = Optimizers.code(prompt) if code else prompt
1726
- prompt = Optimizers.shell_command(prompt) if shell else prompt
1727
- busy_bar.spin_index = (
1728
- 0 if any([quiet, sys.stdout.isatty() == False]) else busy_bar_index
1729
- )
1730
- bot.code_theme = code_theme
1731
- bot.color = font_color
1732
- bot.prettify = prettify
1733
- bot.vertical_overflow = vertical_overflow
1734
- bot.disable_stream = whole
1735
- bot.default(prompt, True, normal_stdout=(sys.stdout.isatty() == False))
1736
-
1737
-
1738
- class Awesome:
1739
- """Awesome commands"""
1740
-
1741
- @staticmethod
1742
- @click.command(context_settings=this.context_settings)
1743
- @click.option(
1744
- "-r",
1745
- "--remote",
1746
- help="Remote source to update from",
1747
- default=AwesomePrompts.awesome_prompt_url,
1748
- )
1749
- @click.option(
1750
- "-o",
1751
- "--output",
1752
- help="Path to save the prompts",
1753
- default=AwesomePrompts.awesome_prompt_path,
1754
- )
1755
- @click.option(
1756
- "-n", "--new", is_flag=True, help="Override the existing contents in path"
1757
- )
1758
- @click.help_option("-h", "--help")
1759
- @this.handle_exception
1760
- def update(remote, output, new):
1761
- """Update awesome-prompts from remote source."""
1762
- AwesomePrompts.awesome_prompt_url = remote
1763
- AwesomePrompts.awesome_prompt_path = output
1764
- AwesomePrompts().update_prompts_from_online(new)
1765
- click.secho(
1766
- f"Prompts saved to - '{AwesomePrompts.awesome_prompt_path}'", fg="cyan"
1767
- )
1768
-
1769
- @staticmethod
1770
- @click.command(context_settings=this.context_settings)
1771
- @click.argument(
1772
- "key",
1773
- required=True,
1774
- type=click.STRING,
1775
- )
1776
- @click.option(
1777
- "-d", "--default", help="Return this value if not found", default=None
1778
- )
1779
- @click.option(
1780
- "-c",
1781
- "--case-sensitive",
1782
- default=True,
1783
- flag_value=False,
1784
- help="Perform case-sensitive search",
1785
- )
1786
- @click.option(
1787
- "-f",
1788
- "--file",
1789
- type=click.Path(exists=True),
1790
- help="Path to existing prompts",
1791
- default=AwesomePrompts.awesome_prompt_path,
1792
- )
1793
- @click.help_option("-h", "--help")
1794
- @this.handle_exception
1795
- def search(
1796
- key,
1797
- default,
1798
- case_sensitive,
1799
- file,
1800
- ):
1801
- """Search for a particular awesome-prompt by key or index"""
1802
- AwesomePrompts.awesome_prompt_path = file
1803
- resp = AwesomePrompts().get_act(
1804
- key,
1805
- default=default,
1806
- case_insensitive=case_sensitive,
1807
- )
1808
- if resp:
1809
- click.secho(resp)
1810
- return resp != default
1811
-
1812
- @staticmethod
1813
- @click.command(context_settings=this.context_settings)
1814
- @click.option("-n", "--name", required=True, help="Prompt name")
1815
- @click.option("-p", "--prompt", required=True, help="Prompt value")
1816
- @click.option(
1817
- "-f",
1818
- "--file",
1819
- type=click.Path(exists=True),
1820
- help="Path to existing prompts",
1821
- default=AwesomePrompts.awesome_prompt_path,
1822
- )
1823
- @click.help_option("-h", "--help")
1824
- @this.handle_exception
1825
- def add(name, prompt, file):
1826
- """Add new prompt to awesome-prompt list"""
1827
- AwesomePrompts.awesome_prompt_path = file
1828
- return AwesomePrompts().add_prompt(name, prompt)
1829
-
1830
- @staticmethod
1831
- @click.command(context_settings=this.context_settings)
1832
- @click.argument("name")
1833
- @click.option(
1834
- "--case-sensitive",
1835
- is_flag=True,
1836
- flag_value=False,
1837
- default=True,
1838
- help="Perform name case-sensitive search",
1839
- )
1840
- @click.option(
1841
- "-f",
1842
- "--file",
1843
- type=click.Path(exists=True),
1844
- help="Path to existing prompts",
1845
- default=AwesomePrompts.awesome_prompt_path,
1846
- )
1847
- @click.help_option("-h", "--help")
1848
- @this.handle_exception
1849
- def delete(name, case_sensitive, file):
1850
- """Delete a specific awesome-prompt"""
1851
- AwesomePrompts.awesome_prompt_path = file
1852
- return AwesomePrompts().delete_prompt(name, case_sensitive)
1853
-
1854
- @staticmethod
1855
- @click.command(context_settings=this.context_settings)
1856
- @click.option(
1857
- "-j",
1858
- "--json",
1859
- is_flag=True,
1860
- help="Display prompts in json format",
1861
- )
1862
- @click.option(
1863
- "-i",
1864
- "--indent",
1865
- type=click.IntRange(1, 20),
1866
- help="Json format indentation level",
1867
- default=4,
1868
- )
1869
- @click.option(
1870
- "-x",
1871
- "--index",
1872
- is_flag=True,
1873
- help="Display prompts with their corresponding indexes",
1874
- )
1875
- @click.option("-c", "--color", help="Prompts stdout font color")
1876
- @click.option("-o", "--output", type=click.Path(), help="Path to save the prompts")
1877
- @click.help_option("-h", "--help")
1878
- def whole(json, indent, index, color, output):
1879
- """Stdout all awesome prompts"""
1880
- ap = AwesomePrompts()
1881
- awesome_prompts = ap.all_acts if index else ap.get_acts()
1882
-
1883
- if json:
1884
- # click.secho(formatted_awesome_prompts, fg=color)
1885
- rich.print_json(data=awesome_prompts, indent=indent)
1886
-
1887
- else:
1888
- awesome_table = Table(show_lines=True, title="All Awesome-Prompts")
1889
- awesome_table.add_column("index", justify="center", style="yellow")
1890
- awesome_table.add_column("Act Name/Index", justify="left", style="cyan")
1891
- awesome_table.add_column(
1892
- "Prompt",
1893
- style=color,
1894
- )
1895
- for index, key_value in enumerate(awesome_prompts.items()):
1896
- awesome_table.add_row(str(index), str(key_value[0]), key_value[1])
1897
- rich.print(awesome_table)
1898
-
1899
- if output:
1900
- from json import dump
1901
-
1902
- with open(output, "w") as fh:
1903
- dump(awesome_prompts, fh, indent=4)
1904
-
1905
-
1906
- class Gpt4free:
1907
- """Commands for gpt4free"""
1908
-
1909
- @staticmethod
1910
- @click.command(context_settings=this.context_settings)
1911
- @busy_bar.run(index=1, immediate=True)
1912
- @click.help_option("-h", "--help")
1913
- def version():
1914
- """Check current installed version of gpt4free"""
1915
- version_string = this.run_system_command("pip show g4f")[1].stdout.split("\n")[
1916
- 1
1917
- ]
1918
- click.secho(version_string, fg="cyan")
1919
-
1920
- @staticmethod
1921
- @click.command(context_settings=this.context_settings)
1922
- @click.help_option("-h", "--help")
1923
- @click.option(
1924
- "-e",
1925
- "--extra",
1926
- help="Extra required dependencies category",
1927
- multiple=True,
1928
- type=click.Choice(
1929
- ["all", "image", "webdriver", "openai", "api", "gui", "none"]
1930
- ),
1931
- default=["all"],
1932
- )
1933
- @click.option("-l", "--log", is_flag=True, help="Stdout installation logs")
1934
- @click.option(
1935
- "-s",
1936
- "--sudo",
1937
- is_flag=True,
1938
- flag_value="sudo ",
1939
- help="Install with sudo privileges",
1940
- )
1941
- @busy_bar.run(index=1, immediate=True)
1942
- def update(extra, log, sudo):
1943
- """Update GPT4FREE package (Models, Providers etc)"""
1944
- if "none" in extra:
1945
- command = f"{sudo or ''}pip install --upgrade g4f"
1946
- else:
1947
- command = f"{sudo or ''}pip install --upgrade g4f[{','.join(extra)}]"
1948
- is_successful, response = this.run_system_command(command)
1949
- if log and is_successful:
1950
- click.echo(response.stdout)
1951
- version_string = this.run_system_command("pip show g4f")[1].stdout.split("\n")[
1952
- 1
1953
- ]
1954
- click.secho(f"GPT4FREE updated successfully - {version_string}", fg="cyan")
1955
-
1956
- @staticmethod
1957
- @click.command("list", context_settings=this.context_settings)
1958
- @click.argument("target")
1959
- @click.option("-w", "--working", is_flag=True, help="Restrict to working providers")
1960
- @click.option("-u", "--url", is_flag=True, help="Restrict to providers with url")
1961
- @click.option(
1962
- "-s", "--stream", is_flag=True, help="Restrict to providers supporting stream"
1963
- )
1964
- @click.option(
1965
- "-c",
1966
- "--context",
1967
- is_flag=True,
1968
- help="Restrict to providers supporing context natively",
1969
- )
1970
- @click.option(
1971
- "-35",
1972
- "--gpt35",
1973
- is_flag=True,
1974
- help="Restrict to providers supporting gpt3.5_turbo model",
1975
- )
1976
- @click.option(
1977
- "-4", "--gpt4", is_flag=True, help="Restrict to providers supporting gpt4 model"
1978
- )
1979
- @click.option(
1980
- "-se",
1981
- "--selenium",
1982
- is_flag=True,
1983
- help="Restrict to selenium dependent providers",
1984
- )
1985
- @click.option("-j", "--json", is_flag=True, help="Format output in json")
1986
- @click.help_option("-h", "--help")
1987
- def show(target, working, url, stream, context, gpt35, gpt4, selenium, json):
1988
- """List available models and providers"""
1989
- available_targets = ["models", "providers"]
1990
- assert (
1991
- target in available_targets
1992
- ), f"Target must be one of [{', '.join(available_targets)}]"
1993
- if target == "providers":
1994
- hunted_providers = list(
1995
- set(
1996
- map(
1997
- lambda provider: (
1998
- provider["name"] if all(list(provider.values())) else None
1999
- ),
2000
- this.g4f_providers_in_dict(
2001
- url=url,
2002
- working=working,
2003
- stream=stream,
2004
- context=context,
2005
- gpt35=gpt35,
2006
- gpt4=gpt4,
2007
- selenium=selenium,
2008
- ),
2009
- )
2010
- )
2011
- )
2012
- while None in hunted_providers:
2013
- hunted_providers.remove(None)
2014
-
2015
- hunted_providers.sort()
2016
- if json:
2017
- rich.print_json(data=dict(providers=hunted_providers), indent=4)
2018
-
2019
- else:
2020
- table = Table(show_lines=True)
2021
- table.add_column("No.", style="yellow", justify="center")
2022
- table.add_column("Provider", style="cyan")
2023
- for no, provider in enumerate(hunted_providers):
2024
- table.add_row(str(no), provider)
2025
- rich.print(table)
2026
- else:
2027
- models = dict(
2028
- Bard=[
2029
- "palm",
2030
- ],
2031
- HuggingFace=[
2032
- "h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
2033
- "h2ogpt-gm-oasst1-en-2048-falcon-40b-v1",
2034
- "h2ogpt-gm-oasst1-en-2048-open-llama-13b",
2035
- "gpt-neox-20b",
2036
- "oasst-sft-1-pythia-12b",
2037
- "oasst-sft-4-pythia-12b-epoch-3.5",
2038
- "santacoder",
2039
- "bloom",
2040
- "flan-t5-xxl",
2041
- ],
2042
- Anthropic=[
2043
- "claude-instant-v1",
2044
- "claude-v1",
2045
- "claude-v2",
2046
- ],
2047
- Cohere=[
2048
- "command-light-nightly",
2049
- "command-nightly",
2050
- ],
2051
- OpenAI=[
2052
- "code-davinci-002",
2053
- "text-ada-001",
2054
- "text-babbage-001",
2055
- "text-curie-001",
2056
- "text-davinci-002",
2057
- "text-davinci-003",
2058
- "gpt-3.5-turbo-16k",
2059
- "gpt-3.5-turbo-16k-0613",
2060
- "gpt-4-0613",
2061
- ],
2062
- Replicate=[
2063
- "llama13b-v2-chat",
2064
- "llama7b-v2-chat",
2065
- ],
2066
- )
2067
- for provider in webscout.g4f.Provider.__providers__:
2068
- if hasattr(provider, "models"):
2069
- models[provider.__name__] = provider.models
2070
- if json:
2071
- for key, value in models.items():
2072
- while None in value:
2073
- value.remove(None)
2074
- value.sort()
2075
- models[key] = value
2076
-
2077
- rich.print_json(data=models, indent=4)
2078
- else:
2079
- table = Table(show_lines=True)
2080
- table.add_column("No.", justify="center", style="white")
2081
- table.add_column("Base Provider", style="cyan")
2082
- table.add_column("Model(s)", style="yellow")
2083
- for count, provider_models in enumerate(models.items()):
2084
- models = provider_models[1]
2085
- models.sort()
2086
- table.add_row(str(count), provider_models[0], "\n".join(models))
2087
- rich.print(table)
2088
-
2089
- @staticmethod
2090
- @click.command(context_settings=this.context_settings)
2091
- @click.argument("port", type=click.INT, required=False)
2092
- @click.option(
2093
- "-a", "--address", help="Host on this particular address", default="127.0.0.1"
2094
- )
2095
- @click.option("-d", "--debug", is_flag=True, help="Start server in debug mode")
2096
- @click.option(
2097
- "-o", "--open", is_flag=True, help="Proceed to the interface immediately"
2098
- )
2099
- @click.help_option("-h", "--help")
2100
- def gui(port, address, debug, open):
2101
- """Launch gpt4free web interface"""
2102
- from g4f.gui import run_gui
2103
-
2104
- port = port or 8000
2105
- t1 = thr(
2106
- target=run_gui,
2107
- args=(
2108
- address,
2109
- port,
2110
- debug,
2111
- ),
2112
- )
2113
- # run_gui(host=address, port=port, debug=debug)
2114
- t1.start()
2115
- if open:
2116
- click.launch(f"http://{address}:{port}")
2117
- t1.join()
2118
-
2119
- @staticmethod
2120
- @click.command(context_settings=this.context_settings)
2121
- @click.option(
2122
- "-t",
2123
- "--timeout",
2124
- type=click.INT,
2125
- help="Provider's response generation timeout",
2126
- default=20,
2127
- )
2128
- @click.option(
2129
- "-r",
2130
- "--thread",
2131
- type=click.INT,
2132
- help="Test n amount of providers at once",
2133
- default=5,
2134
- )
2135
- @click.option("-q", "--quiet", is_flag=True, help="Suppress progress bar")
2136
- @click.option(
2137
- "-j", "--json", is_flag=True, help="Stdout test results in json format"
2138
- )
2139
- @click.option("-d", "--dry-test", is_flag=True, help="Return previous test results")
2140
- @click.option(
2141
- "-b", "--best", is_flag=True, help="Stdout the fastest provider <name only>"
2142
- )
2143
- @click.option(
2144
- "-se",
2145
- "--selenium",
2146
- help="Test even selenium dependent providers",
2147
- is_flag=True,
2148
- )
2149
- @click.option(
2150
- "-dl",
2151
- "--disable-logging",
2152
- is_flag=True,
2153
- help="Disable logging",
2154
- )
2155
- @click.option("-y", "--yes", is_flag=True, help="Okay to all confirmations")
2156
- @click.help_option("-h", "--help")
2157
- def test(
2158
- timeout, thread, quiet, json, dry_test, best, selenium, disable_logging, yes
2159
- ):
2160
- """Test and save working providers"""
2161
- from webscout.g4f import TestProviders
2162
-
2163
- test = TestProviders(
2164
- test_at_once=thread,
2165
- quiet=quiet,
2166
- timeout=timeout,
2167
- selenium=selenium,
2168
- do_log=disable_logging == False,
2169
- )
2170
- if best:
2171
- click.secho(test.best)
2172
- return
2173
- elif dry_test:
2174
- results = test.get_results(
2175
- run=False,
2176
- )
2177
- else:
2178
- if (
2179
- yes
2180
- or os.path.isfile(webscout.AIutel.results_path)
2181
- and click.confirm("Are you sure to run new test")
2182
- ):
2183
- results = test.get_results(run=True)
2184
- else:
2185
- results = test.get_results(
2186
- run=False,
2187
- )
2188
- if json:
2189
- rich.print_json(data=dict(results=results))
2190
- else:
2191
- table = Table(
2192
- title="G4f Providers Test Results",
2193
- show_lines=True,
2194
- )
2195
- table.add_column("No.", style="white", justify="center")
2196
- table.add_column("Provider", style="yellow", justify="left")
2197
- table.add_column("Response Time(s)", style="cyan")
2198
-
2199
- for no, provider in enumerate(results, start=1):
2200
- table.add_row(
2201
- str(no), provider["name"], str(round(provider["time"], 2))
2202
- )
2203
- rich.print(table)
2204
-
2205
-
2206
-
2207
- @staticmethod
2208
- @click.command(context_settings=this.context_settings)
2209
- @click.argument("prompt")
2210
- @click.option(
2211
- "-d",
2212
- "--directory",
2213
- type=click.Path(exists=True),
2214
- help="Folder for saving the images",
2215
- default=os.getcwd(),
2216
- )
2217
- @click.option(
2218
- "-a",
2219
- "--amount",
2220
- type=click.IntRange(1, 100),
2221
- help="Total images to be generated",
2222
- default=1,
2223
- )
2224
- @click.option("-n", "--name", help="Name for the generated images")
2225
- @click.option(
2226
- "-t",
2227
- "--timeout",
2228
- type=click.IntRange(5, 300),
2229
- help="Http request timeout in seconds",
2230
- )
2231
- @click.option("-p", "--proxy", help="Http request proxy")
2232
- @click.option(
2233
- "-nd",
2234
- "--no-additives",
2235
- is_flag=True,
2236
- help="Disable prompt altering for effective image generation",
2237
- )
2238
- @click.option("-q", "--quiet", is_flag=True, help="Suppress progress bar")
2239
- @click.help_option("-h", "--help")
2240
- def generate_image(
2241
- prompt, directory, amount, name, timeout, proxy, no_additives, quiet
2242
- ):
2243
- """Generate images with pollinations.ai"""
2244
- with Progress() as progress:
2245
- task = progress.add_task(
2246
- f"[cyan]Generating ...[{amount}]",
2247
- total=amount,
2248
- visible=quiet == False,
2249
- )
2250
-
2251
-
2252
-
2253
- class Utils:
2254
- """Utilities command"""
2255
-
2256
- @staticmethod
2257
- @click.command(context_settings=this.context_settings)
2258
- @click.argument("source", required=False)
2259
- @click.option(
2260
- "-d", "--dev", is_flag=True, help="Update from version control (development)"
2261
- )
2262
- @click.option(
2263
- "-s",
2264
- "--sudo",
2265
- is_flag=True,
2266
- flag_value="sudo ",
2267
- help="Install with sudo privileges",
2268
- )
2269
- @click.help_option("-h", "--help")
2270
- @busy_bar.run(index=1, immediate=True)
2271
- def update(source, dev, sudo):
2272
- """Install latest version of webscout"""
2273
- if dev:
2274
- source = "git+" + webscout.__repo__ + ".git"
2275
- source = "webscout" if source is None else source
2276
- assert (
2277
- "tgpt" in source or source == "."
2278
- ), f"Cannot update webscout from the source '{source}'"
2279
- click.secho(
2280
- f"[*] Updating from '{'pip' if source=='webscout' else source}'",
2281
- fg="yellow",
2282
- )
2283
- this.run_system_command(f"{sudo or ''}pip install --upgrade {source}")
2284
- response = this.run_system_command("pip show webscout")[1]
2285
- click.secho(response.stdout)
2286
- click.secho("Congratulations! webscout updated successfully.", fg="cyan")
2287
-
2288
- @staticmethod
2289
- @click.command(context_settings=this.context_settings)
2290
- @click.option("-w", "--whole", is_flag=True, help="Stdout whole json info")
2291
- @click.option(
2292
- "-v", "--version", is_flag=True, help="Stdout latest version name only"
2293
- )
2294
- @click.option("-b", "--body", is_flag=True, help="Stdout changelog info only")
2295
- @click.option(
2296
- "-e", "--executable", is_flag=True, help="Stdout url to binary for your system"
2297
- )
2298
- @click.help_option("-h", "--help")
2299
- def latest(whole, version, body, executable):
2300
- """Check webscout latest version info"""
2301
- from webscout.utils import Updates
2302
-
2303
- update = Updates()
2304
- if whole:
2305
- rich.print_json(data=update.latest(whole=True))
2306
-
2307
- elif version:
2308
- rich.print(update.latest_version)
2309
- elif body:
2310
- rich.print(Markdown(update.latest()["body"]))
2311
- elif executable:
2312
- rich.print(update.executable())
2313
- else:
2314
- rich.print_json(data=update.latest())
2315
-
2316
-
2317
- def make_commands():
2318
- """Make webscout chained commands"""
2319
-
2320
- # generate
2321
- EntryGroup.webai_.add_command(ChatGenerate.generate)
2322
-
2323
- # webai
2324
- EntryGroup.webai_.add_command(Chatwebai.webai)
2325
-
2326
- # utils
2327
- EntryGroup.utils.add_command(Utils.update)
2328
- EntryGroup.utils.add_command(Utils.latest)
2329
-
2330
- # gpt4free
2331
- EntryGroup.gpt4free.add_command(Gpt4free.version)
2332
- EntryGroup.gpt4free.add_command(Gpt4free.update)
2333
- EntryGroup.gpt4free.add_command(Gpt4free.show)
2334
- EntryGroup.gpt4free.add_command(Gpt4free.gui)
2335
- EntryGroup.gpt4free.add_command(Gpt4free.test)
2336
-
2337
- # Awesome
2338
- EntryGroup.awesome.add_command(Awesome.add)
2339
- EntryGroup.awesome.add_command(Awesome.delete)
2340
- EntryGroup.awesome.add_command(Awesome.search)
2341
- EntryGroup.awesome.add_command(Awesome.update)
2342
- EntryGroup.awesome.add_command(Awesome.whole)
2343
-
2344
-
2345
- # @this.handle_exception
2346
- def main(*args):
2347
- """Fireup console programmically"""
2348
- sys.argv += list(args)
2349
- args = sys.argv
2350
- if len(args) == 1:
2351
- sys.argv.insert(1, "webai") # Just a hack to make default command
2352
- try:
2353
- make_commands()
2354
- return EntryGroup.webai_()
2355
- except Exception as e:
2356
- logging.error(this.getExc(e))
2357
- sys.exit(1)
2358
-
2359
-
2360
- if __name__ == "__main__":
1
+ import webscout
2
+ import click
3
+ import cmd
4
+ import logging
5
+ import os
6
+ import sys
7
+ import clipman
8
+ import re
9
+ import rich
10
+ import getpass
11
+ import json
12
+ import re
13
+ import sys
14
+ import datetime
15
+ import time
16
+ import subprocess
17
+ from threading import Thread as thr
18
+ from functools import wraps
19
+ from rich.panel import Panel
20
+ from rich.style import Style
21
+ from rich.markdown import Markdown
22
+ from rich.console import Console
23
+ from rich.live import Live
24
+ from rich.table import Table
25
+ from rich.prompt import Prompt
26
+ from rich.progress import Progress
27
+ from typing import Iterator
28
+ from webscout.AIutel import Optimizers
29
+ from webscout.AIutel import default_path
30
+ from webscout.AIutel import AwesomePrompts
31
+ from webscout.AIutel import RawDog
32
+ from webscout.AIutel import Audio
33
+ from webscout import available_providers
34
+ from colorama import Fore
35
+ from colorama import init as init_colorama
36
+ from dotenv import load_dotenv
37
+ import g4f
38
+ import webscout.AIutel
39
+
40
+ init_colorama(autoreset=True)
41
+
42
+ load_dotenv() # loads .env variables
43
+
44
+ logging.basicConfig(
45
+ format="%(asctime)s - %(levelname)s : %(message)s ",
46
+ datefmt="%H:%M:%S",
47
+ level=logging.INFO,
48
+ )
49
+
50
+ try:
51
+ clipman.init()
52
+ except Exception as e:
53
+ logging.debug(f"Dropping clipman in favor of pyperclip - {(e)}")
54
+ import pyperclip
55
+
56
+ clipman.set = pyperclip.copy
57
+ clipman.get = pyperclip.paste
58
+
59
+
60
+ class this:
61
+ """Console's common variables"""
62
+
63
+ rich_code_themes = ["monokai", "paraiso-dark", "igor", "vs", "fruity", "xcode"]
64
+
65
+ default_provider = "phind"
66
+
67
+ getExc = lambda e: e.args[1] if len(e.args) > 1 else str(e)
68
+
69
+ context_settings = dict(auto_envvar_prefix="Webscout")
70
+
71
+ """Console utils"""
72
+
73
+ @staticmethod
74
+ def run_system_command(
75
+ command: str, exit_on_error: bool = True, stdout_error: bool = True
76
+ ):
77
+ """Run commands against system
78
+ Args:
79
+ command (str): shell command
80
+ exit_on_error (bool, optional): Exit on error. Defaults to True.
81
+ stdout_error (bool, optional): Print out the error. Defaults to True.
82
+
83
+ Returns:
84
+ tuple : (is_successfull, object[Exception|Subprocess.run])
85
+ """
86
+ try:
87
+ # Run the command and capture the output
88
+ result = subprocess.run(
89
+ command,
90
+ shell=True,
91
+ check=True,
92
+ text=True,
93
+ stdout=subprocess.PIPE,
94
+ stderr=subprocess.PIPE,
95
+ )
96
+ return (True, result)
97
+ except subprocess.CalledProcessError as e:
98
+ # Handle error if the command returns a non-zero exit code
99
+ if stdout_error:
100
+ click.secho(f"Error Occurred: while running '{command}'", fg="yellow")
101
+ click.secho(e.stderr, fg="red")
102
+ sys.exit(e.returncode) if exit_on_error else None
103
+ return (False, e)
104
+
105
+ def g4f_providers_in_dict(
106
+ url=True,
107
+ working=True,
108
+ stream=False,
109
+ context=False,
110
+ gpt35=False,
111
+ gpt4=False,
112
+ selenium=False,
113
+ ):
114
+ from webscout.g4f import GPT4FREE
115
+ import g4f.Provider.selenium as selenium_based
116
+
117
+ selenium_based_providers: list = dir(selenium_based)
118
+ hunted_providers = []
119
+ required_attrs = (
120
+ "url",
121
+ "working",
122
+ "supports_gpt_35_turbo",
123
+ "supports_gpt_4",
124
+ "supports_stream",
125
+ "supports_message_history",
126
+ )
127
+
128
+ def sanitize_provider(provider: object):
129
+ for attr in required_attrs:
130
+ if not hasattr(provider, attr):
131
+ setattr(provider, attr, False)
132
+
133
+ return provider
134
+
135
+ for provider_name, provider_class in g4f.Provider.__map__.items():
136
+ provider = sanitize_provider(provider_class)
137
+ provider_meta = dict(name=provider_name)
138
+ if url:
139
+ provider_meta["url"] = provider.url
140
+ if working:
141
+ provider_meta["working"] = provider.working
142
+ if stream:
143
+ provider_meta["stream"] = provider.supports_stream
144
+ if context:
145
+ provider_meta["context"] = provider.supports_message_history
146
+ if gpt35:
147
+ provider_meta["gpt35_turbo"] = provider.supports_gpt_35_turbo
148
+ if gpt4:
149
+ provider_meta["gpt4"] = provider.supports_gpt_4
150
+ if selenium:
151
+ try:
152
+ selenium_based_providers.index(provider_meta["name"])
153
+ value = True
154
+ except ValueError:
155
+ value = False
156
+ provider_meta["non_selenium"] = value
157
+
158
+ hunted_providers.append(provider_meta)
159
+
160
+ return hunted_providers
161
+
162
+ @staticmethod
163
+ def stream_output(
164
+ iterable: Iterator,
165
+ title: str = "",
166
+ is_markdown: bool = True,
167
+ style: object = Style(),
168
+ transient: bool = False,
169
+ title_generator: object = None,
170
+ title_generator_params: dict = {},
171
+ code_theme: str = "monokai",
172
+ vertical_overflow: str = "ellipsis",
173
+ ) -> None:
174
+ """Stdout streaming response
175
+
176
+ Args:
177
+ iterable (Iterator): Iterator containing contents to be stdout
178
+ title (str, optional): Content title. Defaults to ''.
179
+ is_markdown (bool, optional): Flag for markdown content. Defaults to True.
180
+ style (object, optional): `rich.style` instance. Defaults to Style().
181
+ transient (bool, optional): Flag for transient. Defaults to False.
182
+ title_generator (object, optional): Function for generating title. Defaults to None.
183
+ title_generator_params (dict, optional): Kwargs for `title_generator` function. Defaults to {}.
184
+ code_theme (str, optional): Theme for styling codes. Defaults to `monokai`
185
+ vertical_overflow (str, optional): Vertical overflow behaviour on content display. Defaultss to ellipsis.
186
+ """
187
+ render_this = ""
188
+ with Live(
189
+ render_this,
190
+ transient=transient,
191
+ refresh_per_second=8,
192
+ vertical_overflow=vertical_overflow,
193
+ ) as live:
194
+ for entry in iterable:
195
+ render_this += entry
196
+ live.update(
197
+ Panel(
198
+ (
199
+ Markdown(entry, code_theme=code_theme)
200
+ if is_markdown
201
+ else entry
202
+ ),
203
+ title=title,
204
+ style=style,
205
+ )
206
+ )
207
+ if title_generator:
208
+ title = title_generator(**title_generator_params)
209
+ live.update(
210
+ Panel(
211
+ Markdown(entry, code_theme=code_theme) if is_markdown else entry,
212
+ title=title,
213
+ style=style,
214
+ )
215
+ )
216
+
217
+ @staticmethod
218
+ def clear_history_file(file_path, is_true):
219
+ """When --new flag is True"""
220
+ if is_true and os.path.isfile(file_path):
221
+ try:
222
+ os.remove(file_path)
223
+ except Exception as e:
224
+ logging.error(
225
+ f"Failed to clear previous chat history - {this.getExc(e)}"
226
+ )
227
+
228
+ @staticmethod
229
+ def handle_exception(func):
230
+ """Safely handles cli-based exceptions and exit status-codes"""
231
+
232
+ @wraps(func)
233
+ def decorator(*args, **kwargs):
234
+ try:
235
+ exit_status = func(*args, **kwargs)
236
+ except Exception as e:
237
+ exit_status = False
238
+ logging.error(this.getExc(e))
239
+ finally:
240
+ sys.exit(0 if exit_status not in (False, "") else 1)
241
+
242
+ return decorator
243
+
244
+
245
+ class busy_bar:
246
+ querying = None
247
+ __spinner = (
248
+ (),
249
+ ("-", "\\", "|", "/"),
250
+ (
251
+ "█■■■■",
252
+ "■█■■■",
253
+ "■■█■■",
254
+ "■■■█■",
255
+ "■■■■█",
256
+ ),
257
+ ("⣾ ", "⣽ ", "⣻ ", "⢿ ", "⡿ ", "⣟ ", "⣯ ", "⣷ "),
258
+ )
259
+ spin_index = 0
260
+ sleep_time = 0.1
261
+
262
+ @classmethod
263
+ def __action(
264
+ cls,
265
+ ):
266
+ while cls.querying:
267
+ for spin in cls.__spinner[cls.spin_index]:
268
+ print(" " + spin, end="\r", flush=True)
269
+ if not cls.querying:
270
+ break
271
+ time.sleep(cls.sleep_time)
272
+
273
+ @classmethod
274
+ def start_spinning(
275
+ cls,
276
+ ):
277
+ try:
278
+ cls.querying = True
279
+ t1 = thr(
280
+ target=cls.__action,
281
+ args=(),
282
+ )
283
+ t1.start()
284
+ except Exception as e:
285
+ cls.querying = False
286
+ logging.debug(this.getExc(e))
287
+ t1.join()
288
+
289
+ @classmethod
290
+ def stop_spinning(cls):
291
+ """Stop displaying busy-bar"""
292
+ if cls.querying:
293
+ cls.querying = False
294
+ time.sleep(cls.sleep_time)
295
+
296
+ @classmethod
297
+ def run(cls, help: str = "Exception", index: int = None, immediate: bool = False):
298
+ """Handle function exceptions safely why showing busy bar
299
+
300
+ Args:
301
+ help (str, optional): Message to be shown incase of an exception. Defaults to ''.
302
+ index (int, optional): Busy bars spin index. Defaults to `default`.
303
+ immediate (bool, optional): Start the spinning immediately. Defaults to False.
304
+ """
305
+ if isinstance(index, int):
306
+ cls.spin_index = index
307
+
308
+ def decorator(func):
309
+ @wraps(func) # Preserves function metadata
310
+ def main(*args, **kwargs):
311
+ try:
312
+ if immediate:
313
+ cls.start_spinning()
314
+ return func(*args, **kwargs)
315
+ except KeyboardInterrupt:
316
+ cls.stop_spinning()
317
+ return
318
+ except EOFError:
319
+ cls.querying = False
320
+ sys.exit(logging.info("Stopping program"))
321
+ except Exception as e:
322
+ logging.error(f"{help} - {this.getExc(e)}")
323
+ finally:
324
+ cls.stop_spinning()
325
+
326
+ return main
327
+
328
+ return decorator
329
+
330
+
331
+ class Main(cmd.Cmd):
332
+ intro = (
333
+ "Welcome to webai Chat in terminal. "
334
+ "Type 'help' or 'h' for usage info.\n"
335
+ )
336
+
337
+ def __init__(
338
+ self,
339
+ max_tokens,
340
+ temperature,
341
+ top_k,
342
+ top_p,
343
+ model,
344
+ auth,
345
+ timeout,
346
+ disable_conversation,
347
+ filepath,
348
+ update_file,
349
+ intro,
350
+ history_offset,
351
+ awesome_prompt,
352
+ proxy_path,
353
+ provider,
354
+ quiet=False,
355
+ chat_completion=False,
356
+ ignore_working=False,
357
+ rawdog=False,
358
+ internal_exec=False,
359
+ confirm_script=False,
360
+ interpreter="python",
361
+ *args,
362
+ **kwargs,
363
+ ):
364
+ super().__init__(*args, **kwargs)
365
+ if proxy_path:
366
+ with open(proxy_path) as fh:
367
+ proxies = json.load(fh)
368
+ else:
369
+ proxies = {}
370
+
371
+ try:
372
+ getOr = lambda option, default: option if option else default
373
+
374
+ if rawdog:
375
+
376
+ self.RawDog = RawDog(
377
+ quiet=quiet,
378
+ internal_exec=internal_exec,
379
+ confirm_script=confirm_script,
380
+ interpreter=interpreter,
381
+ prettify=True,
382
+ )
383
+ intro = self.RawDog.intro_prompt
384
+ getpass.getuser = lambda: "RawDog"
385
+
386
+ if provider == "g4fauto":
387
+ from webscout.g4f import TestProviders
388
+
389
+ test = TestProviders(quiet=quiet, timeout=timeout)
390
+ g4fauto = test.best if ignore_working else test.auto
391
+ if isinstance(g4fauto, str):
392
+ provider = "g4fauto+" + g4fauto
393
+ from webscout.g4f import GPT4FREE
394
+
395
+ self.bot = GPT4FREE(
396
+ provider=g4fauto,
397
+ auth=auth,
398
+ max_tokens=max_tokens,
399
+ model=model,
400
+ chat_completion=chat_completion,
401
+ ignore_working=ignore_working,
402
+ timeout=timeout,
403
+ intro=intro,
404
+ filepath=filepath,
405
+ update_file=update_file,
406
+ proxies=proxies,
407
+ history_offset=history_offset,
408
+ act=awesome_prompt,
409
+ )
410
+ else:
411
+ raise Exception(
412
+ "No working g4f provider found. "
413
+ "Consider running 'webscout gpt4free test -y' first"
414
+ )
415
+
416
+ elif provider == "leo":
417
+ from webscout.AI import LEO
418
+
419
+ self.bot = LEO(
420
+ is_conversation=disable_conversation,
421
+ max_tokens=max_tokens,
422
+ temperature=temperature,
423
+ top_k=top_k,
424
+ top_p=top_p,
425
+ model=getOr(model, "llama-2-13b-chat"),
426
+ brave_key=getOr(auth, "qztbjzBqJueQZLFkwTTJrieu8Vw3789u"),
427
+ timeout=timeout,
428
+ intro=intro,
429
+ filepath=filepath,
430
+ update_file=update_file,
431
+ proxies=proxies,
432
+ history_offset=history_offset,
433
+ act=awesome_prompt,
434
+ )
435
+
436
+ elif provider == "openai":
437
+ assert auth, (
438
+ "OpenAI's API-key is required. " "Use the flag `--key` or `-k`"
439
+ )
440
+ from webscout.AI import OPENAI
441
+
442
+ self.bot = OPENAI(
443
+ api_key=auth,
444
+ is_conversation=disable_conversation,
445
+ max_tokens=max_tokens,
446
+ temperature=temperature,
447
+ presence_penalty=top_p,
448
+ frequency_penalty=top_k,
449
+ top_p=top_p,
450
+ model=getOr(model, model),
451
+ timeout=timeout,
452
+ intro=intro,
453
+ filepath=filepath,
454
+ update_file=update_file,
455
+ proxies=proxies,
456
+ history_offset=history_offset,
457
+ act=awesome_prompt,
458
+ )
459
+
460
+ elif provider == "opengpt":
461
+ from webscout.AI import OPENGPT
462
+
463
+ self.bot = OPENGPT(
464
+ is_conversation=disable_conversation,
465
+ max_tokens=max_tokens,
466
+ timeout=timeout,
467
+ intro=intro,
468
+ filepath=filepath,
469
+ update_file=update_file,
470
+ proxies=proxies,
471
+ history_offset=history_offset,
472
+ act=awesome_prompt,
473
+ )
474
+ elif provider == "yepchat":
475
+ from webscout.AI import YEPCHAT
476
+
477
+ self.bot = YEPCHAT(
478
+ is_conversation=disable_conversation,
479
+ max_tokens=max_tokens,
480
+ temperature=temperature,
481
+ presence_penalty=top_p,
482
+ frequency_penalty=top_k,
483
+ top_p=top_p,
484
+ model=getOr(model, "Mixtral-8x7B-Instruct-v0.1"),
485
+ timeout=timeout,
486
+ intro=intro,
487
+ filepath=filepath,
488
+ update_file=update_file,
489
+ proxies=proxies,
490
+ history_offset=history_offset,
491
+ act=awesome_prompt,
492
+ )
493
+ elif provider == "groq":
494
+ assert auth, (
495
+ "GROQ's API-key is required. " "Use the flag `--key` or `-k`"
496
+ )
497
+ from webscout.AI import GROQ
498
+
499
+
500
+ self.bot = GROQ(
501
+ api_key=auth,
502
+ is_conversation=disable_conversation,
503
+ max_tokens=max_tokens,
504
+ temperature=temperature,
505
+ presence_penalty=top_p,
506
+ frequency_penalty=top_k,
507
+ top_p=top_p,
508
+ model=getOr(model, "mixtral-8x7b-32768"),
509
+ timeout=timeout,
510
+ intro=intro,
511
+ filepath=filepath,
512
+ update_file=update_file,
513
+ proxies=proxies,
514
+ history_offset=history_offset,
515
+ act=awesome_prompt,
516
+ )
517
+ elif provider == "sean":
518
+ from webscout.AI import Sean
519
+
520
+ self.bot = Sean(
521
+ is_conversation=disable_conversation,
522
+ max_tokens=max_tokens,
523
+ timeout=timeout,
524
+ intro=intro,
525
+ filepath=filepath,
526
+ update_file=update_file,
527
+ proxies=proxies,
528
+ history_offset=history_offset,
529
+ act=awesome_prompt,
530
+ )
531
+ elif provider == "cohere":
532
+ assert auth, (
533
+ "Cohere's API-key is required. Use the flag `--key` or `-k`"
534
+ )
535
+ from webscout.AI import Cohere
536
+ self.bot = Cohere(
537
+ api_key=auth,
538
+ is_conversation=disable_conversation,
539
+ max_tokens=max_tokens,
540
+ temperature=temperature,
541
+ top_k=top_k,
542
+ top_p=top_p,
543
+ model=getOr(model, "command-r-plus"),
544
+ timeout=timeout,
545
+ intro=intro,
546
+ filepath=filepath,
547
+ update_file=update_file,
548
+ proxies=proxies,
549
+ history_offset=history_offset,
550
+ act=awesome_prompt,
551
+ )
552
+ elif provider == "reka":
553
+ from webscout.AI import REKA
554
+
555
+ self.bot = REKA(
556
+ api_key=auth,
557
+ is_conversation=disable_conversation,
558
+ max_tokens=max_tokens,
559
+ timeout=timeout,
560
+ intro=intro,
561
+ filepath=filepath,
562
+ update_file=update_file,
563
+ proxies=proxies,
564
+ history_offset=history_offset,
565
+ act=awesome_prompt,
566
+ model=getOr(model, "reka-core"),
567
+ # quiet=quiet,
568
+ )
569
+
570
+ elif provider == "koboldai":
571
+ from webscout.AI import KOBOLDAI
572
+
573
+ self.bot = KOBOLDAI(
574
+ is_conversation=disable_conversation,
575
+ max_tokens=max_tokens,
576
+ temperature=temperature,
577
+ top_p=top_p,
578
+ timeout=timeout,
579
+ intro=intro,
580
+ filepath=filepath,
581
+ update_file=update_file,
582
+ proxies=proxies,
583
+ history_offset=history_offset,
584
+ act=awesome_prompt,
585
+ )
586
+
587
+ elif provider == "gemini":
588
+ from webscout.AI import GEMINI
589
+
590
+ assert auth, (
591
+ "Path to gemini.google.com.cookies.json file is required. "
592
+ "Use the flag `--key` or `-k`"
593
+ )
594
+ self.bot = GEMINI(
595
+ cookie_file=auth,
596
+ proxy=proxies,
597
+ timeout=timeout,
598
+ )
599
+
600
+ elif provider == "phind":
601
+ from webscout.AI import PhindSearch
602
+
603
+ self.bot = PhindSearch(
604
+ is_conversation=disable_conversation,
605
+ max_tokens=max_tokens,
606
+ timeout=timeout,
607
+ intro=intro,
608
+ filepath=filepath,
609
+ update_file=update_file,
610
+ proxies=proxies,
611
+ history_offset=history_offset,
612
+ act=awesome_prompt,
613
+ model=getOr(model, "Phind Model"),
614
+ quiet=quiet,
615
+ )
616
+
617
+ elif provider == "blackboxai":
618
+
619
+ from webscout.AI import BLACKBOXAI
620
+
621
+ self.bot = BLACKBOXAI(
622
+ is_conversation=disable_conversation,
623
+ max_tokens=max_tokens,
624
+ timeout=timeout,
625
+ intro=intro,
626
+ filepath=filepath,
627
+ update_file=update_file,
628
+ proxies=proxies,
629
+ history_offset=history_offset,
630
+ act=awesome_prompt,
631
+ )
632
+
633
+
634
+ elif provider in webscout.gpt4free_providers:
635
+ from webscout.g4f import GPT4FREE
636
+
637
+ self.bot = GPT4FREE(
638
+ provider=provider,
639
+ is_conversation=disable_conversation,
640
+ auth=auth,
641
+ max_tokens=max_tokens,
642
+ model=model,
643
+ chat_completion=chat_completion,
644
+ ignore_working=ignore_working,
645
+ timeout=timeout,
646
+ intro=intro,
647
+ filepath=filepath,
648
+ update_file=update_file,
649
+ proxies=proxies,
650
+ history_offset=history_offset,
651
+ act=awesome_prompt,
652
+ )
653
+
654
+
655
+ elif provider == "perplexity":
656
+ from webscout.AI import PERPLEXITY
657
+
658
+ self.bot = PERPLEXITY(
659
+ is_conversation=disable_conversation,
660
+ max_tokens=max_tokens,
661
+ timeout=timeout,
662
+ intro=intro,
663
+ filepath=filepath,
664
+ update_file=update_file,
665
+ proxies=proxies,
666
+ history_offset=history_offset,
667
+ act=awesome_prompt,
668
+ quiet=quiet,
669
+ )
670
+
671
+ else:
672
+ raise NotImplementedError(
673
+ f"The provider `{provider}` is not yet implemented."
674
+ )
675
+
676
+ except Exception as e:
677
+ logging.error(this.getExc(e))
678
+ click.secho("Quitting", fg="red")
679
+ sys.exit(1)
680
+ self.prettify = True
681
+ self.color = "cyan"
682
+ self.code_theme = "monokai"
683
+ self.quiet = quiet
684
+ self.vertical_overflow = "ellipsis"
685
+ self.disable_stream = False
686
+ self.provider = provider
687
+ self.disable_coloring = False
688
+ self.internal_exec = internal_exec
689
+ self.confirm_script = confirm_script
690
+ self.interpreter = interpreter
691
+ self.rawdog = rawdog
692
+ self.read_aloud = False
693
+ self.read_aloud_voice = "Brian"
694
+ self.path_to_last_response_audio = None
695
+ self.__init_time = time.time()
696
+ self.__start_time = time.time()
697
+ self.__end_time = time.time()
698
+
699
+ @property
700
+ def prompt(self):
701
+ current_time = datetime.datetime.now().strftime("%H:%M:%S")
702
+
703
+ def find_range(start, end, hms: bool = False):
704
+ in_seconds = round(end - start, 1)
705
+ return (
706
+ str(datetime.timedelta(seconds=in_seconds)).split(".")[0].zfill(8)
707
+ if hms
708
+ else in_seconds
709
+ )
710
+ if not self.disable_coloring:
711
+ cmd_prompt = (
712
+ f"╭─[`{Fore.GREEN}{getpass.getuser().capitalize()}@webai]`"
713
+ f"(`{Fore.YELLOW}{self.provider})`"
714
+ f"~[`{Fore.LIGHTWHITE_EX}⏰{Fore.MAGENTA}{current_time}-`"
715
+ f"{Fore.LIGHTWHITE_EX}💻{Fore.BLUE}{find_range(self.__init_time, time.time(), True)}-`"
716
+ f"{Fore.LIGHTWHITE_EX}⚡️{Fore.RED}{find_range(self.__start_time, self.__end_time)}s]`"
717
+ f"\n╰─>"
718
+ )
719
+ whitelist = ["[", "]", "~", "-", "(", ")"]
720
+ for character in whitelist:
721
+ cmd_prompt = cmd_prompt.replace(character + "`", Fore.RESET + character)
722
+ return cmd_prompt
723
+
724
+ else:
725
+ return (
726
+ f"╭─[{getpass.getuser().capitalize()}@webscout]({self.provider})"
727
+ f"~[⏰{current_time}"
728
+ f"-💻{find_range(self.__init_time, time.time(), True)}"
729
+ f"-⚡️{find_range(self.__start_time, self.__end_time)}s]"
730
+ f"~[⏰{current_time}"
731
+ f"-💻{find_range(self.__init_time, time.time(), True)}"
732
+ f"-⚡️{find_range(self.__start_time, self.__end_time)}s]"
733
+ "\n╰─>"
734
+ )
735
+
736
+ def output_bond(
737
+ self,
738
+ title: str,
739
+ text: str,
740
+ color: str = "cyan",
741
+ frame: bool = True,
742
+ is_json: bool = False,
743
+ ):
744
+ """Print prettified output
745
+
746
+ Args:
747
+ title (str): Title
748
+ text (str): Info to be printed
749
+ color (str, optional): Output color. Defaults to "cyan".
750
+ frame (bool, optional): Add frame. Defaults to True.
751
+ """
752
+ if is_json:
753
+ text = f"""
754
+ ```json
755
+ {json.dumps(text,indent=4)}
756
+ ```
757
+ """
758
+ rich.print(
759
+ Panel(
760
+ Markdown(text, code_theme=self.code_theme),
761
+ title=title.title(),
762
+ style=Style(
763
+ color=color,
764
+ frame=frame,
765
+ ),
766
+ ),
767
+ )
768
+ if is_json and click.confirm("Do you wish to save this"):
769
+ default_path = title + ".json"
770
+ save_to = click.prompt(
771
+ "Enter path to save to", default=default_path, type=click.STRING
772
+ )
773
+ with open(save_to, "a") as fh:
774
+ json.dump(text, fh, indent=4)
775
+ click.secho(f"Successfuly saved to `{save_to}`", fg="green")
776
+
777
+ def do_h(self, line):
778
+ """Show help info in tabular form"""
779
+ table = Table(
780
+ title="Help info",
781
+ show_lines=True,
782
+ )
783
+ table.add_column("No.", style="white", justify="center")
784
+ table.add_column("Command", style="yellow", justify="left")
785
+ table.add_column("Function", style="cyan")
786
+ command_methods = [
787
+ getattr(self, method)
788
+ for method in dir(self)
789
+ if callable(getattr(self, method)) and method.startswith("do_")
790
+ ]
791
+ command_methods.append(self.default)
792
+ command_methods.reverse()
793
+ for no, method in enumerate(command_methods):
794
+ table.add_row(
795
+ str(no + 1),
796
+ method.__name__[3:] if not method == self.default else method.__name__,
797
+ method.__doc__,
798
+ )
799
+ Console().print(table)
800
+
801
+ @busy_bar.run("Settings saved")
802
+ def do_settings(self, line):
803
+ """Configure settings"""
804
+ self.prettify = click.confirm(
805
+ "\nPrettify markdown response", default=self.prettify
806
+ )
807
+ busy_bar.spin_index = click.prompt(
808
+ "Spin bar index [0: None, 1:/, 2:■█■■■, 3:⣻]",
809
+ default=busy_bar.spin_index,
810
+ type=click.IntRange(0, 3),
811
+ )
812
+ self.color = click.prompt(
813
+ "Response stdout font color", default=self.color or "white"
814
+ )
815
+ self.code_theme = Prompt.ask(
816
+ "Enter code_theme", choices=this.rich_code_themes, default=self.code_theme
817
+ )
818
+ self.vertical_overflow = Prompt.ask(
819
+ "\nVertical overflow behaviour",
820
+ choices=["ellipsis", "visible", "crop"],
821
+ default=self.vertical_overflow,
822
+ )
823
+ self.bot.max_tokens_to_sample = click.prompt(
824
+ "\nMaximum tokens to sample",
825
+ type=click.INT,
826
+ default=self.bot.max_tokens_to_sample,
827
+ )
828
+ self.bot.temperature = click.prompt(
829
+ "Temperature", type=click.FLOAT, default=self.bot.temperature
830
+ )
831
+ self.bot.top_k = click.prompt(
832
+ "Chance of topic being repeated, top_k",
833
+ type=click.FLOAT,
834
+ default=self.bot.top_k,
835
+ )
836
+ self.bot.top_p = click.prompt(
837
+ "Sampling threshold during inference time, top_p",
838
+ type=click.FLOAT,
839
+ default=self.bot.top_p,
840
+ )
841
+ self.bot.model = click.prompt(
842
+ "Model name", type=click.STRING, default=self.bot.model
843
+ )
844
+
845
+ @busy_bar.run(help="System error")
846
+ def do_copy_this(self, line):
847
+ """Copy last response
848
+ Usage:
849
+ copy_this:
850
+ text-copied = {whole last-response}
851
+ copy_this code:
852
+ text-copied = {All codes in last response}
853
+ """
854
+ if self.bot.last_response:
855
+ global last_response
856
+ last_response = self.bot.get_message(self.bot.last_response)
857
+ if not "code" in line:
858
+ clipman.set(last_response)
859
+ click.secho("Last response copied successfully!", fg="cyan")
860
+ return
861
+
862
+ # Copies just code
863
+ sanitized_codes = []
864
+ code_blocks = re.findall(r"```.*?```", last_response, re.DOTALL)
865
+ for code_block in code_blocks:
866
+ new_code_block = re.sub(
867
+ "^```.*$", "", code_block.strip(), flags=re.MULTILINE
868
+ )
869
+ if bool(new_code_block.strip()):
870
+ sanitized_codes.append(new_code_block)
871
+ if sanitized_codes:
872
+ if len(sanitized_codes) > 1:
873
+ if not click.confirm("Do you wish to copy all codes"):
874
+ for index, code in enumerate(sanitized_codes):
875
+ rich.print(
876
+ Panel(
877
+ Markdown(
878
+ code_blocks[index], code_theme=self.code_theme
879
+ ),
880
+ title=f"Index : {index}",
881
+ title_align="left",
882
+ )
883
+ )
884
+
885
+ clipman.set(
886
+ sanitized_codes[
887
+ click.prompt(
888
+ "Enter code index",
889
+ type=click.IntRange(0, len(sanitized_codes) - 1),
890
+ )
891
+ ]
892
+ )
893
+ click.secho("Code copied successfully", fg="cyan")
894
+ else:
895
+ clipman.set("\n\n".join(sanitized_codes))
896
+ click.secho(
897
+ f"All {len(sanitized_codes)} codes copied successfully!",
898
+ fg="cyan",
899
+ )
900
+ else:
901
+ clipman.set(sanitized_codes[0])
902
+ click.secho("Code copied successfully!", fg="cyan")
903
+ else:
904
+ click.secho("No code found in the last response!", fg="red")
905
+ else:
906
+ click.secho("Chat with AI first.", fg="yellow")
907
+
908
+ @busy_bar.run()
909
+ def do_with_copied(self, line):
910
+ """Attach last copied text to the prompt
911
+ Usage:
912
+ from_copied:
913
+ prompt = {text-copied}
914
+ from_copied Debug this code:
915
+ prompt = Debug this code {newline} {text-copied}
916
+ """
917
+ issued_prompt = (
918
+ f"{line}\n{clipman.get()}" if bool(line.strip()) else clipman.get()
919
+ )
920
+ click.secho(issued_prompt, fg="yellow")
921
+ if click.confirm("Do you wish to proceed"):
922
+ self.default(issued_prompt)
923
+
924
+ @busy_bar.run()
925
+ def do_code(self, line):
926
+ """Enhance prompt for code generation
927
+ usage :
928
+ code <Code description>
929
+ """
930
+ self.default(Optimizers.code(line))
931
+
932
+ @busy_bar.run()
933
+ def do_shell(self, line):
934
+ """Enhance prompt for system command (shell) generation
935
+ Usage:
936
+ shell <Action to be accomplished>
937
+ """
938
+ self.default(Optimizers.shell_command(line))
939
+ if click.confirm("Do you wish to run the command(s) generated in your system"):
940
+ self.do_sys(self.bot.get_message(self.bot.last_response))
941
+
942
+ @busy_bar.run("While changing directory")
943
+ def do_cd(self, line):
944
+ """Change directory
945
+ Usage :
946
+ cd <path-to-directory>
947
+ """
948
+ assert line, "File path is required"
949
+ os.chdir(line)
950
+
951
+ def do_clear(self, line):
952
+ """Clear console"""
953
+ sys.stdout.write("\u001b[2J\u001b[H")
954
+ sys.stdout.flush()
955
+
956
+ @busy_bar.run("While handling history")
957
+ def do_history(self, line):
958
+ """Show current conversation history"""
959
+ history = self.bot.conversation.chat_history
960
+ formatted_history = re.sub(
961
+ "\nLLM :",
962
+ "\n\n**LLM** :",
963
+ re.sub("\nUser :", "\n\n**User** :", history),
964
+ )
965
+ self.output_bond("Chat History", formatted_history, self.color)
966
+ if click.confirm("Do you wish to save this chat"):
967
+ save_to = click.prompt(
968
+ "Enter path/file-name", default="llama-conversation.txt"
969
+ )
970
+ with open(save_to, "a") as fh:
971
+ fh.write(history)
972
+ click.secho(f"Conversation saved successfully to '{save_to}'", fg="cyan")
973
+
974
+ @busy_bar.run("while resetting conversation")
975
+ def do_reset(self, line):
976
+ """Start new conversation thread"""
977
+ self.bot.conversation.chat_history = click.prompt(
978
+ "Introductory prompt", default=self.bot.conversation.intro
979
+ )
980
+ if hasattr(self.bot, "reset"):
981
+ self.bot.reset()
982
+ click.secho("Conversation reset successfully. New one created.", fg="cyan")
983
+
984
+ @busy_bar.run("while loading conversation")
985
+ def do_load(self, line):
986
+ """Load conversation history from file"""
987
+ history_file = click.prompt("Enter path to history path", default=line)
988
+ if not os.path.isfile(history_file):
989
+ click.secho(f"Path `{history_file}` does not exist!", fg="red")
990
+ return
991
+ with open(history_file) as fh:
992
+ self.bot.conversation.chat_history = fh.read()
993
+ click.secho("Conversation loaded successfully.", fg="cyan")
994
+
995
+ def do_last_response(self, line):
996
+ """Show whole last response in json format"""
997
+ self.output_bond(
998
+ "Last Response",
999
+ self.bot.last_response,
1000
+ is_json=True,
1001
+ )
1002
+ @busy_bar.run(help="While rereading aloud", index=3, immediate=True)
1003
+ def do_reread(self, line):
1004
+ """Reread aloud last ai response"""
1005
+ if not self.path_to_last_response_audio:
1006
+ raise Exception("Path to last response audio is null")
1007
+ Audio.play(self.path_to_last_response_audio)
1008
+
1009
+ @busy_bar.run()
1010
+ def do_exec(self, line):
1011
+ """Exec python code in last response with RawDog"""
1012
+ last_response = self.bot.get_message(self.bot.last_response)
1013
+ assert last_response, "Last response is null"
1014
+ assert "```python" in last_response, "Last response has no python code"
1015
+ if self.rawdog:
1016
+ self.RawDog.main(last_response)
1017
+ else:
1018
+ rawdog = RawDog(
1019
+ quiet=self.quiet,
1020
+ internal_exec=self.internal_exec,
1021
+ confirm_script=self.confirm_script,
1022
+ interpreter=self.interpreter,
1023
+ prettify=self.prettify,
1024
+ )
1025
+ rawdog.main(last_response)
1026
+
1027
+ @busy_bar.run()
1028
+ def do_rawdog(self, line):
1029
+ """Repeat executing last rawdog's python code"""
1030
+ assert self.rawdog, "Session not in rawdog mode. Restart with --rawdog"
1031
+ self.default(self.bot.get_message(self.bot.last_response))
1032
+
1033
+ @busy_bar.run()
1034
+ def default(self, line, exit_on_error: bool = False, normal_stdout: bool = False):
1035
+ """Chat with LLM"""
1036
+ if not bool(line):
1037
+ return
1038
+ if line.startswith("./"):
1039
+ os.system(line[2:])
1040
+
1041
+ elif self.rawdog:
1042
+ self.__start_time = time.time()
1043
+ busy_bar.start_spinning()
1044
+ ai_response = self.bot.chat(line, stream=False)
1045
+ busy_bar.stop_spinning()
1046
+ is_feedback = self.RawDog.main(ai_response)
1047
+ if is_feedback:
1048
+ return self.default(is_feedback)
1049
+ self.__end_time = time.time()
1050
+
1051
+ else:
1052
+ self.__start_time = time.time()
1053
+ try:
1054
+
1055
+ def generate_response():
1056
+ # Ensure response is yielded
1057
+ def for_stream():
1058
+ return self.bot.chat(line, stream=True)
1059
+
1060
+ def for_non_stream():
1061
+ yield self.bot.chat(line, stream=False)
1062
+
1063
+ return for_non_stream() if self.disable_stream else for_stream()
1064
+
1065
+ busy_bar.start_spinning()
1066
+ generated_response = generate_response()
1067
+
1068
+ if normal_stdout or not self.prettify and not self.disable_stream:
1069
+ cached_response: str = ""
1070
+ if not normal_stdout:
1071
+ busy_bar.stop_spinning()
1072
+ for response in generated_response:
1073
+ offset = len(cached_response)
1074
+ print(response[offset:], end="")
1075
+ cached_response = response
1076
+ if not normal_stdout:
1077
+ print("")
1078
+ return
1079
+
1080
+ if self.quiet:
1081
+ busy_bar.stop_spinning()
1082
+ console_ = Console()
1083
+ with Live(
1084
+ console=console_,
1085
+ refresh_per_second=16,
1086
+ vertical_overflow=self.vertical_overflow,
1087
+ ) as live:
1088
+ for response in generated_response:
1089
+ live.update(
1090
+ Markdown(response, code_theme=self.code_theme)
1091
+ if self.prettify
1092
+ else response
1093
+ )
1094
+ else:
1095
+ busy_bar.stop_spinning()
1096
+ this.stream_output(
1097
+ generated_response,
1098
+ title="AI Response",
1099
+ is_markdown=self.prettify,
1100
+ style=Style(
1101
+ color=self.color,
1102
+ ),
1103
+ code_theme=self.code_theme,
1104
+ vertical_overflow=self.vertical_overflow,
1105
+ )
1106
+ except (KeyboardInterrupt, EOFError):
1107
+ busy_bar.stop_spinning()
1108
+ print("")
1109
+ return False # Exit cmd
1110
+
1111
+ except Exception as e:
1112
+ # logging.exception(e)
1113
+ busy_bar.stop_spinning()
1114
+ logging.error(this.getExc(e))
1115
+ if exit_on_error:
1116
+ sys.exit(1)
1117
+
1118
+ else:
1119
+ self.post_default()
1120
+
1121
+ finally:
1122
+ self.__end_time = time.time()
1123
+ @busy_bar.run(help="While reading aloud", immediate=True, index=3)
1124
+ def post_default(self):
1125
+ """Actions to be taken after upon successfull complete response generation triggered by `default` function"""
1126
+ last_text: str = self.bot.get_message(self.bot.last_response)
1127
+ if self.read_aloud and last_text is not None:
1128
+ # Talk back to user
1129
+ self.path_to_last_response_audio = Audio.text_to_audio(
1130
+ last_text, voice=self.read_aloud_voice, auto=True
1131
+ )
1132
+ Audio.play(self.path_to_last_response_audio)
1133
+ def do_sys(self, line):
1134
+ """Execute system commands
1135
+ shortcut [./<command>]
1136
+ Usage:
1137
+ sys <System command>
1138
+ or
1139
+ ./<System command>
1140
+ """
1141
+ os.system(line)
1142
+
1143
+ def do_exit(self, line):
1144
+ """Quit this program"""
1145
+ if click.confirm("Are you sure to exit"):
1146
+ click.secho("Okay Goodbye!", fg="yellow")
1147
+ return True
1148
+
1149
+
1150
+ class EntryGroup:
1151
+ """Entry commands"""
1152
+
1153
+ # @staticmethod
1154
+ @click.group()
1155
+ @click.version_option(
1156
+ webscout.__version__, "-v", "--version", package_name="webscout"
1157
+ )
1158
+ @click.help_option("-h", "--help")
1159
+ def webai_():
1160
+ pass
1161
+
1162
+ @staticmethod
1163
+ @webai_.group()
1164
+ @click.help_option("-h", "--help")
1165
+ def utils():
1166
+ """Utility endpoint for webscout"""
1167
+ pass
1168
+
1169
+ @staticmethod
1170
+ @webai_.group()
1171
+ @click.help_option("-h", "--help")
1172
+ def gpt4free():
1173
+ """Discover gpt4free models, providers etc"""
1174
+ pass
1175
+
1176
+ @staticmethod
1177
+ @webai_.group()
1178
+ @click.help_option("-h", "--help")
1179
+ def awesome():
1180
+ """Perform CRUD operations on awesome-prompts"""
1181
+ pass
1182
+
1183
+
1184
+ import webscout
1185
+ class Chatwebai:
1186
+ """webai command"""
1187
+
1188
+ @staticmethod
1189
+ @click.command(context_settings=this.context_settings)
1190
+ @click.option(
1191
+ "-m",
1192
+ "--model",
1193
+ help="Model name for text-generation", # default="llama-2-13b-chat"
1194
+ )
1195
+ @click.option(
1196
+ "-t",
1197
+ "--temperature",
1198
+ help="Charge of the generated text's randomness",
1199
+ type=click.FloatRange(0, 1),
1200
+ default=0.2,
1201
+ )
1202
+ @click.option(
1203
+ "-mt",
1204
+ "--max-tokens",
1205
+ help="Maximum number of tokens to be generated upon completion",
1206
+ type=click.INT,
1207
+ default=600,
1208
+ )
1209
+ @click.option(
1210
+ "-tp",
1211
+ "--top-p",
1212
+ help="Sampling threshold during inference time",
1213
+ type=click.FLOAT,
1214
+ default=0.999,
1215
+ )
1216
+ @click.option(
1217
+ "-tk",
1218
+ "--top-k",
1219
+ help="Chance of topic being repeated",
1220
+ type=click.FLOAT,
1221
+ default=0,
1222
+ )
1223
+ @click.option(
1224
+ "-k",
1225
+ "--key",
1226
+ help="LLM API access key or auth value or path to LLM with provider.",
1227
+ )
1228
+ @click.option(
1229
+ "-ct",
1230
+ "--code-theme",
1231
+ help="Theme for displaying codes in response",
1232
+ type=click.Choice(this.rich_code_themes),
1233
+ default="monokai",
1234
+ )
1235
+ @click.option(
1236
+ "-bi",
1237
+ "--busy-bar-index",
1238
+ help="Index of busy bar icon : [0: None, 1:/, 2:■█■■■, 3:⣻]",
1239
+ type=click.IntRange(0, 3),
1240
+ default=3,
1241
+ )
1242
+ @click.option("-fc", "--font-color", help="Stdout font color")
1243
+ @click.option(
1244
+ "-to", "--timeout", help="Http requesting timeout", type=click.INT, default=30
1245
+ )
1246
+ @click.argument("prompt", required=False)
1247
+ @click.option(
1248
+ "--prettify/--raw",
1249
+ help="Flag for prettifying markdowned response",
1250
+ default=True,
1251
+ )
1252
+ @click.option(
1253
+ "-dc",
1254
+ "--disable-conversation",
1255
+ is_flag=True,
1256
+ default=True, # is_conversation = True
1257
+ help="Disable chatting conversationally (Stable)",
1258
+ )
1259
+ @click.option(
1260
+ "-fp",
1261
+ "--filepath",
1262
+ type=click.Path(),
1263
+ default=os.path.join(default_path, "chat-history.txt"),
1264
+ help="Path to chat history - new will be created incase doesn't exist",
1265
+ )
1266
+ @click.option(
1267
+ "--update-file/--retain-file",
1268
+ help="Controls updating chat history in file",
1269
+ default=True,
1270
+ )
1271
+ @click.option(
1272
+ "-i",
1273
+ "--intro",
1274
+ help="Conversation introductory prompt",
1275
+ )
1276
+ @click.option(
1277
+ "-ho",
1278
+ "--history-offset",
1279
+ help="Limit conversation history to this number of last texts",
1280
+ type=click.IntRange(100, 16000),
1281
+ default=10250,
1282
+ )
1283
+ @click.option(
1284
+ "-ap",
1285
+ "--awesome-prompt",
1286
+ default="0",
1287
+ callback=lambda ctx, param, value: (
1288
+ int(value) if str(value).isdigit() else value
1289
+ ),
1290
+ help="Awesome prompt key or index. Alt. to intro",
1291
+ )
1292
+ @click.option(
1293
+ "-pp",
1294
+ "--proxy-path",
1295
+ type=click.Path(exists=True),
1296
+ help="Path to .json file containing proxies",
1297
+ )
1298
+ @click.option(
1299
+ "-p",
1300
+ "--provider",
1301
+ type=click.Choice(available_providers),
1302
+ default=this.default_provider,
1303
+ help="Name of LLM provider.",
1304
+ metavar=(
1305
+ f"[{'|'.join(webscout.webai)}] etc, "
1306
+ "run 'webscout gpt4free list providers -w' to "
1307
+ "view more providers and 'webscout gpt4free test -y' "
1308
+ "for advanced g4f providers test"
1309
+ ),
1310
+ )
1311
+ @click.option(
1312
+ "-vo",
1313
+ "--vertical-overflow",
1314
+ help="Vertical overflow behaviour on content display",
1315
+ type=click.Choice(["visible", "crop", "ellipsis"]),
1316
+ default="ellipsis",
1317
+ )
1318
+ @click.option(
1319
+ "-w",
1320
+ "--whole",
1321
+ is_flag=True,
1322
+ default=False,
1323
+ help="Disable streaming response",
1324
+ )
1325
+ @click.option(
1326
+ "-q",
1327
+ "--quiet",
1328
+ is_flag=True,
1329
+ help="Flag for controlling response-framing and response verbosity",
1330
+ default=False,
1331
+ )
1332
+ @click.option(
1333
+ "-n",
1334
+ "--new",
1335
+ help="Overwrite the filepath contents",
1336
+ is_flag=True,
1337
+ )
1338
+ @click.option(
1339
+ "-wc",
1340
+ "--with-copied",
1341
+ is_flag=True,
1342
+ help="Postfix prompt with last copied text",
1343
+ )
1344
+ @click.option(
1345
+ "-nc", "--no-coloring", is_flag=True, help="Disable intro prompt font-coloring"
1346
+ )
1347
+ @click.option(
1348
+ "-cc",
1349
+ "--chat-completion",
1350
+ is_flag=True,
1351
+ help="Provide native context for gpt4free providers",
1352
+ )
1353
+ @click.option(
1354
+ "-iw",
1355
+ "--ignore-working",
1356
+ is_flag=True,
1357
+ help="Ignore working status of the provider",
1358
+ )
1359
+ @click.option(
1360
+ "-rd",
1361
+ "--rawdog",
1362
+ is_flag=True,
1363
+ help="Generate and auto-execute Python scripts - (experimental)",
1364
+ )
1365
+ @click.option(
1366
+ "-ix",
1367
+ "--internal-exec",
1368
+ is_flag=True,
1369
+ help="RawDog : Execute scripts with exec function instead of out-of-script interpreter",
1370
+ )
1371
+ @click.option(
1372
+ "-cs",
1373
+ "--confirm-script",
1374
+ is_flag=True,
1375
+ help="RawDog : Give consent to generated scripts prior to execution",
1376
+ )
1377
+ @click.option(
1378
+ "-int",
1379
+ "--interpreter",
1380
+ default="python",
1381
+ help="RawDog : Python's interpreter name",
1382
+ )
1383
+ @click.option(
1384
+ "-ttm",
1385
+ "--talk-to-me",
1386
+ is_flag=True,
1387
+ help="Audiolize responses upon complete generation",
1388
+ )
1389
+ @click.option(
1390
+ "-ttmv",
1391
+ "--talk-to-me-voice",
1392
+ help="The voice to use for speech synthesis",
1393
+ type=click.Choice(Audio.all_voices),
1394
+ metavar="|".join(Audio.all_voices[:8]),
1395
+ default="Brian",
1396
+ )
1397
+ @click.help_option("-h", "--help")
1398
+ def webai(
1399
+ model,
1400
+ temperature,
1401
+ max_tokens,
1402
+ top_p,
1403
+ top_k,
1404
+ key,
1405
+ code_theme,
1406
+ busy_bar_index,
1407
+ font_color,
1408
+ timeout,
1409
+ prompt,
1410
+ prettify,
1411
+ disable_conversation,
1412
+ filepath,
1413
+ update_file,
1414
+ intro,
1415
+ history_offset,
1416
+ awesome_prompt,
1417
+ proxy_path,
1418
+ provider,
1419
+ vertical_overflow,
1420
+ whole,
1421
+ quiet,
1422
+ new,
1423
+ with_copied,
1424
+ no_coloring,
1425
+ chat_completion,
1426
+ ignore_working,
1427
+ rawdog,
1428
+ internal_exec,
1429
+ confirm_script,
1430
+ interpreter,
1431
+ talk_to_me,
1432
+ talk_to_me_voice,
1433
+ ):
1434
+ """Chat with AI webaily (Default)"""
1435
+ this.clear_history_file(filepath, new)
1436
+ bot = Main(
1437
+ max_tokens,
1438
+ temperature,
1439
+ top_k,
1440
+ top_p,
1441
+ model,
1442
+ key,
1443
+ timeout,
1444
+ disable_conversation,
1445
+ filepath,
1446
+ update_file,
1447
+ intro,
1448
+ history_offset,
1449
+ awesome_prompt,
1450
+ proxy_path,
1451
+ provider,
1452
+ quiet,
1453
+ chat_completion,
1454
+ ignore_working,
1455
+ rawdog=rawdog,
1456
+ internal_exec=internal_exec,
1457
+ confirm_script=confirm_script,
1458
+ interpreter=interpreter,
1459
+ )
1460
+ busy_bar.spin_index = busy_bar_index
1461
+ bot.code_theme = code_theme
1462
+ bot.color = font_color
1463
+ bot.disable_coloring = no_coloring
1464
+ bot.prettify = prettify
1465
+ bot.vertical_overflow = vertical_overflow
1466
+ bot.disable_stream = whole
1467
+ bot.read_aloud = talk_to_me
1468
+ bot.read_aloud_voice = talk_to_me_voice
1469
+ if prompt:
1470
+ if with_copied:
1471
+ prompt = prompt + "\n" + clipman.get()
1472
+ bot.default(prompt)
1473
+ bot.cmdloop()
1474
+
1475
+
1476
+ class ChatGenerate:
1477
+ """Generate command"""
1478
+
1479
+ @staticmethod
1480
+ @click.command(context_settings=this.context_settings)
1481
+ @click.option(
1482
+ "-m",
1483
+ "--model",
1484
+ help="Model name for text-generation",
1485
+ )
1486
+ @click.option(
1487
+ "-t",
1488
+ "--temperature",
1489
+ help="Charge of the generated text's randomness",
1490
+ type=click.FloatRange(0, 1),
1491
+ default=0.2,
1492
+ )
1493
+ @click.option(
1494
+ "-mt",
1495
+ "--max-tokens",
1496
+ help="Maximum number of tokens to be generated upon completion",
1497
+ type=click.INT,
1498
+ default=600,
1499
+ )
1500
+ @click.option(
1501
+ "-tp",
1502
+ "--top-p",
1503
+ help="Sampling threshold during inference time",
1504
+ type=click.FLOAT,
1505
+ default=0.999,
1506
+ )
1507
+ @click.option(
1508
+ "-tk",
1509
+ "--top-k",
1510
+ help="Chance of topic being repeated",
1511
+ type=click.FLOAT,
1512
+ default=0,
1513
+ )
1514
+ @click.option(
1515
+ "-k",
1516
+ "--key",
1517
+ help="LLM API access key or auth value or path to LLM with provider.",
1518
+ )
1519
+ @click.option(
1520
+ "-ct",
1521
+ "--code-theme",
1522
+ help="Theme for displaying codes in response",
1523
+ type=click.Choice(this.rich_code_themes),
1524
+ default="monokai",
1525
+ )
1526
+ @click.option(
1527
+ "-bi",
1528
+ "--busy-bar-index",
1529
+ help="Index of busy bar icon : [0: None, 1:/, 2:■█■■■, 3:⣻]",
1530
+ type=click.IntRange(0, 3),
1531
+ default=3,
1532
+ )
1533
+ @click.option(
1534
+ "-fc",
1535
+ "--font-color",
1536
+ help="Stdout font color",
1537
+ )
1538
+ @click.option(
1539
+ "-to", "--timeout", help="Http requesting timeout", type=click.INT, default=30
1540
+ )
1541
+ @click.argument("prompt", required=False)
1542
+ @click.option(
1543
+ "--prettify/--raw",
1544
+ help="Flag for prettifying markdowned response",
1545
+ default=True,
1546
+ )
1547
+ @click.option(
1548
+ "-w",
1549
+ "--whole",
1550
+ is_flag=True,
1551
+ default=False,
1552
+ help="Disable streaming response",
1553
+ )
1554
+ @click.option(
1555
+ "-c",
1556
+ "--code",
1557
+ is_flag=True,
1558
+ default=False,
1559
+ help="Optimize prompt for code generation",
1560
+ )
1561
+ @click.option(
1562
+ "-s",
1563
+ "--shell",
1564
+ is_flag=True,
1565
+ default=False,
1566
+ help="Optimize prompt for shell command generation",
1567
+ )
1568
+ @click.option(
1569
+ "-dc",
1570
+ "--disable-conversation",
1571
+ is_flag=True,
1572
+ default=True, # is_conversation = True
1573
+ help="Disable chatting conversationally (Stable)",
1574
+ )
1575
+ @click.option(
1576
+ "-fp",
1577
+ "--filepath",
1578
+ type=click.Path(),
1579
+ default=os.path.join(default_path, "chat-history.txt"),
1580
+ help="Path to chat history - new will be created incase doesn't exist",
1581
+ )
1582
+ @click.option(
1583
+ "--update-file/--retain-file",
1584
+ help="Controls updating chat history in file",
1585
+ default=True,
1586
+ )
1587
+ @click.option(
1588
+ "-i",
1589
+ "--intro",
1590
+ help="Conversation introductory prompt",
1591
+ )
1592
+ @click.option(
1593
+ "-ho",
1594
+ "--history-offset",
1595
+ help="Limit conversation history to this number of last texts",
1596
+ type=click.IntRange(100, 16000),
1597
+ default=10250,
1598
+ )
1599
+ @click.option(
1600
+ "-ap",
1601
+ "--awesome-prompt",
1602
+ default="0",
1603
+ callback=lambda ctx, param, value: (
1604
+ int(value) if str(value).isdigit() else value
1605
+ ),
1606
+ help="Awesome prompt key or index. Alt. to intro",
1607
+ )
1608
+ @click.option(
1609
+ "-pp",
1610
+ "--proxy-path",
1611
+ type=click.Path(exists=True),
1612
+ help="Path to .json file containing proxies",
1613
+ )
1614
+ @click.option(
1615
+ "-p",
1616
+ "--provider",
1617
+ type=click.Choice(webscout.available_providers),
1618
+ default=this.default_provider,
1619
+ help="Name of LLM provider.",
1620
+ metavar=(
1621
+ f"[{'|'.join(webscout.webai)}] etc, "
1622
+ "run 'webscout gpt4free list providers -w' to "
1623
+ "view more providers and 'webscout gpt4free test -y' "
1624
+ "for advanced g4f providers test"
1625
+ ),
1626
+ )
1627
+ @click.option(
1628
+ "-vo",
1629
+ "--vertical-overflow",
1630
+ help="Vertical overflow behaviour on content display",
1631
+ type=click.Choice(["visible", "crop", "ellipsis"]),
1632
+ default="ellipsis",
1633
+ )
1634
+ @click.option(
1635
+ "-q",
1636
+ "--quiet",
1637
+ is_flag=True,
1638
+ help="Flag for controlling response-framing and response verbosity",
1639
+ default=False,
1640
+ )
1641
+ @click.option(
1642
+ "-n",
1643
+ "--new",
1644
+ help="Override the filepath contents",
1645
+ is_flag=True,
1646
+ )
1647
+ @click.option(
1648
+ "-wc",
1649
+ "--with-copied",
1650
+ is_flag=True,
1651
+ help="Postfix prompt with last copied text",
1652
+ )
1653
+ @click.option(
1654
+ "-iw",
1655
+ "--ignore-working",
1656
+ is_flag=True,
1657
+ help="Ignore working status of the provider",
1658
+ )
1659
+ @click.option(
1660
+ "-rd",
1661
+ "--rawdog",
1662
+ is_flag=True,
1663
+ help="Generate and auto-execute Python scripts - (experimental)",
1664
+ )
1665
+ @click.option(
1666
+ "-ix",
1667
+ "--internal-exec",
1668
+ is_flag=True,
1669
+ help="RawDog : Execute scripts with exec function instead of out-of-script interpreter",
1670
+ )
1671
+ @click.option(
1672
+ "-cs",
1673
+ "--confirm-script",
1674
+ is_flag=True,
1675
+ help="RawDog : Give consent to generated scripts prior to execution",
1676
+ )
1677
+ @click.option(
1678
+ "-int",
1679
+ "--interpreter",
1680
+ default="python",
1681
+ help="RawDog : Python's interpreter name",
1682
+ )
1683
+ @click.option(
1684
+ "-ttm",
1685
+ "--talk-to-me",
1686
+ is_flag=True,
1687
+ help="Audiolize responses upon complete generation",
1688
+ )
1689
+ @click.option(
1690
+ "-ttmv",
1691
+ "--talk-to-me-voice",
1692
+ help="The voice to use for speech synthesis",
1693
+ type=click.Choice(Audio.all_voices),
1694
+ metavar="|".join(Audio.all_voices[:8]),
1695
+ default="Brian",
1696
+ )
1697
+ @click.help_option("-h", "--help")
1698
+ def generate(
1699
+ model,
1700
+ temperature,
1701
+ max_tokens,
1702
+ top_p,
1703
+ top_k,
1704
+ key,
1705
+ code_theme,
1706
+ busy_bar_index,
1707
+ font_color,
1708
+ timeout,
1709
+ prompt,
1710
+ prettify,
1711
+ whole,
1712
+ code,
1713
+ shell,
1714
+ disable_conversation,
1715
+ filepath,
1716
+ update_file,
1717
+ intro,
1718
+ history_offset,
1719
+ awesome_prompt,
1720
+ proxy_path,
1721
+ provider,
1722
+ vertical_overflow,
1723
+ quiet,
1724
+ new,
1725
+ with_copied,
1726
+ ignore_working,
1727
+ rawdog,
1728
+ internal_exec,
1729
+ confirm_script,
1730
+ interpreter,
1731
+ talk_to_me,
1732
+ talk_to_me_voice,
1733
+ ):
1734
+ """Generate a quick response with AI"""
1735
+ this.clear_history_file(filepath, new)
1736
+ bot = Main(
1737
+ max_tokens,
1738
+ temperature,
1739
+ top_k,
1740
+ top_p,
1741
+ model,
1742
+ key,
1743
+ timeout,
1744
+ disable_conversation,
1745
+ filepath,
1746
+ update_file,
1747
+ intro,
1748
+ history_offset,
1749
+ awesome_prompt,
1750
+ proxy_path,
1751
+ provider,
1752
+ quiet,
1753
+ ignore_working=ignore_working,
1754
+ rawdog=rawdog,
1755
+ internal_exec=internal_exec,
1756
+ confirm_script=confirm_script,
1757
+ interpreter=interpreter,
1758
+ )
1759
+ prompt = prompt if prompt else ""
1760
+ copied_placeholder = "{{copied}}"
1761
+ stream_placeholder = "{{stream}}"
1762
+
1763
+ if with_copied or copied_placeholder in prompt:
1764
+ last_copied_text = clipman.get()
1765
+ assert last_copied_text, "No copied text found, issue prompt"
1766
+
1767
+ if copied_placeholder in prompt:
1768
+ prompt = prompt.replace(copied_placeholder, last_copied_text)
1769
+
1770
+ else:
1771
+ sep = "\n" if prompt else ""
1772
+ prompt = prompt + sep + last_copied_text
1773
+
1774
+ if not prompt and sys.stdin.isatty(): # No prompt issued and no piped input
1775
+ help_info = (
1776
+ "Usage: webscout generate [OPTIONS] PROMPT\n"
1777
+ "Try 'webscout generate --help' for help.\n"
1778
+ "Error: Missing argument 'PROMPT'."
1779
+ )
1780
+ click.secho(
1781
+ help_info
1782
+ ) # Let's try to mimic the click's missing argument help info
1783
+ sys.exit(1)
1784
+
1785
+ if not sys.stdin.isatty(): # Piped input detected - True
1786
+ # Let's try to read piped input
1787
+ stream_text = click.get_text_stream("stdin").read()
1788
+ if stream_placeholder in prompt:
1789
+ prompt = prompt.replace(stream_placeholder, stream_text)
1790
+ else:
1791
+ prompt = prompt + "\n" + stream_text if prompt else stream_text
1792
+
1793
+ assert stream_placeholder not in prompt, (
1794
+ "No piped input detected ~ " + stream_placeholder
1795
+ )
1796
+ assert copied_placeholder not in prompt, (
1797
+ "No copied text found ~ " + copied_placeholder
1798
+ )
1799
+
1800
+ prompt = Optimizers.code(prompt) if code else prompt
1801
+ prompt = Optimizers.shell_command(prompt) if shell else prompt
1802
+ busy_bar.spin_index = (
1803
+ 0 if any([quiet, sys.stdout.isatty() == False]) else busy_bar_index
1804
+ )
1805
+ bot.code_theme = code_theme
1806
+ bot.color = font_color
1807
+ bot.prettify = prettify
1808
+ bot.vertical_overflow = vertical_overflow
1809
+ bot.disable_stream = whole
1810
+ bot.read_aloud = talk_to_me
1811
+ bot.read_aloud_voice = talk_to_me_voice
1812
+ bot.default(prompt, True, normal_stdout=(sys.stdout.isatty() == False))
1813
+
1814
+
1815
+ class Awesome:
1816
+ """Awesome commands"""
1817
+
1818
+ @staticmethod
1819
+ @click.command(context_settings=this.context_settings)
1820
+ @click.option(
1821
+ "-r",
1822
+ "--remote",
1823
+ help="Remote source to update from",
1824
+ default=AwesomePrompts.awesome_prompt_url,
1825
+ )
1826
+ @click.option(
1827
+ "-o",
1828
+ "--output",
1829
+ help="Path to save the prompts",
1830
+ default=AwesomePrompts.awesome_prompt_path,
1831
+ )
1832
+ @click.option(
1833
+ "-n", "--new", is_flag=True, help="Override the existing contents in path"
1834
+ )
1835
+ @click.help_option("-h", "--help")
1836
+ @this.handle_exception
1837
+ def update(remote, output, new):
1838
+ """Update awesome-prompts from remote source."""
1839
+ AwesomePrompts.awesome_prompt_url = remote
1840
+ AwesomePrompts.awesome_prompt_path = output
1841
+ AwesomePrompts().update_prompts_from_online(new)
1842
+ click.secho(
1843
+ f"Prompts saved to - '{AwesomePrompts.awesome_prompt_path}'", fg="cyan"
1844
+ )
1845
+
1846
+ @staticmethod
1847
+ @click.command(context_settings=this.context_settings)
1848
+ @click.argument(
1849
+ "key",
1850
+ required=True,
1851
+ type=click.STRING,
1852
+ )
1853
+ @click.option(
1854
+ "-d", "--default", help="Return this value if not found", default=None
1855
+ )
1856
+ @click.option(
1857
+ "-c",
1858
+ "--case-sensitive",
1859
+ default=True,
1860
+ flag_value=False,
1861
+ help="Perform case-sensitive search",
1862
+ )
1863
+ @click.option(
1864
+ "-f",
1865
+ "--file",
1866
+ type=click.Path(exists=True),
1867
+ help="Path to existing prompts",
1868
+ default=AwesomePrompts.awesome_prompt_path,
1869
+ )
1870
+ @click.help_option("-h", "--help")
1871
+ @this.handle_exception
1872
+ def search(
1873
+ key,
1874
+ default,
1875
+ case_sensitive,
1876
+ file,
1877
+ ):
1878
+ """Search for a particular awesome-prompt by key or index"""
1879
+ AwesomePrompts.awesome_prompt_path = file
1880
+ resp = AwesomePrompts().get_act(
1881
+ key,
1882
+ default=default,
1883
+ case_insensitive=case_sensitive,
1884
+ )
1885
+ if resp:
1886
+ click.secho(resp)
1887
+ return resp != default
1888
+
1889
+ @staticmethod
1890
+ @click.command(context_settings=this.context_settings)
1891
+ @click.option("-n", "--name", required=True, help="Prompt name")
1892
+ @click.option("-p", "--prompt", required=True, help="Prompt value")
1893
+ @click.option(
1894
+ "-f",
1895
+ "--file",
1896
+ type=click.Path(exists=True),
1897
+ help="Path to existing prompts",
1898
+ default=AwesomePrompts.awesome_prompt_path,
1899
+ )
1900
+ @click.help_option("-h", "--help")
1901
+ @this.handle_exception
1902
+ def add(name, prompt, file):
1903
+ """Add new prompt to awesome-prompt list"""
1904
+ AwesomePrompts.awesome_prompt_path = file
1905
+ return AwesomePrompts().add_prompt(name, prompt)
1906
+
1907
+ @staticmethod
1908
+ @click.command(context_settings=this.context_settings)
1909
+ @click.argument("name")
1910
+ @click.option(
1911
+ "--case-sensitive",
1912
+ is_flag=True,
1913
+ flag_value=False,
1914
+ default=True,
1915
+ help="Perform name case-sensitive search",
1916
+ )
1917
+ @click.option(
1918
+ "-f",
1919
+ "--file",
1920
+ type=click.Path(exists=True),
1921
+ help="Path to existing prompts",
1922
+ default=AwesomePrompts.awesome_prompt_path,
1923
+ )
1924
+ @click.help_option("-h", "--help")
1925
+ @this.handle_exception
1926
+ def delete(name, case_sensitive, file):
1927
+ """Delete a specific awesome-prompt"""
1928
+ AwesomePrompts.awesome_prompt_path = file
1929
+ return AwesomePrompts().delete_prompt(name, case_sensitive)
1930
+
1931
+ @staticmethod
1932
+ @click.command(context_settings=this.context_settings)
1933
+ @click.option(
1934
+ "-j",
1935
+ "--json",
1936
+ is_flag=True,
1937
+ help="Display prompts in json format",
1938
+ )
1939
+ @click.option(
1940
+ "-i",
1941
+ "--indent",
1942
+ type=click.IntRange(1, 20),
1943
+ help="Json format indentation level",
1944
+ default=4,
1945
+ )
1946
+ @click.option(
1947
+ "-x",
1948
+ "--index",
1949
+ is_flag=True,
1950
+ help="Display prompts with their corresponding indexes",
1951
+ )
1952
+ @click.option("-c", "--color", help="Prompts stdout font color")
1953
+ @click.option("-o", "--output", type=click.Path(), help="Path to save the prompts")
1954
+ @click.help_option("-h", "--help")
1955
+ def whole(json, indent, index, color, output):
1956
+ """Stdout all awesome prompts"""
1957
+ ap = AwesomePrompts()
1958
+ awesome_prompts = ap.all_acts if index else ap.get_acts()
1959
+
1960
+ if json:
1961
+ # click.secho(formatted_awesome_prompts, fg=color)
1962
+ rich.print_json(data=awesome_prompts, indent=indent)
1963
+
1964
+ else:
1965
+ awesome_table = Table(show_lines=True, title="All Awesome-Prompts")
1966
+ awesome_table.add_column("index", justify="center", style="yellow")
1967
+ awesome_table.add_column("Act Name/Index", justify="left", style="cyan")
1968
+ awesome_table.add_column(
1969
+ "Prompt",
1970
+ style=color,
1971
+ )
1972
+ for index, key_value in enumerate(awesome_prompts.items()):
1973
+ awesome_table.add_row(str(index), str(key_value[0]), key_value[1])
1974
+ rich.print(awesome_table)
1975
+
1976
+ if output:
1977
+ from json import dump
1978
+
1979
+ with open(output, "w") as fh:
1980
+ dump(awesome_prompts, fh, indent=4)
1981
+
1982
+
1983
+ class Gpt4free:
1984
+ """Commands for gpt4free"""
1985
+
1986
+ @staticmethod
1987
+ @click.command(context_settings=this.context_settings)
1988
+ @busy_bar.run(index=1, immediate=True)
1989
+ @click.help_option("-h", "--help")
1990
+ def version():
1991
+ """Check current installed version of gpt4free"""
1992
+ version_string = this.run_system_command("pip show g4f")[1].stdout.split("\n")[
1993
+ 1
1994
+ ]
1995
+ click.secho(version_string, fg="cyan")
1996
+
1997
+ @staticmethod
1998
+ @click.command(context_settings=this.context_settings)
1999
+ @click.help_option("-h", "--help")
2000
+ @click.option(
2001
+ "-e",
2002
+ "--extra",
2003
+ help="Extra required dependencies category",
2004
+ multiple=True,
2005
+ type=click.Choice(
2006
+ ["all", "image", "webdriver", "openai", "api", "gui", "none"]
2007
+ ),
2008
+ default=["all"],
2009
+ )
2010
+ @click.option("-l", "--log", is_flag=True, help="Stdout installation logs")
2011
+ @click.option(
2012
+ "-s",
2013
+ "--sudo",
2014
+ is_flag=True,
2015
+ flag_value="sudo ",
2016
+ help="Install with sudo privileges",
2017
+ )
2018
+ @busy_bar.run(index=1, immediate=True)
2019
+ def update(extra, log, sudo):
2020
+ """Update GPT4FREE package (Models, Providers etc)"""
2021
+ if "none" in extra:
2022
+ command = f"{sudo or ''}pip install --upgrade g4f"
2023
+ else:
2024
+ command = f"{sudo or ''}pip install --upgrade g4f[{','.join(extra)}]"
2025
+ is_successful, response = this.run_system_command(command)
2026
+ if log and is_successful:
2027
+ click.echo(response.stdout)
2028
+ version_string = this.run_system_command("pip show g4f")[1].stdout.split("\n")[
2029
+ 1
2030
+ ]
2031
+ click.secho(f"GPT4FREE updated successfully - {version_string}", fg="cyan")
2032
+
2033
+ @staticmethod
2034
+ @click.command("list", context_settings=this.context_settings)
2035
+ @click.argument("target")
2036
+ @click.option("-w", "--working", is_flag=True, help="Restrict to working providers")
2037
+ @click.option("-u", "--url", is_flag=True, help="Restrict to providers with url")
2038
+ @click.option(
2039
+ "-s", "--stream", is_flag=True, help="Restrict to providers supporting stream"
2040
+ )
2041
+ @click.option(
2042
+ "-c",
2043
+ "--context",
2044
+ is_flag=True,
2045
+ help="Restrict to providers supporing context natively",
2046
+ )
2047
+ @click.option(
2048
+ "-35",
2049
+ "--gpt35",
2050
+ is_flag=True,
2051
+ help="Restrict to providers supporting gpt3.5_turbo model",
2052
+ )
2053
+ @click.option(
2054
+ "-4", "--gpt4", is_flag=True, help="Restrict to providers supporting gpt4 model"
2055
+ )
2056
+ @click.option(
2057
+ "-se",
2058
+ "--selenium",
2059
+ is_flag=True,
2060
+ help="Restrict to selenium dependent providers",
2061
+ )
2062
+ @click.option("-j", "--json", is_flag=True, help="Format output in json")
2063
+ @click.help_option("-h", "--help")
2064
+ def show(target, working, url, stream, context, gpt35, gpt4, selenium, json):
2065
+ """List available models and providers"""
2066
+ available_targets = ["models", "providers"]
2067
+ assert (
2068
+ target in available_targets
2069
+ ), f"Target must be one of [{', '.join(available_targets)}]"
2070
+ if target == "providers":
2071
+ hunted_providers = list(
2072
+ set(
2073
+ map(
2074
+ lambda provider: (
2075
+ provider["name"] if all(list(provider.values())) else None
2076
+ ),
2077
+ this.g4f_providers_in_dict(
2078
+ url=url,
2079
+ working=working,
2080
+ stream=stream,
2081
+ context=context,
2082
+ gpt35=gpt35,
2083
+ gpt4=gpt4,
2084
+ selenium=selenium,
2085
+ ),
2086
+ )
2087
+ )
2088
+ )
2089
+ while None in hunted_providers:
2090
+ hunted_providers.remove(None)
2091
+
2092
+ hunted_providers.sort()
2093
+ if json:
2094
+ rich.print_json(data=dict(providers=hunted_providers), indent=4)
2095
+
2096
+ else:
2097
+ table = Table(show_lines=True)
2098
+ table.add_column("No.", style="yellow", justify="center")
2099
+ table.add_column("Provider", style="cyan")
2100
+ for no, provider in enumerate(hunted_providers):
2101
+ table.add_row(str(no), provider)
2102
+ rich.print(table)
2103
+ else:
2104
+ models = dict(
2105
+ Bard=[
2106
+ "palm",
2107
+ ],
2108
+ HuggingFace=[
2109
+ "h2ogpt-gm-oasst1-en-2048-falcon-7b-v3",
2110
+ "h2ogpt-gm-oasst1-en-2048-falcon-40b-v1",
2111
+ "h2ogpt-gm-oasst1-en-2048-open-llama-13b",
2112
+ "gpt-neox-20b",
2113
+ "oasst-sft-1-pythia-12b",
2114
+ "oasst-sft-4-pythia-12b-epoch-3.5",
2115
+ "santacoder",
2116
+ "bloom",
2117
+ "flan-t5-xxl",
2118
+ ],
2119
+ Anthropic=[
2120
+ "claude-instant-v1",
2121
+ "claude-v1",
2122
+ "claude-v2",
2123
+ ],
2124
+ Cohere=[
2125
+ "command-light-nightly",
2126
+ "command-nightly",
2127
+ ],
2128
+ OpenAI=[
2129
+ "code-davinci-002",
2130
+ "text-ada-001",
2131
+ "text-babbage-001",
2132
+ "text-curie-001",
2133
+ "text-davinci-002",
2134
+ "text-davinci-003",
2135
+ "gpt-3.5-turbo-16k",
2136
+ "gpt-3.5-turbo-16k-0613",
2137
+ "gpt-4-0613",
2138
+ ],
2139
+ Replicate=[
2140
+ "llama13b-v2-chat",
2141
+ "llama7b-v2-chat",
2142
+ ],
2143
+ )
2144
+ for provider in webscout.g4f.Provider.__providers__:
2145
+ if hasattr(provider, "models"):
2146
+ models[provider.__name__] = provider.models
2147
+ if json:
2148
+ for key, value in models.items():
2149
+ while None in value:
2150
+ value.remove(None)
2151
+ value.sort()
2152
+ models[key] = value
2153
+
2154
+ rich.print_json(data=models, indent=4)
2155
+ else:
2156
+ table = Table(show_lines=True)
2157
+ table.add_column("No.", justify="center", style="white")
2158
+ table.add_column("Base Provider", style="cyan")
2159
+ table.add_column("Model(s)", style="yellow")
2160
+ for count, provider_models in enumerate(models.items()):
2161
+ models = provider_models[1]
2162
+ models.sort()
2163
+ table.add_row(str(count), provider_models[0], "\n".join(models))
2164
+ rich.print(table)
2165
+
2166
+ @staticmethod
2167
+ @click.command(context_settings=this.context_settings)
2168
+ @click.argument("port", type=click.INT, required=False)
2169
+ @click.option(
2170
+ "-a", "--address", help="Host on this particular address", default="127.0.0.1"
2171
+ )
2172
+ @click.option("-d", "--debug", is_flag=True, help="Start server in debug mode")
2173
+ @click.option(
2174
+ "-o", "--open", is_flag=True, help="Proceed to the interface immediately"
2175
+ )
2176
+ @click.help_option("-h", "--help")
2177
+ def gui(port, address, debug, open):
2178
+ """Launch gpt4free web interface"""
2179
+ from g4f.gui import run_gui
2180
+
2181
+ port = port or 8000
2182
+ t1 = thr(
2183
+ target=run_gui,
2184
+ args=(
2185
+ address,
2186
+ port,
2187
+ debug,
2188
+ ),
2189
+ )
2190
+ # run_gui(host=address, port=port, debug=debug)
2191
+ t1.start()
2192
+ if open:
2193
+ click.launch(f"http://{address}:{port}")
2194
+ t1.join()
2195
+
2196
+ @staticmethod
2197
+ @click.command(context_settings=this.context_settings)
2198
+ @click.option(
2199
+ "-t",
2200
+ "--timeout",
2201
+ type=click.INT,
2202
+ help="Provider's response generation timeout",
2203
+ default=20,
2204
+ )
2205
+ @click.option(
2206
+ "-r",
2207
+ "--thread",
2208
+ type=click.INT,
2209
+ help="Test n amount of providers at once",
2210
+ default=5,
2211
+ )
2212
+ @click.option("-q", "--quiet", is_flag=True, help="Suppress progress bar")
2213
+ @click.option(
2214
+ "-j", "--json", is_flag=True, help="Stdout test results in json format"
2215
+ )
2216
+ @click.option("-d", "--dry-test", is_flag=True, help="Return previous test results")
2217
+ @click.option(
2218
+ "-b", "--best", is_flag=True, help="Stdout the fastest provider <name only>"
2219
+ )
2220
+ @click.option(
2221
+ "-se",
2222
+ "--selenium",
2223
+ help="Test even selenium dependent providers",
2224
+ is_flag=True,
2225
+ )
2226
+ @click.option(
2227
+ "-dl",
2228
+ "--disable-logging",
2229
+ is_flag=True,
2230
+ help="Disable logging",
2231
+ )
2232
+ @click.option("-y", "--yes", is_flag=True, help="Okay to all confirmations")
2233
+ @click.help_option("-h", "--help")
2234
+ def test(
2235
+ timeout, thread, quiet, json, dry_test, best, selenium, disable_logging, yes
2236
+ ):
2237
+ """Test and save working providers"""
2238
+ from webscout.g4f import TestProviders
2239
+
2240
+ test = TestProviders(
2241
+ test_at_once=thread,
2242
+ quiet=quiet,
2243
+ timeout=timeout,
2244
+ selenium=selenium,
2245
+ do_log=disable_logging == False,
2246
+ )
2247
+ if best:
2248
+ click.secho(test.best)
2249
+ return
2250
+ elif dry_test:
2251
+ results = test.get_results(
2252
+ run=False,
2253
+ )
2254
+ else:
2255
+ if (
2256
+ yes
2257
+ or os.path.isfile(webscout.AIutel.results_path)
2258
+ and click.confirm("Are you sure to run new test")
2259
+ ):
2260
+ results = test.get_results(run=True)
2261
+ else:
2262
+ results = test.get_results(
2263
+ run=False,
2264
+ )
2265
+ if json:
2266
+ rich.print_json(data=dict(results=results))
2267
+ else:
2268
+ table = Table(
2269
+ title="G4f Providers Test Results",
2270
+ show_lines=True,
2271
+ )
2272
+ table.add_column("No.", style="white", justify="center")
2273
+ table.add_column("Provider", style="yellow", justify="left")
2274
+ table.add_column("Response Time(s)", style="cyan")
2275
+
2276
+ for no, provider in enumerate(results, start=1):
2277
+ table.add_row(
2278
+ str(no), provider["name"], str(round(provider["time"], 2))
2279
+ )
2280
+ rich.print(table)
2281
+
2282
+
2283
+
2284
+ @staticmethod
2285
+ @click.command(context_settings=this.context_settings)
2286
+ @click.argument("prompt")
2287
+ @click.option(
2288
+ "-d",
2289
+ "--directory",
2290
+ type=click.Path(exists=True),
2291
+ help="Folder for saving the images",
2292
+ default=os.getcwd(),
2293
+ )
2294
+ @click.option(
2295
+ "-a",
2296
+ "--amount",
2297
+ type=click.IntRange(1, 100),
2298
+ help="Total images to be generated",
2299
+ default=1,
2300
+ )
2301
+ @click.option("-n", "--name", help="Name for the generated images")
2302
+ @click.option(
2303
+ "-t",
2304
+ "--timeout",
2305
+ type=click.IntRange(5, 300),
2306
+ help="Http request timeout in seconds",
2307
+ )
2308
+ @click.option("-p", "--proxy", help="Http request proxy")
2309
+ @click.option(
2310
+ "-nd",
2311
+ "--no-additives",
2312
+ is_flag=True,
2313
+ help="Disable prompt altering for effective image generation",
2314
+ )
2315
+ @click.option("-q", "--quiet", is_flag=True, help="Suppress progress bar")
2316
+ @click.help_option("-h", "--help")
2317
+ def generate_image(
2318
+ prompt, directory, amount, name, timeout, proxy, no_additives, quiet
2319
+ ):
2320
+ """Generate images with pollinations.ai"""
2321
+ with Progress() as progress:
2322
+ task = progress.add_task(
2323
+ f"[cyan]Generating ...[{amount}]",
2324
+ total=amount,
2325
+ visible=quiet == False,
2326
+ )
2327
+
2328
+
2329
+
2330
+ class Utils:
2331
+ """Utilities command"""
2332
+
2333
+ @staticmethod
2334
+ @click.command(context_settings=this.context_settings)
2335
+ @click.argument("source", required=False)
2336
+ @click.option(
2337
+ "-d", "--dev", is_flag=True, help="Update from version control (development)"
2338
+ )
2339
+ @click.option(
2340
+ "-s",
2341
+ "--sudo",
2342
+ is_flag=True,
2343
+ flag_value="sudo ",
2344
+ help="Install with sudo privileges",
2345
+ )
2346
+ @click.help_option("-h", "--help")
2347
+ @busy_bar.run(index=1, immediate=True)
2348
+ def update(source, dev, sudo):
2349
+ """Install latest version of webscout"""
2350
+ if dev:
2351
+ source = "git+" + webscout.__repo__ + ".git"
2352
+ source = "webscout" if source is None else source
2353
+ assert (
2354
+ "tgpt" in source or source == "."
2355
+ ), f"Cannot update webscout from the source '{source}'"
2356
+ click.secho(
2357
+ f"[*] Updating from '{'pip' if source=='webscout' else source}'",
2358
+ fg="yellow",
2359
+ )
2360
+ this.run_system_command(f"{sudo or ''}pip install --upgrade {source}")
2361
+ response = this.run_system_command("pip show webscout")[1]
2362
+ click.secho(response.stdout)
2363
+ click.secho("Congratulations! webscout updated successfully.", fg="cyan")
2364
+
2365
+ @staticmethod
2366
+ @click.command(context_settings=this.context_settings)
2367
+ @click.option("-w", "--whole", is_flag=True, help="Stdout whole json info")
2368
+ @click.option(
2369
+ "-v", "--version", is_flag=True, help="Stdout latest version name only"
2370
+ )
2371
+ @click.option("-b", "--body", is_flag=True, help="Stdout changelog info only")
2372
+ @click.option(
2373
+ "-e", "--executable", is_flag=True, help="Stdout url to binary for your system"
2374
+ )
2375
+ @click.help_option("-h", "--help")
2376
+ def latest(whole, version, body, executable):
2377
+ """Check webscout latest version info"""
2378
+ from webscout.utils import Updates
2379
+
2380
+ update = Updates()
2381
+ if whole:
2382
+ rich.print_json(data=update.latest(whole=True))
2383
+
2384
+ elif version:
2385
+ rich.print(update.latest_version)
2386
+ elif body:
2387
+ rich.print(Markdown(update.latest()["body"]))
2388
+ elif executable:
2389
+ rich.print(update.executable())
2390
+ else:
2391
+ rich.print_json(data=update.latest())
2392
+
2393
+
2394
+ def make_commands():
2395
+ """Make webscout chained commands"""
2396
+
2397
+ # generate
2398
+ EntryGroup.webai_.add_command(ChatGenerate.generate)
2399
+
2400
+ # webai
2401
+ EntryGroup.webai_.add_command(Chatwebai.webai)
2402
+
2403
+ # utils
2404
+ EntryGroup.utils.add_command(Utils.update)
2405
+ EntryGroup.utils.add_command(Utils.latest)
2406
+
2407
+ # gpt4free
2408
+ EntryGroup.gpt4free.add_command(Gpt4free.version)
2409
+ EntryGroup.gpt4free.add_command(Gpt4free.update)
2410
+ EntryGroup.gpt4free.add_command(Gpt4free.show)
2411
+ EntryGroup.gpt4free.add_command(Gpt4free.gui)
2412
+ EntryGroup.gpt4free.add_command(Gpt4free.test)
2413
+
2414
+ # Awesome
2415
+ EntryGroup.awesome.add_command(Awesome.add)
2416
+ EntryGroup.awesome.add_command(Awesome.delete)
2417
+ EntryGroup.awesome.add_command(Awesome.search)
2418
+ EntryGroup.awesome.add_command(Awesome.update)
2419
+ EntryGroup.awesome.add_command(Awesome.whole)
2420
+
2421
+
2422
+ # @this.handle_exception
2423
+ def main(*args):
2424
+ """Fireup console programmically"""
2425
+ sys.argv += list(args)
2426
+ args = sys.argv
2427
+ if len(args) == 1:
2428
+ sys.argv.insert(1, "webai") # Just a hack to make default command
2429
+ try:
2430
+ make_commands()
2431
+ return EntryGroup.webai_()
2432
+ except Exception as e:
2433
+ logging.error(this.getExc(e))
2434
+ sys.exit(1)
2435
+
2436
+
2437
+ if __name__ == "__main__":
2361
2438
  main()