Skip to content

Commit

Permalink
openai
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurme committed Nov 12, 2024
1 parent db7b518 commit 62a5f99
Showing 1 changed file with 17 additions and 3 deletions.
20 changes: 17 additions & 3 deletions libs/partners/openai/langchain_openai/chat_models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -886,8 +886,13 @@ def get_token_ids(self, text: str) -> List[int]:
_, encoding_model = self._get_encoding_model()
return encoding_model.encode(text)

# TODO: Count bound tools as part of input.
def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
def get_num_tokens_from_messages(
self,
messages: List[BaseMessage],
tools: Optional[
Sequence[Union[Dict[str, Any], Type, Callable, BaseTool]]
] = None,
) -> int:
"""Calculate num tokens for gpt-3.5-turbo and gpt-4 with tiktoken package.
**Requirements**: You must have the ``pillow`` installed if you want to count
Expand All @@ -897,7 +902,16 @@ def get_num_tokens_from_messages(self, messages: List[BaseMessage]) -> int:
counting.
OpenAI reference: https://github.com/openai/openai-cookbook/blob/
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
Args:
messages: The message inputs to tokenize.
tools: If provided, sequence of dict, BaseModel, function, or BaseTools
to be converted to tool schemas.
"""
# TODO: Count bound tools as part of input.
if tools is not None:
warnings.warn("Counting tokens in tool schemas is not yet supported.")
if sys.version_info[1] <= 7:
return super().get_num_tokens_from_messages(messages)
model, encoding = self._get_encoding_model()
Expand Down

0 comments on commit 62a5f99

Please sign in to comment.