This repository was archived by the owner on Jul 22, 2025. It is now read-only.
generated from discourse/discourse-plugin-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 40
FEATURE: Add support for Mistral models #919
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # basically the same as Open AI, except for no support for user names | ||
|
|
||
| module DiscourseAi | ||
| module Completions | ||
| module Dialects | ||
| class Mistral < ChatGpt | ||
| class << self | ||
| def can_translate?(model_provider) | ||
| model_provider == "mistral" | ||
| end | ||
| end | ||
|
|
||
| def translate | ||
| corrected = super | ||
| corrected.each do |msg| | ||
| msg[:content] = "" if msg[:tool_calls] && msg[:role] == "assistant" | ||
| end | ||
| corrected | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def user_msg(msg) | ||
| mapped = super | ||
| if name = mapped.delete(:name) | ||
| if mapped[:content].is_a?(String) | ||
| mapped[:content] = "#{name}: #{mapped[:content]}" | ||
| else | ||
| mapped[:content].each do |inner| | ||
| if inner[:text] | ||
| inner[:text] = "#{name}: #{inner[:text]}" | ||
| break | ||
| end | ||
| end | ||
| end | ||
| end | ||
| mapped | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module DiscourseAi | ||
| module Completions | ||
| module Endpoints | ||
| class Mistral < OpenAi | ||
| def self.can_contact?(model_provider) | ||
| model_provider == "mistral" | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "rails_helper" | ||
| require_relative "dialect_context" | ||
|
|
||
| RSpec.describe DiscourseAi::Completions::Dialects::Mistral do | ||
| fab!(:model) { Fabricate(:mistral_model) } | ||
| let(:context) { DialectContext.new(described_class, model) } | ||
| let(:image100x100) { plugin_file_from_fixtures("100x100.jpg") } | ||
| let(:upload100x100) do | ||
| UploadCreator.new(image100x100, "image.jpg").create_for(Discourse.system_user.id) | ||
| end | ||
|
|
||
| it "does not include user names" do | ||
| prompt = | ||
| DiscourseAi::Completions::Prompt.new( | ||
| messages: [type: :user, content: "Hello, I am Bob", id: "bob"], | ||
| ) | ||
|
|
||
| dialect = described_class.new(prompt, model) | ||
|
|
||
| # mistral has no support for name | ||
| expect(dialect.translate).to eq([{ role: "user", content: "bob: Hello, I am Bob" }]) | ||
| end | ||
|
|
||
| it "can properly encode images" do | ||
| model.update!(vision_enabled: true) | ||
|
|
||
| prompt = | ||
| DiscourseAi::Completions::Prompt.new( | ||
| "You are image bot", | ||
| messages: [type: :user, id: "user1", content: "hello", upload_ids: [upload100x100.id]], | ||
| ) | ||
|
|
||
| encoded = prompt.encoded_uploads(prompt.messages.last) | ||
|
|
||
| image = "data:image/jpeg;base64,#{encoded[0][:base64]}" | ||
|
|
||
| dialect = described_class.new(prompt, model) | ||
|
|
||
| content = dialect.translate[1][:content] | ||
|
|
||
| expect(content).to eq( | ||
| [{ type: "image_url", image_url: { url: image } }, { type: "text", text: "user1: hello" }], | ||
| ) | ||
| end | ||
|
|
||
| it "can properly map tool calls to mistral format" do | ||
| result = [ | ||
| { | ||
| role: "system", | ||
| content: | ||
| "I want you to act as a title generator for written pieces. I will provide you with a text,\nand you will generate five attention-grabbing titles. Please keep the title concise and under 20 words,\nand ensure that the meaning is maintained. Replies will utilize the language type of the topic.\n", | ||
| }, | ||
| { role: "user", content: "user1: This is a message by a user" }, | ||
| { role: "assistant", content: "I'm a previous bot reply, that's why there's no user" }, | ||
| { role: "user", content: "user1: This is a new message by a user" }, | ||
| { | ||
| role: "assistant", | ||
| content: "", | ||
| tool_calls: [ | ||
| { | ||
| type: "function", | ||
| function: { | ||
| arguments: "{\"location\":\"Sydney\",\"unit\":\"c\"}", | ||
| name: "get_weather", | ||
| }, | ||
| id: "tool_id", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: "tool", | ||
| tool_call_id: "tool_id", | ||
| content: "\"I'm a tool result\"", | ||
| name: "get_weather", | ||
| }, | ||
| ] | ||
| expect(context.multi_turn_scenario).to eq(result) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the
provider_idbe overridden? As-is, this currently returns the one for OpenAi.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh sure ... good catch will fix