[docs]classLangSmithParams(TypedDict,total=False):"""LangSmith parameters for tracing."""ls_provider:str"""Provider of the model."""ls_model_name:str"""Name of the model."""ls_model_type:Literal["chat","llm"]"""Type of the model. Should be 'chat' or 'llm'."""ls_temperature:Optional[float]"""Temperature for generation."""ls_max_tokens:Optional[int]"""Max tokens for generation."""ls_stop:Optional[list[str]]"""Stop words for generation."""
@cache# Cache the tokenizerdefget_tokenizer()->Any:"""Get a GPT-2 tokenizer instance. This function is cached to avoid re-loading the tokenizer every time it is called. """try:fromtransformersimportGPT2TokenizerFast# type: ignore[import]exceptImportErrorase:msg=("Could not import transformers python package. ""This is needed in order to calculate get_token_ids. ""Please install it with `pip install transformers`.")raiseImportError(msg)frome# create a GPT-2 tokenizer instancereturnGPT2TokenizerFast.from_pretrained("gpt2")def_get_token_ids_default_method(text:str)->list[int]:"""Encode the text into token IDs."""# get the cached tokenizertokenizer=get_tokenizer()# tokenize the text using the GPT-2 tokenizerreturntokenizer.encode(text)LanguageModelInput=Union[PromptValue,str,Sequence[MessageLikeRepresentation]]LanguageModelOutput=Union[BaseMessage,str]LanguageModelLike=Runnable[LanguageModelInput,LanguageModelOutput]LanguageModelOutputVar=TypeVar("LanguageModelOutputVar",BaseMessage,str)def_get_verbosity()->bool:fromlangchain_core.globalsimportget_verbosereturnget_verbose()
[docs]classBaseLanguageModel(RunnableSerializable[LanguageModelInput,LanguageModelOutputVar],ABC):"""Abstract base class for interfacing with language models. All language model wrappers inherited from BaseLanguageModel. """cache:Union[BaseCache,bool,None]=Field(default=None,exclude=True)"""Whether to cache the response. * If true, will use the global cache. * If false, will not use a cache * If None, will use the global cache if it's set, otherwise no cache. * If instance of BaseCache, will use the provided cache. Caching is not currently supported for streaming methods of models. """verbose:bool=Field(default_factory=_get_verbosity,exclude=True,repr=False)"""Whether to print out response text."""callbacks:Callbacks=Field(default=None,exclude=True)"""Callbacks to add to the run trace."""tags:Optional[list[str]]=Field(default=None,exclude=True)"""Tags to add to the run trace."""metadata:Optional[dict[str,Any]]=Field(default=None,exclude=True)"""Metadata to add to the run trace."""custom_get_token_ids:Optional[Callable[[str],list[int]]]=Field(default=None,exclude=True)"""Optional encoder to use for counting tokens."""model_config=ConfigDict(arbitrary_types_allowed=True,)@field_validator("verbose",mode="before")defset_verbose(cls,verbose:Optional[bool])->bool:"""If verbose is None, set it. This allows users to pass in None as verbose to access the global setting. Args: verbose: The verbosity setting to use. Returns: The verbosity setting to use. """ifverboseisNone:return_get_verbosity()else:returnverbose@property@overridedefInputType(self)->TypeAlias:"""Get the input type for this runnable."""fromlangchain_core.prompt_valuesimport(ChatPromptValueConcrete,StringPromptValue,)# This is a version of LanguageModelInput which replaces the abstract# base class BaseMessage with a union of its subclasses, which makes# for a much better schema.returnUnion[str,Union[StringPromptValue,ChatPromptValueConcrete],list[AnyMessage],]@abstractmethoddefgenerate_prompt(self,prompts:list[PromptValue],stop:Optional[list[str]]=None,callbacks:Callbacks=None,**kwargs:Any,)->LLMResult:"""Pass a sequence of prompts to the model and return model generations. This method should make use of batched calls for models that expose a batched API. Use this method when you want to: 1. take advantage of batched calls, 2. need more output from the model than just the top generated value, 3. are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models). Args: prompts: List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models). stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. callbacks: Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output. """@abstractmethodasyncdefagenerate_prompt(self,prompts:list[PromptValue],stop:Optional[list[str]]=None,callbacks:Callbacks=None,**kwargs:Any,)->LLMResult:"""Asynchronously pass a sequence of prompts and return model generations. This method should make use of batched calls for models that expose a batched API. Use this method when you want to: 1. take advantage of batched calls, 2. need more output from the model than just the top generated value, 3. are building chains that are agnostic to the underlying language model type (e.g., pure text completion models vs chat models). Args: prompts: List of PromptValues. A PromptValue is an object that can be converted to match the format of any language model (string for pure text generation models and BaseMessages for chat models). stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. callbacks: Callbacks to pass through. Used for executing additional functionality, such as logging or streaming, throughout generation. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: An LLMResult, which contains a list of candidate Generations for each input prompt and additional model provider-specific output. """
[docs]defwith_structured_output(self,schema:Union[dict,type],**kwargs:Any)->Runnable[LanguageModelInput,Union[dict,BaseModel]]:"""Not implemented on this class."""# Implement this on child class if there is a way of steering the model to# generate responses that match a given schema.raiseNotImplementedError
@deprecated("0.1.7",alternative="invoke",removal="1.0")@abstractmethoddefpredict(self,text:str,*,stop:Optional[Sequence[str]]=None,**kwargs:Any)->str:"""Pass a single string input to the model and return a string. Use this method when passing in raw text. If you want to pass in specific types of chat messages, use predict_messages. Args: text: String input to pass to the model. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: Top model prediction as a string. """@deprecated("0.1.7",alternative="invoke",removal="1.0")@abstractmethoddefpredict_messages(self,messages:list[BaseMessage],*,stop:Optional[Sequence[str]]=None,**kwargs:Any,)->BaseMessage:"""Pass a message sequence to the model and return a message. Use this method when passing in chat messages. If you want to pass in raw text, use predict. Args: messages: A sequence of chat messages corresponding to a single model input. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: Top model prediction as a message. """@deprecated("0.1.7",alternative="ainvoke",removal="1.0")@abstractmethodasyncdefapredict(self,text:str,*,stop:Optional[Sequence[str]]=None,**kwargs:Any)->str:"""Asynchronously pass a string to the model and return a string. Use this method when calling pure text generation models and only the top candidate generation is needed. Args: text: String input to pass to the model. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: Top model prediction as a string. """@deprecated("0.1.7",alternative="ainvoke",removal="1.0")@abstractmethodasyncdefapredict_messages(self,messages:list[BaseMessage],*,stop:Optional[Sequence[str]]=None,**kwargs:Any,)->BaseMessage:"""Asynchronously pass messages to the model and return a message. Use this method when calling chat models and only the top candidate generation is needed. Args: messages: A sequence of chat messages corresponding to a single model input. stop: Stop words to use when generating. Model output is cut off at the first occurrence of any of these substrings. **kwargs: Arbitrary additional keyword arguments. These are usually passed to the model provider API call. Returns: Top model prediction as a message. """@propertydef_identifying_params(self)->Mapping[str,Any]:"""Get the identifying parameters."""returnself.lc_attributes
[docs]defget_token_ids(self,text:str)->list[int]:"""Return the ordered ids of the tokens in a text. Args: text: The string input to tokenize. Returns: A list of ids corresponding to the tokens in the text, in order they occur in the text. """ifself.custom_get_token_idsisnotNone:returnself.custom_get_token_ids(text)else:return_get_token_ids_default_method(text)
[docs]defget_num_tokens(self,text:str)->int:"""Get the number of tokens present in the text. Useful for checking if an input fits in a model's context window. Args: text: The string input to tokenize. Returns: The integer number of tokens in the text. """returnlen(self.get_token_ids(text))
[docs]defget_num_tokens_from_messages(self,messages:list[BaseMessage],tools:Optional[Sequence]=None,)->int:"""Get the number of tokens in the messages. Useful for checking if an input fits in a model's context window. **Note**: the base implementation of get_num_tokens_from_messages ignores tool schemas. Args: messages: The message inputs to tokenize. tools: If provided, sequence of dict, BaseModel, function, or BaseTools to be converted to tool schemas. Returns: The sum of the number of tokens across the messages. """iftoolsisnotNone:warnings.warn("Counting tokens in tool schemas is not yet supported. Ignoring tools.",stacklevel=2,)returnsum(self.get_num_tokens(get_buffer_string([m]))forminmessages)
@classmethoddef_all_required_field_names(cls)->set:"""DEPRECATED: Kept for backwards compatibility. Use get_pydantic_field_names. """returnget_pydantic_field_names(cls)