Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Foundation pieces for more capable assistants #61

Merged
merged 18 commits into from
Dec 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/chains/data_extraction_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ defmodule LangChain.Chains.DataExtractionChain do
@function_name "information_extraction"
@extraction_template ~s"Extract and save the relevant entities mentioned in the following passage together with their properties. Use the value `null` when missing in the passage.

Passage:
<%= @input %>"
Passage:
<%= @input %>"

@doc """
Run the data extraction chain.
Expand Down
35 changes: 21 additions & 14 deletions lib/chains/llm_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ defmodule LangChain.Chains.LLMChain do
alias LangChain.MessageDelta
alias LangChain.Function
alias LangChain.LangChainError
alias LangChain.Utils

@primary_key false
embedded_schema do
field :llm, :any, virtual: true
field :verbose, :boolean, default: false
# verbosely log each delta message.
field :verbose_deltas, :boolean, default: false
field :functions, {:array, :any}, default: [], virtual: true
# set and managed privately through functions
field :function_map, :map, default: %{}, virtual: true
field :_function_map, :map, default: %{}, virtual: true

# List of `Message` structs for creating the conversation with the LLM.
field :messages, {:array, :any}, default: [], virtual: true
Expand Down Expand Up @@ -62,6 +65,11 @@ defmodule LangChain.Chains.LLMChain do

@doc """
Start a new LLMChain configuration.

{:ok, chain} = LLMChain.new(%{
llm: %ChatOpenAI{model: "gpt-3.5-turbo", stream: true},
messages: [%Message.new_system!("You are a helpful assistant.")]
})
"""
@spec new(attrs :: map()) :: {:ok, t} | {:error, Ecto.Changeset.t()}
def new(attrs \\ %{}) do
Expand All @@ -73,6 +81,11 @@ defmodule LangChain.Chains.LLMChain do

@doc """
Start a new LLMChain configuration and return it or raise an error if invalid.

chain = LLMChain.new!(%{
llm: %ChatOpenAI{model: "gpt-3.5-turbo", stream: true},
messages: [%Message.new_system!("You are a helpful assistant.")]
})
"""
@spec new!(attrs :: map()) :: t() | no_return()
def new!(attrs \\ %{}) do
Expand All @@ -88,18 +101,10 @@ defmodule LangChain.Chains.LLMChain do
def common_validation(changeset) do
changeset
|> validate_required(@required_fields)
|> validate_llm_is_struct()
|> Utils.validate_llm_is_struct()
|> build_functions_map_from_functions()
end

defp validate_llm_is_struct(changeset) do
case get_change(changeset, :llm) do
nil -> changeset
llm when is_struct(llm) -> changeset
_other -> add_error(changeset, :llm, "LLM must be a struct")
end
end

@doc false
def build_functions_map_from_functions(changeset) do
functions = get_field(changeset, :functions, [])
Expand All @@ -110,7 +115,7 @@ defmodule LangChain.Chains.LLMChain do
Map.put(acc, f.name, f)
end)

put_change(changeset, :function_map, fun_map)
put_change(changeset, :_function_map, fun_map)
end

@doc """
Expand Down Expand Up @@ -213,8 +218,10 @@ defmodule LangChain.Chains.LLMChain do
{:ok, add_message(chain, message)}

{:ok, [[%MessageDelta{} | _] | _] = deltas} ->
if chain.verbose, do: IO.inspect(deltas, label: "DELTA MESSAGE LIST RESPONSE")
{:ok, apply_deltas(chain, deltas)}
if chain.verbose_deltas, do: IO.inspect(deltas, label: "DELTA MESSAGE LIST RESPONSE")
updated_chain = apply_deltas(chain, deltas)
if chain.verbose, do: IO.inspect(updated_chain.last_message, label: "COMBINED DELTA MESSAGE RESPONSE")
{:ok, updated_chain}

{:error, reason} ->
if chain.verbose, do: IO.inspect(reason, label: "ERROR")
Expand Down Expand Up @@ -395,7 +402,7 @@ defmodule LangChain.Chains.LLMChain do
use_context = context || chain.custom_context

# find and execute the linked function
case chain.function_map[message.function_name] do
case chain._function_map[message.function_name] do
%Function{} = function ->
if chain.verbose, do: IO.inspect(function.name, label: "EXECUTING FUNCTION")

Expand Down
Loading
Loading