Skip to content
This repository was archived by the owner on Jul 22, 2025. It is now read-only.

Commit 42b0f8a

Browse files
committed
improve test
1 parent 9929513 commit 42b0f8a

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/completions/dialects/xml_tools.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def instructions
5454
end
5555
end
5656

57+
DONE_MESSAGE =
58+
"Regardless of what you think, REPLY IMMEDIATELY, WITHOUT MAKING ANY FURTHER TOOL CALLS, YOU ARE OUT OF TOOL CALL QUOTA!"
59+
5760
def from_raw_tool(raw_message)
5861
result = (<<~TEXT).strip
5962
<function_results>
@@ -67,8 +70,7 @@ def from_raw_tool(raw_message)
6770
TEXT
6871

6972
if @injecting_done
70-
result +
71-
"\n\nRegardless of what you think, REPLY IMMEDIATELY, WITHOUT MAKING ANY FURTHER TOOL CALLS, YOU ARE OUT OF TOOL CALL QUOTA!"
73+
"#{result}\n\n#{DONE_MESSAGE}"
7274
else
7375
result
7476
end

spec/lib/completions/dialects/dialect_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ def trim(messages)
77
trim_messages(messages)
88
end
99

10+
def system_msg(msg)
11+
msg
12+
end
13+
14+
def user_msg(msg)
15+
msg
16+
end
17+
18+
def model_msg(msg)
19+
msg
20+
end
21+
1022
def tokenizer
1123
DiscourseAi::Tokenizer::OpenAiTokenizer
1224
end
@@ -15,6 +27,57 @@ def tokenizer
1527
RSpec.describe DiscourseAi::Completions::Dialects::Dialect do
1628
fab!(:llm_model)
1729

30+
describe "#translate" do
31+
let(:five_token_msg) { "This represents five tokens." }
32+
let(:tools) do
33+
[
34+
{
35+
name: "echo",
36+
description: "echo a string",
37+
parameters: [
38+
{ name: "text", type: "string", description: "string to echo", required: true },
39+
],
40+
},
41+
]
42+
end
43+
44+
it "injects done message when tool_choice is :none and last message follows tool pattern" do
45+
tool_call_prompt = { name: "echo", arguments: { text: "test message" } }
46+
47+
prompt = DiscourseAi::Completions::Prompt.new("System instructions", tools: tools)
48+
prompt.push(type: :user, content: "echo test message")
49+
prompt.push(type: :tool_call, content: tool_call_prompt.to_json, id: "123", name: "echo")
50+
prompt.push(type: :tool, content: "test message".to_json, name: "echo", id: "123")
51+
prompt.tool_choice = :none
52+
53+
dialect = TestDialect.new(prompt, llm_model)
54+
dialect.max_prompt_tokens = 100 # Set high enough to avoid trimming
55+
56+
translated = dialect.translate
57+
58+
expect(translated).to eq(
59+
[
60+
{ type: :system, content: "System instructions" },
61+
{ type: :user, content: "echo test message" },
62+
{
63+
type: :tool_call,
64+
content:
65+
"<function_calls>\n<invoke>\n<tool_name>echo</tool_name>\n<parameters>\n<text>test message</text>\n</parameters>\n</invoke>\n</function_calls>",
66+
id: "123",
67+
name: "echo",
68+
},
69+
{
70+
type: :tool,
71+
id: "123",
72+
name: "echo",
73+
content:
74+
"<function_results>\n<result>\n<tool_name>echo</tool_name>\n<json>\n\"test message\"\n</json>\n</result>\n</function_results>\n\n#{::DiscourseAi::Completions::Dialects::XmlTools::DONE_MESSAGE}",
75+
},
76+
],
77+
)
78+
end
79+
end
80+
1881
describe "#trim_messages" do
1982
let(:five_token_msg) { "This represents five tokens." }
2083

0 commit comments

Comments
 (0)