Skip to content

Commit 6e728ab

Browse files
authored
Multi-turn tool fix
1 parent 06fcd59 commit 6e728ab

File tree

4 files changed

+31
-6
lines changed

4 files changed

+31
-6
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Fixed
1212

13+
## [0.59.1]
14+
15+
### Fixed
16+
- Fixed a bug in multi-turn tool calls for OpenAI models where an empty tools array could have been, which causes an API error.
17+
1318
## [0.59.0]
1419

1520
### Breaking Changes

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PromptingTools"
22
uuid = "670122d1-24a8-4d70-bfce-740807c42192"
33
authors = ["J S @svilupp and contributors"]
4-
version = "0.59.0"
4+
version = "0.59.1"
55

66
[deps]
77
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"

src/llm_openai.jl

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,17 @@ function render(schema::AbstractOpenAISchema,
5656
end
5757
Dict("role" => role4render(schema, msg), "content" => content)
5858
elseif isaitoolrequest(msg)
59-
Dict("role" => role4render(schema, msg), "content" => msg.content,
60-
"tool_calls" => [Dict("id" => tool.tool_call_id, "type" => "function",
61-
"function" => Dict("name" => tool.name,
62-
"arguments" => tool.raw))
63-
for tool in msg.tool_calls])
59+
output = Dict{String, Any}(
60+
"role" => role4render(schema, msg),
61+
"content" => msg.content)
62+
if !isempty(msg.tool_calls)
63+
output["tool_calls"] = [Dict("id" => tool.tool_call_id,
64+
"type" => "function",
65+
"function" => Dict("name" => tool.name,
66+
"arguments" => tool.raw))
67+
for tool in msg.tool_calls]
68+
end
69+
output
6470
elseif istoolmessage(msg)
6571
content = msg.content isa AbstractString ? msg.content : string(msg.content)
6672
Dict("role" => role4render(schema, msg), "content" => content,

test/llm_openai.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,20 @@ using PromptingTools: pick_tokenizer, OPENAI_TOKEN_IDS_GPT35_GPT4, OPENAI_TOKEN_
196196
]
197197
@test conversation == expected_output
198198

199+
# With empty tools
200+
messages = [
201+
SystemMessage("System message"),
202+
UserMessage("User message"),
203+
AIToolRequest(;content="content")
204+
]
205+
conversation = render(schema, messages)
206+
expected_output = Dict{String, Any}[
207+
Dict("role" => "system", "content" => "System message"),
208+
Dict("role" => "user", "content" => "User message"),
209+
Dict("role" => "assistant", "content" => "content")
210+
]
211+
@test conversation == expected_output
212+
199213
# With a list of images and detail="low"
200214
messages = [
201215
SystemMessage("System message 2"),

0 commit comments

Comments
 (0)