Skip to content

Commit

Permalink
Bug fixes and function renaming
Browse files Browse the repository at this point in the history
  • Loading branch information
imclerran committed May 16, 2024
1 parent e805e36 commit 3fc009f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
Expand Down
16 changes: 6 additions & 10 deletions examples/proompt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
8 changes: 4 additions & 4 deletions package/Chat.roc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module [
buildRequestBody,
decodeErrorResponse,
decodeResponse,
decodeResponseToFirstMessage,
decodeTopMessageChoice,
encodeRequestBody,
initClient,
]
Expand Down Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions package/Prompt.roc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module [
decodeErrorResponse,
decodeErrorResponse,
decodeResponse,
decodeResponseToFirstText,
decodeTopTextChoice,
encodeRequestBody,
formatLLamaPrompt,
formatLLamaPromptWithHistory,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 3fc009f

Please sign in to comment.