packages feed

llama-cpp-haskell 0.1.1.2 → 0.2

raw patch · 3 files changed

+40/−32 lines, 3 files

Files

Llama.hs view
@@ -5,6 +5,7 @@ import Conduit import Data.Aeson import Data.Text (Text)+import Data.Word import GHC.Generics import Network.HTTP.Conduit import Network.HTTP.Simple hiding (httpLbs)@@ -16,20 +17,14 @@ data Role = System | User | CustomRole Text deriving Show instance ToJSON Role where   toJSON System = "system"-  toJSON User = "system"+  toJSON User = "user"   toJSON (CustomRole t) = String t  data LlamaMessage = LlamaMessage   { role :: Role   , content :: Text-  } deriving (Show)--instance ToJSON LlamaMessage where-  toJSON m =-    object-      [ "role" .= role m-      , "content" .= Llama.content m-      ]+  } deriving (Show, Generic)+instance ToJSON LlamaMessage  newtype LlamaApplyTemplateRequest = LlamaApplyTemplateRequest   { messages :: [LlamaMessage]@@ -39,36 +34,33 @@ newtype LlamaApplyTemplateResponse = LlamaApplyTemplateResponse   { prompt :: Text   } deriving (Show, Generic)+instance FromJSON LlamaApplyTemplateResponse -instance FromJSON LlamaApplyTemplateResponse where-  parseJSON = withObject "LlamaApplyTemplateResponse" $ \v -> LlamaApplyTemplateResponse-    <$> v .: "prompt"+newtype LlamaDetokenizeRequest = LlamaDetokenizeRequest+  { tokens :: [Token]+  } deriving (Show, Generic)+instance ToJSON LlamaDetokenizeRequest +newtype LlamaDetokenizeResponse = LlamaDetokenizeResponse+  { content :: Text+  } deriving (Show, Generic)+instance FromJSON LlamaDetokenizeResponse+ data Health = HealthOk | HealthNok deriving (Show)  -- Llama request and response data LlamaRequest = LlamaRequest   { prompt :: Text   , stream :: Bool-  } deriving (Show)--instance FromJSON LlamaRequest where-  parseJSON = withObject "LlamaRequest" $ \v -> LlamaRequest-    <$> v .: "prompt"-    <*> v .: "stream"--instance ToJSON LlamaRequest where-  toJSON (LlamaRequest p s) =-    object ["prompt" .= p, "stream" .= s]+  } deriving (Show, Generic)+instance ToJSON LlamaRequest  newtype LlamaResponse = LlamaResponse-  { generatedText :: Text-  } deriving (Show)--instance FromJSON LlamaResponse where-  parseJSON = withObject "LlamaResponse" $ \v -> LlamaResponse-    <$> v .: "content"+  { content :: Text+  } deriving (Show, Generic)+instance FromJSON LlamaResponse +type Token = Word32 type URL = String  applyTemplate :: URL -> Manager -> LlamaApplyTemplateRequest -> IO (Maybe Text)@@ -111,6 +103,21 @@                     , requestHeaders = [("Content-Type", "application/json")]                     }   pure $ httpSource req getResponseBody .| eventConduit++detokenize :: URL -> [Token] -> IO (Maybe Text)+detokenize url input = do+  let request = parseRequest_ $ url ++ "/detokenize"+      body = encode $ LlamaDetokenizeRequest input+      req = request { method = "POST"+                    , requestBody = RequestBodyLBS body+                    , requestHeaders = [("Content-Type", "application/json")]+                    }+  response <- httpLBS req+  case decode (responseBody response) of+    Just (LlamaDetokenizeResponse text) -> return (Just text)+    Nothing -> do+      liftIO $ hPutStrLn stderr "Failed to decode Llama response"+      return Nothing  llama :: URL -> Text -> IO (Maybe Text) llama url input = do
Main.hs view
@@ -9,6 +9,7 @@  import Conduit import Data.Conduit.Lazy+import qualified Data.Text as T import qualified Data.Text.IO as T import Options.Generic import System.Exit@@ -40,7 +41,7 @@       response <- llamaTemplated (url opts) (LlamaApplyTemplateRequest request)       case response of         Nothing -> T.hPutStrLn stderr "Got no response from the server." >> exitFailure-        Just r -> T.putStrLn r+        Just r -> T.putStrLn $ if stripThinking opts then snd $ T.breakOnEnd "</think>" r else r     True -> do       hSetBuffering stdout NoBuffering       conduit <- llamaTemplatedStreaming (url opts) (LlamaApplyTemplateRequest request)
llama-cpp-haskell.cabal view
@@ -1,6 +1,6 @@ cabal-version:      2.2 name:               llama-cpp-haskell-version:            0.1.1.2+version:            0.2 synopsis:           Haskell bindings for the llama.cpp llama-server and a simple CLI description:        This is the interface that allows one to interface with llama-server RPC API using Haskell concepts. It also includes a `llamacall` binary to do it from your favorite command line shell and use it in scripting. license:            AGPL-3.0-only@@ -8,7 +8,7 @@ author:             Sergey Alirzaev maintainer:         l29ah@riseup.net -- copyright:-category:           Text+category:           Text, LLM, Llama, Machine Learning, AI, Network, CLI build-type:         Simple tested-with:        GHC == 9.12.2 -- extra-source-files:@@ -20,7 +20,7 @@ Source-repository this   type:              git   location:          https://github.com/l29ah/llama-cpp-haskell.git-  tag:               0.1.1.2+  tag:               0.2  common stuff     ghc-options: -Wall