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

Add LLM::{Conversation,LazyConversation}#last_message #65

Merged
merged 2 commits into from
Mar 27, 2025

Conversation

0x1eef
Copy link
Member

@0x1eef 0x1eef commented Mar 27, 2025

This new method provides a short-hand way to reference the
most recent message in a conversation, with an option to filter
by the role of a message as well.

For example:

message = conversation.last_message
message = conversation.last_message(role: 'assistant') 
message = conversation.last_message(role: 'user')

It is also aliased as recent_message

message = conversation.recent_message
message = conversation.recent_message(role: 'assistant') 
message = conversation.recent_message(role: 'user')

@0x1eef 0x1eef requested review from antaz and Copilot March 27, 2025 09:01
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR introduces a new shorthand method for retrieving the last message in a conversation, with optional filtering by role, and provides an alias for the method. Key changes include:

  • Adding tests to verify the behavior of last_message (and its alias recent_message) in LazyConversation.
  • Implementing last_message and recent_message in both Conversation and LazyConversation classes.
  • Defining the assistant_role method for various providers with slight variations based on provider-specific API expectations.

Reviewed Changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated no comments.

Show a summary per file
File Description
spec/llm/lazy_conversation_spec.rb Adds specs for the last_message method for different roles in conversation testing.
lib/llm/providers/openai.rb Implements assistant_role returning "assistant".
lib/llm/providers/ollama.rb Implements assistant_role returning "assistant".
lib/llm/providers/gemini.rb Implements assistant_role returning "model" (diverging from other providers).
lib/llm/providers/anthropic.rb Implements assistant_role returning "assistant".
lib/llm/provider.rb Adds an abstract assistant_role method to enforce implementation in providers.
lib/llm/message.rb Ensures roles are stored as strings by converting the given role to a string.
lib/llm/lazy_conversation.rb Adds last_message (and alias recent_message) to retrieve the last message by role.
lib/llm/conversation.rb Adds last_message (and alias recent_message) similar to LazyConversation.
Comments suppressed due to low confidence (2)

lib/llm/providers/gemini.rb:50

  • [nitpick] The assistant_role method returns 'model' here, which differs from other providers returning 'assistant'. Confirm that this is intentional for the Gemini provider to reflect its API behavior.
      "model"

spec/llm/lazy_conversation_spec.rb:35

  • [nitpick] The test expects an assistant message with role 'model' in this context. Please verify that the intended provider configuration is being used so that this expectation aligns correctly with the provider's assistant_role value.
              role: "model",

This new method provides a short-hand way to reference the most
recent message in a conversation, with an option to filter by
the role of a message as well.

For example:
message = conversation.last_message
message = conversation.last_message(role: 'assistant')
message = conversation.last_message(role: 'user')

It is also aliased as 'recent_message':
message = conversation.recent_message
message = conversation.recent_message(role: 'assistant')
message = conversation.recent_message(role: 'user')
@0x1eef 0x1eef force-pushed the conversation/last_message branch from d58b76a to 6652471 Compare March 27, 2025 09:14
Base automatically changed from anthropic/embedding_specs to main March 27, 2025 22:47
@0x1eef 0x1eef merged commit d292d04 into main Mar 27, 2025
1 check passed
@0x1eef 0x1eef deleted the conversation/last_message branch March 27, 2025 22:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant