openai: Servant bindings to OpenAI

[ bsd3, library, program, unclassified ] [ Propose Tags ] [ Report a vulnerability ]
Versions [RSS] 1.0.0, 1.0.1
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
Distributions
Executables openai-example
Downloads 10 total (10 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