Skip to content

Commit

Permalink
Further cleanup of tools example
Browse files Browse the repository at this point in the history
  • Loading branch information
imclerran committed Sep 20, 2024
1 parent 1d5a3b1 commit 93685dc
Showing 1 changed file with 18 additions and 29 deletions.
47 changes: 18 additions & 29 deletions examples/tools.roc
Original file line number Diff line number Diff line change
Expand Up @@ -30,26 +30,22 @@ loop : { client : Client, previousMessages : List Message } -> Task [Done {}, St
loop = \{ client, previousMessages } ->
Stdout.write! "You: "
query = Stdin.line!
messages = Chat.appendUserMessage previousMessages query
when query is
"goodbye" -> Task.ok (Done {})
_ -> handlePrompt client messages

## Handle the prompt and send the request to the OpenRouter API
handlePrompt : Client, List Message -> Task [Step { client : Client, previousMessages : List Message }] _
handlePrompt = \client, messages ->
response = Http.send (Chat.buildHttpRequest client messages {}) |> Task.result!
updatedMessages = getMessagesFromResponse messages response |> Tools.callTools! client toolHandlerMap
printLastMessage! updatedMessages
Task.ok (Step { client, previousMessages: updatedMessages })
_ ->
messages = Chat.appendUserMessage previousMessages query
response = Http.send (Chat.buildHttpRequest client messages {}) |> Task.result!
updatedMessages = getMessagesFromResponse messages response |> Tools.callTools! client toolHandlerMap
printLastMessage! updatedMessages
Task.ok (Step { client, previousMessages: updatedMessages })

# Print the last message in the list of messages. Will only print assistant messages.
printLastMessage : List Message -> Task {} _
printLastMessage = \messages ->
when List.last messages is
Ok { role, content } if role == "assistant" ->
Stdout.line! ("\nAssistant: $(content)\n" |> Ansi.color { fg: Standard Magenta })

Ok { role, content } if role == "system" ->
Stdout.line! ("\nAssistant: $(content)\n" |> Ansi.color { fg: Standard Cyan })

Expand Down Expand Up @@ -80,20 +76,16 @@ getApiKey =

## tool for the utcNow function
utcNowTool : Tool
utcNowTool =
Tools.buildTool
"utcNow"
"Get the current UTC time as an ISO 8601 string"
[]
utcNowTool = Tools.buildTool "utcNow" "Get the current UTC time as an ISO 8601 string" []

## Handler for the utcNow tool
utcNow : Str -> Task Str _
utcNow = \_args ->
Utc.now! {}
|> Utc.toNanosSinceEpoch
|> DateTime.fromNanosSinceEpoch
|> DateTime.toIsoStr
|> Task.ok
|> Utc.toNanosSinceEpoch
|> DateTime.fromNanosSinceEpoch
|> DateTime.toIsoStr
|> Task.ok

## tool for the toCdt function
toCdtTool : Tools.Tool
Expand Down Expand Up @@ -124,18 +116,18 @@ toCdt = \args ->
## tool for the toCst function
toCstTool : Tools.Tool
toCstTool =
utcTimeParam = {
name: "utcTime",
type: "string",
description: "An ISO 8601 formatted time to convert from UTC to CST",
utcTimeParam = {
name: "utcTime",
type: "string",
description: "An ISO 8601 formatted time to convert from UTC to CST",
required: Bool.true,
}
Tools.buildTool "toCst" "Convert a UTC time to a CST time" [utcTimeParam]

## Handler for the toCst tool
toCst : Str -> Task Str _
toCst = \args ->
{ utcTime } =
utcTime =
args
|> Str.toUtf8
|> Decode.fromBytes Json.utf8
Expand All @@ -150,7 +142,4 @@ toCst = \args ->
## Map of tool names to tool handlers
toolHandlerMap : Dict Str (Str -> Task Str _)
toolHandlerMap =
Dict.empty {}
|> Dict.insert "utcNow" utcNow
|> Dict.insert "toCdt" toCdt
|> Dict.insert "toCst" toCst
Dict.fromList [("utcNow", utcNow), ("toCdt", toCdt), ("toCst", toCst)]

0 comments on commit 93685dc

Please sign in to comment.