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

adding mistral llm for text tasks #80

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

sankn123
Copy link

@sankn123 sankn123 commented Nov 22, 2024

This pull request introduces support for integrating an open-source free LLM by Mistral AI into the project. The integration ensures that users can leverage the capabilities of the director without relying on paid API keys from providers like OpenAI, Anthropic, or other commercial models.

TL;DR:
Added support for Mistral AI LLM.

Summary by CodeRabbit

  • New Features

    • Introduced support for the Mistral AI model, including new configuration options and functionality.
    • Added new enumerations for LLM types and environment prefixes.
  • Improvements

    • Enhanced the default LLM selection process to include Mistral AI based on environment variables.
  • Bug Fixes

    • Implemented error handling for Mistral API calls to improve reliability.


def _format_messages(self, messages: list):
formatted_messages = []
# if messages[0]["role"] == RoleTypes.system:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please remove commented code.

# return formatted_tools

def chat_completions(
self, messages: list, tools: list = [], stop=None, response_format=None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add response_format support.

config = MistralAIConfig()
super().__init__(config=config)
try:
import mistralai
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add the mistralai library to the list of dependencies in the requirements.txt file

@ashish-spext
Copy link
Contributor

Hi, this is a great value add for testing with Mistral.

@sankn123 @ankit-v2-3 can you also test all the agents with latest Mistral model?
With xAI Grok we have observed that some agents (Profanity Remover, PromptClip and Subtitle) are not functioning correctly because they do not provide responses in JSON mode.
Could you verify if the same issue occurs with the Mistral models?

Let's utilize this thread to document the experience with different agents with Mistral models.

Copy link
Contributor

coderabbitai bot commented Dec 19, 2024

Walkthrough

The pull request introduces support for the Mistral AI model in the backend system. This involves adding a new LLM type to the constants, updating the default LLM selection logic, and creating a new implementation file for Mistral AI. The changes extend the existing LLM framework to include Mistral as a configurable and usable language model, with proper configuration, message formatting, and API interaction capabilities.

Changes

File Change Summary
backend/director/constants.py Added MISTRAL to LLMType enum and MISTRAL_ to EnvPrefix enum
backend/director/llm/__init__.py Updated get_default_llm() to support Mistral AI model selection
backend/director/llm/mistral.py New file implementing Mistral AI integration with:
- MistralChatModel enum
- MistralAIConfig configuration class
- MistralAI main implementation class

Sequence Diagram

sequenceDiagram
    participant User
    participant GetDefaultLLM
    participant MistralAI
    
    User->>GetDefaultLLM: Request default LLM
    GetDefaultLLM->>GetDefaultLLM: Check environment variables
    alt Mistral selected
        GetDefaultLLM->>MistralAI: Initialize Mistral
        MistralAI-->>GetDefaultLLM: Return Mistral instance
    else Other LLM selected
        GetDefaultLLM->>GetDefaultLLM: Select alternative LLM
    end
    GetDefaultLLM-->>User: Return configured LLM
Loading

Poem

🐰 A Mistral Hop of Code Delight

In constants new, a model bright
Mistral joins our language dance
With config sharp and API's glance
A rabbit's leap of coding might! 🚀


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: 0

🧹 Nitpick comments (3)
backend/director/llm/mistral.py (2)

58-58: Raise original exception with additional context
In the 'except' block, consider using 'raise e from None' or 'raise e from err' to help distinguish between errors triggered by the original exception and errors resulting from handling that exception.

🧰 Tools
🪛 Ruff (0.8.2)

58-58: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


162-163: Avoid using mutable default arguments
Using 'list' as a default argument can lead to unexpected behavior from mutable objects. Replace 'tools: list = []' with 'tools: Optional[list] = None' and initialize 'tools' within the function.

🧰 Tools
🪛 Ruff (0.8.2)

162-162: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

backend/director/llm/__init__.py (1)

16-16: Use direct type conversion instead of ternary
Use 'mistral = bool(os.getenv("MISTRAL_API_KEY"))' instead of 'True if os.getenv("MISTRAL_API_KEY") else False' for conciseness and clarity.

-    mistral = True if os.getenv("MISTRAL_API_KEY") else False
+    mistral = bool(os.getenv("MISTRAL_API_KEY"))
🧰 Tools
🪛 Ruff (0.8.2)

16-16: Use bool(...) instead of True if ... else False

Replace with `bool(...)

(SIM210)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 605635b and bfef898.

📒 Files selected for processing (3)
  • backend/director/constants.py (2 hunks)
  • backend/director/llm/__init__.py (2 hunks)
  • backend/director/llm/mistral.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
backend/director/llm/__init__.py

16-16: Use bool(...) instead of True if ... else False

Replace with `bool(...)

(SIM210)

backend/director/llm/mistral.py

58-58: Within an except clause, raise exceptions with raise ... from err or raise ... from None to distinguish them from errors in exception handling

(B904)


162-162: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

🔇 Additional comments (7)
backend/director/llm/mistral.py (3)

56-56: Add the 'mistralai' dependency into requirements.txt
This comment has already been raised, but it remains relevant for this commit. Please ensure that the 'mistralai' library is listed in requirements.txt so it can be installed as a project dependency.


65-67: Remove or clarify commented-out code
As previously requested, please remove or provide justification for the commented-out lines. Keeping removed logic in comments makes the code less maintainable.


162-162: Implement 'response_format' usage
A prior review comment requested adding 'response_format' support. Currently, the parameter is declared but not used. If you intend to return the response in different formats, be sure to incorporate 'response_format' logic.

🧰 Tools
🪛 Ruff (0.8.2)

162-162: Do not use mutable data structures for argument defaults

Replace with None; initialize within function

(B006)

backend/director/constants.py (2)

22-22: Enum addition looks good
The new 'MISTRAL' member for 'LLMType' aligns with the Mistral integration.


31-31: Enum addition looks good
The new 'MISTRAL_' environment prefix is consistent with convention and matches the usage in the config class.

backend/director/llm/__init__.py (2)

24-25: Mistral LLM integration
This conditional branching correctly returns an instance of MistralAI. Ensure coverage in your unit tests to confirm the default environment logic.


7-7: Validate that the 'mistralai' import does not cause circular dependencies
While importing 'MistralAI' works here, ensure that importing this module doesn’t create circular dependencies in other parts of the codebase.

✅ Verification successful

Let me analyze the imports further to ensure there are no circular dependencies.


No circular dependencies detected with the MistralAI import

The import structure is clean and hierarchical:

  • backend/director/llm/mistral.py only imports from base modules and constants
  • backend/director/llm/__init__.py imports the concrete implementation MistralAI from the mistral module
  • No circular references are found between these modules
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Attempt to find import loops that might involve 'mistralAI'
# We'll look for import statements referencing 'mistral' or 'mistralai'
ast-grep --pattern $'import mistral$_' 
rg -A 5 mistral

Length of output: 2432


Script:

#!/bin/bash
# Check for any imports of director.llm or director.llm.* in mistral.py
rg -l "from director\.llm" backend/director/llm/mistral.py
# Check the content of mistral.py to see all its imports
cat backend/director/llm/mistral.py

Length of output: 7118

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.

3 participants