Class: Langchain::Utils::TokenLength::GooglePalmValidator
- Inherits:
-
BaseValidator
- Object
- BaseValidator
- Langchain::Utils::TokenLength::GooglePalmValidator
- Defined in:
- lib/langchain/utils/token_length/google_palm_validator.rb
Overview
This class is meant to validate the length of the text passed in to Google Palm’s API. It is used to validate the token length before the API call is made
Constant Summary collapse
- TOKEN_LIMITS =
{ # Source: # This data can be pulled when `list_models()` method is called: https://github.com/andreibondarev/google_palm_api#usage # chat-bison-001 is the only model that currently supports countMessageTokens functions "chat-bison-001" => { "input_token_limit" => 4000, # 4096 is the limit but the countMessageTokens does not return anything higher than 4000 "output_token_limit" => 1024 } # "text-bison-001" => { # "input_token_limit" => 8196, # "output_token_limit" => 1024 # }, # "embedding-gecko-001" => { # "input_token_limit" => 1024 # } }.freeze
Class Method Summary collapse
-
.token_length(text, model_name = "chat-bison-001", options = {}) ⇒ Integer
Calculate token length for a given text and model name.
- .token_length_from_messages(messages, model_name, options = {}) ⇒ Object
- .token_limit(model_name) ⇒ Object
Methods inherited from BaseValidator
limit_exceeded_exception, validate_max_tokens!
Class Method Details
.token_length(text, model_name = "chat-bison-001", options = {}) ⇒ Integer
Calculate token length for a given text and model name
38 39 40 41 42 43 44 |
# File 'lib/langchain/utils/token_length/google_palm_validator.rb', line 38 def self.token_length(text, model_name = "chat-bison-001", = {}) response = [:llm].client.(model: model_name, prompt: text) raise Langchain::LLM::ApiError.new(response["error"]["message"]) unless response["error"].nil? response.dig("tokenCount") end |
.token_length_from_messages(messages, model_name, options = {}) ⇒ Object
46 47 48 |
# File 'lib/langchain/utils/token_length/google_palm_validator.rb', line 46 def self.(, model_name, = {}) .sum { || token_length(.to_json, model_name, ) } end |
.token_limit(model_name) ⇒ Object
50 51 52 |
# File 'lib/langchain/utils/token_length/google_palm_validator.rb', line 50 def self.token_limit(model_name) TOKEN_LIMITS.dig(model_name, "input_token_limit") end |