Skip to content

Commit

Permalink
Finish implementing get_token_ids (#4787)
Browse files Browse the repository at this point in the history
  • Loading branch information
zachschillaci27 authored and vowelparrot committed May 17, 2023
1 parent 73faf43 commit 1627f31
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions langchain/llms/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -800,8 +800,8 @@ def _llm_type(self) -> str:
"""Return type of llm."""
return "openai-chat"

def get_num_tokens(self, text: str) -> int:
"""Calculate num tokens with tiktoken package."""
def get_token_ids(self, text: str) -> List[int]:
"""Get the token IDs using the tiktoken package."""
# tiktoken NOT supported for Python < 3.8
if sys.version_info[1] < 8:
return super().get_num_tokens(text)
Expand All @@ -817,10 +817,8 @@ def get_num_tokens(self, text: str) -> int:
enc = tiktoken.encoding_for_model("gpt-3.5-turbo")

# encode the text using the GPT-3.5-Turbo encoder
token_ids = enc.encode(
return enc.encode(
text,
allowed_special=self.allowed_special,
disallowed_special=self.disallowed_special,
)

return len(token_ids)

0 comments on commit 1627f31

Please sign in to comment.