Skip to content

Commit

Permalink
community
Browse files Browse the repository at this point in the history
  • Loading branch information
ccurme committed Nov 12, 2024
1 parent 62a5f99 commit 29ba5fd
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 5 deletions.
24 changes: 22 additions & 2 deletions libs/community/langchain_community/chat_models/anyscale.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,22 @@
import logging
import os
import sys
from typing import TYPE_CHECKING, Any, Dict, Optional, Set
import warnings
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Optional,
Sequence,
Set,
Type,
Union,
)

import requests
from langchain_core.messages import BaseMessage
from langchain_core.tools import BaseTool
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
from pydantic import Field, SecretStr, model_validator

Expand Down Expand Up @@ -197,10 +209,18 @@ def _get_encoding_model(self) -> tuple[str, tiktoken.Encoding]:
encoding = tiktoken_.get_encoding(model)
return model, encoding

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 with tiktoken package.
Official documentation: https://github.com/openai/openai-cookbook/blob/main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb
"""
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
24 changes: 22 additions & 2 deletions libs/community/langchain_community/chat_models/everlyai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,21 @@

import logging
import sys
from typing import TYPE_CHECKING, Any, Dict, Optional, Set
import warnings
from typing import (
TYPE_CHECKING,
Any,
Callable,
Dict,
Optional,
Sequence,
Set,
Type,
Union,
)

from langchain_core.messages import BaseMessage
from langchain_core.tools import BaseTool
from langchain_core.utils import convert_to_secret_str, get_from_dict_or_env
from pydantic import Field, model_validator

Expand Down Expand Up @@ -138,11 +150,19 @@ def _get_encoding_model(self) -> tuple[str, tiktoken.Encoding]:
encoding = tiktoken_.get_encoding(model)
return model, encoding

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 with tiktoken package.
Official documentation: https://github.com/openai/openai-cookbook/blob/
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
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
11 changes: 10 additions & 1 deletion libs/community/langchain_community/chat_models/openai.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
)
from langchain_core.outputs import ChatGeneration, ChatGenerationChunk, ChatResult
from langchain_core.runnables import Runnable
from langchain_core.tools import BaseTool
from langchain_core.utils import (
get_from_dict_or_env,
get_pydantic_field_names,
Expand Down Expand Up @@ -644,11 +645,19 @@ def get_token_ids(self, text: str) -> List[int]:
_, encoding_model = self._get_encoding_model()
return encoding_model.encode(text)

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.
Official documentation: https://github.com/openai/openai-cookbook/blob/
main/examples/How_to_format_inputs_to_ChatGPT_models.ipynb"""
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 29ba5fd

Please sign in to comment.