Jido.AI.Actions.Langchain (Jido AI v0.5.2)
View SourceA 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:
Jido.AI.Actions.Langchain.ToolResponse
- For working with tools/function callingJido.AI.Actions.Langchain.BooleanResponse
- For specialized yes/no answers with explanations
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
Provider | Adapter | Notes |
---|---|---|
openai | LangChain.ChatModels.ChatOpenAI | GPT models, function calling |
anthropic | LangChain.ChatModels.ChatAnthropic | Claude models |
openrouter | LangChain.ChatModels.ChatOpenAI | OpenAI-compatible API for multiple providers |
Summary
Functions
Callback implementation for Jido.Action.on_after_run/1
.
Callback implementation for Jido.Action.on_after_validate_params/1
.
Callback implementation for Jido.Action.on_error/4
.
Executes the Action with the given parameters and context.
Validates the input parameters for the Action.
Functions
Callback implementation for Jido.Action.on_after_run/1
.
Callback implementation for Jido.Action.on_after_validate_params/1
.
Callback implementation for Jido.Action.on_error/4
.
Executes the Action with the given parameters and context.
The run/2
function must be implemented in the module using Jido.Action.
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"}