"""**Prompt values** for language model prompts.Prompt values are used to represent different pieces of prompts.They can be used to represent text, images, or chat message pieces."""from__future__importannotationsfromabcimportABC,abstractmethodfromcollections.abcimportSequencefromtypingimportLiteral,castfromtyping_extensionsimportTypedDictfromlangchain_core.load.serializableimportSerializablefromlangchain_core.messagesimport(AnyMessage,BaseMessage,HumanMessage,get_buffer_string,)
[docs]classPromptValue(Serializable,ABC):"""Base abstract class for inputs to any language model. PromptValues can be converted to both LLM (pure text-generation) inputs and ChatModel inputs. """@classmethoddefis_lc_serializable(cls)->bool:"""Return whether this class is serializable. Defaults to True."""returnTrue@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. This is used to determine the namespace of the object when serializing. Defaults to ["langchain", "schema", "prompt"]. """return["langchain","schema","prompt"]
[docs]@abstractmethoddefto_string(self)->str:"""Return prompt value as string."""
[docs]@abstractmethoddefto_messages(self)->list[BaseMessage]:"""Return prompt as a list of Messages."""
[docs]classStringPromptValue(PromptValue):"""String prompt value."""text:str"""Prompt text."""type:Literal["StringPromptValue"]="StringPromptValue"@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. This is used to determine the namespace of the object when serializing. Defaults to ["langchain", "prompts", "base"]. """return["langchain","prompts","base"]
[docs]defto_string(self)->str:"""Return prompt as string."""returnself.text
[docs]defto_messages(self)->list[BaseMessage]:"""Return prompt as messages."""return[HumanMessage(content=self.text)]
[docs]classChatPromptValue(PromptValue):"""Chat prompt value. A type of a prompt value that is built from messages. """messages:Sequence[BaseMessage]"""List of messages."""
[docs]defto_string(self)->str:"""Return prompt as string."""returnget_buffer_string(self.messages)
[docs]defto_messages(self)->list[BaseMessage]:"""Return prompt as a list of messages."""returnlist(self.messages)
@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. This is used to determine the namespace of the object when serializing. Defaults to ["langchain", "prompts", "chat"]. """return["langchain","prompts","chat"]
[docs]classImageURL(TypedDict,total=False):"""Image URL."""detail:Literal["auto","low","high"]"""Specifies the detail level of the image. Defaults to "auto". Can be "auto", "low", or "high"."""url:str"""Either a URL of the image or the base64 encoded image data."""
[docs]defto_string(self)->str:"""Return prompt (image URL) as string."""returnself.image_url["url"]
[docs]defto_messages(self)->list[BaseMessage]:"""Return prompt (image URL) as messages."""return[HumanMessage(content=[cast("dict",self.image_url)])]
[docs]classChatPromptValueConcrete(ChatPromptValue):"""Chat prompt value which explicitly lists out the message types it accepts. For use in external schemas. """messages:Sequence[AnyMessage]"""Sequence of messages."""type:Literal["ChatPromptValueConcrete"]="ChatPromptValueConcrete"@classmethoddefget_lc_namespace(cls)->list[str]:"""Get the namespace of the langchain object. This is used to determine the namespace of the object when serializing. Defaults to ["langchain", "prompts", "chat"]. """return["langchain","prompts","chat"]