-- | The `Tool` typemodule OpenAI.V1.Tool ( -- * Types Tool(..) , RankingOptions(..) , FileSearch(..) , Function(..) , ToolChoice(..) , CodeInterpreterContainer(..) -- * Helpers , codeInterpreter , codeInterpreterAuto , codeInterpreterWithFiles ) whereimport Data.Aeson ((.:), (.:?), (.=))import OpenAI.Preludeimport qualified Data.Aeson as Aesonimport qualified Data.Vector as V-- | The ranking options for the file searchdata RankingOptions = RankingOptions { ranker :: Maybe Text , score_threshold :: Double } deriving stock (Generic, Show) deriving anyclass (FromJSON, ToJSON)-- | Overrides for the file search tooldata FileSearch = FileSearch { max_num_results :: Maybe Natural , ranking_options :: Maybe RankingOptions } deriving stock (Generic, Show) deriving anyclass (FromJSON, ToJSON)-- | The Function tooldata Function = Function { description :: Maybe Text , name :: Text , parameters :: Maybe Value , strict :: Maybe Bool } deriving stock (Generic, Show) deriving anyclass (FromJSON, ToJSON)-- | A tool enabled on the assistantdata Tool = Tool_Code_Interpreter{ container :: Maybe CodeInterpreterContainer } | Tool_File_Search{ file_search :: FileSearch } | Tool_Function{ function :: Function } | Tool_Web_Search deriving stock (Generic, Show)toolOptions :: OptionstoolOptions = aesonOptions { sumEncoding = TaggedObject{ tagFieldName = "type", contentsFieldName = "" } , tagSingleConstructors = True , constructorTagModifier = stripPrefix "Tool_" }instance FromJSON Tool where parseJSON = genericParseJSON toolOptionsinstance ToJSON Tool where toJSON = genericToJSON toolOptions-- | Controls which (if any) tool is called by the modeldata ToolChoice = ToolChoiceNone | ToolChoiceAuto | ToolChoiceRequired | ToolChoiceTool Tool deriving stock (Generic, Show)instance FromJSON ToolChoice where parseJSON "none" = pure ToolChoiceNone parseJSON "auto" = pure ToolChoiceAuto parseJSON "required" = pure ToolChoiceRequired parseJSON other = fmap ToolChoiceTool (parseJSON other)instance ToJSON ToolChoice where toJSON ToolChoiceNone = "none" toJSON ToolChoiceAuto = "auto" toJSON ToolChoiceRequired = "required" toJSON (ToolChoiceTool tool) = toJSON tool-- | Code Interpreter container referencedata CodeInterpreterContainer = CodeInterpreterContainer_Auto{ file_ids :: Maybe (Vector Text) } | CodeInterpreterContainer_ID{ container_id :: Text } deriving stock (Generic, Show)instance ToJSON CodeInterpreterContainer where toJSON (CodeInterpreterContainer_ID container_id) = toJSON container_id toJSON (CodeInterpreterContainer_Auto file_ids) = Aeson.object $ "type" .= String "auto" : maybe [] (\ids -> ["file_ids" .= ids]) file_idsinstance FromJSON CodeInterpreterContainer where parseJSON (String s) = pure $ CodeInterpreterContainer_ID s parseJSON (Object o) = do t <- o .: "type" case (t :: Text) of "auto" -> CodeInterpreterContainer_Auto <$> o .:? "file_ids" _ -> fail "Unknown code interpreter container object" parseJSON _ = fail "Invalid code interpreter container value"-- | Convenience: Code Interpreter without an explicit container (for Assistants API)codeInterpreter :: ToolcodeInterpreter = Tool_Code_Interpreter { container = Nothing }-- | Convenience: Code Interpreter with automatic new container and no filescodeInterpreterAuto :: ToolcodeInterpreterAuto = Tool_Code_Interpreter { container = Just CodeInterpreterContainer_Auto{ file_ids = Nothing } }-- | Convenience: Code Interpreter with automatic new container seeded with filescodeInterpreterWithFiles :: [Text] -> ToolcodeInterpreterWithFiles xs = let mFiles = case xs of [] -> Nothing _ -> Just (V.fromList xs) in Tool_Code_Interpreter { container = Just CodeInterpreterContainer_Auto{ file_ids = mFiles } }