Last active
November 4, 2022 16:48
-
-
Save bryanhelmig/3d7ef811d82c99a21564101dfdc3dbaa to your computer and use it in GitHub Desktop.
handle openapi bytes (davinci, babbage, curie, ada, etc.)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def try_strip_bytes_prefix(token: str) -> str: | |
if token.startswith("bytes:") and "\\" in token: | |
return token[6:] | |
return token | |
def try_bytes_fix(tokens: list[str]) -> str: | |
# this encode/decode dance fixes places where openai provides | |
# "bytes: \\xf0\\x9f\\x98" + "bytes:\\x83" for tokens and replaces | |
# with the appropriate emoji (e.g. "😃") | |
return ( | |
"".join(try_strip_bytes_prefix(token) for token in tokens) | |
.encode() | |
.decode("unicode-escape") | |
.encode("latin-1") | |
.decode("utf-8") | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment