diff --git a/README.md b/README.md index 1be165d..0eb972a 100644 --- a/README.md +++ b/README.md @@ -28,7 +28,7 @@ main = client = Chat.initClient { apiKey, model: "openai/gpt-4o" } messages = Chat.appendUserMessage [] "Hello, world!" response = Http.send! (Chat.buildRequest client messages) - when Chat.decodeResponseToFirstMessage response.body is + when Chat.decodeTopMessageChoice response.body is Ok message -> Stdout.line message.content Err _ -> Stdout.line "Error decoding API response" ``` diff --git a/examples/proompt.roc b/examples/proompt.roc index a836605..59cce74 100644 --- a/examples/proompt.roc +++ b/examples/proompt.roc @@ -22,16 +22,12 @@ main = |> Client.setMaxTokens 8 query = Prompt.formatLLamaPrompt { prompt: "Hello, computer!" } response = Http.send! (Prompt.buildHttpRequest client query) - when Prompt.decodeResponse response.body is - Ok body -> - when List.first body.choices is - Ok choice -> Stdout.line (choice.text |> Str.trim) - Err _ -> Stdout.line "No choices found in API response" - - Err _ -> - when Prompt.decodeErrorResponse response.body is - Ok { error } -> Stdout.line error.message - Err _ -> Stdout.line "Failed to decode API response" + when Prompt.decodeTopTextChoice response.body is + Ok text -> Stdout.line (text |> Str.trim) + Err NoChoices -> Stdout.line "No choices found in API response" + Err InvalidResponse -> when Prompt.decodeErrorResponse response.body is + Ok { error } -> Stdout.line error.message + Err _ -> Stdout.line "Failed to decode API response" ## Get the API key from the environmental variable getApiKey = diff --git a/package/Chat.roc b/package/Chat.roc index 53303a6..7d090ec 100644 --- a/package/Chat.roc +++ b/package/Chat.roc @@ -9,7 +9,7 @@ module [ buildRequestBody, decodeErrorResponse, decodeResponse, - decodeResponseToFirstMessage, + decodeTopMessageChoice, encodeRequestBody, initClient, ] @@ -115,9 +115,9 @@ decodeResponse = \bodyBytes -> decoded.result ## Decode the JSON response body to the first message in the list of choices -decodeResponseToFirstMessage : List U8 -> Result Message [InvalidResponse, NoChoices] -decodeResponseToFirstMessage = \bodyBytes -> - when decodeResponse bodyBytes is +decodeTopMessageChoice : List U8 -> Result Message [InvalidResponse, NoChoices] +decodeTopMessageChoice = \responseBodyBytes -> + when decodeResponse responseBodyBytes is Ok body -> when List.get body.choices 0 is Ok choice -> Ok choice.message diff --git a/package/Prompt.roc b/package/Prompt.roc index eb0c829..c3af08e 100644 --- a/package/Prompt.roc +++ b/package/Prompt.roc @@ -6,7 +6,7 @@ module [ decodeErrorResponse, decodeErrorResponse, decodeResponse, - decodeResponseToFirstText, + decodeTopTextChoice, encodeRequestBody, formatLLamaPrompt, formatLLamaPromptWithHistory, @@ -113,9 +113,9 @@ decodeResponse = \bodyBytes -> decoded.result ## Decode the JSON response body to the first message in the list of choices -decodeResponseToFirstText : List U8 -> Result Message [InvalidResponse, NoChoices] -decodeResponseToFirstText = \bodyBytes -> - when decodeResponse bodyBytes is +decodeTopTextChoice : List U8 -> Result Str [InvalidResponse, NoChoices] +decodeTopTextChoice = \responseBodyBytes -> + when decodeResponse responseBodyBytes is Ok body -> when List.get body.choices 0 is Ok choice -> Ok choice.text