|
5 | 5 | RSpec.describe DiscourseAi::Completions::Dialects::Ollama do |
6 | 6 | fab!(:model) { Fabricate(:ollama_model) } |
7 | 7 | let(:context) { DialectContext.new(described_class, model) } |
| 8 | + let(:dialect_class) { DiscourseAi::Completions::Dialects::Dialect.dialect_for(model) } |
8 | 9 |
|
9 | 10 | describe "#translate" do |
10 | 11 | context "when native tool support is enabled" do |
|
59 | 60 | describe "#tools" do |
60 | 61 | context "when native tools are enabled" do |
61 | 62 | it "returns the translated tools from the OllamaTools class" do |
62 | | - tool = instance_double(DiscourseAi::Completions::Dialects::OllamaTools) |
| 63 | + model.update!(provider_params: { enable_native_tool: true }) |
63 | 64 |
|
64 | | - allow(model).to receive(:lookup_custom_param).with("enable_native_tool").and_return(true) |
65 | | - allow(tool).to receive(:translated_tools) |
66 | | - allow(DiscourseAi::Completions::Dialects::OllamaTools).to receive(:new).and_return(tool) |
67 | | - |
68 | | - context.dialect_tools |
69 | | - |
70 | | - expect(DiscourseAi::Completions::Dialects::OllamaTools).to have_received(:new).with( |
71 | | - context.prompt.tools, |
72 | | - ) |
73 | | - expect(tool).to have_received(:translated_tools) |
| 65 | + tool = { name: "noop", description: "do nothing" } |
| 66 | + messages = [ |
| 67 | + { type: :user, content: "echo away" }, |
| 68 | + { type: :tool_call, content: "{}", name: "noop" }, |
| 69 | + { type: :tool, content: "{}", name: "noop" }, |
| 70 | + ] |
| 71 | + prompt = DiscourseAi::Completions::Prompt.new("a bot", tools: [tool], messages: messages) |
| 72 | + dialect = dialect_class.new(prompt, model) |
| 73 | + |
| 74 | + expected = [ |
| 75 | + { role: "system", content: "a bot" }, |
| 76 | + { role: "user", content: "echo away" }, |
| 77 | + { |
| 78 | + role: "assistant", |
| 79 | + content: nil, |
| 80 | + tool_calls: [{ type: "function", function: { name: "noop" } }], |
| 81 | + }, |
| 82 | + { role: "tool", content: "{}", name: "noop" }, |
| 83 | + ] |
| 84 | + expect(dialect.translate).to eq(expected) |
74 | 85 | end |
75 | 86 | end |
76 | 87 |
|
77 | 88 | context "when native tools are disabled" do |
78 | 89 | it "returns the translated tools from the XmlTools class" do |
79 | | - tool = instance_double(DiscourseAi::Completions::Dialects::XmlTools) |
| 90 | + model.update!(provider_params: { enable_native_tool: false }) |
80 | 91 |
|
81 | | - allow(model).to receive(:lookup_custom_param).with("enable_native_tool").and_return(false) |
82 | | - allow(tool).to receive(:translated_tools) |
83 | | - allow(DiscourseAi::Completions::Dialects::XmlTools).to receive(:new).and_return(tool) |
| 92 | + tool = { name: "noop", description: "do nothing" } |
| 93 | + messages = [ |
| 94 | + { type: :user, content: "echo away" }, |
| 95 | + { type: :tool_call, content: "{}", name: "noop" }, |
| 96 | + { type: :tool, content: "{}", name: "noop" }, |
| 97 | + ] |
| 98 | + prompt = DiscourseAi::Completions::Prompt.new("a bot", tools: [tool], messages: messages) |
| 99 | + dialect = dialect_class.new(prompt, model) |
84 | 100 |
|
85 | | - context.dialect_tools |
| 101 | + expected = %w[system user assistant user] |
| 102 | + roles = dialect.translate.map { |x| x[:role] } |
86 | 103 |
|
87 | | - expect(DiscourseAi::Completions::Dialects::XmlTools).to have_received(:new).with( |
88 | | - context.prompt.tools, |
89 | | - ) |
90 | | - expect(tool).to have_received(:translated_tools) |
| 104 | + # notice, no tool role |
| 105 | + expect(roles).to eq(expected) |
91 | 106 | end |
92 | 107 | end |
93 | 108 | end |
|
0 commit comments