Open AI API
Integrating OpenAI API and DeepSeek to your Lightning Rails Project

Step 1: Get an OpenAI API Key
Sign up or log in to OpenAI.
Navigate to API Keys in the OpenAI dashboard.
Generate a new API key and copy it. You will not be able to see it again.
Step 2: Install the OpenAI Ruby Gem
In your Gemfile, add:
gem "ruby-openai"
Then, run:
bundle install
Step 3: Configure API Key
Store your API key securely in your .env file:
OPENAI_ACCESS_TOKEN=sk-...MIQ4
Step 4: Create an OpenAI Service
Create a service file open_ai_api.rb inside app/services/
:
# app/services/open_ai_api.rb
class OpenAiApi
def initialize
@client = OpenAI::Client.new(access_token: ENV["OPENAI_ACCESS_TOKEN"])
end
def analyze_product(product)
chatgpt_response = @client.chat(parameters: {
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "You are a product analyst expert specialized in evaluating digital products and services. You have deep knowledge of market trends, user experience, and business models. Your analysis should be structured, data-driven, and actionable." },
{ role: "user", content: "Here is a product to analyze: #{product}. Please follow this process:\n\n1. Identify the key features and unique selling points\n2. Evaluate the market potential and target audience\n3. Analyze pricing strategy and business model\n4. Assess technical implementation and scalability\n5. Provide specific recommendations for improvement\n\nFormat your response in clear sections with bullet points where appropriate. Be concise but thorough in your analysis." }
]
})
chatgpt_response["choices"][0]["message"]["content"]
end
end
Step 5: Use the Service in a Controller
For example, in products_controller.rb
:
class ProductsController < ApplicationController
def analyze
@product = params[:product]
openai_service = OpenAiApi.new
@analysis = openai_service.analyze_product(@product)
end
end
Then, you can create a route in config/routes.rb:
post "/analyze_product", to: "products#analyze"
Switching to DeepSeek API
If you want to switch from OpenAI to DeepSeek, follow these modifications:
Step 1: Change API Key
Sign up for an API key at DeepSeek and store it in .env
:
DEEPSEEK_ACCESS_TOKEN=ds-...MIQ4
Step 2: Use the Same OpenAI Wrapper for DeepSeek
DeepSeek API should compatible with the OpenAI client wrapper, meaning you only need to modify your environment variable:
class DeepSeekApi
def initialize
@client = OpenAI::Client.new(access_token: ENV["DEEPSEEK_ACCESS_TOKEN"], uri_base: "https://api.deepseek.com/v1")
end
def analyze_product(product)
chatgpt_response = @client.chat(parameters: {
model: "deepseek-chat",
messages: [
{ role: "system", content: "You are a product analyst expert..." },
{ role: "user", content: "Here is a product to analyze: #{product}..." }
]
})
chatgpt_response["choices"][0]["message"]["content"]
end
end
Step 3: Use DeepSeek Instead
Replace OpenAiApi references with DeepSeekApi in your controllers:
deep_seek_service = DeepSeekApi.new
@analysis = deep_seek_service.analyze_product(@product)
That's it! You have now integrated OpenAI with your Lightning Rails project and learned how to switch to DeepSeek using the same API wrapper.
Last updated
Was this helpful?