Open AI API

Integrating OpenAI API and DeepSeek to your Lightning Rails Project

The anatomy of the perfect Prompt - From Open AI CEO

Step 1: Get an OpenAI API Key

  1. Sign up or log in to OpenAI.

  2. Navigate to API Keys in the OpenAI dashboard.

  3. 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:

Then, run:

Step 3: Configure API Key

Store your API key securely in your .env file:

Step 4: Create an OpenAI Service

Create a service file open_ai_api.rb inside app/services/:

๐Ÿ’ก Pro Tip: Use this master prompt for better results

Copy and adapt this prompt to your needs, it was published on twitter by Greg Brockman, the current president of Open AI:

Step 5: Use the Service in a Controller

For example, in products_controller.rb:

Then, you can create a route in config/routes.rb:


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:

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:

Step 3: Use DeepSeek Instead

Replace OpenAiApi references with DeepSeekApi in your controllers:

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?