Initial commit
This commit is contained in:
commit
2305885dc6
14 files changed
+648
No files matched your search
@@ -0,0 +1,28 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# OPTIONS_GHC -Wall #-}
|
||||
|
||||
module AI where
|
||||
|
||||
import Control.Lens (re, traversed, (&), (.~), (^.), (^?))
|
||||
import Control.Monad.Reader (liftIO)
|
||||
import Creds
|
||||
import Data.Aeson (object, (.=))
|
||||
import Data.Aeson.Lens (key, _Array, _String)
|
||||
import Data.Text (Text)
|
||||
import Data.Text.Strict.Lens (utf8)
|
||||
import Network.Wreq (defaults, header, postWith, responseBody)
|
||||
import UnliftIO (MonadIO)
|
||||
import Control.Exception.Annotated (checkpointCallStack)
|
||||
import Control.Monad.Catch (MonadCatch)
|
||||
|
||||
completions :: (MonadIO m, MonadCatch m) => Creds -> Text -> m (Maybe Text)
|
||||
completions c prompt = checkpointCallStack $ do
|
||||
let opts = defaults & header "Authorization" .~ ["Bearer " <> c ^. openrouterKey . re utf8]
|
||||
let j =
|
||||
object
|
||||
[ "model" .= ("openai/gpt-4o-mini" :: Text),
|
||||
"messages"
|
||||
.= [object ["role" .= ("user" :: Text), "content" .= prompt]]
|
||||
]
|
||||
r <- liftIO $ postWith opts "https://openrouter.ai/api/v1/chat/completions" j
|
||||
pure $ r ^? responseBody . key "choices" . _Array . traversed . key "message" . key "content" . _String
|
||||
Reference in new issue
Block a user