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

Commit 527ab2b

Browse files
committed
support XML tools which work better for Google
1 parent 099aec0 commit 527ab2b

File tree

3 files changed

+44
-25
lines changed

3 files changed

+44
-25
lines changed

app/models/llm_model.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ def self.provider_params
2727
open_ai: {
2828
organization: :text,
2929
},
30+
google: {
31+
disable_native_tools: :checkbox,
32+
},
3033
hugging_face: {
3134
disable_system_prompt: :checkbox,
3235
},

lib/completions/dialects/gemini.rb

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def can_translate?(model_provider)
1111
end
1212

1313
def native_tool_support?
14-
true
14+
!llm_model.lookup_custom_param("disable_native_tools")
1515
end
1616

1717
def translate
@@ -84,10 +84,16 @@ def beta_api?
8484
end
8585

8686
def system_msg(msg)
87+
content = msg[:content]
88+
89+
if !native_tool_support? && tools_dialect.instructions.present?
90+
content = content.to_s + "\n\n#{tools_dialect.instructions}"
91+
end
92+
8793
if beta_api?
88-
{ role: "system", content: msg[:content] }
94+
{ role: "system", content: content }
8995
else
90-
{ role: "user", parts: { text: msg[:content] } }
96+
{ role: "user", parts: { text: content } }
9197
end
9298
end
9399

@@ -125,35 +131,43 @@ def uploaded_parts(message)
125131
end
126132

127133
def tool_call_msg(msg)
128-
call_details = JSON.parse(msg[:content], symbolize_names: true)
129-
part = {
130-
functionCall: {
131-
name: msg[:name] || call_details[:name],
132-
args: call_details[:arguments],
133-
},
134-
}
134+
if native_tool_support?
135+
call_details = JSON.parse(msg[:content], symbolize_names: true)
136+
part = {
137+
functionCall: {
138+
name: msg[:name] || call_details[:name],
139+
args: call_details[:arguments],
140+
},
141+
}
135142

136-
if beta_api?
137-
{ role: "model", parts: [part] }
143+
if beta_api?
144+
{ role: "model", parts: [part] }
145+
else
146+
{ role: "model", parts: part }
147+
end
138148
else
139-
{ role: "model", parts: part }
149+
super
140150
end
141151
end
142152

143153
def tool_msg(msg)
144-
part = {
145-
functionResponse: {
146-
name: msg[:name] || msg[:id],
147-
response: {
148-
content: msg[:content],
154+
if native_tool_support?
155+
part = {
156+
functionResponse: {
157+
name: msg[:name] || msg[:id],
158+
response: {
159+
content: msg[:content],
160+
},
149161
},
150-
},
151-
}
162+
}
152163

153-
if beta_api?
154-
{ role: "function", parts: [part] }
164+
if beta_api?
165+
{ role: "function", parts: [part] }
166+
else
167+
{ role: "function", parts: part }
168+
end
155169
else
156-
{ role: "function", parts: part }
170+
super
157171
end
158172
end
159173
end

lib/completions/endpoints/gemini.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ def model_uri
5858
end
5959

6060
def prepare_payload(prompt, model_params, dialect)
61-
tools = dialect.tools
61+
@native_tool_support = dialect.native_tool_support?
62+
63+
tools = dialect.tools if @native_tool_support
6264

6365
payload = default_options.merge(contents: prompt[:messages])
6466
payload[:systemInstruction] = {
@@ -222,7 +224,7 @@ def extract_prompt_for_tokenizer(prompt)
222224
end
223225

224226
def xml_tools_enabled?
225-
false
227+
!@native_tool_support
226228
end
227229
end
228230
end

0 commit comments

Comments
 (0)