The core primatives for any language model interfacing. Docprompt uses these for the prompt garden, but
supports free conversion to and from these types from other libaries.
OpenAIMessage
Bases: BaseModel
Source code in docprompt/tasks/message.py
| class OpenAIMessage(BaseModel):
role: Literal["system", "user", "assistant"]
content: Union[str, List[OpenAIComplexContent]]
def to_langchain_message(self):
try:
from langchain.schema import AIMessage, HumanMessage, SystemMessage
except ImportError:
raise ImportError(
"Could not import langchain.schema. Install with `docprompt[langchain]`"
)
role_mapping = {
"system": SystemMessage,
"user": HumanMessage,
"assistant": AIMessage,
}
dumped = self.model_dump(mode="json", exclude_unset=True, exclude_none=True)
return role_mapping[self.role](content=dumped["content"])
def to_openai(self):
return self.model_dump(mode="json", exclude_unset=True, exclude_none=True)
def to_llamaindex_chat_message(self):
try:
from llama_index.core.base.llms.types import ChatMessage, MessageRole
except ImportError:
raise ImportError(
"Could not import llama_index.core. Install with `docprompt[llamaindex]`"
)
role_mapping = {
"system": MessageRole.SYSTEM,
"user": MessageRole.USER,
"assistant": MessageRole.ASSISTANT,
}
dumped = self.model_dump(mode="json", exclude_unset=True, exclude_none=True)
return ChatMessage.from_str(
content=dumped["content"], role=role_mapping[self.role]
)
@classmethod
def from_image_uri(cls, image_uri: str) -> "OpenAIMessage":
"""Create an image message from a URI.
Args:
role: The role of the message.
image_uri: The URI of the image.
"""
image_url = OpenAIImageURL(url=image_uri)
content = OpenAIComplexContent(type="image_url", image_url=image_url)
message = cls(role="user", content=[content])
return message
|
from_image_uri(image_uri)
classmethod
Create an image message from a URI.
Parameters:
Name |
Type |
Description |
Default |
role |
|
|
required
|
image_uri |
str
|
|
required
|
Source code in docprompt/tasks/message.py
| @classmethod
def from_image_uri(cls, image_uri: str) -> "OpenAIMessage":
"""Create an image message from a URI.
Args:
role: The role of the message.
image_uri: The URI of the image.
"""
image_url = OpenAIImageURL(url=image_uri)
content = OpenAIComplexContent(type="image_url", image_url=image_url)
message = cls(role="user", content=[content])
return message
|