Jido.AI.Actions.Instructor (Jido AI v0.5.2)

View Source

A low-level action that provides direct access to Instructor's chat completion functionality. Supports most Instructor options and integrates with Jido's Model and Prompt structures.

Features

  • Multi-provider support (Anthropic, OpenAI, Ollama, llamacpp, Together, OpenRouter, etc.)
  • Streaming capabilities with array or partial response models
  • Response mode configuration (:json, :function_call)
  • Structured output validation with automatic retries

Usage

# Define a response model with Ecto
defmodule WeatherResponse do
  use Ecto.Schema

  embedded_schema do
    field :temperature, :float
    field :conditions, :string
    field :city, :string
  end
end

# Create a structured response
{:ok, result, _} = Jido.AI.Actions.Instructor.run(%{
  model: %Jido.AI.Model{provider: :anthropic, model: "claude-3-sonnet-20240229", api_key: "key"},
  prompt: Jido.AI.Prompt.new(:user, "What's the weather in Tokyo?"),
  response_model: WeatherResponse,
  max_retries: 2
})

Support Matrix

ProviderAdapterConfiguration
anthropicAnthropicapi_key
openaiOpenAIopenai: [api_key: "key"]
openrouterOpenAIopenai: [api_key, api_url]
ollamaOpenAIopenai: [api_key, api_url]
llamacppLlamacppllamacpp: [api_url]
togetherOpenAIopenai: [api_key, api_url]
otherDefaults to Anthropicapi_key

Summary

Functions

Callback implementation for Jido.Action.on_after_run/1.

Executes the Action with the given parameters and context.

Validates the input parameters for the Action.

Functions

category()

description()

name()

on_after_run(result)

Callback implementation for Jido.Action.on_after_run/1.

on_after_validate_params(params)

Callback implementation for Jido.Action.on_after_validate_params/1.

on_error(failed_params, error, context, opts)

Callback implementation for Jido.Action.on_error/4.

run(params, context)

@spec run(map(), map()) :: {:ok, map()} | {:error, any()}

Executes the Action with the given parameters and context.

The run/2 function must be implemented in the module using Jido.Action.

schema()

tags()

to_json()

to_tool()

validate_params(params)

@spec validate_params(map()) :: {:ok, map()} | {:error, String.t()}

Validates the input parameters for the Action.

Examples

iex> defmodule ExampleAction do
...>   use Jido.Action,
...>     name: "example_action",
...>     schema: [
...>       input: [type: :string, required: true]
...>     ]
...> end
...> ExampleAction.validate_params(%{input: "test"})
{:ok, %{input: "test"}}

iex> ExampleAction.validate_params(%{})
{:error, "Invalid parameters for Action: Required key :input not found"}

vsn()