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

Commit 9929513

Browse files
committed
cleaner chain breaker for last tool call (works in thinking)
remove unused code
1 parent 8158c9a commit 9929513

File tree

2 files changed

+51
-24
lines changed

2 files changed

+51
-24
lines changed

lib/completions/dialects/dialect.rb

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ def initialize(generic_prompt, llm_model, opts: {})
4646

4747
VALID_ID_REGEX = /\A[a-zA-Z0-9_]+\z/
4848

49-
def can_end_with_assistant_msg?
50-
false
51-
end
52-
5349
def native_tool_support?
5450
false
5551
end
@@ -71,34 +67,51 @@ def self.no_more_tool_calls_text
7167
"I WILL NOT USE TOOLS IN THIS REPLY, user expressed they wanted to stop using tool calls.\nHere is the best, complete, answer I can come up with given the information I have."
7268
end
7369

70+
def self.no_more_tool_calls_text_user
71+
"DO NOT USE TOOLS IN YOUR REPLY. Return the best answer you can given the information I supplied you."
72+
end
73+
7474
def no_more_tool_calls_text
7575
self.class.no_more_tool_calls_text
7676
end
7777

78+
def no_more_tool_calls_text_user
79+
self.class.no_more_tool_calls_text_user
80+
end
81+
7882
def translate
79-
messages = prompt.messages
83+
messages = trim_messages(prompt.messages)
84+
last_message = messages.last
85+
inject_done_on_last_tool_call = false
8086

81-
# Some models use an assistant msg to improve long-context responses.
82-
if messages.last[:type] == :model && can_end_with_assistant_msg?
83-
messages = messages.dup
84-
messages.pop
87+
if !native_tool_support? && last_message && last_message[:type].to_sym == :tool &&
88+
prompt.tool_choice == :none
89+
inject_done_on_last_tool_call = true
8590
end
8691

87-
translated = trim_messages(messages).map { |msg| send("#{msg[:type]}_msg", msg) }.compact
88-
89-
if !native_tool_support?
90-
if prompt.tools.present? && prompt.tool_choice.present?
91-
if prompt.tool_choice == :none
92-
translated << model_msg(role: "assistant", content: no_more_tool_calls_text)
93-
else
94-
translated << model_msg(
95-
role: "assistant",
96-
content:
97-
"User required I call the tool: #{prompt.tool_choice} I will make sure I use it now:",
98-
)
92+
translated =
93+
messages
94+
.map do |msg|
95+
case msg[:type].to_sym
96+
when :system
97+
system_msg(msg)
98+
when :user
99+
user_msg(msg)
100+
when :model
101+
model_msg(msg)
102+
when :tool
103+
if inject_done_on_last_tool_call && msg == last_message
104+
tools_dialect.inject_done { tool_msg(msg) }
105+
else
106+
tool_msg(msg)
107+
end
108+
when :tool_call
109+
tool_call_msg(msg)
110+
else
111+
raise ArgumentError, "Unknown message type: #{msg[:type]}"
112+
end
99113
end
100-
end
101-
end
114+
.compact
102115

103116
translated
104117
end

lib/completions/dialects/xml_tools.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def instructions
5555
end
5656

5757
def from_raw_tool(raw_message)
58-
(<<~TEXT).strip
58+
result = (<<~TEXT).strip
5959
<function_results>
6060
<result>
6161
<tool_name>#{raw_message[:name] || raw_message[:id]}</tool_name>
@@ -65,6 +65,13 @@ def from_raw_tool(raw_message)
6565
</result>
6666
</function_results>
6767
TEXT
68+
69+
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!"
72+
else
73+
result
74+
end
6875
end
6976

7077
def from_raw_tool_call(raw_message)
@@ -86,6 +93,13 @@ def from_raw_tool_call(raw_message)
8693
TEXT
8794
end
8895

96+
def inject_done(&blk)
97+
@injecting_done = true
98+
blk.call
99+
ensure
100+
@injecting_done = false
101+
end
102+
89103
private
90104

91105
attr_reader :raw_tools

0 commit comments

Comments
 (0)