[docs]@beta()classStructuredPrompt(ChatPromptTemplate):"""Structured prompt template for a language model."""schema_:Union[dict,type]"""Schema for the structured prompt."""structured_output_kwargs:dict[str,Any]=Field(default_factory=dict)def__init__(self,messages:Sequence[MessageLikeRepresentation],schema_:Optional[Union[dict,type[BaseModel]]]=None,*,structured_output_kwargs:Optional[dict[str,Any]]=None,template_format:PromptTemplateFormat="f-string",**kwargs:Any,)->None:schema_=schema_orkwargs.pop("schema")structured_output_kwargs=structured_output_kwargsor{}forkinset(kwargs).difference(get_pydantic_field_names(self.__class__)):structured_output_kwargs[k]=kwargs.pop(k)super().__init__(messages=messages,schema_=schema_,structured_output_kwargs=structured_output_kwargs,template_format=template_format,**kwargs,)@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. For example, if the class is `langchain.llms.openai.OpenAI`, then the namespace is ["langchain", "llms", "openai"] """returncls.__module__.split(".")
[docs]@classmethoddeffrom_messages_and_schema(cls,messages:Sequence[MessageLikeRepresentation],schema:Union[dict,type],**kwargs:Any,)->ChatPromptTemplate:"""Create a chat prompt template from a variety of message formats. Examples: Instantiation from a list of message templates: .. code-block:: python from langchain_core.prompts import StructuredPrompt class OutputSchema(BaseModel): name: str value: int template = StructuredPrompt( [ ("human", "Hello, how are you?"), ("ai", "I'm doing well, thanks!"), ("human", "That's good to hear."), ], OutputSchema, ) Args: messages: sequence of message representations. A message can be represented using the following formats: (1) BaseMessagePromptTemplate, (2) BaseMessage, (3) 2-tuple of (message type, template); e.g., ("human", "{user_input}"), (4) 2-tuple of (message class, template), (5) a string which is shorthand for ("human", template); e.g., "{user_input}" schema: a dictionary representation of function call, or a Pydantic model. kwargs: Any additional kwargs to pass through to ``ChatModel.with_structured_output(schema, **kwargs)``. Returns: a structured prompt template """returncls(messages,schema,**kwargs)
def__or__(self,other:Union[Runnable[Any,Other],Callable[[Any],Other],Callable[[Iterator[Any]],Iterator[Other]],Mapping[str,Union[Runnable[Any,Other],Callable[[Any],Other],Any]],],)->RunnableSerializable[dict,Other]:returnself.pipe(other)defpipe(self,*others:Union[Runnable[Any,Other],Callable[[Any],Other],Callable[[Iterator[Any]],Iterator[Other]],Mapping[str,Union[Runnable[Any,Other],Callable[[Any],Other],Any]],],name:Optional[str]=None,)->RunnableSerializable[dict,Other]:"""Pipe the structured prompt to a language model. Args: others: The language model to pipe the structured prompt to. name: The name of the pipeline. Defaults to None. Returns: A RunnableSequence object. Raises: NotImplementedError: If the first element of `others` is not a language model. """if(othersandisinstance(others[0],BaseLanguageModel)orhasattr(others[0],"with_structured_output")):returnRunnableSequence(self,others[0].with_structured_output(self.schema_,**self.structured_output_kwargs),*others[1:],name=name,)else:msg="Structured prompts need to be piped to a language model."raiseNotImplementedError(msg)