Module: DemoLorem::Helpers::AiApiHelper
- Included in:
- Generator
- Defined in:
- lib/demo_lorem/helpers/ai_api_helper.rb
Overview
AiApiHelper module fetches AI-generated content using OpenAI's API for a given topic and length
Instance Method Summary collapse
Instance Method Details
#fetch_ai_content(topic, length, api_key) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/demo_lorem/helpers/ai_api_helper.rb', line 13 def fetch_ai_content(topic, length, api_key) return 'OpenAI API key is required for AI-based generation' if api_key.nil? begin client = OpenAI::Client.new(access_token: api_key) response = client.completions( parameters: { model: 'text-davinci-003', prompt: "Generate #{length} characters of text about #{topic}", max_tokens: length } ) response['choices'].first['text'].strip rescue StandardError => e "An error occurred while fetching AI content: #{e.}" end end |