|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "rails_helper" |
| 4 | +require_relative "dialect_context" |
| 5 | + |
| 6 | +RSpec.describe DiscourseAi::Completions::Dialects::Mistral do |
| 7 | + fab!(:model) { Fabricate(:mistral_model) } |
| 8 | + let(:context) { DialectContext.new(described_class, model) } |
| 9 | + let(:image100x100) { plugin_file_from_fixtures("100x100.jpg") } |
| 10 | + let(:upload100x100) do |
| 11 | + UploadCreator.new(image100x100, "image.jpg").create_for(Discourse.system_user.id) |
| 12 | + end |
| 13 | + |
| 14 | + it "does not include user names" do |
| 15 | + prompt = |
| 16 | + DiscourseAi::Completions::Prompt.new( |
| 17 | + messages: [type: :user, content: "Hello, I am Bob", id: "bob"], |
| 18 | + ) |
| 19 | + |
| 20 | + dialect = described_class.new(prompt, model) |
| 21 | + |
| 22 | + # mistral has no support for name |
| 23 | + expect(dialect.translate).to eq([{ role: "user", content: "bob: Hello, I am Bob" }]) |
| 24 | + end |
| 25 | + |
| 26 | + it "can properly encode images" do |
| 27 | + model.update!(vision_enabled: true) |
| 28 | + |
| 29 | + prompt = |
| 30 | + DiscourseAi::Completions::Prompt.new( |
| 31 | + "You are image bot", |
| 32 | + messages: [type: :user, id: "user1", content: "hello", upload_ids: [upload100x100.id]], |
| 33 | + ) |
| 34 | + |
| 35 | + encoded = prompt.encoded_uploads(prompt.messages.last) |
| 36 | + |
| 37 | + image = "data:image/jpeg;base64,#{encoded[0][:base64]}" |
| 38 | + |
| 39 | + dialect = described_class.new(prompt, model) |
| 40 | + |
| 41 | + content = dialect.translate[1][:content] |
| 42 | + |
| 43 | + expect(content).to eq( |
| 44 | + [{ type: "image_url", image_url: { url: image } }, { type: "text", text: "user1: hello" }], |
| 45 | + ) |
| 46 | + end |
| 47 | + |
| 48 | + it "can properly map tool calls to mistral format" do |
| 49 | + result = [ |
| 50 | + { |
| 51 | + role: "system", |
| 52 | + content: |
| 53 | + "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", |
| 54 | + }, |
| 55 | + { role: "user", content: "user1: This is a message by a user" }, |
| 56 | + { role: "assistant", content: "I'm a previous bot reply, that's why there's no user" }, |
| 57 | + { role: "user", content: "user1: This is a new message by a user" }, |
| 58 | + { |
| 59 | + role: "assistant", |
| 60 | + content: "", |
| 61 | + tool_calls: [ |
| 62 | + { |
| 63 | + type: "function", |
| 64 | + function: { |
| 65 | + arguments: "{\"location\":\"Sydney\",\"unit\":\"c\"}", |
| 66 | + name: "get_weather", |
| 67 | + }, |
| 68 | + id: "tool_id", |
| 69 | + }, |
| 70 | + ], |
| 71 | + }, |
| 72 | + { |
| 73 | + role: "tool", |
| 74 | + tool_call_id: "tool_id", |
| 75 | + content: "\"I'm a tool result\"", |
| 76 | + name: "get_weather", |
| 77 | + }, |
| 78 | + ] |
| 79 | + expect(context.multi_turn_scenario).to eq(result) |
| 80 | + |
| 81 | + bang |
| 82 | + end |
| 83 | +end |
0 commit comments