Skip to content

Configuring OpenAI Credentials

Peter Goldstein edited this page Jan 16, 2023 · 2 revisions

API Keys and Organization IDs

To use Asimov you will need to have created an account with OpenAI. You can create an account at https://beta.openai.com .

Clients (like Asimov) authenticate to the OpenAI API using API keys. Once you have an OpenAI account you can generate API keys at https://beta.openai.com/account/api-keys .

In addition to API keys, OpenAI supports the use of Organizations to control access to different resources. If you need to access resources in a particular Organization you will need to get the id of that organization and pass it to Asimov. You can see the organizations of which you're a member at https://beta.openai.com/account/org-settings .

Configuring an Asimov::Client

An individual Asimov::Client can be initialized with an api_key and (optionally) an organization_id. For example

client = Asimov::Client.new(api_key: 'my_api_key', organization_id: 'my organization_id')

will return an Asimov::Client that uses an API key of my_api_key and an organization_id of my_organization_id. If one or both of these values is not provided the client will fall back to the default credentials (see below).

If no default api_key is set and no api_key parameter is provided then the initializer will raise a Asimov::MissingApiKeyError.

Setting Default Credentials

To avoid needing to set the API key (and Organization ID) every time your application instantiates an Asimov::Client you can configure your application to use default credentials.

Asimov.configure do |config|
  config.api_key = 'my_api_key'
  config.organization_id = 'my_organization_id'
end

A good practice for applications is to set the credentials in the environment and have the application load them dynamically on start up. For example:

Asimov.configure do |config|
  config.api_key = ENV.fetch('OPENAI_API_KEY')
  config.organization_id = ENV.fetch('OPENAI_ORGANIZATION_ID', nil)
end

This code requires that an OpenAI API key be set in the environment, or it will fail with an error.

Configuring Rails applications

Rails applications should place this default credentials code in a Rails initializer, such as config/initializers/asimov.rb.