Packages
langchain
0.3.0-rc.2
0.9.2
0.9.1
0.9.0
0.8.14
0.8.13
0.8.12
0.8.11
0.8.10
0.8.9
0.8.8
0.8.7
0.8.6
0.8.5
0.8.4
0.8.3
0.8.2
0.8.1
0.8.0
0.7.0
0.6.3
0.6.2
0.6.1
0.6.0
0.5.2
0.5.1
0.5.0
0.4.1
0.4.0
0.4.0-rc.3
0.4.0-rc.2
0.4.0-rc.1
0.4.0-rc.0
0.3.3
0.3.2
0.3.1
0.3.0
0.3.0-rc.2
0.3.0-rc.1
0.3.0-rc.0
0.2.0
0.1.10
0.1.9
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
Elixir implementation of a LangChain style framework that lets Elixir projects integrate with and leverage LLMs.
Current section
Files
Jump to
Current section
Files
lib/chat_models/chat_model.ex
defmodule LangChain.ChatModels.ChatModel do
require Logger
alias LangChain.Message
alias LangChain.MessageDelta
alias LangChain.Function
alias LangChain.LangChainError
alias LangChain.Utils
@type call_response ::
{:ok, Message.t() | [Message.t()] | [MessageDelta.t()]} | {:error, LangChainError.t()}
@type tool :: Function.t()
@type tools :: [tool()]
@type t :: Ecto.Schema.t()
@callback call(
t(),
String.t() | [Message.t()],
[LangChain.Function.t()]
) :: call_response()
@callback serialize_config(t()) :: %{String.t() => any()}
@callback restore_from_map(%{String.t() => any()}) :: {:ok, struct()} | {:error, String.t()}
@doc """
Create a serializable map from a ChatModel's current configuration that can
later be restored.
"""
def serialize_config(%chat_module{} = model) do
# plucks the module from the struct and, because of the behaviour, assumes
# the module defines a `serialize_config/1` function that is executed.
chat_module.serialize_config(model)
end
@doc """
Restore a ChatModel from a serialized config map.
"""
@spec restore_from_map(nil | %{String.t() => any()}) :: {:ok, struct()} | {:error, String.t()}
def restore_from_map(nil), do: {:error, "No data to restore"}
def restore_from_map(%{"module" => module_name} = data) do
case Utils.module_from_name(module_name) do
{:ok, module} ->
module.restore_from_map(data)
{:error, _reason} = error ->
error
end
end
end