Source code for langchain_community.chains.ernie_functions.base
"""Methods for creating chains that use Ernie function-calling APIs."""importinspectfromtypingimport(Any,Callable,Dict,List,Optional,Sequence,Tuple,Type,Union,cast,)fromlangchain.chainsimportLLMChainfromlangchain_core.language_modelsimportBaseLanguageModelfromlangchain_core.output_parsersimport(BaseGenerationOutputParser,BaseLLMOutputParser,BaseOutputParser,)fromlangchain_core.promptsimportBasePromptTemplatefromlangchain_core.pydantic_v1importBaseModelfromlangchain_core.runnablesimportRunnablefromlangchain_core.utils.pydanticimportis_basemodel_subclassfromlangchain_community.output_parsers.ernie_functionsimport(JsonOutputFunctionsParser,PydanticAttrOutputFunctionsParser,PydanticOutputFunctionsParser,)fromlangchain_community.utils.ernie_functionsimportconvert_pydantic_to_ernie_functionPYTHON_TO_JSON_TYPES={"str":"string","int":"number","float":"number","bool":"boolean",}def_get_python_function_name(function:Callable)->str:"""Get the name of a Python function."""returnfunction.__name__def_parse_python_function_docstring(function:Callable)->Tuple[str,dict]:"""Parse the function and argument descriptions from the docstring of a function. Assumes the function docstring follows Google Python style guide. """docstring=inspect.getdoc(function)ifdocstring:docstring_blocks=docstring.split("\n\n")descriptors=[]args_block=Nonepast_descriptors=Falseforblockindocstring_blocks:ifblock.startswith("Args:"):args_block=blockbreakelifblock.startswith("Returns:")orblock.startswith("Example:"):# Don't break in case Args come afterpast_descriptors=Trueelifnotpast_descriptors:descriptors.append(block)else:continuedescription=" ".join(descriptors)else:description=""args_block=Nonearg_descriptions={}ifargs_block:arg=Noneforlineinargs_block.split("\n")[1:]:if":"inline:arg,desc=line.split(":")arg_descriptions[arg.strip()]=desc.strip()elifarg:arg_descriptions[arg.strip()]+=" "+line.strip()returndescription,arg_descriptionsdef_get_python_function_arguments(function:Callable,arg_descriptions:dict)->dict:"""Get JsonSchema describing a Python functions arguments. Assumes all function arguments are of primitive types (int, float, str, bool) or are subclasses of pydantic.BaseModel. """properties={}annotations=inspect.getfullargspec(function).annotationsforarg,arg_typeinannotations.items():ifarg=="return":continueifisinstance(arg_type,type)andis_basemodel_subclass(arg_type):# Mypy error:# "type" has no attribute "schema"properties[arg]=arg_type.schema()# type: ignore[attr-defined]elifarg_type.__name__inPYTHON_TO_JSON_TYPES:properties[arg]={"type":PYTHON_TO_JSON_TYPES[arg_type.__name__]}ifarginarg_descriptions:ifargnotinproperties:properties[arg]={}properties[arg]["description"]=arg_descriptions[arg]returnpropertiesdef_get_python_function_required_args(function:Callable)->List[str]:"""Get the required arguments for a Python function."""spec=inspect.getfullargspec(function)required=spec.args[:-len(spec.defaults)]ifspec.defaultselsespec.argsrequired+=[kforkinspec.kwonlyargsifknotin(spec.kwonlydefaultsor{})]is_class=type(function)istypeifis_classandrequired[0]=="self":required=required[1:]returnrequired
[docs]defconvert_python_function_to_ernie_function(function:Callable,)->Dict[str,Any]:"""Convert a Python function to an Ernie function-calling API compatible dict. Assumes the Python function has type hints and a docstring with a description. If the docstring has Google Python style argument descriptions, these will be included as well. """description,arg_descriptions=_parse_python_function_docstring(function)return{"name":_get_python_function_name(function),"description":description,"parameters":{"type":"object","properties":_get_python_function_arguments(function,arg_descriptions),"required":_get_python_function_required_args(function),},}
[docs]defconvert_to_ernie_function(function:Union[Dict[str,Any],Type[BaseModel],Callable],)->Dict[str,Any]:"""Convert a raw function/class to an Ernie function. Args: function: Either a dictionary, a pydantic.BaseModel class, or a Python function. If a dictionary is passed in, it is assumed to already be a valid Ernie function. Returns: A dict version of the passed in function which is compatible with the Ernie function-calling API. """ifisinstance(function,dict):returnfunctionelifisinstance(function,type)andis_basemodel_subclass(function):returncast(Dict,convert_pydantic_to_ernie_function(function))elifcallable(function):returnconvert_python_function_to_ernie_function(function)else:raiseValueError(f"Unsupported function type {type(function)}. Functions must be passed in"f" as Dict, pydantic.BaseModel, or Callable.")
[docs]defget_ernie_output_parser(functions:Sequence[Union[Dict[str,Any],Type[BaseModel],Callable]],)->Union[BaseOutputParser,BaseGenerationOutputParser]:"""Get the appropriate function output parser given the user functions. Args: functions: Sequence where element is a dictionary, a pydantic.BaseModel class, or a Python function. If a dictionary is passed in, it is assumed to already be a valid Ernie function. Returns: A PydanticOutputFunctionsParser if functions are Pydantic classes, otherwise a JsonOutputFunctionsParser. If there's only one function and it is not a Pydantic class, then the output parser will automatically extract only the function arguments and not the function name. """function_names=[convert_to_ernie_function(f)["name"]forfinfunctions]ifisinstance(functions[0],type)andis_basemodel_subclass(functions[0]):iflen(functions)>1:pydantic_schema:Union[Dict,Type[BaseModel]]={name:fnforname,fninzip(function_names,functions)}else:pydantic_schema=functions[0]output_parser:Union[BaseOutputParser,BaseGenerationOutputParser]=(PydanticOutputFunctionsParser(pydantic_schema=pydantic_schema))else:output_parser=JsonOutputFunctionsParser(args_only=len(functions)<=1)returnoutput_parser
[docs]defcreate_ernie_fn_runnable(functions:Sequence[Union[Dict[str,Any],Type[BaseModel],Callable]],llm:Runnable,prompt:BasePromptTemplate,*,output_parser:Optional[Union[BaseOutputParser,BaseGenerationOutputParser]]=None,**kwargs:Any,)->Runnable:"""Create a runnable sequence that uses Ernie functions. Args: functions: A sequence of either dictionaries, pydantic.BaseModels classes, or Python functions. If dictionaries are passed in, they are assumed to already be a valid Ernie functions. If only a single function is passed in, then it will be enforced that the model use that function. pydantic.BaseModels and Python functions should have docstrings describing what the function does. For best results, pydantic.BaseModels should have descriptions of the parameters and Python functions should have Google Python style args descriptions in the docstring. Additionally, Python functions should only use primitive types (str, int, float, bool) or pydantic.BaseModels for arguments. llm: Language model to use, assumed to support the Ernie function-calling API. prompt: BasePromptTemplate to pass to the model. output_parser: BaseLLMOutputParser to use for parsing model outputs. By default will be inferred from the function types. If pydantic.BaseModels are passed in, then the OutputParser will try to parse outputs using those. Otherwise model outputs will simply be parsed as JSON. If multiple functions are passed in and they are not pydantic.BaseModels, the chain output will include both the name of the function that was returned and the arguments to pass to the function. Returns: A runnable sequence that will pass in the given functions to the model when run. Example: .. code-block:: python from typing import Optional from langchain.chains.ernie_functions import create_ernie_fn_chain from langchain_community.chat_models import ErnieBotChat from langchain_core.prompts import ChatPromptTemplate from langchain.pydantic_v1 import BaseModel, Field class RecordPerson(BaseModel): \"\"\"Record some identifying information about a person.\"\"\" name: str = Field(..., description="The person's name") age: int = Field(..., description="The person's age") fav_food: Optional[str] = Field(None, description="The person's favorite food") class RecordDog(BaseModel): \"\"\"Record some identifying information about a dog.\"\"\" name: str = Field(..., description="The dog's name") color: str = Field(..., description="The dog's color") fav_food: Optional[str] = Field(None, description="The dog's favorite food") llm = ErnieBotChat(model_name="ERNIE-Bot-4") prompt = ChatPromptTemplate.from_messages( [ ("user", "Make calls to the relevant function to record the entities in the following input: {input}"), ("assistant", "OK!"), ("user", "Tip: Make sure to answer in the correct format"), ] ) chain = create_ernie_fn_runnable([RecordPerson, RecordDog], llm, prompt) chain.invoke({"input": "Harry was a chubby brown beagle who loved chicken"}) # -> RecordDog(name="Harry", color="brown", fav_food="chicken") """# noqa: E501ifnotfunctions:raiseValueError("Need to pass in at least one function. Received zero.")ernie_functions=[convert_to_ernie_function(f)forfinfunctions]llm_kwargs:Dict[str,Any]={"functions":ernie_functions,**kwargs}iflen(ernie_functions)==1:llm_kwargs["function_call"]={"name":ernie_functions[0]["name"]}output_parser=output_parserorget_ernie_output_parser(functions)returnprompt|llm.bind(**llm_kwargs)|output_parser
[docs]defcreate_structured_output_runnable(output_schema:Union[Dict[str,Any],Type[BaseModel]],llm:Runnable,prompt:BasePromptTemplate,*,output_parser:Optional[Union[BaseOutputParser,BaseGenerationOutputParser]]=None,**kwargs:Any,)->Runnable:"""Create a runnable that uses an Ernie function to get a structured output. Args: output_schema: Either a dictionary or pydantic.BaseModel class. If a dictionary is passed in, it's assumed to already be a valid JsonSchema. For best results, pydantic.BaseModels should have docstrings describing what the schema represents and descriptions for the parameters. llm: Language model to use, assumed to support the Ernie function-calling API. prompt: BasePromptTemplate to pass to the model. output_parser: BaseLLMOutputParser to use for parsing model outputs. By default will be inferred from the function types. If pydantic.BaseModels are passed in, then the OutputParser will try to parse outputs using those. Otherwise model outputs will simply be parsed as JSON. Returns: A runnable sequence that will pass the given function to the model when run. Example: .. code-block:: python from typing import Optional from langchain.chains.ernie_functions import create_structured_output_chain from langchain_community.chat_models import ErnieBotChat from langchain_core.prompts import ChatPromptTemplate from langchain.pydantic_v1 import BaseModel, Field class Dog(BaseModel): \"\"\"Identifying information about a dog.\"\"\" name: str = Field(..., description="The dog's name") color: str = Field(..., description="The dog's color") fav_food: Optional[str] = Field(None, description="The dog's favorite food") llm = ErnieBotChat(model_name="ERNIE-Bot-4") prompt = ChatPromptTemplate.from_messages( [ ("user", "Use the given format to extract information from the following input: {input}"), ("assistant", "OK!"), ("user", "Tip: Make sure to answer in the correct format"), ] ) chain = create_structured_output_chain(Dog, llm, prompt) chain.invoke({"input": "Harry was a chubby brown beagle who loved chicken"}) # -> Dog(name="Harry", color="brown", fav_food="chicken") """# noqa: E501ifisinstance(output_schema,dict):function:Any={"name":"output_formatter","description":("Output formatter. Should always be used to format your response to the"" user."),"parameters":output_schema,}else:class_OutputFormatter(BaseModel):"""Output formatter. Should always be used to format your response to the user."""# noqa: E501output:output_schema# type: ignorefunction=_OutputFormatteroutput_parser=output_parserorPydanticAttrOutputFunctionsParser(pydantic_schema=_OutputFormatter,attr_name="output")returncreate_ernie_fn_runnable([function],llm,prompt,output_parser=output_parser,**kwargs,)
""" --- Legacy --- """
[docs]defcreate_ernie_fn_chain(functions:Sequence[Union[Dict[str,Any],Type[BaseModel],Callable]],llm:BaseLanguageModel,prompt:BasePromptTemplate,*,output_key:str="function",output_parser:Optional[BaseLLMOutputParser]=None,**kwargs:Any,)->LLMChain:# type: ignore[valid-type]"""[Legacy] Create an LLM chain that uses Ernie functions. Args: functions: A sequence of either dictionaries, pydantic.BaseModels classes, or Python functions. If dictionaries are passed in, they are assumed to already be a valid Ernie functions. If only a single function is passed in, then it will be enforced that the model use that function. pydantic.BaseModels and Python functions should have docstrings describing what the function does. For best results, pydantic.BaseModels should have descriptions of the parameters and Python functions should have Google Python style args descriptions in the docstring. Additionally, Python functions should only use primitive types (str, int, float, bool) or pydantic.BaseModels for arguments. llm: Language model to use, assumed to support the Ernie function-calling API. prompt: BasePromptTemplate to pass to the model. output_key: The key to use when returning the output in LLMChain.__call__. output_parser: BaseLLMOutputParser to use for parsing model outputs. By default will be inferred from the function types. If pydantic.BaseModels are passed in, then the OutputParser will try to parse outputs using those. Otherwise model outputs will simply be parsed as JSON. If multiple functions are passed in and they are not pydantic.BaseModels, the chain output will include both the name of the function that was returned and the arguments to pass to the function. Returns: An LLMChain that will pass in the given functions to the model when run. Example: .. code-block:: python from typing import Optional from langchain.chains.ernie_functions import create_ernie_fn_chain from langchain_community.chat_models import ErnieBotChat from langchain_core.prompts import ChatPromptTemplate from langchain.pydantic_v1 import BaseModel, Field class RecordPerson(BaseModel): \"\"\"Record some identifying information about a person.\"\"\" name: str = Field(..., description="The person's name") age: int = Field(..., description="The person's age") fav_food: Optional[str] = Field(None, description="The person's favorite food") class RecordDog(BaseModel): \"\"\"Record some identifying information about a dog.\"\"\" name: str = Field(..., description="The dog's name") color: str = Field(..., description="The dog's color") fav_food: Optional[str] = Field(None, description="The dog's favorite food") llm = ErnieBotChat(model_name="ERNIE-Bot-4") prompt = ChatPromptTemplate.from_messages( [ ("user", "Make calls to the relevant function to record the entities in the following input: {input}"), ("assistant", "OK!"), ("user", "Tip: Make sure to answer in the correct format"), ] ) chain = create_ernie_fn_chain([RecordPerson, RecordDog], llm, prompt) chain.run("Harry was a chubby brown beagle who loved chicken") # -> RecordDog(name="Harry", color="brown", fav_food="chicken") """# noqa: E501ifnotfunctions:raiseValueError("Need to pass in at least one function. Received zero.")ernie_functions=[convert_to_ernie_function(f)forfinfunctions]output_parser=output_parserorget_ernie_output_parser(functions)llm_kwargs:Dict[str,Any]={"functions":ernie_functions,}iflen(ernie_functions)==1:llm_kwargs["function_call"]={"name":ernie_functions[0]["name"]}llm_chain=LLMChain(# type: ignore[misc]llm=llm,prompt=prompt,output_parser=output_parser,llm_kwargs=llm_kwargs,output_key=output_key,**kwargs,)returnllm_chain
[docs]defcreate_structured_output_chain(output_schema:Union[Dict[str,Any],Type[BaseModel]],llm:BaseLanguageModel,prompt:BasePromptTemplate,*,output_key:str="function",output_parser:Optional[BaseLLMOutputParser]=None,**kwargs:Any,)->LLMChain:# type: ignore[valid-type]"""[Legacy] Create an LLMChain that uses an Ernie function to get a structured output. Args: output_schema: Either a dictionary or pydantic.BaseModel class. If a dictionary is passed in, it's assumed to already be a valid JsonSchema. For best results, pydantic.BaseModels should have docstrings describing what the schema represents and descriptions for the parameters. llm: Language model to use, assumed to support the Ernie function-calling API. prompt: BasePromptTemplate to pass to the model. output_key: The key to use when returning the output in LLMChain.__call__. output_parser: BaseLLMOutputParser to use for parsing model outputs. By default will be inferred from the function types. If pydantic.BaseModels are passed in, then the OutputParser will try to parse outputs using those. Otherwise model outputs will simply be parsed as JSON. Returns: An LLMChain that will pass the given function to the model. Example: .. code-block:: python from typing import Optional from langchain.chains.ernie_functions import create_structured_output_chain from langchain_community.chat_models import ErnieBotChat from langchain_core.prompts import ChatPromptTemplate from langchain.pydantic_v1 import BaseModel, Field class Dog(BaseModel): \"\"\"Identifying information about a dog.\"\"\" name: str = Field(..., description="The dog's name") color: str = Field(..., description="The dog's color") fav_food: Optional[str] = Field(None, description="The dog's favorite food") llm = ErnieBotChat(model_name="ERNIE-Bot-4") prompt = ChatPromptTemplate.from_messages( [ ("user", "Use the given format to extract information from the following input: {input}"), ("assistant", "OK!"), ("user", "Tip: Make sure to answer in the correct format"), ] ) chain = create_structured_output_chain(Dog, llm, prompt) chain.run("Harry was a chubby brown beagle who loved chicken") # -> Dog(name="Harry", color="brown", fav_food="chicken") """# noqa: E501ifisinstance(output_schema,dict):function:Any={"name":"output_formatter","description":("Output formatter. Should always be used to format your response to the"" user."),"parameters":output_schema,}else:class_OutputFormatter(BaseModel):"""Output formatter. Should always be used to format your response to the user."""# noqa: E501output:output_schema# type: ignorefunction=_OutputFormatteroutput_parser=output_parserorPydanticAttrOutputFunctionsParser(pydantic_schema=_OutputFormatter,attr_name="output")returncreate_ernie_fn_chain([function],llm,prompt,output_key=output_key,output_parser=output_parser,**kwargs,)