langchain_community.chains.ernie_functions.base
.create_structured_output_chain¶
- langchain_community.chains.ernie_functions.base.create_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 [source]¶
[旧版本] 使用Ernie函数获取结构化输出的LLMChain。
- 参数
output_schema (Union[Dict[str, Any], Type[BaseModel]]) – 可以是字典或pydantic.BaseModel类。如果传入的是字典,假定它已经是一个有效的JsonSchema。为了获得最佳结果,pydantic.BaseModels应包含描述模式含义和参数描述的文档字符串。
llm (BaseLanguageModel) – 使用语言模型,假设其支持Ernie函数调用API。
prompt (BasePromptTemplate) – 要传递给模型的BasePromptTemplate。
output_key (str) – 在LLMChain.__call__中返回输出时使用的键。
output_parser (可选[BaseLLMOutputParser]) – 用于解析模型输出的BaseLLMOutputParser基类。默认情况下,将根据函数类型推断。如果传递了pydantic.BaseModels,则OutputParser将尝试使用这些模型来解析输出。否则,模型输出将简单地按JSON格式解析。
kwargs (任意) –
- 返回
将给定函数传递给模型的LLMChain。
- 返回类型
示例
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")