最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

undefined method `api_key=' for OpenAI:Module for ruby - Stack Overflow

programmeradmin4浏览0评论

I have the following code:

require 'openai'

# Set up your API key
OpenAI.api_key = "your_api_key_here"

# Create a function to interact with the API
def generate_text(prompt, temperature=0.7, max_tokens=100, top_p=1.0)
  begin
    response = OpenAI::Completion.create(
      engine: "text-davinci-002", # You can choose from available engines like "text-davinci-002", "text-curie-002", "text-babbage-002", etc.
      prompt: prompt,
      temperature: temperature,
      max_tokens: max_tokens,
      top_p: top_p,
      n: 1,
      stop: nil,
      echo: false
    )

    return response.choices.first.text.strip
  rescue StandardError => e
    puts "Error generating text: #{e.message}"
    return nil
  end
end

# Use the function with a clear and specific prompt
prompt = "Translate the following English text to French: 'Hello, how are you?'"
generated_text = generate_text(prompt)

puts "Generated text: #{generated_text}"

...and when I run this I get the error undefined method api_key= for OpenAI:Module .

I am using the ruby-openai gem and ruby 3.2.2:

gem list ruby-openai
*** LOCAL GEMS ***
ruby-openai (7.3.1)

Thanks in advance!

I have the following code:

require 'openai'

# Set up your API key
OpenAI.api_key = "your_api_key_here"

# Create a function to interact with the API
def generate_text(prompt, temperature=0.7, max_tokens=100, top_p=1.0)
  begin
    response = OpenAI::Completion.create(
      engine: "text-davinci-002", # You can choose from available engines like "text-davinci-002", "text-curie-002", "text-babbage-002", etc.
      prompt: prompt,
      temperature: temperature,
      max_tokens: max_tokens,
      top_p: top_p,
      n: 1,
      stop: nil,
      echo: false
    )

    return response.choices.first.text.strip
  rescue StandardError => e
    puts "Error generating text: #{e.message}"
    return nil
  end
end

# Use the function with a clear and specific prompt
prompt = "Translate the following English text to French: 'Hello, how are you?'"
generated_text = generate_text(prompt)

puts "Generated text: #{generated_text}"

...and when I run this I get the error undefined method api_key= for OpenAI:Module .

I am using the ruby-openai gem and ruby 3.2.2:

gem list ruby-openai
*** LOCAL GEMS ***
ruby-openai (7.3.1)

Thanks in advance!

Share Improve this question asked Jan 20 at 16:01 Mark LocklearMark Locklear 5,3251 gold badge57 silver badges87 bronze badges 3
  • 1 As the error states the OpenAI module does not have an api_key method. The Docs show the passing of an "access_token" through the initialization of the client. Maybe that is what you were trying to do? Sidenote: I don't see you using a OpenAI::Client anywhere. thsi class appears to be the basis for the entire library so I would start there. – engineersmnky Commented Jan 20 at 18:05
  • I tired using access_token also and I get the same undefined method error. – Mark Locklear Commented Jan 21 at 13:32
  • i don't get the same error. your configuration and code doesn't match any exmaples in the docs. at least, read the friendly manual: github.com/alexrudall/ruby-openai#completions – Alex Commented Jan 23 at 6:10
Add a comment  | 

1 Answer 1

Reset to default 0

OK, finally carved out some time to work this out. Here is a soltuion that works:

require 'openai'
require 'dotenv'

Dotenv.load

# Configure the OpenAI API client
OpenAI.configure do |config|
  config.access_token = ENV.fetch("OPENAI_API_KEY") # Assumes you've set the environment variable
end


# Initialize the OpenAI client
client = OpenAI::Client.new

# Define a simple prompt
prompt = "Translate the following English text to French: 'Hello, how are you?'"

# Make a request to the ChatGPT API (GPT-4)
response = client.chat(
  parameters: {
    model: "gpt-4", # Or "gpt-3.5-turbo" if you prefer
    messages: [
      { role: "system", content: "You are a helpful assistant." },
      { role: "user", content: prompt }
    ],
    temperature: 0.7
  }
)

# Display the response
if response && response['choices']
  puts "ChatGPT says: #{response['choices'].first['message']['content']}"
else
  puts "Error: No response from ChatGPT."
end
发布评论

评论列表(0)

  1. 暂无评论