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

Commit d4608d7

Browse files
committed
add function call encoding tests
1 parent 0c85a37 commit d4608d7

File tree

4 files changed

+33
-5
lines changed

4 files changed

+33
-5
lines changed

lib/completions/endpoints/gemini.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def add_to_function_buffer(function_buffer, payload: nil, partial: nil)
179179
if partial[:args]
180180
argument_fragments =
181181
partial[:args].reduce(+"") do |memo, (arg_name, value)|
182-
memo << "\n<#{arg_name}>#{value}</#{arg_name}>"
182+
memo << "\n<#{arg_name}>#{CGI.escapeHTML(value)}</#{arg_name}>"
183183
end
184184
argument_fragments << "\n"
185185

spec/lib/completions/endpoints/anthropic_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"y\\": \\"s"} }
7575
7676
event: content_block_delta
77-
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"am"} }
77+
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":"<a>m"} }
7878
7979
event: content_block_delta
8080
data: {"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":" "} }
@@ -118,7 +118,7 @@
118118
<function_calls>
119119
<invoke>
120120
<tool_name>search</tool_name>
121-
<parameters><search_query>sam sam</search_query>
121+
<parameters><search_query>s&lt;a&gt;m sam</search_query>
122122
<category>general</category></parameters>
123123
<tool_id>toolu_01DjrShFRRHp9SnHYRFRc53F</tool_id>
124124
</invoke>

spec/lib/completions/endpoints/gemini_spec.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,34 @@ def tool_response
182182
expect(parsed[:tool_config]).to eq({ function_calling_config: { mode: "AUTO" } })
183183
end
184184

185+
it "properly encodes tool calls" do
186+
prompt = DiscourseAi::Completions::Prompt.new("Hello", tools: [echo_tool])
187+
188+
llm = DiscourseAi::Completions::Llm.proxy("custom:#{model.id}")
189+
url = "#{model.url}:generateContent?key=123"
190+
191+
response_json = { "functionCall" => { name: "echo", args: {text: "<S>ydney" } } }
192+
response = gemini_mock.response(response_json, tool_call: true).to_json
193+
194+
stub_request(:post, url).to_return(status: 200, body: response)
195+
196+
response = llm.generate(prompt, user: user)
197+
198+
expected = (<<~XML).strip
199+
<function_calls>
200+
<invoke>
201+
<tool_name>echo</tool_name>
202+
<parameters>
203+
<text>&lt;S&gt;ydney</text>
204+
</parameters>
205+
<tool_id>tool_0</tool_id>
206+
</invoke>
207+
</function_calls>
208+
XML
209+
210+
expect(response.strip).to eq(expected)
211+
end
212+
185213
it "Supports Vision API" do
186214
prompt =
187215
DiscourseAi::Completions::Prompt.new(

spec/lib/completions/endpoints/open_ai_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ def request_body(prompt, stream: false, tool_call: false)
294294
type: "function",
295295
function: {
296296
name: "echo",
297-
arguments: "{\"text\":\"hello\"}",
297+
arguments: "{\"text\":\"h<e>llo\"}",
298298
},
299299
},
300300
],
@@ -325,7 +325,7 @@ def request_body(prompt, stream: false, tool_call: false)
325325
<invoke>
326326
<tool_name>echo</tool_name>
327327
<parameters>
328-
<text>hello</text>
328+
<text>h&lt;e&gt;llo</text>
329329
</parameters>
330330
<tool_id>call_I8LKnoijVuhKOM85nnEQgWwd</tool_id>
331331
</invoke>

0 commit comments

Comments
 (0)