Skip to content

Commit c379057

Browse files
committed
added a small example application: find place names in input text
1 parent cd2283f commit c379057

File tree

1 file changed

+15
-1
lines changed

1 file changed

+15
-1
lines changed

OpenAiApiClient/GenText.hs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Network.HTTP.Client.TLS
66
import System.Environment (getEnv)
77
import qualified Data.Text as T
88
import Data.Maybe (fromMaybe)
9+
import Data.Text (splitOn)
910

1011
-- example derived from the openai-client library documentation
1112

@@ -45,7 +46,20 @@ completionRequestToString prompt = do
4546
return $ fromMaybe "No content" $ T.unpack <$> content
4647
_ -> return "No choices returned"
4748

49+
-- find place names
50+
findPlaces :: String -> IO [String]
51+
findPlaces text = do
52+
let prompt = "Extract only the place names separated by commas from the following text:\n\n" ++ text
53+
response <- completionRequestToString prompt
54+
-- Convert Text to String using T.unpack before filtering
55+
let places = filter (not . null) $ map T.unpack $ splitOn "," (T.pack response)
56+
-- Strip leading and trailing whitespace from each place name
57+
return $ map (T.unpack . T.strip . T.pack) places
58+
4859
main :: IO ()
4960
main = do
5061
response <- completionRequestToString "Write a hello world program in Haskell"
51-
putStrLn response
62+
putStrLn response
63+
64+
places <- findPlaces "I visited London, Paris, and New York last year."
65+
print places

0 commit comments

Comments
 (0)