Packages
langchain
0.3.0-rc.0
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/utils/api_override.ex
defmodule LangChain.Utils.ApiOverride do
@moduledoc """
Tools for overriding API results. Used for testing.
Works by setting and checking for special use of the Process dictionary.
## Test Example
import LangChain.Utils.ApiOverride
model = ChatOpenAI.new!(%{temperature: 1, stream: true})
# Define the fake response to return
fake_messages = [
[MessageDelta.new!(%{role: :assistant, content: nil, status: :incomplete})],
[MessageDelta.new!(%{content: "Sock", status: :incomplete})]
]
# Made NOT LIVE here. Will not make the external call to the LLM
set_api_override({:ok, fake_messages})
# We can construct an LLMChain from a PromptTemplate and an LLM.
{:ok, updated_chain, _response} =
%{llm: model, verbose: false}
|> LLMChain.new!()
|> LLMChain.add_message(
Message.new_user!("What is a good name for a company that makes colorful socks?")
)
|> LLMChain.run()
assert %Message{role: :assistant, content: "Sock"} = updated_chain.last_message
"""
@key :fake_api_response
@doc """
Return if an override for the API response is set. Used for testing.
"""
@spec override_api_return? :: boolean()
def override_api_return?() do
@key in Process.get_keys()
end
@doc """
Set the data and callback to use as a fake API response. An `:ok` tuple
indicates a successful API call. The `fake_response_data` is the data to treat
as returned. The `callback_name` is the callback handler name to execute.
set_api_override({:ok, fake_response_data, callback_name_to_fire})
## Examples
set_api_override({:ok, Message.new_assistant!(%{content: "154 bottles"}, :on_llm_new_message})
set_api_override({:ok, MessageDelta.new!(%{content: "Hi"}), :on_llm_new_delta})
"""
@spec set_api_override(term()) :: :ok
def set_api_override(config_tuple) do
Process.put(@key, config_tuple)
:ok
end
@doc """
Get the API override to return. Returned as `{:ok, config_tuple}`. If not set,
it returns `:not_set`.
"""
@spec get_api_override() :: {:ok, term()} | :not_set
def get_api_override() do
case Process.get(@key, :not_set) do
:not_set ->
:not_set
value ->
{:ok, value}
end
end
end