Skip to content

Commit

Permalink
Merge pull request #42 from imclerran/refactoring
Browse files Browse the repository at this point in the history
Refactoring
  • Loading branch information
imclerran authored Jan 3, 2025
2 parents 644a511 + 933f565 commit 79531fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 53 deletions.
29 changes: 7 additions & 22 deletions examples/chat.roc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ loop = \{ client, previousMessages } ->

_ ->
response = Http.send (Chat.buildHttpRequest client messages {}) |> Task.result!
updatedMessages = getMessagesFromResponse messages response
updatedMessages = Chat.updateMessageList response messages
printLastMessage! updatedMessages
Task.ok (Step { client, previousMessages: updatedMessages })

Expand All @@ -67,21 +67,6 @@ printLastMessage = \messages ->

_ -> Task.ok {}

## decode the response from the OpenRouter API and append the first message to the list of messages
getMessagesFromResponse : List Message, Result Http.Response _ -> List Message
getMessagesFromResponse = \messages, responseRes ->
when responseRes is
Ok response ->
when Chat.decodeTopMessageChoice response.body is
Ok message -> List.append messages message
Err (ApiError err) -> Chat.appendSystemMessage messages "API error: $(err.message)" {}
Err NoChoices -> Chat.appendSystemMessage messages "No choices in API response" {}
Err (BadJson str) -> Chat.appendSystemMessage messages "Could not decode JSON response:\n$(str)" {}
Err DecodingError -> Chat.appendSystemMessage messages "Error decoding API response" {}

Err (HttpErr err) ->
Chat.appendSystemMessage messages (Http.errorToString err) {}

## Prompt the user to choose a model and return the selected model
getModelChoice : Task Str _
getModelChoice =
Expand Down Expand Up @@ -111,33 +96,33 @@ initializeMessages =
{}

## The default model selection
defaultModel = "meta-llama/llama-3.1-8b-instruct:free"
defaultModel = "google/gemini-flash-1.5-8b"

## Define the model choices
modelChoices =
Dict.empty {}
|> Dict.insert "1" defaultModel
|> Dict.insert "2" "mistralai/mixtral-8x7b-instruct"
|> Dict.insert "3" "mistralai/mistral-small"
|> Dict.insert "3" "x-ai/grok-beta"
|> Dict.insert "4" "mistralai/mistral-large"
|> Dict.insert "5" "gryphe/mythomax-l2-13b"
|> Dict.insert "6" "microsoft/wizardlm-2-8x22b"
|> Dict.insert "7" "openai/gpt-3.5-turbo"
|> Dict.insert "8" "openai/gpt-4o"
|> Dict.insert "9" "google/gemini-pro-1.5"
|> Dict.insert "9" "google/gemini-2.0-flash-thinking-exp:free"

## Define the preferred providers for each model
preferredProviders =
Dict.empty {}
|> Dict.insert "meta-llama/llama-3-8b-instruct:free" []
|> Dict.insert defaultModel []
|> Dict.insert "mistralai/mixtral-8x7b-instruct" ["Fireworks", "Together", "Lepton"]
|> Dict.insert "mistralai/mistral-small" []
|> Dict.insert "x-ai/grok-beta" []
|> Dict.insert "mistralai/mistral-large" []
|> Dict.insert "gryphe/mythomax-l2-13b" ["DeepInfra", "Fireworks", "Together"]
|> Dict.insert "microsoft/wizardlm-2-8x22b" []
|> Dict.insert "openai/gpt-3.5-turbo" []
|> Dict.insert "openai/gpt-4o" []
|> Dict.insert "google/gemini-pro-1.5" []
|> Dict.insert "google/gemini-2.0-flash-thinking-exp:free" []

## Generate a string to print for the model selection menu
modelMenuString =
Expand Down
17 changes: 1 addition & 16 deletions examples/tools.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ main =
Stdout.write! "You: "
messages = Chat.appendUserMessage previousMessages Stdin.line! {}
response = Http.send (Chat.buildHttpRequest client messages {}) |> Task.result!
updatedMessages = updateMessagesFromResponse response messages |> Tools.handleToolCalls! client toolHandlerMap
updatedMessages = Chat.updateMessageList response messages |> Tools.handleToolCalls! client toolHandlerMap
printLastMessage! updatedMessages
Task.ok (Step { previousMessages: updatedMessages })

Expand All @@ -48,21 +48,6 @@ printLastMessage = \messages ->

_ -> Task.ok {}

## decode the response from the OpenRouter API and append the first message to the list of messages
updateMessagesFromResponse : Result Http.Response _, List Message -> List Message
updateMessagesFromResponse = \responseRes, messages ->
when responseRes is
Ok response ->
when Chat.decodeTopMessageChoice response.body is
Ok message -> List.append messages message
Err (ApiError err) -> Chat.appendSystemMessage messages "API error: $(err.message)" {}
Err NoChoices -> Chat.appendSystemMessage messages "No choices in API response" {}
Err (BadJson str) -> Chat.appendSystemMessage messages "Could not decode JSON response:\n$(str)" {}
Err DecodingError -> Chat.appendSystemMessage messages "Error decoding API response" {}

Err (HttpErr err) ->
Chat.appendSystemMessage messages (Http.errorToString err) {}

## Map of tool names to tool handlers
toolHandlerMap : Dict Str (Str -> Task Str _)
toolHandlerMap =
Expand Down
8 changes: 6 additions & 2 deletions package/Chat.roc
Original file line number Diff line number Diff line change
Expand Up @@ -252,12 +252,16 @@ updateMessageList = \responseRes, messages->
Ok response ->
when decodeTopMessageChoice response.body is
Ok message -> List.append messages message
Err (ApiError err) -> appendSystemMessage messages "API error: $(err.message)" {}
Err (ApiError err) -> appendSystemMessage messages "API error: $(Inspect.toStr err)" {} #err.message
Err NoChoices -> appendSystemMessage messages "No choices in API response" {}
Err (BadJson str) -> appendSystemMessage messages "Could not decode JSON response:\n$(str)" {}
Err DecodingError -> appendSystemMessage messages "Error decoding API response" {}

Err (HttpErr (BadStatus { code }) ) ->
appendSystemMessage messages "Http error: $(Num.toStr code)" {}

Err (HttpErr _) -> messages
Err (HttpErr _) ->
appendSystemMessage messages "Http error" {}

## Encode the request body to be sent in the Http request.
encodeRequestBody : ChatRequestBody -> List U8
Expand Down
14 changes: 1 addition & 13 deletions package/Tools.roc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module { sendHttpReq } -> [Tool, ToolCall, buildTool, handleToolCalls, dispatchT
import InternalTools
import Chat
import Client exposing [Client]
import Shared exposing [HttpResponse]

## A tool that can be called by the AI model.
## ```
Expand Down Expand Up @@ -59,7 +58,7 @@ handleToolCalls = \messages, client, toolHandlerMap ->
toolMessages = dispatchToolCalls! toolCalls toolHandlerMap
messagesWithTools = List.join [messages, toolMessages]
response = sendHttpReq (Chat.buildHttpRequest client messagesWithTools {}) |> Task.result!
messagesWithResponse = updateMessagesFromResponse messagesWithTools response
messagesWithResponse = Chat.updateMessageList response messagesWithTools
handleToolCalls messagesWithResponse client toolHandlerMap

_ -> Task.ok messages
Expand Down Expand Up @@ -95,17 +94,6 @@ callTool = \toolCall, handler ->
cached: Bool.false,
}

## Get the messages from the response and return the updated list of messages.
updateMessagesFromResponse : List Message, Result HttpResponse _ -> List Message
updateMessagesFromResponse = \messages, responseRes ->
when responseRes is
Ok response ->
when Chat.decodeTopMessageChoice response.body is
Ok message -> List.append messages message
_ -> messages

Err (HttpErr _) -> messages

## Build a tool object with the given name, description, and parameters.
## ```
## buildTool = \name, description, parameters -> ...
Expand Down

0 comments on commit 79531fa

Please sign in to comment.