Skip to content

Commit eee95a6

Browse files
committed
changed OpenAI example to print plain text completion instead of a structure
1 parent 43bfe5f commit eee95a6

File tree

3 files changed

+97
-10
lines changed

3 files changed

+97
-10
lines changed

OpenAiApiClient/GenText.hs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Network.HTTP.Client
99
import Network.HTTP.Client.TLS
1010
import System.Environment (getEnv)
1111
import qualified Data.Text as T
12+
import Data.Maybe (fromMaybe)
1213

1314
request :: ChatCompletionRequest
1415
request = ChatCompletionRequest
@@ -34,13 +35,17 @@ request = ChatCompletionRequest
3435
}
3536

3637
main :: IO ()
37-
main =
38-
do manager <- newManager tlsManagerSettings
39-
apiKey <- T.pack <$> getEnv "OPENAI_KEY"
40-
-- create a openai client that automatically retries up to 4 times on network
41-
-- errors
42-
let client = makeOpenAIClient apiKey manager 4
43-
result <- completeChat client request
44-
case result of
45-
Left failure -> print failure
46-
Right success -> print $ chrChoices success
38+
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"

OpenAiApiClient/OpenAiApiClient.cabal

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,9 @@ 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: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,79 @@
33
I am using the library written by Alexander Thiemann at:
44

55
https://github.com/agrafix/openai-hs/tree/main/openai-hs
6+
7+
If I just print:
8+
9+
```
10+
case result of
11+
Left failure -> print failure
12+
Right success -> print $ chrChoices success
13+
```
14+
15+
then the output looks like this:
16+
17+
```
18+
[ChatChoice {chchIndex = 0, chchMessage = ChatMessage {chmContent = Just "Certainly! Here is a simple "Hello, World!" program in Haskell:\n\nhaskell\nmain :: IO ()\nmain = putStrLn \"Hello, World!\"\n\n\nTo run this program, follow these steps:\n\n1. Save the code in a file with a .hs extension, for example, HelloWorld.hs.\n2. Open a terminal and navigate to the directory where you saved the file.\n3. Compile the program using the Glasgow Haskell Compiler (GHC) by running:\n sh\n ghc HelloWorld.hs\n \n4. This will produce an executable file named HelloWorld (or HelloWorld.exe on Windows).\n5. Run the executable by typing:\n sh\n ./HelloWorld\n \n\nYou should see the output:\n\nHello, World!\n", chmRole = "assistant", chmFunctionCall = Nothing, chmName = Nothing}, chchFinishReason = Just "stop"}]
19+
```
20+
21+
If I print:
22+
23+
```
24+
Right success -> -- print $ chrChoices success
25+
case chrChoices success of
26+
(ChatChoice {chchMessage = ChatMessage {chmContent = content}}:_) ->
27+
putStrLn $ fromMaybe "No content" $ T.unpack <$> content
28+
_ -> putStrLn "No choices returned"
29+
```
30+
31+
then the putput looks like this:
32+
33+
```
34+
Certainly! Here's a simple "Hello, World!" program in Haskell:
35+
36+
```haskell
37+
main :: IO ()
38+
main = putStrLn "Hello, World!"
39+
```
40+
41+
To run this program:
42+
43+
1. Save the code in a file, for example, `HelloWorld.hs`.
44+
2. Open a terminal (command prompt).
45+
3. Navigate to the directory where you saved the file.
46+
4. Compile the program using GHC (Glasgow Haskell Compiler):
47+
48+
```sh
49+
ghc --make HelloWorld.hs
50+
```
51+
52+
5. Run the compiled program:
53+
54+
```sh
55+
./HelloWorld
56+
```
57+
58+
You should see the output:
59+
60+
```
61+
Hello, World!
62+
```
63+
64+
Alternatively, you can run the Haskell code without compiling by using an interpreter like GHCi (the interactive environment for Haskell):
65+
66+
1. Open a terminal.
67+
2. Start GHCi by typing `ghci`.
68+
3. Load your file with the following command:
69+
70+
```sh
71+
:load HelloWorld.hs
72+
```
73+
74+
4. Run the `main` function:
75+
76+
```sh
77+
main
78+
```
79+
80+
This should also produce the output "Hello, World!" in the terminal.
81+
```

0 commit comments

Comments
 (0)