DEV Community

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

Posted on • Originally published at rubystacknews.com on

Integrating DeepSeek API with Ruby

March 10, 2025

Artificial Intelligence is revolutionizing the way we interact with text, and DeepSeek provides a powerful API to harness its capabilities. Whether you’re looking to correct grammar, generate poetry, or build AI-powered applications, this guide will show you how to integrate DeepSeek into your Ruby projects seamlessly.


💡 Want to Incorporate AI into Your Ruby or Ruby on Rails Project?

If you’re looking to enhance your application with AI-driven text processing, let’s connect! Feel free to reach out and discuss how we can integrate DeepSeek into your project. 🚀 Get in touch here.


Prerequisites

To get started with DeepSeek’s API, follow these steps:

1. Get Your API Key

Sign up on DeepSeek’s platform to obtain your API key.

2. Install Required Ruby Gems

Ensure you have the necessary Ruby gems installed for making HTTP requests and handling JSON data:

gem install faraday json
Enter fullscreen mode Exit fullscreen mode

Obtaining Available Models

To fetch the list of available models from DeepSeek, use the following curl command:

curl -L -X GET 'https://api.deepseek.com/models' \
-H 'Accept: application/json' \
-H 'Authorization: Bearer <token>'
Enter fullscreen mode Exit fullscreen mode

Example Response

{
  "object": "list",
  "data": [
    {"id": "deepseek-chat", "object": "model", "owned_by": "deepseek"},
    {"id": "deepseek-reasoner", "object": "model", "owned_by": "deepseek"}
  ]
}
Enter fullscreen mode Exit fullscreen mode

Example Code

Below is a simple Ruby script to interact with the DeepSeek API using Faraday for HTTP requests.

require 'faraday'
require 'json'

# Replace with your actual API key
DEEPSEEK_API_KEY = 'your-api-key-here'
API_URL = 'https://api.deepseek.com/v1/chat/completions' # Example endpoint

def call_deepseek_api(prompt)
  headers = {
    'Content-Type' => 'application/json',
    'Authorization' => "Bearer #{DEEPSEEK_API_KEY}"
  }

  body = {
    model: "deepseek-chat", # Specify the model to use
    messages: [{ role: "user", content: prompt }],
    temperature: 0.7, # Adjust creativity (0 = strict, 1 = creative)
    max_tokens: 500 # Limit response length
  }.to_json

  response = Faraday.post(API_URL, body, headers)

  if response.success?
    JSON.parse(response.body)['choices'][0]['message']['content']
  else
    "Error: #{response.status} - #{response.body}"
  end
end

# Example Usage
text = "I want to write very bad text on englush for check the application."
prompt = "Can you correct this text for me? #{text}"
response = call_deepseek_api(prompt)
puts response

# Another Example
prompt = "Write a haiku about artificial intelligence."
response = call_deepseek_api(prompt)
puts response
Enter fullscreen mode Exit fullscreen mode

Key Notes

1. Endpoints

Confirm the exact API endpoint in DeepSeek’s documentation (e.g., /v1/chat/completions).

2. Important Parameters

  • temperature: Controls randomness (lower = more deterministic, higher = more creative).
  • max_tokens: Limits the response length.
  • messages: Pass conversation history for better context.

3. Error Handling

For production use, consider adding:

  • Retries in case of network failures.
  • Timeouts to prevent long waits for a response.

Example Output

Running the code above might return something like:

---

**Original Text:**  
"I want to write very bad text on englush for check the application."

**Corrected Text:**  
"I want to write a very poorly written text in English to test the application."

---
Enter fullscreen mode Exit fullscreen mode

Need Expert Ruby on Rails Developers to Elevate Your Project?

Fill out our form! >>


Need Expert Ruby on Rails Developers to Elevate Your Project?


Conclusion

Integrating DeepSeek into your Ruby projects opens up exciting possibilities for AI-driven text processing. Whether you’re correcting text, generating poetry, or exploring new AI-powered functionalities, this API makes it easy to get started. Explore DeepSeek’s documentation for more advanced features, and consider building a wrapper class to streamline your workflow.

Now it’s your turn—how will you use DeepSeek in your projects? 🚀


Top comments (0)