Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decodeTop... functions now parse and return the ApiError response if possible #3

Merged
merged 4 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.DS_Store
examples/
!examples/*.*
examples/*
!examples/+.*
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ main =
response = Http.send! (Chat.buildRequest client messages)
when Chat.decodeTopMessageChoice response.body is
Ok message -> Stdout.line message.content
Err (HttpError err) -> Stdout.line err.message
Err _ -> Stdout.line "Error decoding API response"
```

Expand Down
14 changes: 4 additions & 10 deletions examples/chat.roc
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,10 @@ handlePrompt = \client, messages ->
## decode the response from the OpenRouter API and append the first message to the list of messages
getMessagesFromResponse : List Message, Http.Response -> List Message
getMessagesFromResponse = \messages, response ->
when Chat.decodeResponse response.body is
Ok body ->
when List.get body.choices 0 is
Ok choice -> List.append messages choice.message
Err _ -> Chat.appendSystemMessage messages "Error getting first choice from API response"

Err _ ->
when Chat.decodeErrorResponse response.body is
Ok { error } -> Chat.appendSystemMessage messages error.message
Err _ -> Chat.appendSystemMessage messages "Error decoding API response"
when Chat.decodeTopMessageChoice response.body is
Ok message -> List.append messages message
Err (HttpError err) -> Chat.appendSystemMessage messages err.message
Err _ -> Chat.appendSystemMessage messages "Error decoding API response"

## Get the API key from the environmental variable
getApiKey =
Expand Down
5 changes: 2 additions & 3 deletions examples/proompt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ main =
response = Http.send! (Prompt.buildHttpRequest client query)
when Prompt.decodeTopTextChoice response.body is
Ok text -> Stdout.line (text |> Str.trim)
Err (HttpError error) -> Stdout.line error.message
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"
Err InvalidResponse -> Stdout.line "Invalid API response"

## Get the API key from the environmental variable
getApiKey =
Expand Down
8 changes: 5 additions & 3 deletions package/Chat.roc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module [
import json.Json
import json.Option exposing [Option]

import Shared exposing [RequestObject, dropLeadingGarbage]
import Shared exposing [RequestObject, ApiError, dropLeadingGarbage]
import Client exposing [Client]

## The OpenAI ChatML standard message used to query the AI model.
Expand Down Expand Up @@ -115,14 +115,16 @@ decodeResponse = \bodyBytes ->
decoded.result

## Decode the JSON response body to the first message in the list of choices
decodeTopMessageChoice : List U8 -> Result Message [InvalidResponse, NoChoices]
decodeTopMessageChoice : List U8 -> Result Message [HttpError ApiError, InvalidResponse, NoChoices]
decodeTopMessageChoice = \responseBodyBytes ->
when decodeResponse responseBodyBytes is
Ok body ->
when List.get body.choices 0 is
Ok choice -> Ok choice.message
Err _ -> Err NoChoices
Err _ -> Err InvalidResponse
Err _ -> when Shared.decodeErrorResponse responseBodyBytes is
Ok err -> Err (HttpError err.error)
Err _ -> Err InvalidResponse

## Decode the JSON response body of an API error message
decodeErrorResponse = Shared.decodeErrorResponse
Expand Down
8 changes: 5 additions & 3 deletions package/Prompt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import json.Json
import json.Option exposing [Option]

import Client exposing [Client]
import Shared exposing [RequestObject]
import Shared exposing [RequestObject, ApiError]

## The structure of the request body to be sent in the Http request
PromptRequestBody : {
Expand Down Expand Up @@ -113,14 +113,16 @@ decodeResponse = \bodyBytes ->
decoded.result

## Decode the JSON response body to the first message in the list of choices
decodeTopTextChoice : List U8 -> Result Str [InvalidResponse, NoChoices]
decodeTopTextChoice : List U8 -> Result Str [HttpError ApiError, InvalidResponse, NoChoices]
decodeTopTextChoice = \responseBodyBytes ->
when decodeResponse responseBodyBytes is
Ok body ->
when List.get body.choices 0 is
Ok choice -> Ok choice.text
Err _ -> Err NoChoices
Err _ -> Err InvalidResponse
Err _ -> when decodeErrorResponse responseBodyBytes is
Ok err -> Err (HttpError err.error)
Err _ -> Err InvalidResponse

## Decode the JSON response body of an API error message
decodeErrorResponse = Shared.decodeErrorResponse
Expand Down
15 changes: 9 additions & 6 deletions package/Shared.roc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module [TimeoutConfig, ErrorResponse, RequestObject, ResponseFormat, dropLeadingGarbage, decodeErrorResponse]
module [ApiError, TimeoutConfig, ErrorResponse, RequestObject, ResponseFormat, dropLeadingGarbage, decodeErrorResponse]

import json.Json

Expand All @@ -17,15 +17,18 @@ RequestObject : {

## The structure of the JSON error response from the OpenAI API
ErrorResponse : {
error : {
code : U16,
message : Str,
},
error: ApiError,
}

## The API error status code and description
ApiError : {
code : U16,
message : Str,
}

## Tells the LLM how to respond to the user. Should be either "text" or "json_object"
ResponseFormat : {
type: Str,
#extra: U8, # This field only to avoid a compiler bug which occurs when for a record holding a single string
}

## Drop leading garbage characters from the response body
Expand Down
Loading