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

feat: chat history #286

Open
wants to merge 5 commits into
base: dev
Choose a base branch
from
Open

feat: chat history #286

wants to merge 5 commits into from

Conversation

JarbasAl
Copy link
Member

@JarbasAl JarbasAl commented Nov 13, 2024

for usage with https://github.com/OpenVoiceOS/ovos-persona

Summary by CodeRabbit

  • New Features

    • Introduced the ChatMessageSolver class for enhanced chat interactions.
    • Added functions to find and load chat solver plugins.
    • New methods for processing chat history and generating responses.
  • Bug Fixes

    • Improved error handling in plugin loading functions.
  • Documentation

    • Added detailed docstrings for new methods to clarify usage and parameters.
  • Refactor

    • Updated import statements and modified enum entries for clarity and future renaming.

Copy link
Contributor

coderabbitai bot commented Nov 13, 2024

Walkthrough

The pull request introduces a new solver type, ChatMessageSolver, to the ovos_plugin_manager. This includes two new functions for managing chat solver plugins, find_chat_solver_plugins() and load_chat_solver_plugin(module_name: str). Additionally, a new class ChatMessageSolver is created, which extends QuestionSolver and provides methods for handling chat interactions. The changes also involve updates to the PluginTypes and PluginConfigTypes enums, preparing for future renaming while maintaining existing functionality.

Changes

File Path Change Summary
ovos_plugin_manager/solvers.py - Added ChatMessageSolver class.
- Added methods: find_chat_solver_plugins(), load_chat_solver_plugin(module_name: str).
- Updated import statements and minor formatting changes to existing method signatures.
ovos_plugin_manager/templates/solvers.py - Added ChatMessageSolver class extending QuestionSolver.
- Added methods: continue_chat(self, messages: List[Dict[str, str]], lang: Optional[str]), get_chat_completion(self, messages: List[Dict[str, str]], lang: Optional[str] = None).
- Overrode get_spoken_answer method.
ovos_plugin_manager/utils/__init__.py - Updated PluginTypes and PluginConfigTypes enums with comments for future renaming.
- Added new entry CHAT_SOLVER to PluginConfigTypes.

Possibly related PRs

  • feat/embeddings plugins #240: This PR introduces new functions for managing plugins, similar to the find_chat_solver_plugins() and load_chat_solver_plugin(module_name: str) functions added in the main PR, indicating a related focus on enhancing plugin management capabilities.
  • planned_deprecations #254: Although this PR primarily discusses planned deprecations, it also involves changes to the ovos_plugin_manager/solvers.py file, which is directly related to the main PR's modifications in the same file.

Suggested labels

enhancement

Poem

In the burrows deep and wide,
A new solver hops with pride.
Chat messages it will embrace,
In the plugin world, it finds its place.
With functions new, it leaps and bounds,
Enhancing chats with joyful sounds! 🐇✨

Warning

Rate limit exceeded

@JarbasAl has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 11 minutes and 24 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 89c2c49 and 9afae80.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 89c2c49 and 9afae80.

📒 Files selected for processing (1)
  • ovos_plugin_manager/templates/solvers.py (2 hunks)
🔇 Additional comments (5)
ovos_plugin_manager/templates/solvers.py (5)

401-413: LGTM! Well-documented class definition.

The class definition is clear and the docstring provides an excellent example of the expected message format.


415-429: LGTM! Well-defined abstract method.

The abstract method is properly defined with comprehensive documentation of parameters and return type.


430-436: LGTM! Proper language handling implementation.

The method correctly implements language detection and translation through decorators while maintaining a clean delegation to continue_chat.


455-466: LGTM! Clean implementation of backward compatibility.

The override properly maintains API compatibility while clearly documenting the conversion process from single query to chat format.


437-454: ⚠️ Potential issue

Fix Liskov Substitution Principle violation.

The method signature differs from the base class's stream_utterances, which expects query: str. This violates LSP and could cause runtime errors.

Since this issue was previously identified and the fix suggestion is still valid, I'm marking this as a duplicate of the previous review comment. Please refer to the earlier suggestion for the recommended fixes:

  1. Create a new method for streaming chat messages, or
  2. Override the base method to handle both signatures

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (3)
ovos_plugin_manager/utils/__init__.py (1)

31-61: Consider tracking the planned plugin type renaming systematically

The multiple TODO comments indicate a significant planned migration to rename plugin types with an "opm.*" prefix. This kind of widespread change requires careful planning to avoid breaking changes.

Consider:

  1. Creating a tracking issue for this migration
  2. Documenting the migration plan including:
    • Timeline for deprecation
    • Communication strategy for downstream dependencies
    • Migration guide for plugin developers
  3. Implementing a deprecation mechanism before the rename

Would you like me to help create a GitHub issue to track this migration plan?

ovos_plugin_manager/templates/solvers.py (2)

402-409: Improve docstring formatting for ChatMessageSolver

The docstring for the ChatMessageSolver class can be enhanced for clarity and adherence to PEP 257 conventions. Consider reformatting the example to use proper indentation and provide a more detailed description of the class's purpose.

Apply this diff to adjust the docstring:

-    """take chat history as input LLM style
-        messages = [
-           {"role": "system", "content": "You are a helpful assistant."},
-           {"role": "user", "content": "Knock knock."},
-           {"role": "assistant", "content": "Who's there?"},
-           {"role": "user", "content": "Orange."},
-        ]
-        """
+    """
+    Take chat history as input in LLM style.
+
+    Example:
+        messages = [
+            {"role": "system", "content": "You are a helpful assistant."},
+            {"role": "user", "content": "Knock knock."},
+            {"role": "assistant", "content": "Who's there?"},
+            {"role": "user", "content": "Orange."},
+        ]
+    """

411-413: Set default value for lang parameter in continue_chat method

For consistency with other methods in the class and to handle cases where lang might not be provided, consider setting a default value of None for the lang parameter in the continue_chat method.

Apply this diff:

-    def continue_chat(self, messages: List[Dict[str, str]],
-                      lang: Optional[str]) -> Optional[str]:
+    def continue_chat(self, messages: List[Dict[str, str]],
+                      lang: Optional[str] = None) -> Optional[str]:
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9149751 and 9dd869f.

📒 Files selected for processing (3)
  • ovos_plugin_manager/solvers.py (3 hunks)
  • ovos_plugin_manager/templates/solvers.py (1 hunks)
  • ovos_plugin_manager/utils/__init__.py (3 hunks)
🔇 Additional comments (7)
ovos_plugin_manager/utils/__init__.py (2)

Line range hint 178-182: LGTM! Well-structured deprecation notice

The deprecation of normalize_lang is handled appropriately with:

  • Clear migration instructions to the new function
  • Version targeting for removal
  • Maintained functionality during transition

51-51: Verify documentation for the new chat solver plugin type

The addition of CHAT_SOLVER looks good and follows the established pattern. However, we should ensure it's properly documented.

Let's check for documentation:

Also applies to: 95-95

ovos_plugin_manager/solvers.py (3)

1-4: LGTM: Import changes follow existing patterns

The addition of ChatMessageSolver to the imports maintains consistency with the existing codebase structure.


191-191: LGTM: Minor formatting improvements

The indentation adjustments in function signatures improve readability while maintaining functionality.

Also applies to: 322-322


11-12: Verify PluginTypes.CHAT_SOLVER enum value exists

The new functions rely on PluginTypes.CHAT_SOLVER. Let's verify this enum value is properly defined.

Also applies to: 21-22

✅ Verification successful

PluginTypes.CHAT_SOLVER is properly defined and used consistently

The verification confirms that:

  • CHAT_SOLVER is defined in PluginTypes with value "opm.solver.chat"
  • It's correctly used in solvers.py for both finding and loading plugins
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify CHAT_SOLVER is defined in PluginTypes

# Search for CHAT_SOLVER definition in PluginTypes
rg -l "class PluginTypes" | xargs rg "CHAT_SOLVER"

# Also check if any existing plugins are using this type
fd -e py | xargs rg "PluginTypes\.CHAT_SOLVER"

Length of output: 350

ovos_plugin_manager/templates/solvers.py (2)

423-424: Confirm overridden get_spoken_answer behavior

The get_spoken_answer method is overridden to funnel the query into continue_chat by wrapping it in a chat message. This approach is consistent with the intended design of ChatMessageSolver.


415-420: Verify compatibility of decorators with messages parameter

The @auto_detect_lang and @auto_translate decorators are applied to get_chat_completion with text_keys=["messages"] and translate_keys=["messages"]. Since messages is a list of dictionaries, ensure that these decorators can handle this data structure appropriately.

Run the following script to check if the decorators support lists of dictionaries:

✅ Verification successful

Let me gather more context about how these decorators handle the data structures.


Let me check how the messages parameter is handled in the actual implementation.


Let me check the translation handling in the solver base class.


Let me check the translation handling in the language module since it's imported.


Decorators correctly handle list of dictionaries parameter

The @auto_detect_lang and @auto_translate decorators are compatible with the messages parameter. The translation system includes recursive handling of nested structures through translate_dict and translate_list methods, which can process the chat messages format:

messages = [
   {"role": "system", "content": "You are a helpful assistant."},
   {"role": "user", "content": "Knock knock."}
]
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify if `auto_detect_lang` and `auto_translate` can process `messages` as a list of dictionaries.

# Search for implementations of `auto_detect_lang` and `auto_translate` to analyze their handling of complex data types
rg -A 10 'def auto_detect_lang' ovos_plugin_manager/templates/solvers.py
rg -A 10 'def auto_translate' ovos_plugin_manager/templates/solvers.py

Length of output: 1010


Script:

#!/bin/bash
# Get more context around the decorator implementations
rg -B 5 -A 30 'def auto_detect_lang' ovos_plugin_manager/templates/solvers.py
rg -B 5 -A 30 'def auto_translate' ovos_plugin_manager/templates/solvers.py

# Also search for any usage examples of these decorators with list/dict parameters
ast-grep --pattern 'auto_detect_lang(text_keys=$_)'
ast-grep --pattern 'auto_translate(translate_keys=$_)'

Length of output: 5876


Script:

#!/bin/bash
# Get the implementation details of how messages are processed
rg -B 10 -A 20 'def continue_chat' ovos_plugin_manager/templates/solvers.py

# Also check for any recursive handling of dictionaries in the translation logic
rg -A 20 'def translate' ovos_plugin_manager/templates/solvers.py

Length of output: 1400


Script:

#!/bin/bash
# Search for translation handling in the AbstractSolver class
rg -B 5 -A 20 'class AbstractSolver' ovos_plugin_manager/templates/solvers.py

# Check for any dictionary/list handling in translation methods
rg -B 5 -A 20 'def translate.*dict' ovos_plugin_manager/templates/solvers.py
rg -B 5 -A 20 'def translate.*list' ovos_plugin_manager/templates/solvers.py

Length of output: 237


Script:

#!/bin/bash
# Check the language module for translation handling
rg -B 5 -A 20 'class LanguageTranslator' ovos_plugin_manager/templates/language.py

# Look for any recursive translation methods that might handle nested structures
rg -B 5 -A 20 'def translate' ovos_plugin_manager/templates/language.py

Length of output: 3672

ovos_plugin_manager/solvers.py Show resolved Hide resolved
@github-actions github-actions bot added feature and removed feature labels Nov 13, 2024
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
ovos_plugin_manager/templates/solvers.py (2)

401-410: Enhance class docstring with more details

The docstring could be more comprehensive by including:

  • Purpose and use cases of the class
  • Description of the message format requirements
  • Return type expectations
 class ChatMessageSolver(QuestionSolver):
-    """take chat history as input LLM style
+    """A solver that processes chat history in LLM-style format to generate contextual responses.
+    
+    This class extends QuestionSolver to handle multi-turn conversations by maintaining
+    chat history in a format compatible with Large Language Models (LLMs).
+    
+    The chat messages should be provided as a list of dictionaries, where each message
+    contains a 'role' (system/user/assistant) and 'content' (the message text).
+    
     messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
     ]
+    
+    Returns:
+        str: The generated response based on the chat history.
     """

411-416: Add docstring and proper type hints to abstract method

The abstract method lacks documentation and proper type hints.

     @abc.abstractmethod
     def continue_chat(self, messages: List[Dict[str, str]],
                       lang: Optional[str],
-                      units: Optional[str] = None) -> Optional[str]:
+                      units: Optional[str] = None) -> str:
+        """Process the chat history and generate the next response.
+
+        Args:
+            messages (List[Dict[str, str]]): List of message dictionaries containing
+                'role' and 'content' keys.
+            lang (Optional[str]): The language code for the response.
+            units (Optional[str]): Optional unit system for numerical values.
+
+        Returns:
+            str: The generated response based on the chat history.
+
+        Raises:
+            NotImplementedError: When the method is not implemented by a concrete class.
+        """
         pass
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 9dd869f and 48143e6.

📒 Files selected for processing (1)
  • ovos_plugin_manager/templates/solvers.py (2 hunks)
🔇 Additional comments (2)
ovos_plugin_manager/templates/solvers.py (2)

417-423: LGTM! Well-structured implementation with proper language handling

The implementation correctly uses decorators for language detection and translation, maintaining consistency with the rest of the codebase.


443-448: LGTM! Good implementation maintaining API compatibility

The method correctly implements the base class interface by converting a single query into the chat message format.

ovos_plugin_manager/templates/solvers.py Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Outside diff range and nitpick comments (4)
ovos_plugin_manager/templates/solvers.py (4)

401-410: Enhance class docstring documentation

While the example is helpful, the docstring could be more comprehensive by including:

  • Purpose and use cases of the class
  • Description of the chat message format requirements
  • Example of the expected return value
 class ChatMessageSolver(QuestionSolver):
-    """take chat history as input LLM style
+    """A solver that processes chat history in LLM-style format to generate contextual responses.
+    
+    This class extends QuestionSolver to handle multi-turn conversations, maintaining
+    context across messages. It expects chat messages in a format similar to LLM APIs:
+    
     messages = [
        {"role": "system", "content": "You are a helpful assistant."},
        {"role": "user", "content": "Knock knock."},
        {"role": "assistant", "content": "Who's there?"},
        {"role": "user", "content": "Orange."},
     ]
+    
+    Returns:
+        A string containing the assistant's response based on the chat history.
     """

411-416: Add docstring to abstract method

The abstract method should include a comprehensive docstring to guide implementers.

     @abc.abstractmethod
     def continue_chat(self, messages: List[Dict[str, str]],
                       lang: Optional[str],
                       units: Optional[str] = None) -> Optional[str]:
+        """Generate a response based on the chat history.
+
+        Args:
+            messages (List[Dict[str, str]]): List of chat messages, each containing 'role' and 'content'.
+            lang (Optional[str]): The language code for the response. If None, will be auto-detected.
+            units (Optional[str]): Optional unit system for numerical values.
+
+        Returns:
+            Optional[str]: The generated response or None if no response could be generated.
+        """
         pass

424-441: Consider adding language handling decorators

The method should handle language detection and translation consistently with get_chat_completion.

+    @auto_detect_lang(text_keys=["messages"])
+    @auto_translate(translate_keys=["messages"])
     def stream_chat_utterances(self, messages: List[Dict[str, str]],
                                lang: Optional[str] = None,
                                units: Optional[str] = None) -> Iterable[str]:

442-447: Improve method documentation

The comment could better explain the purpose and implementation details of this override.

     def get_spoken_answer(self, query: str,
                           lang: Optional[str] = None,
                           units: Optional[str] = None) -> Optional[str]:
-        # just for api compat since it's a subclass, shouldn't be directly used
+        """Override of QuestionSolver.get_spoken_answer for API compatibility.
+        
+        This implementation converts the single query into a chat message format
+        and delegates to continue_chat. While functional, direct use of chat-specific
+        methods is recommended for chat-based interactions.
+        """
         return self.continue_chat(messages=[{"role": "user", "content": query}], lang=lang, units=units)
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 48143e6 and 89c2c49.

📒 Files selected for processing (1)
  • ovos_plugin_manager/templates/solvers.py (2 hunks)
🔇 Additional comments (1)
ovos_plugin_manager/templates/solvers.py (1)

417-423: LGTM! Well-structured implementation.

The method correctly implements language detection and translation using decorators, maintaining consistency with the parent class pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant