Skip to content

Commit

Permalink
Add optional parameter to limit how long handleToolCalls can loop aut…
Browse files Browse the repository at this point in the history
…onomously
  • Loading branch information
imclerran committed Jan 6, 2025
1 parent 79531fa commit 41ff947
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
2 changes: 1 addition & 1 deletion examples/roc-agent.roc
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ main =
Stdout.write! "You: "
messages = Chat.appendUserMessage previousMessages Stdin.line! {}
response = Http.send (Chat.buildHttpRequest client messages {}) |> Task.result!
updatedMessages = Chat.updateMessageList response messages |> Tools.handleToolCalls! client toolHandlerMap
updatedMessages = Chat.updateMessageList response messages |> Tools.handleToolCalls! client toolHandlerMap { maxModelCalls: 10 }
printLastMessage! updatedMessages
Task.ok (Step { previousMessages: updatedMessages })

Expand Down
12 changes: 7 additions & 5 deletions package/Tools.roc
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ Message : {
## Using the given toolHandlerMap, check the last message for tool calls, call all the tools in the tool call list, send the results back to the model, and handle any additional tool calls that may have been generated. If or when no more tool calls are present, return the updated list of messages.
##
## The Dict maps function tool names strings to roc functions that take their arguments as a JSON string, parse the json, and return the tool's response.
handleToolCalls : List Message, Client, Dict Str (Str -> Task Str _) -> Task (List Message) _
handleToolCalls = \messages, client, toolHandlerMap ->
handleToolCalls : List Message, Client, Dict Str (Str -> Task Str _), { maxModelCalls ? U32 } -> Task (List Message) _
handleToolCalls = \messages, client, toolHandlerMap, { maxModelCalls ? Num.maxU32 } ->
when List.last messages is
Ok { role, toolCalls } if role == "assistant" ->
if List.isEmpty toolCalls then
if List.isEmpty toolCalls || maxModelCalls == 0 then
Task.ok messages
else
# requestClient = if maxModelCalls == 1 then Client.setTools client [] else client
tc = if maxModelCalls > 1 then { toolChoice: Auto } else { toolChoice: None }
toolMessages = dispatchToolCalls! toolCalls toolHandlerMap
messagesWithTools = List.join [messages, toolMessages]
response = sendHttpReq (Chat.buildHttpRequest client messagesWithTools {}) |> Task.result!
response = sendHttpReq (Chat.buildHttpRequest client messagesWithTools tc) |> Task.result!
messagesWithResponse = Chat.updateMessageList response messagesWithTools
handleToolCalls messagesWithResponse client toolHandlerMap
handleToolCalls messagesWithResponse client toolHandlerMap { maxModelCalls: maxModelCalls - 1 }

_ -> Task.ok messages

Expand Down

0 comments on commit 41ff947

Please sign in to comment.