create_ernie_fn_chain#
- langchain_community.chains.ernie_functions.base.create_ernie_fn_chain(functions: Sequence[Dict[str, Any] | Type[BaseModel] | Callable], llm: BaseLanguageModel, prompt: BasePromptTemplate, *, output_key: str = 'function', output_parser: BaseLLMOutputParser | None = None, **kwargs: Any) LLMChain [source]#
[遗留] 创建一个使用Ernie函数的LLM链。
- Parameters:
functions (Sequence[Dict[str, Any] | Type[BaseModel] | Callable]) – 一个由字典、pydantic.BaseModels 类或 Python 函数组成的序列。如果传入的是字典,则假定它们已经是有效的 Ernie 函数。如果只传入一个函数,则会强制模型使用该函数。pydantic.BaseModels 和 Python 函数应具有描述函数功能的文档字符串。为了获得最佳效果,pydantic.BaseModels 应包含参数的描述,而 Python 函数应在文档字符串中包含 Google Python 风格的参数描述。此外,Python 函数的参数应仅使用基本类型(str、int、float、bool)或 pydantic.BaseModels。
llm (BaseLanguageModel) – 使用的语言模型,假设支持Ernie函数调用API。
prompt (BasePromptTemplate) – 传递给模型的BasePromptTemplate。
output_key (str) – 在LLMChain.__call__中返回输出时使用的键。
output_parser (BaseLLMOutputParser | None) – 用于解析模型输出的BaseLLMOutputParser。默认情况下,将从函数类型推断。如果传入的是pydantic.BaseModels,则OutputParser将尝试使用这些模型解析输出。否则,模型输出将简单地解析为JSON。如果传入多个函数且它们不是pydantic.BaseModels,则链输出将包括返回的函数名称以及传递给函数的参数。
kwargs (Any)
- Returns:
一个LLMChain,在运行时将给定的函数传递给模型。
- Return type:
示例
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 pydantic 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")