Jido.Character.Renderer behaviour (Jido Character v1.0.0)

Copy Markdown View Source

Behaviour and dispatcher for character renderers.

A renderer transforms a character map into:

Configuration Priority

Renderers are resolved in this order:

  1. Per-call options (:renderer key)
  2. Global application config
  3. Built-in Markdown renderer (default)

Global Configuration

# config/config.exs
config :jido_character, Jido.Character.Renderer,
  renderer: MyApp.CustomRenderer,
  renderer_opts: [format: :json]

Implementing a Custom Renderer

defmodule MyApp.CustomRenderer do
  @behaviour Jido.Character.Renderer

  @impl true
  def to_system_prompt(character, opts) do
    # Return a string prompt
    "You are #{character.name}..."
  end

  @impl true
  def to_context(character, opts) do
    # Return a ReqLLM.Context
    prompt = to_system_prompt(character, opts)
    ReqLLM.Context.new([ReqLLM.Context.system(prompt)])
  end
end

The to_context/2 callback is optional. If not implemented, the dispatcher will wrap the result of to_system_prompt/2 in a ReqLLM.Context.

Summary

Callbacks

Render a character to a ReqLLM.Context struct.

Render a character to a system prompt string.

Functions

Returns the default renderer module.

Render a character to a ReqLLM.Context struct.

Render a character to a system prompt string.

Types

character()

@type character() :: map()

opts()

@type opts() :: keyword()

Callbacks

to_context(character, opts)

(optional)
@callback to_context(character(), opts()) :: ReqLLM.Context.t()

Render a character to a ReqLLM.Context struct.

to_system_prompt(character, opts)

@callback to_system_prompt(character(), opts()) :: String.t()

Render a character to a system prompt string.

Functions

default_renderer()

@spec default_renderer() :: module()

Returns the default renderer module.

to_context(character, opts \\ [])

@spec to_context(character(), opts()) :: ReqLLM.Context.t()

Render a character to a ReqLLM.Context struct.

If the renderer doesn't implement to_context/2, falls back to wrapping the system prompt from to_system_prompt/2.

Options

Examples

context = Jido.Character.Renderer.to_context(character)
context = Jido.Character.Renderer.to_context(character, renderer: MyCustomRenderer)

to_system_prompt(character, opts \\ [])

@spec to_system_prompt(character(), opts()) :: String.t()

Render a character to a system prompt string.

Options

Examples

prompt = Jido.Character.Renderer.to_system_prompt(character)
prompt = Jido.Character.Renderer.to_system_prompt(character, renderer: MyCustomRenderer)