-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
108 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,45 @@ | ||
{-# LANGUAGE OverloadedStrings #-} | ||
|
||
module Data.Ollama.Common.Utils (defaultOllama, OllamaClient (..)) where | ||
module Data.Ollama.Common.Utils (defaultOllama, OllamaClient (..), encodeImage) where | ||
|
||
import Control.Exception (IOException, try) | ||
import Data.ByteString qualified as BS | ||
import Data.ByteString.Base64 qualified as Base64 | ||
import Data.Char (toLower) | ||
import Data.Ollama.Common.Types | ||
import Data.Text (Text) | ||
import Data.Text.Encoding qualified as TE | ||
import System.Directory | ||
import System.FilePath | ||
|
||
defaultOllama :: OllamaClient | ||
defaultOllama = OllamaClient "http://127.0.0.1:11434" | ||
|
||
supportedExtensions :: [String] | ||
supportedExtensions = [".jpg", ".jpeg", ".png"] | ||
|
||
safeReadFile :: FilePath -> IO (Either IOException BS.ByteString) | ||
safeReadFile = try . BS.readFile | ||
|
||
asPath :: FilePath -> IO (Maybe BS.ByteString) | ||
asPath filePath = do | ||
exists <- doesFileExist filePath | ||
if exists | ||
then either (const Nothing) Just <$> safeReadFile filePath | ||
else return Nothing | ||
|
||
isSupportedExtension :: FilePath -> Bool | ||
isSupportedExtension path = map toLower (takeExtension path) `elem` supportedExtensions | ||
|
||
{- | | ||
encodeImage is a utility function that takes an image file path (jpg, jpeg, png) and | ||
returns the image data in Base64 encoded format. Since GenerateOps' images field | ||
expects image data in base64. It is helper function that we are providing out of the box. | ||
-} | ||
encodeImage :: FilePath -> IO (Maybe Text) | ||
encodeImage filePath = do | ||
if not (isSupportedExtension filePath) | ||
then return Nothing | ||
else do | ||
maybeContent <- asPath filePath | ||
return $ fmap (TE.decodeUtf8 . Base64.encode) maybeContent |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters