packages feed

openai-2.5.1 / README.md

# `openai`This provides a binding to OpenAI's API using `servant`## Example usage```haskell{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE NamedFieldPuns        #-}{-# LANGUAGE OverloadedStrings     #-}{-# LANGUAGE OverloadedLists       #-}module Main whereimport Data.Foldable (traverse_)import OpenAI.V1import OpenAI.V1.Chat.Completionsimport qualified Data.Text as Textimport qualified Data.Text.IO as Text.IOimport qualified System.Environment as Environmentmain :: IO ()main = do    key <- Environment.getEnv "OPENAI_KEY"    clientEnv <- getClientEnv "https://api.openai.com"    let Methods{ createChatCompletion } = makeMethods clientEnv (Text.pack key) Nothing Nothing    text <- Text.IO.getLine    ChatCompletionObject{ choices } <- createChatCompletion _CreateChatCompletion        { messages = [ User{ content = [ Text{ text } ], name = Nothing } ]        , model = "gpt-4o-mini"        }    let display Choice{ message } = Text.IO.putStrLn (messageToContent message)    traverse_ display choices```### Responses API (JSON)```haskell{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE NamedFieldPuns        #-}{-# LANGUAGE OverloadedStrings     #-}import qualified Data.Text as Textimport qualified OpenAI.V1 as V1import qualified OpenAI.V1.Responses as Responsesmain :: IO ()main = do    key <- System.Environment.getEnv "OPENAI_KEY"    env <- V1.getClientEnv "https://api.openai.com"    let V1.Methods{ createResponse } = V1.makeMethods env (Text.pack key) Nothing Nothing    let req = Responses._CreateResponse            { Responses.model = "gpt-5"            , Responses.input = Just (Responses.Input                [ Responses.Item_Input_Message                    { Responses.role = Responses.User                    , Responses.content = [ Responses.Input_Text{ Responses.text = "Say hello in one sentence." } ]                    , Responses.status = Nothing                    }                ])            }    res <- createResponse req    print res```### Responses API (Structured Outputs)```haskell{-# LANGUAGE DuplicateRecordFields #-}{-# LANGUAGE NamedFieldPuns        #-}{-# LANGUAGE OverloadedLists       #-}{-# LANGUAGE OverloadedStrings     #-}import qualified Data.Aeson as Aesonimport qualified Data.Text as Textimport qualified OpenAI.V1 as V1import qualified OpenAI.V1.Responses as Responsesmain :: IO ()main = do    key <- System.Environment.getEnv "OPENAI_KEY"    env <- V1.getClientEnv "https://api.openai.com"    let V1.Methods{ createResponse } = V1.makeMethods env (Text.pack key) Nothing Nothing    let schemaObject = Aeson.object            [ "type" Aeson..= ("object" :: Text.Text)            , "properties" Aeson..= Aeson.object                [ "greeting" Aeson..= Aeson.object [ "type" Aeson..= ("string" :: Text.Text) ] ]            , "required" Aeson..= (["greeting"] :: [Text.Text])            , "additionalProperties" Aeson..= False            ]    let req = Responses._CreateResponse            { Responses.model = "gpt-5"            , Responses.input = Just (Responses.Input                [ Responses.Item_Input_Message                    { Responses.role = Responses.User                    , Responses.content = [ Responses.Input_Text{ Responses.text = "Say hello." } ]                    , Responses.status = Nothing                    }                ])            , Responses.text = Just Responses._TextConfig                { Responses.format = Responses.TextFormat_JSON_Schema                    { Responses.name = "greeting"                    , Responses.description = Just "Greeting payload"                    , Responses.schema = Just schemaObject                    , Responses.strict = Just True                    }                }            }    res <- createResponse req    print res```## Setup### Using Nix with Flakes (Recommended)This project uses Nix with flakes for development environment setup.1. Ensure you have Nix with flakes enabled2. Copy the sample environment file and configure your OpenAI API key:```bash# Copy the sample environment filecp .envrc.sample .envrc```3. Edit the `.envrc` file and replace the placeholder API key with your actual key4. Use direnv to automatically load the development environment:```bash# Install direnv if you haven't already# macOS: brew install direnv# Linux: your-package-manager install direnv# Enable direnv hook in your shelleval "$(direnv hook bash)" # or zsh, fish, etc.# Clone the repository and enter the directorygit clone https://github.com/MercuryTechnologies/openai.gitcd openai# Allow direnv (this will automatically load the environment)direnv allow```### Manual SetupWithout Nix:```bash# Clone the repositorygit clone https://github.com/MercuryTechnologies/openai.gitcd openai# Build with cabalcabal build```## Environment VariablesSet your OpenAI API key as an environment variable:```bash# Option 1: Set directly in your shellexport OPENAI_KEY="your-openai-api-key"# Option 2: Using .envrc with direnv (recommended)(umask 077; cp .envrc.sample .envrc)# Edit .envrc to add your API keydirenv allow```The API key is needed for running the test suite and example program.## TestingRun the test suite:```bashcabal test```The test suite is in the `tasty/` directory with test data located in `tasty/data/`.## Running the Example```bash# Make sure your API key is set (either via .envrc or export)# If using direnv with proper .envrc setup, this happens automatically# Build and run the examplecabal run openai-example```