Skip to content

Commit

Permalink
summarized chain's last_message get updated
Browse files Browse the repository at this point in the history
- changes when the keep_count is 0
  • Loading branch information
brainlid committed Dec 15, 2024
1 parent a07eca7 commit c453493
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
8 changes: 7 additions & 1 deletion lib/chains/summarize_conversation_chain.ex
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,13 @@ defmodule LangChain.Chains.SummarizeConversationChain do
[system | summary_messages]
end

%LLMChain{to_summarize | messages: starting_messages ++ keeping_items}
new_messages = starting_messages ++ keeping_items

%LLMChain{
to_summarize
| messages: new_messages,
last_message: List.last(new_messages)
}
end

# Convert each `%Message{}` into a text message like `<user>The user message
Expand Down
30 changes: 23 additions & 7 deletions test/chains/summarize_conversation_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,24 @@ defmodule LangChain.Chains.SummarizeConversationChainTest do
assert keep_2.role == :assistant
end

@tag live_call: true, live_anthropic: true
test "set last_message correctly when keep_count is 0", %{llm_anthropic: llm, chain: chain} do
# Made NOT LIVE here
expect(ChatAnthropic, :call, fn _model, _messages, _tools ->
{:ok, Message.new_assistant!("- Fake OpenAI summary")}
end)

summarizer = SummarizeConversationChain.new!(%{llm: llm, threshold_count: 6, keep_count: 0})
chain = LLMChain.add_messages(chain, get_full_conversation())

summarized_chain = SummarizeConversationChain.summarize(summarizer, chain)

[_system, _summary_1, summary_2] = summarized_chain.messages
assert summary_2.role == :assistant
assert summary_2.content == "- Fake OpenAI summary"

assert summarized_chain.last_message == summary_2
end

test "returns unmodified chain when LLM operation fails", %{
llm_anthropic: llm,
chain: chain
Expand Down Expand Up @@ -366,12 +383,11 @@ defmodule LangChain.Chains.SummarizeConversationChainTest do

summarized_chain = SummarizeConversationChain.summarize(summarizer, original_chain)

IO.inspect(summarized_chain.messages)


# TODO:
# IO.inspect(summarized_chain.messages)
# IO.inspect(summarized_chain.last_message)

assert false
assert summarized_chain.last_message.role == :assistant
assert String.starts_with?(summarized_chain.last_message.content, "- ")
end
end

Expand Down Expand Up @@ -541,7 +557,7 @@ Florence: Hotel Davanzati - near the Ponte Vecchio
Amalfi: Hotel Marina Riviera - beautiful sea views
Would you like me to check current availability and rates for your September dates?|),
Message.new_user!(
Message.new_user!(
"That would be helpful! My dates are September 10-24. Could you also suggest some must-see attractions in each city?"
),
Message.new_assistant!(
Expand Down

0 comments on commit c453493

Please sign in to comment.