DEV Community

Germán Alberto Gimenez Silva
Germán Alberto Gimenez Silva

Posted on • Originally published at rubystacknews.com on

🚀 Building Intelligent APIs with Ruby and DeepSeek: A Step-by-Step Guide

February 17, 2025

How to Combine Ruby’s Simplicity with AI’s Power

As developers, we’re always looking for ways to build smarter, more efficient systems. Today, I want to share how you can create a Ruby-based API and supercharge it with AI capabilities using platforms like DeepSeek (hypothetical example) or similar services. Let’s dive in!


Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>


Need Expert Ruby on Rails Developers to Elevate Your Project?


Why Ruby and AI?

  • Ruby is renowned for its developer-friendly syntax and rapid prototyping (thanks to frameworks like Rails or Sinatra).
  • AI APIs (e.g., DeepSeek, OpenAI, or Anthropic) add natural language processing, predictions, or automation to your apps.
  • Together, they let you build intelligent systems in record time.

Step 1: Build a Ruby API

Let’s start by creating a lightweight API using Ruby on Rails (API-only mode).

Example Code :

# 1. Create a Rails API project  
rails new SmartAPI --api  
cd SmartAPI  

# 2. Generate a controller for AI interactions  
rails generate controller Api::V1::DeepSeek ask 
Enter fullscreen mode Exit fullscreen mode

Configure Routes (config/routes.rb):

namespace :api do  
  namespace :v1 do  
    get 'deep_seek/ask', to: 'deep_seek#ask'  
  end  
end 
Enter fullscreen mode Exit fullscreen mode

Controller Logic (app/controllers/api/v1/deep_seek_controller.rb):

module Api  
  module V1  
    class DeepSeekController < ApplicationController  
      def ask  
        user_query = params[:query]  
        # Add AI integration here (next step!)  
        render json: { query: user_query, response: "AI response goes here!" }  
      end  
    end  
  end  
end 
Enter fullscreen mode Exit fullscreen mode

Start the server with rails s, and test your endpoint:

curl http://localhost:3000/api/v1/deep_seek/ask?query="Hello, DeepSeek!" 
Enter fullscreen mode Exit fullscreen mode

Step 2: Integrate DeepSeek’s AI

Assume DeepSeek offers an API for text generation (similar to OpenAI). Here’s how to connect it:

Install HTTParty (add to Gemfile):

gem 'httparty' 
Enter fullscreen mode Exit fullscreen mode

Run bundle install.

Create a Service Class (app/services/deep_seek_service.rb):

require 'httparty'  

class DeepSeekService  
  include HTTParty  
  base_uri 'https://api.deepseek.com/v1'  

  def initialize(api_key)  
    @headers = {  
      "Authorization" => "Bearer #{api_key}",  
      "Content-Type" => "application/json"  
    }  
  end  

  def ask(prompt)  
    body = {  
      model: "deepseek-7b",  
      messages: [{ role: "user", content: prompt }]  
    }.to_json  

    self.class.post("/chat/completions", headers: @headers, body: body)  
  end  
end 
Enter fullscreen mode Exit fullscreen mode

Update the Controller :

def ask  
  user_query = params[:query]  
  api_key = ENV['DEEPSEEK_API_KEY'] # Always use environment variables!  
  service = DeepSeekService.new(api_key)  
  ai_response = service.ask(user_query)  
  render json: ai_response  
end 
Enter fullscreen mode Exit fullscreen mode

Example Use Case

Imagine building a customer support chatbot :

  1. Your Ruby API receives a user’s question.
  2. It calls DeepSeek’s API to generate a response.
  3. Returns the AI’s answer in real-time.

Result :

{  
  "query": "How do I reset my password?",  
  "response": "To reset your password, go to Settings > Security, and click 'Forgot Password'."  
} 
Enter fullscreen mode Exit fullscreen mode

Best Practices

  1. Security : Never hardcode API keys. Use dotenv or Rails credentials.
  2. Error Handling : Add rescues for API failures.
  3. Rate Limiting : Protect your API from abuse with tools like rack-attack.
  4. Testing : Write RSpec tests for your service class and endpoints.

Why This Matters

  • Speed : Ruby on Rails lets you ship faster.
  • Scalability : AI integration can handle complex tasks without reinventing the wheel.
  • Innovation : Combine Ruby’s elegance with cutting-edge AI to solve real-world problems.

Final Thoughts

Whether you’re building a chatbot, analytics tool, or automation workflow, Ruby and AI APIs are a powerful duo. Have you experimented with AI integrations? Let me know in the comments!

Try it yourself : Clone a sample repo or share your own tips below! 👇

Ruby #AI #APIs #Developer #TechInnovation #DeepSeek

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

DEV is better (more customized, reading settings like dark mode etc) when you're signed in!

Okay