Skip to content

Commit 6c5101b

Browse files
committed
improved OpenAI API example
1 parent b26489d commit 6c5101b

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

OpenAiApiClient/GenText.hs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,49 @@
1-
-- Example from https://github.com/agrafix/openai-hs/tree/main/openai-hs
2-
-- code by Alexander Thiemann
3-
41
{-# LANGUAGE OverloadedStrings #-}
52
import OpenAI.Client
63

74
import Network.HTTP.Client
8-
95
import Network.HTTP.Client.TLS
106
import System.Environment (getEnv)
117
import qualified Data.Text as T
128
import Data.Maybe (fromMaybe)
139

14-
request :: ChatCompletionRequest
15-
request = ChatCompletionRequest
16-
{ chcrModel = ModelId "gpt-4o"
17-
, chcrMessages =
18-
[ChatMessage { chmContent = Just "Write a hello world program in Haskell"
19-
, chmRole = "user"
20-
, chmFunctionCall = Nothing
21-
, chmName = Nothing
22-
}
23-
]
24-
, chcrFunctions = Nothing
25-
, chcrTemperature = Nothing
26-
, chcrTopP = Nothing
27-
, chcrN = Nothing
28-
, chcrStream = Nothing
29-
, chcrStop = Nothing
30-
, chcrMaxTokens = Nothing
31-
, chcrPresencePenalty = Nothing
32-
, chcrFrequencyPenalty = Nothing
33-
, chcrLogitBias = Nothing
34-
, chcrUser = Nothing
35-
}
10+
completionRequestToString :: String -> IO String
11+
completionRequestToString prompt = do
12+
manager <- newManager tlsManagerSettings
13+
apiKey <- T.pack <$> getEnv "OPENAI_KEY"
14+
let client = makeOpenAIClient apiKey manager 4
15+
let request = ChatCompletionRequest
16+
{ chcrModel = ModelId "gpt-4o"
17+
, chcrMessages =
18+
[ ChatMessage
19+
{ chmContent = Just (T.pack prompt)
20+
, chmRole = "user"
21+
, chmFunctionCall = Nothing
22+
, chmName = Nothing
23+
}
24+
]
25+
, chcrFunctions = Nothing
26+
, chcrTemperature = Nothing
27+
, chcrTopP = Nothing
28+
, chcrN = Nothing
29+
, chcrStream = Nothing
30+
, chcrStop = Nothing
31+
, chcrMaxTokens = Nothing
32+
, chcrPresencePenalty = Nothing
33+
, chcrFrequencyPenalty = Nothing
34+
, chcrLogitBias = Nothing
35+
, chcrUser = Nothing
36+
}
37+
result <- completeChat client request
38+
case result of
39+
Left failure -> return (show failure)
40+
Right success ->
41+
case chrChoices success of
42+
(ChatChoice {chchMessage = ChatMessage {chmContent = content}} : _) ->
43+
return $ fromMaybe "No content" $ T.unpack <$> content
44+
_ -> return "No choices returned"
3645

3746
main :: IO ()
3847
main = do
39-
manager <- newManager tlsManagerSettings
40-
apiKey <- T.pack <$> (getEnv "OPENAI_KEY")
41-
-- create a openai client that automatically retries up to 4 times on network
42-
-- errors
43-
let client = makeOpenAIClient apiKey manager 4
44-
result <- completeChat client request
45-
case result of
46-
Left failure -> print failure
47-
Right success -> -- print $ chrChoices success
48-
case chrChoices success of
49-
(ChatChoice {chchMessage = ChatMessage {chmContent = content}}:_) ->
50-
putStrLn $ fromMaybe "No content" $ T.unpack <$> content
51-
_ -> putStrLn "No choices returned"
48+
response <- completionRequestToString "Write a hello world program in Haskell"
49+
putStrLn response

OpenAiApiClient/OpenAiApiClient.cabal

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,3 @@ executable GenText
1717
default-language: Haskell2010
1818
build-depends: base >= 4.7 && < 5, mtl >= 2.2.2, text, http-client >= 0.7.13.1, openai-hs, http-client-tls
1919

20-
executable Completion
21-
hs-source-dirs: .
22-
main-is: GenText.hs
23-
default-language: Haskell2010
24-
build-depends: base >= 4.7 && < 5, mtl >= 2.2.2, text, http-client >= 0.7.13.1, openai-hs, http-client-tls, http-conduit, aeson
25-

OpenAiApiClient/README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,12 @@ Alternatively, you can run the Haskell code without compiling by using an interp
7878
```
7979

8080
This should also produce the output "Hello, World!" in the terminal.
81-
```
81+
```
82+
83+
84+
## Run using Replit.com, Nix, Cabal
85+
86+
cabal build
87+
cabal run
88+
89+
Note: I had to manually install: cabal install cpphs

0 commit comments

Comments
 (0)