2023-11-23 10:58:54 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-01-12 12:36:44 -05:00
|
|
|
RSpec.describe DiscourseAi::Completions::Dialects::Claude do
|
2024-06-07 09:52:01 -04:00
|
|
|
let :opus_dialect_klass do
|
2024-07-30 12:44:57 -04:00
|
|
|
DiscourseAi::Completions::Dialects::Dialect.dialect_for("anthropic")
|
2024-06-07 09:52:01 -04:00
|
|
|
end
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
fab!(:llm_model) { Fabricate(:anthropic_model, name: "claude-3-opus") }
|
|
|
|
|
2023-11-23 10:58:54 -05:00
|
|
|
describe "#translate" do
|
2024-03-18 15:48:46 -04:00
|
|
|
it "can insert OKs to make stuff interleve properly" do
|
|
|
|
messages = [
|
|
|
|
{ type: :user, id: "user1", content: "1" },
|
|
|
|
{ type: :model, content: "2" },
|
|
|
|
{ type: :user, id: "user1", content: "4" },
|
|
|
|
{ type: :user, id: "user1", content: "5" },
|
|
|
|
{ type: :model, content: "6" },
|
|
|
|
]
|
|
|
|
|
|
|
|
prompt = DiscourseAi::Completions::Prompt.new("You are a helpful bot", messages: messages)
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
dialect = opus_dialect_klass.new(prompt, llm_model)
|
2024-03-18 15:48:46 -04:00
|
|
|
translated = dialect.translate
|
|
|
|
|
|
|
|
expected_messages = [
|
|
|
|
{ role: "user", content: "user1: 1" },
|
|
|
|
{ role: "assistant", content: "2" },
|
|
|
|
{ role: "user", content: "user1: 4" },
|
|
|
|
{ role: "assistant", content: "OK" },
|
|
|
|
{ role: "user", content: "user1: 5" },
|
|
|
|
{ role: "assistant", content: "6" },
|
|
|
|
]
|
|
|
|
|
|
|
|
expect(translated.messages).to eq(expected_messages)
|
2023-12-18 16:06:01 -05:00
|
|
|
end
|
2024-01-10 23:56:40 -05:00
|
|
|
|
2024-06-07 09:52:01 -04:00
|
|
|
it "can properly translate a prompt (legacy tools)" do
|
|
|
|
SiteSetting.ai_anthropic_native_tool_call_models = ""
|
2024-01-15 21:48:00 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
tools = [
|
|
|
|
{
|
|
|
|
name: "echo",
|
|
|
|
description: "echo a string",
|
|
|
|
parameters: [
|
|
|
|
{ name: "text", type: "string", description: "string to echo", required: true },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
tool_call_prompt = { name: "echo", arguments: { text: "something" } }
|
2023-12-18 16:06:01 -05:00
|
|
|
|
2024-03-18 15:48:46 -04:00
|
|
|
messages = [
|
|
|
|
{ type: :user, id: "user1", content: "echo something" },
|
2024-06-05 18:34:23 -04:00
|
|
|
{ type: :tool_call, name: "echo", id: "tool_id", content: tool_call_prompt.to_json },
|
2024-03-18 15:48:46 -04:00
|
|
|
{ type: :tool, id: "tool_id", content: "something".to_json },
|
|
|
|
{ type: :model, content: "I did it" },
|
|
|
|
{ type: :user, id: "user1", content: "echo something else" },
|
|
|
|
]
|
2024-01-15 21:48:00 -05:00
|
|
|
|
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
2024-03-18 15:48:46 -04:00
|
|
|
"You are a helpful bot",
|
|
|
|
messages: messages,
|
|
|
|
tools: tools,
|
2024-01-15 21:48:00 -05:00
|
|
|
)
|
|
|
|
|
2024-07-30 12:44:57 -04:00
|
|
|
dialect = opus_dialect_klass.new(prompt, llm_model)
|
2024-06-07 09:52:01 -04:00
|
|
|
translated = dialect.translate
|
|
|
|
|
|
|
|
expect(translated.system_prompt).to start_with("You are a helpful bot")
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
{ role: "user", content: "user1: echo something" },
|
|
|
|
{
|
|
|
|
role: "assistant",
|
|
|
|
content:
|
|
|
|
"<function_calls>\n<invoke>\n<tool_name>echo</tool_name>\n<parameters>\n<text>something</text>\n</parameters>\n</invoke>\n</function_calls>",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "user",
|
|
|
|
content:
|
|
|
|
"<function_results>\n<result>\n<tool_name>tool_id</tool_name>\n<json>\n\"something\"\n</json>\n</result>\n</function_results>",
|
|
|
|
},
|
|
|
|
{ role: "assistant", content: "I did it" },
|
|
|
|
{ role: "user", content: "user1: echo something else" },
|
|
|
|
]
|
|
|
|
expect(translated.messages).to eq(expected)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can properly translate a prompt (native tools)" do
|
|
|
|
SiteSetting.ai_anthropic_native_tool_call_models = "claude-3-opus"
|
|
|
|
|
|
|
|
tools = [
|
|
|
|
{
|
|
|
|
name: "echo",
|
|
|
|
description: "echo a string",
|
|
|
|
parameters: [
|
|
|
|
{ name: "text", type: "string", description: "string to echo", required: true },
|
|
|
|
],
|
|
|
|
},
|
|
|
|
]
|
|
|
|
|
|
|
|
tool_call_prompt = { name: "echo", arguments: { text: "something" } }
|
|
|
|
|
|
|
|
messages = [
|
|
|
|
{ type: :user, id: "user1", content: "echo something" },
|
|
|
|
{ type: :tool_call, name: "echo", id: "tool_id", content: tool_call_prompt.to_json },
|
|
|
|
{ type: :tool, id: "tool_id", content: "something".to_json },
|
|
|
|
{ type: :model, content: "I did it" },
|
|
|
|
{ type: :user, id: "user1", content: "echo something else" },
|
|
|
|
]
|
|
|
|
|
|
|
|
prompt =
|
|
|
|
DiscourseAi::Completions::Prompt.new(
|
|
|
|
"You are a helpful bot",
|
|
|
|
messages: messages,
|
|
|
|
tools: tools,
|
|
|
|
)
|
2024-07-30 12:44:57 -04:00
|
|
|
dialect = opus_dialect_klass.new(prompt, llm_model)
|
2024-03-18 15:48:46 -04:00
|
|
|
translated = dialect.translate
|
|
|
|
|
|
|
|
expect(translated.system_prompt).to start_with("You are a helpful bot")
|
|
|
|
|
|
|
|
expected = [
|
|
|
|
{ role: "user", content: "user1: echo something" },
|
|
|
|
{
|
|
|
|
role: "assistant",
|
2024-06-05 18:34:23 -04:00
|
|
|
content: [
|
|
|
|
{ type: "tool_use", id: "tool_id", name: "echo", input: { text: "something" } },
|
|
|
|
],
|
2024-03-18 15:48:46 -04:00
|
|
|
},
|
|
|
|
{
|
|
|
|
role: "user",
|
2024-06-05 18:34:23 -04:00
|
|
|
content: [{ type: "tool_result", tool_use_id: "tool_id", content: "\"something\"" }],
|
2024-03-18 15:48:46 -04:00
|
|
|
},
|
|
|
|
{ role: "assistant", content: "I did it" },
|
|
|
|
{ role: "user", content: "user1: echo something else" },
|
|
|
|
]
|
|
|
|
expect(translated.messages).to eq(expected)
|
2024-01-15 21:48:00 -05:00
|
|
|
end
|
2023-11-23 10:58:54 -05:00
|
|
|
end
|
|
|
|
end
|