packages feed

openai-2.1.0 / src / OpenAI / V1 / Tool.hs

-- | The `Tool` typemodule OpenAI.V1.Tool    ( -- * Types      Tool(..)    , RankingOptions(..)    , FileSearch(..)    , Function(..)    , ToolChoice(..)    , CodeInterpreterContainer(..)      -- * Constants    , toolChoiceNoneText    , toolChoiceAutoText    , toolChoiceRequiredText      -- * Helpers    , codeInterpreter    , codeInterpreterAuto    , codeInterpreterWithFiles    , toolToResponsesValue    , parseResponsesToolValue    , toolChoiceToResponsesValue    , parseResponsesToolChoiceValue    , flattenToolValue    , unflattenToolValue    ) whereimport Data.Aeson ((.:), (.:?), (.=))import Data.Aeson.Types (Parser)import OpenAI.Preludeimport qualified Data.Aeson as Aesonimport qualified Data.Aeson.Key as Keyimport qualified Data.Aeson.KeyMap as KeyMapimport qualified Data.HashSet as HashSetimport Data.HashSet (HashSet)import Data.List (partition)import qualified Data.Vector as V-- | Tool choice string constantstoolChoiceNoneText, toolChoiceAutoText, toolChoiceRequiredText :: TexttoolChoiceNoneText = "none"toolChoiceAutoText = "auto"toolChoiceRequiredText = "required"-- | 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 toolOptionstoolToResponsesValue :: Tool -> ValuetoolToResponsesValue = flattenToolValue . toJSONparseResponsesToolValue :: Value -> Parser ToolparseResponsesToolValue = parseJSON . unflattenToolValuetoolChoiceToResponsesValue :: ToolChoice -> ValuetoolChoiceToResponsesValue ToolChoiceNone = String toolChoiceNoneTexttoolChoiceToResponsesValue ToolChoiceAuto = String toolChoiceAutoTexttoolChoiceToResponsesValue ToolChoiceRequired = String toolChoiceRequiredTexttoolChoiceToResponsesValue (ToolChoiceTool tool) = toolToResponsesValue toolparseResponsesToolChoiceValue :: Value -> Parser ToolChoiceparseResponsesToolChoiceValue (String s)    | s == toolChoiceNoneText = pure ToolChoiceNone    | s == toolChoiceAutoText = pure ToolChoiceAuto    | s == toolChoiceRequiredText = pure ToolChoiceRequiredparseResponsesToolChoiceValue other = ToolChoiceTool <$> parseResponsesToolValue otherkeyFunction, keyType :: Key.KeykeyFunction = Key.fromText "function"keyType = Key.fromText "type"functionFieldKeys :: HashSet Key.KeyfunctionFieldKeys = HashSet.fromList $ Key.fromText <$> ["description", "name", "parameters", "strict"]isFunctionField :: Key.Key -> BoolisFunctionField = (`HashSet.member` functionFieldKeys)partitionFunctionFields    :: KeyMap.KeyMap Value    -> (KeyMap.KeyMap Value, KeyMap.KeyMap Value)partitionFunctionFields obj =    let (fnPairs, restPairs) = partition (isFunctionField . fst) (KeyMap.toList obj)    in (KeyMap.fromList fnPairs, KeyMap.fromList restPairs)flattenToolValue :: Value -> ValueflattenToolValue value@(Aeson.Object o) =    maybe value flattenFunction (KeyMap.lookup keyFunction o)  where    flattenFunction (Aeson.Object fnFields) =        Aeson.Object (KeyMap.delete keyFunction o <> fnFields)    flattenFunction _ = valueflattenToolValue value = valueunflattenToolValue :: Value -> ValueunflattenToolValue value@(Aeson.Object o)    | KeyMap.lookup keyType o == Just (String "function")    , not (KeyMap.member keyFunction o) =        let (fnFields, rest) = partitionFunctionFields o        in Aeson.Object (KeyMap.insert keyFunction (Aeson.Object fnFields) rest)    | otherwise = valueunflattenToolValue value = value-- | 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 } }