openai-hs: Unofficial OpenAI client

[ bsd3, library, web ] [ Propose Tags ] [ Report a vulnerability ]

Unofficial OpenAI client


[Skip to Readme]

Modules

[Last Documentation]

  • OpenAI
    • OpenAI.Client
      • Internal
        • OpenAI.Client.Internal.Helpers

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.1.0, 0.2.0.0, 0.2.1.0, 0.2.2.0, 0.3.0.0, 0.3.0.1
Dependencies aeson, base (>=4.7 && <5), bytestring, casing, cpphs, http-client, http-types, openai-servant (>=0.2.1), servant, servant-auth-client, servant-client, servant-multipart-client, text [details]
License BSD-3-Clause
Copyright 2021-2023 Alexander Thiemann <mail@thiemann.at>
Author Alexander Thiemann <mail@thiemann.at>
Maintainer Alexander Thiemann <mail@thiemann.at>
Category Web
Home page https://github.com/agrafix/openai-hs#readme
Bug tracker https://github.com/agrafix/openai-hs/issues
Source repo head: git clone https://github.com/agrafix/openai-hs
Uploaded by AlexanderThiemann at 2023-07-05T03:31:25Z
Distributions
Downloads 757 total (18 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs not available [build log]
All reported builds failed as of 2023-07-05 [all 2 reports]

Readme for openai-hs-0.3.0.1

[back to package description]

openai-hs

Unofficial OpenAI SDK/client for Haskell. It's generated via servant-client from openai-servant with a small amount of hand-written code. Contributions are welcome!

Install

# stack
stack install openai-hs

# cabal
cabal install openai-hs

Example

{-# LANGUAGE OverloadedStrings #-}
import OpenAI.Client

import Network.HTTP.Client
import Network.HTTP.Client.TLS
import System.Environment (getEnv)
import qualified Data.Text as T

request :: ChatCompletionRequest
request = ChatCompletionRequest 
         { chcrModel = ModelId "gpt-3.5-turbo"
         , chcrMessages = 
            [ChatMessage { chmContent = "Write a hello world program in Haskell"
                         , chmRole = "user"
                         }
            ]
         , chcrTemperature = Nothing
         , chcrTopP = Nothing
         , chcrN = Nothing
         , chcrStream = Nothing
         , chcrStop = Nothing
         , chcrMaxTokens = Nothing
         , chcrPresencePenalty = Nothing
         , chcrFrequencyPenalty = Nothing
         , chcrLogitBias = Nothing
         , chcrUser = Nothing
         }

main :: IO ()
main =
  do manager <- newManager tlsManagerSettings
     apiKey <- T.pack <$> getEnv "OPENAI_KEY"
     -- create a openai client that automatically retries up to 4 times on network
     -- errors
     let client = makeOpenAIClient apiKey manager 4
     result <- completeChat client request        
     case result of
       Left failure -> print failure
       Right success -> print $ chrChoices success

Features

Supported actions:

  • List engines
  • Retrieve engine
  • Create text completion
  • Run semantic search

Running the tests

You can run all tests with stack test. You'll need an OpenAI API Key assigned to the OPENAI_KEY environment variable.