{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Data.Ollama.Push
(
push
, pushM
) where
import Control.Monad (void)
import Control.Monad.IO.Class (MonadIO (liftIO))
import Data.Aeson
import Data.Ollama.Common.Config (OllamaConfig)
import Data.Ollama.Common.Types (HasDone (getDone))
import Data.Ollama.Common.Utils as CU
import Data.Text (Text)
import GHC.Generics
import GHC.Int (Int64)
data PushOps = PushOps
{ PushOps -> Text
name :: !Text
, PushOps -> Maybe Bool
insecure :: !(Maybe Bool)
, PushOps -> Maybe Bool
stream :: !(Maybe Bool)
}
deriving (Int -> PushOps -> ShowS
[PushOps] -> ShowS
PushOps -> String
(Int -> PushOps -> ShowS)
-> (PushOps -> String) -> ([PushOps] -> ShowS) -> Show PushOps
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PushOps -> ShowS
showsPrec :: Int -> PushOps -> ShowS
$cshow :: PushOps -> String
show :: PushOps -> String
$cshowList :: [PushOps] -> ShowS
showList :: [PushOps] -> ShowS
Show, PushOps -> PushOps -> Bool
(PushOps -> PushOps -> Bool)
-> (PushOps -> PushOps -> Bool) -> Eq PushOps
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PushOps -> PushOps -> Bool
== :: PushOps -> PushOps -> Bool
$c/= :: PushOps -> PushOps -> Bool
/= :: PushOps -> PushOps -> Bool
Eq, (forall x. PushOps -> Rep PushOps x)
-> (forall x. Rep PushOps x -> PushOps) -> Generic PushOps
forall x. Rep PushOps x -> PushOps
forall x. PushOps -> Rep PushOps x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PushOps -> Rep PushOps x
from :: forall x. PushOps -> Rep PushOps x
$cto :: forall x. Rep PushOps x -> PushOps
to :: forall x. Rep PushOps x -> PushOps
Generic, [PushOps] -> Value
[PushOps] -> Encoding
PushOps -> Bool
PushOps -> Value
PushOps -> Encoding
(PushOps -> Value)
-> (PushOps -> Encoding)
-> ([PushOps] -> Value)
-> ([PushOps] -> Encoding)
-> (PushOps -> Bool)
-> ToJSON PushOps
forall a.
(a -> Value)
-> (a -> Encoding)
-> ([a] -> Value)
-> ([a] -> Encoding)
-> (a -> Bool)
-> ToJSON a
$ctoJSON :: PushOps -> Value
toJSON :: PushOps -> Value
$ctoEncoding :: PushOps -> Encoding
toEncoding :: PushOps -> Encoding
$ctoJSONList :: [PushOps] -> Value
toJSONList :: [PushOps] -> Value
$ctoEncodingList :: [PushOps] -> Encoding
toEncodingList :: [PushOps] -> Encoding
$comitField :: PushOps -> Bool
omitField :: PushOps -> Bool
ToJSON)
data PushResp = PushResp
{ PushResp -> Text
status :: !Text
, PushResp -> Maybe Text
digest :: !(Maybe Text)
, PushResp -> Maybe Int64
total :: !(Maybe Int64)
}
deriving (Int -> PushResp -> ShowS
[PushResp] -> ShowS
PushResp -> String
(Int -> PushResp -> ShowS)
-> (PushResp -> String) -> ([PushResp] -> ShowS) -> Show PushResp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> PushResp -> ShowS
showsPrec :: Int -> PushResp -> ShowS
$cshow :: PushResp -> String
show :: PushResp -> String
$cshowList :: [PushResp] -> ShowS
showList :: [PushResp] -> ShowS
Show, PushResp -> PushResp -> Bool
(PushResp -> PushResp -> Bool)
-> (PushResp -> PushResp -> Bool) -> Eq PushResp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: PushResp -> PushResp -> Bool
== :: PushResp -> PushResp -> Bool
$c/= :: PushResp -> PushResp -> Bool
/= :: PushResp -> PushResp -> Bool
Eq, (forall x. PushResp -> Rep PushResp x)
-> (forall x. Rep PushResp x -> PushResp) -> Generic PushResp
forall x. Rep PushResp x -> PushResp
forall x. PushResp -> Rep PushResp x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cfrom :: forall x. PushResp -> Rep PushResp x
from :: forall x. PushResp -> Rep PushResp x
$cto :: forall x. Rep PushResp x -> PushResp
to :: forall x. Rep PushResp x -> PushResp
Generic, Maybe PushResp
Value -> Parser [PushResp]
Value -> Parser PushResp
(Value -> Parser PushResp)
-> (Value -> Parser [PushResp])
-> Maybe PushResp
-> FromJSON PushResp
forall a.
(Value -> Parser a)
-> (Value -> Parser [a]) -> Maybe a -> FromJSON a
$cparseJSON :: Value -> Parser PushResp
parseJSON :: Value -> Parser PushResp
$cparseJSONList :: Value -> Parser [PushResp]
parseJSONList :: Value -> Parser [PushResp]
$comittedField :: Maybe PushResp
omittedField :: Maybe PushResp
FromJSON)
instance HasDone PushResp where
getDone :: PushResp -> Bool
getDone PushResp {Maybe Int64
Maybe Text
Text
status :: PushResp -> Text
digest :: PushResp -> Maybe Text
total :: PushResp -> Maybe Int64
status :: Text
digest :: Maybe Text
total :: Maybe Int64
..} = Text
status Text -> Text -> Bool
forall a. Eq a => a -> a -> Bool
/= Text
"success"
push ::
Text ->
Maybe Bool ->
Maybe Bool ->
Maybe OllamaConfig ->
IO ()
push :: Text -> Maybe Bool -> Maybe Bool -> Maybe OllamaConfig -> IO ()
push Text
modelName Maybe Bool
mInsecure Maybe Bool
mStream Maybe OllamaConfig
mbConfig = do
IO (Either OllamaError PushResp) -> IO ()
forall (f :: * -> *) a. Functor f => f a -> f ()
void (IO (Either OllamaError PushResp) -> IO ())
-> IO (Either OllamaError PushResp) -> IO ()
forall a b. (a -> b) -> a -> b
$
Text
-> ByteString
-> Maybe PushOps
-> Maybe OllamaConfig
-> (Response BodyReader -> IO (Either OllamaError PushResp))
-> IO (Either OllamaError PushResp)
forall payload response.
ToJSON payload =>
Text
-> ByteString
-> Maybe payload
-> Maybe OllamaConfig
-> (Response BodyReader -> IO (Either OllamaError response))
-> IO (Either OllamaError response)
withOllamaRequest
Text
"/api//push"
ByteString
"POST"
(PushOps -> Maybe PushOps
forall a. a -> Maybe a
Just (PushOps -> Maybe PushOps) -> PushOps -> Maybe PushOps
forall a b. (a -> b) -> a -> b
$ PushOps {name :: Text
name = Text
modelName, insecure :: Maybe Bool
insecure = Maybe Bool
mInsecure, stream :: Maybe Bool
stream = Maybe Bool
mStream})
Maybe OllamaConfig
mbConfig
((PushResp -> IO ())
-> IO () -> Response BodyReader -> IO (Either OllamaError PushResp)
forall a.
(HasDone a, FromJSON a) =>
(a -> IO ())
-> IO () -> Response BodyReader -> IO (Either OllamaError a)
commonStreamHandler PushResp -> IO ()
onToken IO ()
onComplete)
where
onToken :: PushResp -> IO ()
onToken :: PushResp -> IO ()
onToken PushResp
_ = String -> IO ()
putStrLn String
"Pushing... "
onComplete :: IO ()
onComplete :: IO ()
onComplete = String -> IO ()
putStrLn String
"Completed"
pushM :: MonadIO m => Text -> Maybe Bool -> Maybe Bool -> Maybe OllamaConfig -> m ()
pushM :: forall (m :: * -> *).
MonadIO m =>
Text -> Maybe Bool -> Maybe Bool -> Maybe OllamaConfig -> m ()
pushM Text
t Maybe Bool
insec Maybe Bool
s Maybe OllamaConfig
mbCfg = IO () -> m ()
forall a. IO a -> m a
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO () -> m ()) -> IO () -> m ()
forall a b. (a -> b) -> a -> b
$ Text -> Maybe Bool -> Maybe Bool -> Maybe OllamaConfig -> IO ()
push Text
t Maybe Bool
insec Maybe Bool
s Maybe OllamaConfig
mbCfg