openai: Servant bindings to OpenAI

[ ai, api, bsd3, library, program, web ] [ Propose Tags ] [ Report a vulnerability ]

This package provides comprehensive and type-safe bindings to OpenAI, providing both a Servant interface and non-Servant interface for convenience.

Read the README below for a fully worked usage example.

Otherwise, browse the OpenAI.V1 module, which is the intended package entrypoint.


[Skip to Readme]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 1.0.0, 1.0.1, 1.1.0, 1.1.1, 1.2.0, 2.0.0, 2.1.0, 2.2.0, 2.2.1, 2.3.0, 2.4.0, 2.5.0, 2.5.1, 2.5.2, 2.5.3
Change log CHANGELOG.md
Dependencies aeson, base (>=4.15.0.0 && <5), bytestring, containers, filepath, http-api-data, http-client-tls, openai, servant, servant-client, servant-multipart-api, servant-multipart-client, text, time, vector [details]
License BSD-3-Clause
Copyright 2024 Gabriella Gonzalez
Author Gabriella Gonzalez
Maintainer GenuineGabriella@gmail.com
Uploaded by GabrielGonzalez at 2025-01-07T23:23:09Z
Reverse Dependencies 1 direct, 0 indirect [details]
Executables openai-example
Downloads 228 total (31 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2025-01-08 [all 1 reports]

Readme for openai-1.0.1

[back to package description]

openai

This provides a binding to OpenAI's API using servant

Example usage:

{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE OverloadedLists       #-}

module Main where

import Data.Foldable (traverse_)
import OpenAI.V1
import OpenAI.V1.Chat.Completions

import qualified Data.Text as Text
import qualified Data.Text.IO as Text.IO
import qualified System.Environment as Environment

main :: IO ()
main = do
    key <- Environment.getEnv "OPENAI_KEY"

    clientEnv <- getClientEnv "https://api.openai.com"

    let Methods{ createChatCompletion } = makeMethods clientEnv (Text.pack key)

    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