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

View Source

A low-level action that provides direct access to Langchain's chat completion functionality. Supports multiple providers through Langchain adapters and integrates with Jido's Model and Prompt structures.

This module serves as a foundation for more specialized AI actions like:

Features

  • Multi-provider support (OpenAI, Anthropic, OpenRouter)
  • Tool/function calling capabilities
  • Response quality control with retry mechanisms
  • Support for various LLM parameters (temperature, top_p, etc.)
  • Structured error handling and logging
  • Streaming support (when provider allows)

Usage

# Basic usage
{:ok, result} = Jido.AI.Actions.Langchain.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?")
})

# With function calling / tools
{:ok, result} = Jido.AI.Actions.Langchain.run(%{
  model: %Jido.AI.Model{provider: :openai, model: "gpt-4o", api_key: "key"},
  prompt: prompt,
  tools: [Jido.Actions.Weather.GetWeather, Jido.Actions.Search.WebSearch],
  temperature: 0.2
})

# With OpenRouter (for accessing multiple model providers via one API)
{:ok, result} = Jido.AI.Actions.Langchain.run(%{
  model: %Jido.AI.Model{
    provider: :openrouter,
    model: "anthropic/claude-3-opus",
    api_key: "key",
    base_url: "https://openrouter.ai/api/v1"
  },
  prompt: prompt
})

# Streaming responses
{:ok, stream} = Jido.AI.Actions.Langchain.run(%{
  model: model,
  prompt: prompt,
  stream: true
})

Enum.each(stream, fn chunk ->
  IO.puts(chunk.content)
end)

Building Custom Actions

This module can be used as a foundation for building more specialized actions. For example, to create a domain-specific completion:

defmodule MyApp.FoodRecommendationAction do
  use Jido.Action,
    name: "food_recommendation",
    description: "Get restaurant recommendations"

  alias Jido.AI.Actions.Langchain

  def run(params, context) do
    # Enhance with domain-specific prompt engineering
    prompt = create_food_prompt(params)

    # Use BaseCompletion for the heavy lifting
    BaseCompletion.run(%{
      model: params.model,
      prompt: prompt,
      temperature: 0.7
    }, context)
  end

  defp create_food_prompt(params) do
    # Your domain-specific prompt building logic
  end
end

Support Matrix

ProviderAdapterNotes
openaiLangChain.ChatModels.ChatOpenAIGPT models, function calling
anthropicLangChain.ChatModels.ChatAnthropicClaude models
openrouterLangChain.ChatModels.ChatOpenAIOpenAI-compatible API for multiple providers

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()