webscout 3.4__py3-none-any.whl → 3.6__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.
@@ -484,4 +484,384 @@ class AsyncOPENGPT(AsyncProvider):
484
484
  str: Message extracted
485
485
  """
486
486
  assert isinstance(response, dict), "Response should be of dict data-type only"
487
- return response["content"]
487
+ return response["content"]
488
+
489
+ class OPENGPTv2(Provider):
490
+ def __init__(
491
+ self,
492
+ generate_new_agents: bool = False,
493
+ assistant_name: str = "webscout",
494
+ retrieval_description: str = (
495
+ "Can be used to look up information that was uploaded to this assistant.\n"
496
+ "If the user is referencing particular files, that is often a good hint that information may be here.\n"
497
+ "If the user asks a vague question, they are likely meaning to look up info from this retriever, "
498
+ "and you should call it!"
499
+ ),
500
+ agent_system_message: str = "You are a helpful assistant.",
501
+ chat_retrieval_llm_type: str = "GPT 3.5 Turbo",
502
+ chat_retrieval_system_message: str = "You are a helpful assistant.",
503
+ chatbot_llm_type: str = "GPT 3.5 Turbo",
504
+ chatbot_system_message: str = "You are a helpful assistant.",
505
+ enable_action_server: bool = False,
506
+ enable_ddg_search: bool = False,
507
+ enable_arxiv: bool = False,
508
+ enable_press_releases: bool = False,
509
+ enable_pubmed: bool = False,
510
+ enable_sec_filings: bool = False,
511
+ enable_retrieval: bool = False,
512
+ enable_search_tavily: bool = False,
513
+ enable_search_short_answer_tavily: bool = False,
514
+ enable_you_com_search: bool = False,
515
+ enable_wikipedia: bool = False,
516
+ is_public: bool = True,
517
+ is_conversation: bool = True,
518
+ max_tokens: int = 600,
519
+ timeout: int = 30,
520
+ intro: str = None,
521
+ filepath: str = None,
522
+ update_file: bool = True,
523
+ proxies: dict = {},
524
+ history_offset: int = 10250,
525
+ act: str = None,
526
+
527
+ ):
528
+ """
529
+ Initializes the OPENGPTv2 Provider.
530
+
531
+ Args:
532
+ api_endpoint: The API endpoint for OpenGPTs.
533
+ generate_new_agents: If True, generates new assistant and user IDs.
534
+ assistant_name: The name of the assistant to create if generating new IDs.
535
+ agent_type: The type of agent to create.
536
+ retrieval_description: Description of the retrieval tool.
537
+ agent_system_message: System message for the agent.
538
+ chat_retrieval_llm_type: LLM type for chat retrieval.
539
+ chat_retrieval_system_message: System message for chat retrieval.
540
+ chatbot_llm_type: LLM type for the chatbot.
541
+ chatbot_system_message: System message for the chatbot.
542
+ enable_action_server: Whether to enable the "Action Server by Robocorp" tool.
543
+ enable_ddg_search: Whether to enable the "Duck Duck Go Search" tool.
544
+ enable_arxiv: Whether to enable the "Arxiv" tool.
545
+ enable_press_releases: Whether to enable the "Press Releases (Kay.ai)" tool.
546
+ enable_pubmed: Whether to enable the "PubMed" tool.
547
+ enable_sec_filings: Whether to enable the "SEC Filings (Kay.ai)" tool.
548
+ enable_retrieval: Whether to enable the "Retrieval" tool.
549
+ enable_search_tavily: Whether to enable the "Search (Tavily)" tool.
550
+ enable_search_short_answer_tavily: Whether to enable the "Search (short answer, Tavily)" tool.
551
+ enable_you_com_search: Whether to enable the "You.com Search" tool.
552
+ enable_wikipedia: Whether to enable the "Wikipedia" tool.
553
+ is_public: Whether the assistant should be public.
554
+ is_conversation: Whether to maintain conversation history.
555
+ max_tokens: Maximum tokens for responses.
556
+ timeout: Timeout for API requests.
557
+ intro: Initial prompt.
558
+ filepath: Path to store conversation history.
559
+ update_file: Whether to update the conversation history file.
560
+ proxies: Proxies to use for requests.
561
+ history_offset: Maximum conversation history size.
562
+ act: Key for Awesome Prompts to use as intro.
563
+ """
564
+ self.api_endpoint = "https://opengpts-example-vz4y4ooboq-uc.a.run.app/runs/stream"
565
+ self.session = requests.Session()
566
+ self.ids_file = "openGPT_IDs.txt"
567
+ agent_type="GPT 3.5 Turbo"
568
+ (
569
+ self.assistant_id,
570
+ self.user_id,
571
+ ) = self._manage_assistant_and_user_ids(
572
+ generate_new_agents,
573
+ assistant_name,
574
+ agent_type,
575
+ retrieval_description,
576
+ agent_system_message,
577
+ chat_retrieval_llm_type,
578
+ chat_retrieval_system_message,
579
+ chatbot_llm_type,
580
+ chatbot_system_message,
581
+ enable_action_server,
582
+ enable_ddg_search,
583
+ enable_arxiv,
584
+ enable_press_releases,
585
+ enable_pubmed,
586
+ enable_sec_filings,
587
+ enable_retrieval,
588
+ enable_search_tavily,
589
+ enable_search_short_answer_tavily,
590
+ enable_you_com_search,
591
+ enable_wikipedia,
592
+ is_public,
593
+ )
594
+ self.last_response = {}
595
+ self.max_tokens_to_sample = max_tokens
596
+ self.stream_chunk_size = 64
597
+ self.timeout = timeout
598
+ self.__available_optimizers = (
599
+ method
600
+ for method in dir(Optimizers)
601
+ if callable(getattr(Optimizers, method)) and not method.startswith("__")
602
+ )
603
+ Conversation.intro = (
604
+ AwesomePrompts().get_act(
605
+ act, raise_not_found=True, default=None, case_insensitive=True
606
+ )
607
+ if act
608
+ else intro or Conversation.intro
609
+ )
610
+ self.conversation = Conversation(
611
+ is_conversation,
612
+ self.max_tokens_to_sample,
613
+ filepath,
614
+ update_file,
615
+ )
616
+ self.conversation.history_offset = history_offset
617
+ self.session.proxies.update(proxies)
618
+
619
+ def _manage_assistant_and_user_ids(
620
+ self,
621
+ generate_new_agents: bool = False,
622
+ assistant_name: str = "New Assistant",
623
+ agent_type: str = "GPT 3.5 Turbo",
624
+ retrieval_description: str = (
625
+ "Can be used to look up information that was uploaded to this assistant.\n"
626
+ "If the user is referencing particular files, that is often a good hint that information may be here.\n"
627
+ "If the user asks a vague question, they are likely meaning to look up info from this retriever, "
628
+ "and you should call it!"
629
+ ),
630
+ agent_system_message: str = "You are a helpful assistant.",
631
+ chat_retrieval_llm_type: str = "GPT 3.5 Turbo",
632
+ chat_retrieval_system_message: str = "You are a helpful assistant.",
633
+ chatbot_llm_type: str = "GPT 3.5 Turbo",
634
+ chatbot_system_message: str = "You are a helpful assistant.",
635
+ enable_action_server: bool = False,
636
+ enable_ddg_search: bool = False,
637
+ enable_arxiv: bool = False,
638
+ enable_press_releases: bool = False,
639
+ enable_pubmed: bool = False,
640
+ enable_sec_filings: bool = False,
641
+ enable_retrieval: bool = False,
642
+ enable_search_tavily: bool = False,
643
+ enable_search_short_answer_tavily: bool = False,
644
+ enable_you_com_search: bool = False,
645
+ enable_wikipedia: bool = False,
646
+ is_public: bool = True,
647
+ ) -> tuple[str, str]:
648
+ """
649
+ Generates or retrieves assistant and user IDs.
650
+
651
+ If 'generate_new_agents' is True, new IDs are created and saved to 'openGPT_IDs.txt'.
652
+ Otherwise, IDs are loaded from 'openGPT_IDs.txt'.
653
+
654
+ Args:
655
+ generate_new_agents: If True, generate new IDs; otherwise, load from the file.
656
+ assistant_name: The name of the assistant (used when generating new IDs).
657
+ agent_type: The type of the agent.
658
+ retrieval_description: Description for the retrieval tool.
659
+ agent_system_message: The system message for the agent.
660
+ chat_retrieval_llm_type: The LLM type for chat retrieval.
661
+ chat_retrieval_system_message: The system message for chat retrieval.
662
+ chatbot_llm_type: The LLM type for the chatbot.
663
+ chatbot_system_message: The system message for the chatbot.
664
+ enable_action_server: Whether to enable the "Action Server by Robocorp" tool.
665
+ enable_ddg_search: Whether to enable the "Duck Duck Go Search" tool.
666
+ enable_arxiv: Whether to enable the "Arxiv" tool.
667
+ enable_press_releases: Whether to enable the "Press Releases (Kay.ai)" tool.
668
+ enable_pubmed: Whether to enable the "PubMed" tool.
669
+ enable_sec_filings: Whether to enable the "SEC Filings (Kay.ai)" tool.
670
+ enable_retrieval: Whether to enable the "Retrieval" tool.
671
+ enable_search_tavily: Whether to enable the "Search (Tavily)" tool.
672
+ enable_search_short_answer_tavily: Whether to enable the "Search (short answer, Tavily)" tool.
673
+ enable_you_com_search: Whether to enable the "You.com Search" tool.
674
+ enable_wikipedia: Whether to enable the "Wikipedia" tool.
675
+ is_public: Whether the assistant should be public.
676
+
677
+ Returns:
678
+ A tuple containing the assistant ID and user ID.
679
+ """
680
+
681
+ if generate_new_agents:
682
+ user_id = str(uuid.uuid4())
683
+ assistant_url = f"https://opengpts-example-vz4y4ooboq-uc.a.run.app/assistants/{str(uuid.uuid4())}"
684
+
685
+ headers = {"Cookie": f"opengpts_user_id={user_id}"}
686
+
687
+ tools = []
688
+ if enable_action_server:
689
+ tools.append("Action Server by Robocorp")
690
+ if enable_ddg_search:
691
+ tools.append("DDG Search")
692
+ if enable_arxiv:
693
+ tools.append("Arxiv")
694
+ if enable_press_releases:
695
+ tools.append("Press Releases (Kay.ai)")
696
+ if enable_pubmed:
697
+ tools.append("PubMed")
698
+ if enable_sec_filings:
699
+ tools.append("SEC Filings (Kay.ai)")
700
+ if enable_retrieval:
701
+ tools.append("Retrieval")
702
+ if enable_search_tavily:
703
+ tools.append("Search (Tavily)")
704
+ if enable_search_short_answer_tavily:
705
+ tools.append("Search (short answer, Tavily)")
706
+ if enable_you_com_search:
707
+ tools.append("You.com Search")
708
+ if enable_wikipedia:
709
+ tools.append("Wikipedia")
710
+
711
+ payload = {
712
+ "name": assistant_name,
713
+ "config": {
714
+ "configurable": {
715
+ "thread_id": "",
716
+ "type": "agent",
717
+ "type==agent/agent_type": agent_type,
718
+ "type==agent/retrieval_description": retrieval_description,
719
+ "type==agent/system_message": agent_system_message,
720
+ "type==agent/tools": tools,
721
+ "type==chat_retrieval/llm_type": chat_retrieval_llm_type,
722
+ "type==chat_retrieval/system_message": chat_retrieval_system_message,
723
+ "type==chatbot/llm_type": chatbot_llm_type,
724
+ "type==chatbot/system_message": chatbot_system_message,
725
+ },
726
+ "public": is_public,
727
+ },
728
+ }
729
+
730
+ response = requests.put(assistant_url, headers=headers, json=payload)
731
+ response.raise_for_status()
732
+
733
+ json_data = response.json()
734
+ assistant_id = json_data["assistant_id"]
735
+
736
+ with open(self.ids_file, "w") as f: # Overwrite the file with new IDs
737
+ f.write(f"Assistant ID: {assistant_id}\nUser ID: {user_id}\n")
738
+
739
+ return assistant_id, user_id
740
+ else:
741
+ try:
742
+ with open(self.ids_file, "r") as f:
743
+ lines = f.readlines()
744
+ assistant_id = lines[0].split(":")[1].strip()
745
+ user_id = lines[1].split(":")[1].strip()
746
+ return assistant_id, user_id
747
+ except FileNotFoundError:
748
+ return None, None
749
+
750
+ def ask(
751
+ self,
752
+ prompt: str,
753
+ stream: bool = False,
754
+ raw: bool = False,
755
+ optimizer: str = None,
756
+ conversationally: bool = False,
757
+ ) -> dict:
758
+ """Chat with AI
759
+
760
+ Args:
761
+ prompt (str): Prompt to be send.
762
+ stream (bool, optional): Flag for streaming response. Defaults to False.
763
+ raw (bool, optional): Stream back raw response as received. Defaults to False.
764
+ optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
765
+ conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
766
+ Returns:
767
+ dict : {}
768
+ ```json
769
+ {
770
+ "text" : "print('How may I help you today?')"
771
+ }
772
+ ```
773
+ """
774
+ conversation_prompt = self.conversation.gen_complete_prompt(prompt)
775
+ if optimizer:
776
+ if optimizer in self.__available_optimizers:
777
+ conversation_prompt = getattr(Optimizers, optimizer)(
778
+ conversation_prompt if conversationally else prompt
779
+ )
780
+ else:
781
+ raise Exception(
782
+ f"Optimizer is not one of {self.__available_optimizers}"
783
+ )
784
+ headers = {"Cookie": f"opengpts_user_id={self.user_id}"}
785
+ payload = {
786
+ "input": [
787
+ {
788
+ "content": conversation_prompt,
789
+ "additional_kwargs": {},
790
+ "type": "human",
791
+ "example": False,
792
+ },
793
+ ],
794
+ "assistant_id": self.assistant_id,
795
+ "thread_id": "",
796
+ }
797
+
798
+ response = self.session.post(
799
+ self.api_endpoint, headers=headers, json=payload, stream=stream, timeout=self.timeout
800
+ )
801
+ complete_response = ""
802
+ printed_length = 0
803
+ initial_responses_to_ignore = 2
804
+
805
+ for line in response.iter_lines(decode_unicode=True, chunk_size=1):
806
+ if line:
807
+ try:
808
+ content = json.loads(re.sub("data:", "", line))[-1]["content"]
809
+ if initial_responses_to_ignore > 0:
810
+ initial_responses_to_ignore -= 1
811
+ else:
812
+ if stream:
813
+ print(content[printed_length:], end="", flush=True)
814
+ printed_length = len(content)
815
+ complete_response = content
816
+ except:
817
+ continue
818
+ self.conversation.update_chat_history(prompt, complete_response)
819
+ self.last_response.update(dict(text=complete_response))
820
+ return self.last_response
821
+
822
+ def chat(
823
+ self,
824
+ prompt: str,
825
+ stream: bool = False,
826
+ optimizer: str = None,
827
+ conversationally: bool = False,
828
+ ) -> str:
829
+ """Generate response `str`
830
+ Args:
831
+ prompt (str): Prompt to be send.
832
+ stream (bool, optional): Flag for streaming response. Defaults to False.
833
+ optimizer (str, optional): Prompt optimizer name - `[code, shell_command]`. Defaults to None.
834
+ conversationally (bool, optional): Chat conversationally when using optimizer. Defaults to False.
835
+ Returns:
836
+ str: Response generated
837
+ """
838
+
839
+ def for_stream():
840
+ for response in self.ask(
841
+ prompt, True, optimizer=optimizer, conversationally=conversationally
842
+ ):
843
+ yield self.get_message(response)
844
+
845
+ def for_non_stream():
846
+ return self.get_message(
847
+ self.ask(
848
+ prompt,
849
+ False,
850
+ optimizer=optimizer,
851
+ conversationally=conversationally,
852
+ )
853
+ )
854
+
855
+ return for_stream() if stream else for_non_stream()
856
+
857
+ def get_message(self, response: dict) -> str:
858
+ """Retrieves message only from response
859
+
860
+ Args:
861
+ response (dict): Response generated by `self.ask`
862
+
863
+ Returns:
864
+ str: Message extracted
865
+ """
866
+ assert isinstance(response, dict), "Response should be of dict data-type only"
867
+ return response["text"]