acheong08 / ChatGPT

Reverse engineered ChatGPT API
GNU General Public License v2.0
28k stars 4.48k forks source link

Add supported list of engines to `NotImplementedError` #1438

Closed Gteditor99 closed 1 year ago

Gteditor99 commented 1 year ago

A temporary measure to account for user error in #1437.

Will have to look into bug in:

    def get_token_count(self, convo_id: str = "default") -> int:
        """
        Get token count
        """
        if self.engine not in ENGINES:
            raise NotImplementedError(f"Unsupported engine {self.engine}")

        tiktoken.model.MODEL_TO_ENCODING["gpt-4"] = "cl100k_base"

        encoding = tiktoken.encoding_for_model(self.engine)

        num_tokens = 0
        for message in self.conversation[convo_id]:
            # every message follows <im_start>{role/name}\n{content}<im_end>\n
            num_tokens += 5
            for key, value in message.items():
                num_tokens += len(encoding.encode(value))
                if key == "name":  # if there's a name, the role is omitted
                    num_tokens += 5  # role is always required and always 1 token
        num_tokens += 5  # every reply is primed with <im_start>assistant
        return num_tokens