Skip to content

Commit

Permalink
ihp-openai: Added response_format field
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed May 9, 2024
1 parent 8aaad08 commit c2aec33
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion ihp-openai/IHP/OpenAI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ data CompletionRequest = CompletionRequest
, presencePenalty :: !(Maybe Double)
, frequencePenalty :: !(Maybe Double)
, stream :: !Bool
, responseFormat :: !(Maybe ResponseFormat)
} deriving (Eq, Show)

data Message = Message
Expand All @@ -38,8 +39,17 @@ data Role
| AssistantRole
deriving (Eq, Show)

data ResponseFormat
= ResponseFormat { type_ :: !ResponseFormatType }
deriving (Eq, Show)

data ResponseFormatType
= Text
| JsonObject
deriving (Eq, Show)

instance ToJSON CompletionRequest where
toJSON CompletionRequest { model, messages, maxTokens, temperature, presencePenalty, frequencePenalty, stream } =
toJSON CompletionRequest { model, messages, maxTokens, temperature, presencePenalty, frequencePenalty, stream, responseFormat } =
object
[ "model" .= model
, "messages" .= messages
Expand All @@ -48,6 +58,7 @@ instance ToJSON CompletionRequest where
, "temperature" .= temperature
, "presence_penalty" .= presencePenalty
, "frequency_penalty" .= frequencePenalty
, "response_format" .= responseFormat
]

instance ToJSON Role where
Expand All @@ -59,6 +70,14 @@ instance ToJSON Message where
toJSON Message { role, content } =
object [ "role" .= role, "content" .= content ]

instance ToJSON ResponseFormat where
toJSON ResponseFormat { type_ } =
object [ "type" .= type_ ]

instance ToJSON ResponseFormatType where
toJSON Text = toJSON ("text" :: Text)
toJSON JsonObject = toJSON ("json_object" :: Text)

userMessage :: Text -> Message
userMessage content = Message { role = UserRole, content }

Expand All @@ -77,6 +96,7 @@ newCompletionRequest = CompletionRequest
, frequencePenalty = Nothing
, model = "gpt-3.5-turbo"
, stream = False
, responseFormat = Nothing
}

data CompletionResult = CompletionResult
Expand Down

0 comments on commit c2aec33

Please sign in to comment.