Undefined method `api_key=' for OpenAI:Module (NoMethodError)


I am new to Open AI and want to create a simple prototype of a RoR application which runs a generative chatbot. I have created my web app and have imported the
gem ‘openai’ in my Gemfile. I have also created an initializer and set it with my API key thus.

OpenAI.api_key = ENV[‘my-key’]

However, when I try to do a dev start, I get the following error:

undefined method `api_key=’ for OpenAI:Module (NoMethodError)

I have tried running bundle install multiple times but it does not fix the issue.

Any help will be much appreciated!

Which library are you using? There are two listed by openAI for ruby are:

In case of both of them you need to create an instance of OpenAI::Client passing API key as constructor argument.

For the first one nileshtrivedi/openai:

client = OpenAI::Client.new(api_key: ENV["OPENAI_API_KEY"])

And for the second alexrudall/ruby-openai:

client = OpenAI::Client.new(access_token: ENV["OPENAI_API_KEY"])

Hey Walaszczykm,

Thanks for your answer :blush:

I am trying with the second one, and it gives me the following message:

`initialize': missing keyword: api_key (ArgumentError)

and when I try replacing access_token with api_key, I have the following message:

undefined method `transcribe' for #<OpenAI::Client:0x00007f8d7e98ac70> (NoMethodError)

Any idea why it behaves this way :question:

Thank you in advance

1 Like

‘::Ruby::OpenAI’ module is removed. Please add this ‘OpenAI’
change all references from Ruby::OpenAI to OpenAI .