Skip to content

Commit

Permalink
Fixed openai crash when Message.name is not set
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jun 9, 2024
1 parent 52debe8 commit 2ae65b7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
15 changes: 7 additions & 8 deletions ihp-openai/IHP/OpenAI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,13 @@ instance ToJSON Role where
toJSON ToolRole = toJSON ("tool" :: Text)

instance ToJSON Message where
toJSON Message { role, content, name, toolCallId, toolCalls } =
object
[ "role" .= role
, "content" .= content
, "name" .= name
, "tool_call_id" .= toolCallId
, "tool_calls" .= emptyListToNothing toolCalls
]
toJSON Message { role, content, name, toolCallId, toolCalls } = object $ Maybe.catMaybes
[ Just ("role" .= role)
, Just ("content" .= content)
, ("name" .=) <$> name
, ("tool_call_id" .=) <$> toolCallId
, if null toolCalls then Nothing else Just ("tool_calls" .= toolCalls)
]

instance ToJSON ResponseFormat where
toJSON Text = object [ "type" .= ("text" :: Text) ]
Expand Down
8 changes: 7 additions & 1 deletion ihp-openai/Test/IHP/OpenAISpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -595,4 +595,10 @@ tests = do
, parameters = Just (JsonSchemaObject [ Property { propertyName = "url", type_ = JsonSchemaString, required = True, description = Just "The url to fetch" }])
}

encode function `shouldBe` "{\"function\":{\"description\":\"Fetches a url\",\"name\":\"fetchUrl\",\"parameters\":{\"properties\":{\"url\":{\"description\":\"The url to fetch\",\"type\":\"string\"}},\"type\":\"object\"}},\"type\":\"function\"}"
encode function `shouldBe` "{\"function\":{\"description\":\"Fetches a url\",\"name\":\"fetchUrl\",\"parameters\":{\"properties\":{\"url\":{\"description\":\"The url to fetch\",\"type\":\"string\"}},\"type\":\"object\"}},\"type\":\"function\"}"

describe "ToJSON Message" do
it "encode a message but only set fields that are not null" do
let message = userMessage ""

encode message `shouldBe` "{\"content\":\"\",\"role\":\"user\"}"

0 comments on commit 2ae65b7

Please sign in to comment.