OpenAI

Enhanced Advanced

The OpenAI integration allows content running on Posit Connect to access the OpenAI API using a shared API key. When content is associated with an OpenAI integration, Connect sets the OPENAI_API_KEY environment variable, which is automatically detected by the official OpenAI SDKs.

This integration is useful when you want to:

Configuring an OpenAI integration in Posit Connect involves two main steps, each performed by different administrators.

Step 1: OpenAI administrator

Obtain an OpenAI API key

The OpenAI administrator creates an API key in the OpenAI Platform.

Note

OpenAI API keys should be treated as sensitive credentials. Consider creating a dedicated API key for use with Connect so it can be rotated or revoked independently.

Step 2: Posit Connect administrator

Create integration in Posit Connect

Using the information from the OpenAI administrator, the Posit Connect administrator creates an integration through the dashboard’s System > Integrations settings. Once the OAuth integration has been created in Connect, it is available for use by all publishers.

Create an OpenAI integration within Connect

Alternatively, the example below shows how to create an OpenAI integration using curl and the Connect Server API.

Note

Replace connect.example.org with the address of the Connect server.

Terminal
curl -H "Authorization: Key ${CONNECT_API_KEY}" \
  -XPOST https://connect.example.org/__api__/v1/oauth/integrations \
  --data '{
    "template": "openai",
    "name": "OpenAI",
    "description": "A helpful description for publishers to use when choosing an integration for their content.",
    "config": {
      "api_key": "<openai-api-key>"
    }
  }'
# 200 OK
# {"guid": "<integration-guid>", ... }

Next steps

Once the integration is configured, publishers can use it in their content. Most OpenAI client libraries detect the the OPENAI_API_KEY environment variable by default:

from openai import OpenAI

# The client automatically uses the OPENAI_API_KEY environment variable
client = OpenAI()

response = client.chat.completions.create(
    model="gpt-4",
    messages=[{"role": "user", "content": "Hello!"}]
)
library(ellmer)

chat <- chat_openai()
chat$chat("Tell me three jokes about statisticians")

Local development

When developing locally, set the OPENAI_API_KEY environment variable in your development environment, or use a .env file. The content will use your local API key during development and the Connect-managed key when deployed.