choonkeat / elm-openai / OpenAI

This package is NOT intended to be run on the browser since apiKey will be exposed. Call it from a server like elm-webapp or lamdera.com instead.

OpenAI.Edits.createEdits input
    |> OpenAI.withConfig cfg
    |> Http.task

See https://beta.openai.com/docs/api-reference/introduction


type alias Config =
{ organizationId : String
, apiKey : String
, baseUrl : Maybe String 
}

The OpenAI API uses API keys for authentication. Visit your API Keys page to retrieve the API key you'll use in your requests.

Organization IDs can be found on your Organization settings page.

https://platform.openai.com/docs/api-reference/authentication

In your index.js, pass these values as flags to your Elm app

const app = Elm.MyApp.init({
    flags: {
        openaiConfig: {
            apiKey: process.env.OPENAI_API_KEY,
            organizationId: process.env.OPENAI_ORG_ID,
            baseUrl: process.env.OPENAI_BASE_URL || null
        }
    }
});

In your server Main.elm, the values will be passed in as a OpenAI.Config record

init : { openaiConfig : OpenAI.Config } -> ( Model, Cmd Msg )
init flags =
    ( model
    , OpenAI.Model.listModels
        |> OpenAI.withConfig flags.openaiConfig
        |> Http.task
        |> Task.attempt Done
    )

withConfig : Config -> Ext.Http.TaskInput x a -> Ext.Http.TaskInput x a

https://platform.openai.com/docs/api-reference/making-requests

Add necessary headers to authorize a http request to OpenAI. This is a necessary step before calling Http.task.

create
    { model = OpenAI.ModelID.TextEmbeddingAda002
    , input = "The food was delicious and the waiter..."
    , user = Nothing
    }
    |> OpenAI.withConfig cfg
    |> Http.task