From 41ff947c6e41fdb4ed7ea61a12c093bea40f706f Mon Sep 17 00:00:00 2001 From: Ian McLerran Date: Mon, 6 Jan 2025 11:31:29 -0600 Subject: [PATCH] Add optional parameter to limit how long handleToolCalls can loop autonomously --- examples/roc-agent.roc | 2 +- package/Tools.roc | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/examples/roc-agent.roc b/examples/roc-agent.roc index dbabeb6..110eed6 100644 --- a/examples/roc-agent.roc +++ b/examples/roc-agent.roc @@ -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 }) diff --git a/package/Tools.roc b/package/Tools.roc index c4829f8..244813a 100644 --- a/package/Tools.roc +++ b/package/Tools.roc @@ -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