Skip to content

Commit

Permalink
removed now unused Message and MessageDelta fields
Browse files Browse the repository at this point in the history
- function_name
- arguments
- updated docs/examples
- updated fixtures/tests
  • Loading branch information
brainlid committed Apr 30, 2024
1 parent 891c792 commit e236649
Show file tree
Hide file tree
Showing 10 changed files with 82 additions and 300 deletions.
12 changes: 1 addition & 11 deletions lib/message.ex
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,6 @@ defmodule LangChain.Message do
# A `:tool` role contains one or more `tool_results` from the system having
# used tools.
field :tool_results, :any, virtual: true

# # When responding to a tool call, the `tool_call_id` specifies which tool
# # call this is giving a response to.
# field :tool_call_id, :string
# # A tool response state that flags that an error occurred with the tool call
# field :is_error, :boolean, default: false
# TODO: Remove "function_name"
field :function_name, :string
# TODO: Remove "arguments"
field :arguments, :any, virtual: true
end

@type t :: %Message{}
Expand Down Expand Up @@ -424,7 +414,7 @@ defmodule LangChain.Message do
end

@doc """
Return if a Message is a function_call.
Return if a Message is a tool_call.
"""
def is_tool_call?(%Message{role: :assistant, status: :complete, tool_calls: tool_calls})
when is_list(tool_calls) and tool_calls != [],
Expand Down
16 changes: 1 addition & 15 deletions lib/message_delta.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ defmodule LangChain.MessageDelta do
across many message deltas and must be fully assembled before it can be
executed.
## Function calling
* `:function_name` - A message from the LLM expressing the intent to execute a
function that was previously declared available to it.
The `arguments` will eventually be parsed from JSON. However, as deltas are
streamed, the arguments come in as text. Once it is _fully received_ it can
be parsed as JSON, but it cannot be used before it is complete as it will
not be valid JSON.
"""
use Ecto.Schema
import Ecto.Changeset
Expand All @@ -51,15 +41,11 @@ defmodule LangChain.MessageDelta do
field :role, Ecto.Enum, values: [:unknown, :assistant], default: :unknown

field :tool_calls, :any, virtual: true

# TODO: REMOVE THESE vvv
field :function_name, :string
field :arguments, :any, virtual: true
end

@type t :: %MessageDelta{}

@create_fields [:role, :content, :function_name, :arguments, :index, :status, :tool_calls]
@create_fields [:role, :content, :index, :status, :tool_calls]
@required_fields []

@doc """
Expand Down
73 changes: 55 additions & 18 deletions lib/tools/calculator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -35,58 +35,95 @@ defmodule LangChain.Tools.Calculator do
LLM: %LangChain.ChatModels.ChatOpenAI{
endpoint: "https://api.openai.com/v1/chat/completions",
model: "gpt-3.5-turbo",
api_key: nil,
temperature: 0.0,
frequency_penalty: 0.0,
receive_timeout: 60000,
seed: 0,
n: 1,
stream: false
json_response: false,
stream: false,
max_tokens: nil,
user: nil
}
MESSAGES: [
%LangChain.Message{
content: "Answer the following math question: What is 100 + 300 - 200?",
index: nil,
status: :complete,
role: :user,
function_name: nil,
arguments: nil
name: nil,
tool_calls: [],
tool_results: nil
}
]
FUNCTIONS: [
TOOLS: [
%LangChain.Function{
name: "calculator",
description: "Perform basic math calculations",
function: #Function<0.108164323/2 in LangChain.Tools.Calculator.execute>,
description: "Perform basic math calculations or expressions",
display_text: nil,
function: #Function<0.75045395/2 in LangChain.Tools.Calculator.execute>,
async: true,
parameters_schema: %{
type: "object",
required: ["expression"],
properties: %{
expression: %{
description: "A simple mathematical expression.",
type: "string"
type: "string",
description: "A simple mathematical expression"
}
},
required: ["expression"],
type: "object"
}
}
},
parameters: []
}
]
SINGLE MESSAGE RESPONSE: %LangChain.Message{
content: nil,
index: 0,
status: :complete,
role: :assistant,
function_name: "calculator",
arguments: %{"expression" => "100 + 300 - 200"}
name: nil,
tool_calls: [
%LangChain.Message.ToolCall{
status: :complete,
type: :function,
call_id: "call_NlHbo4R5NXTA6lHyjLdGQN9p",
name: "calculator",
arguments: %{"expression" => "100 + 300 - 200"},
index: nil
}
],
tool_results: nil
}
EXECUTING FUNCTION: "calculator"
FUNCTION RESULT: "200"
TOOL RESULTS: %LangChain.Message{
content: nil,
index: nil,
status: :complete,
role: :tool,
name: nil,
tool_calls: [],
tool_results: [
%LangChain.Message.ToolResult{
type: :function,
tool_call_id: "call_NlHbo4R5NXTA6lHyjLdGQN9p",
name: "calculator",
content: "200",
display_text: nil,
is_error: false
}
]
}
SINGLE MESSAGE RESPONSE: %LangChain.Message{
content: "The answer to the math question \"What is 100 + 300 - 200?\" is 200.",
content: "The result of the math question \"100 + 300 - 200\" is 200.",
index: 0,
status: :complete,
role: :assistant,
function_name: nil,
arguments: nil
name: nil,
tool_calls: [],
tool_results: nil
}
"""
require Logger
alias LangChain.Function
Expand Down
4 changes: 0 additions & 4 deletions lib/utils/chat_templates.ex
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,6 @@ defmodule LangChain.Utils.ChatTemplates do
"Conversation roles must alternate user/assistant/user/assistant/..."
end)

# TODO: Check/replace/validate messages don't include tokens for the format.
# - pass in format tokens list? Exclude tokens?
# - need the model's tokenizer config passed in.

# return 3 element tuple of critical message pieces
{system, first_user, rest}
end
Expand Down
16 changes: 0 additions & 16 deletions test/chains/llm_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -376,8 +376,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -396,8 +394,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -416,8 +412,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -436,8 +430,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -456,8 +448,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -476,8 +466,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -496,8 +484,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0
}
],
function_name: nil,
arguments: nil
}
],
[
Expand All @@ -507,8 +493,6 @@ defmodule LangChain.Chains.LLMChainTest do
index: 0,
role: :unknown,
tool_calls: nil,
function_name: nil,
arguments: nil
}
]
]
Expand Down
28 changes: 5 additions & 23 deletions test/chat_models/chat_anthropic_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -400,41 +400,31 @@ defmodule LangChain.ChatModels.ChatAnthropicTest do
content: "",
status: :incomplete,
index: nil,
function_name: nil,
role: :assistant,
arguments: nil
role: :assistant
},
%LangChain.MessageDelta{
content: "Color",
status: :incomplete,
index: nil,
function_name: nil,
role: :assistant,
arguments: nil
role: :assistant
},
%LangChain.MessageDelta{
content: "ful",
status: :incomplete,
index: nil,
function_name: nil,
role: :assistant,
arguments: nil
role: :assistant
},
%LangChain.MessageDelta{
content: " Threads",
status: :incomplete,
index: nil,
function_name: nil,
role: :assistant,
arguments: nil
role: :assistant
},
%LangChain.MessageDelta{
content: "",
status: :complete,
index: nil,
function_name: nil,
role: :assistant,
arguments: nil
role: :assistant
}
]
end
Expand Down Expand Up @@ -1072,10 +1062,6 @@ data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text
# index: nil
# }
# ],
# tool_call_id: nil,
# is_error: false,
# function_name: nil,
# arguments: nil
# }
end

Expand Down Expand Up @@ -1118,10 +1104,6 @@ data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text
# index: nil
# }
# ],
# tool_call_id: nil,
# is_error: false,
# function_name: nil,
# arguments: nil
# }
end
end
Expand Down
Loading

0 comments on commit e236649

Please sign in to comment.