Repository: acheong08/EdgeGPT
File: src/EdgeGPT.py (Python)

Code before:
        return self.response['item']['messages'][1]['text']

    @property
    def sources(self):
        """The source names and details parsed from a completed Chatbot request"""
        return self.response['item']['messages'][1]['sourceAttributions']

    @property
    def sources_dict(self):
        """The source names and details as a dictionary"""
        sources_dict = {}
        name = 'providerDisplayName'
        url = 'seeMoreUrl'
        for source in self.sources:
            if name in source.keys() and url in source.keys():
                sources_dict[source[name]] = source[url]
            else:
                continue
        return sources_dict

    @property
    def python(self):
        """Extract and join any snippets of Python code in the response"""
        codeblocks = self.output.split("```python\n")
        codeblocks = [x.split("```")[0] for x in codeblocks if "```" in x]
        return "\n".join(codeblocks)

    @property
    def suggestions(self):
        """Follow-on questions suggested by the Chatbot"""
        return [x['text'] for x in self.response['item']['messages'][1]['suggestedResponses']]

    def __repr__(self):
        return f"<Chatbot.Query: {self.prompt}>"

    def __str__(self):
        return self.output

def demo():
    q = Query("give an example of a nested list comprehension in Python and explain how to construct it in a memorable way")
    print(f"{q=}")
    print(f"{q.prompt=}")
    print(f"{q.cookies=}")
    print(f"{q.token=}")
    print(f"{q.style=}")
    print(f"{q.output=}")
    print(f"{str(q)=}")
    print(f"{q.python=}")
    print(f"{q.sources=}")
    print(f"{q.sources_dict=}")
    print(f"{q.suggestions=}")

Review comment (suggestion):
Can this be language independent?

Perhaps split by ``` and take the first word as a language property and remove \n

Thanks!

Code after:
        """
        Read the active cookie file and populate the following attributes:

          .current_filepath
          .current_data
          .image_token
        """
        cls.current_filepath = cls.files()[cls.current_file_index]
        print(f"> Importing cookies from: {cls.current_filepath.name}")
        with open(cls.current_filepath, encoding="utf-8") as file:
            cls.current_data = json.load(file)
        cls.image_token = [x for x in cls.current_data if x.get("name") == "_U"]
        cls.image_token = cls.image_token[0].get("value")

    @classmethod
    def import_next(cls):
        """
        Cycle through to the next cookies file.  Import it.  Mark the previous
        file to be ignored for the remainder of the current session.
        """
        cls.ignore_files.add(cls.current_filepath)
        if Cookie.current_file_index >= len(cls.files()):
            Cookie.current_file_index = 0
        Cookie.import_data()


class Query:
    """
    A convenience class that wraps around EdgeGPT.Chatbot to encapsulate input,
    config, and output all together.  Relies on Cookie class for authentication
    """

    index = []
    request_count = {}
    image_dirpath = Path("./").resolve()
    Cookie.import_data()

    def __init__(
        self,
        prompt,
        style="precise",
        content_type="text",
        cookie_file=0,
        echo=True,
        echo_prompt=False,
    ):
        """
        Arguments:

        prompt: Text to enter into Bing Chat
        style: creative, balanced, or precise