View Source LangChain.Message.ContentPart (LangChain v0.2.0)

Models a ContentPart. Some LLMs support combining text, images, and possibly other content as part of a single user message. A ContentPart represents a block, or part, of a message's content that is all of one type.

Types

  • :text - The message part is text.
  • :image_url - The message part is a URL to an image.
  • :image - The message part is image data that is base64 encoded text.

Fields

  • :content - Text content.
  • :options - Options that may be specific to the LLM for a particular message type. For example, Anthropic requires an image's media_type to be provided by the caller. This can be provided using media: "image/png".

Summary

Functions

Create a new ContentPart that contains an image encoded as base64 data. Raises an exception if not valid.

Create a new ContentPart that contains a URL to an image. Raises an exception if not valid.

Build a new message and return an :ok/:error tuple with the result.

Build a new message and return it or raise an error if invalid.

Create a new ContentPart that contains text. Raises an exception if not valid.

Types

@type t() :: %LangChain.Message.ContentPart{
  content: term(),
  options: term(),
  type: term()
}

Functions

Link to this function

image!(content, opts \\ [])

View Source
@spec image!(String.t(), Keyword.t()) :: t() | no_return()

Create a new ContentPart that contains an image encoded as base64 data. Raises an exception if not valid.

Options

  • :media - Provide the "media type" for the image. Examples: "image/jpeg", "image/png", etc. ChatGPT does not require this but other LLMs may.
@spec image_url!(String.t()) :: t() | no_return()

Create a new ContentPart that contains a URL to an image. Raises an exception if not valid.

@spec new(attrs :: map()) :: {:ok, t()} | {:error, Ecto.Changeset.t()}

Build a new message and return an :ok/:error tuple with the result.

@spec new!(attrs :: map()) :: t() | no_return()

Build a new message and return it or raise an error if invalid.

Example

ContentPart.new!(%{type: :text, content: "Greetings!"})

ContentPart.new!(%{type: :image_url, content: "https://example.com/images/house.jpg"})
@spec text!(String.t()) :: t() | no_return()

Create a new ContentPart that contains text. Raises an exception if not valid.