हम cookies इस्तेमाल करते हैं. Cookies और ब्राउज़र स्टोरेज आपको साइन इन रखते हैं और आपकी प्राथमिकताएं याद रखते हैं। आपकी अनुमति से हम यह भी मापते हैं कि साइट कैसे इस्तेमाल होती है। आप कभी भी । हमारी cookie नीति देखें।

Ruby

SDK गाइड

Ruby

api14 के साथ ruby-openai gem इस्तेमाल करें: uri_base सेट करें, चैट, स्ट्रीमिंग, टूल कॉलिंग, स्ट्रक्चर्ड आउटपुट, त्रुटियाँ और रीट्राई के उदाहरण।

api14 के साथ ruby-openai gem इस्तेमाल करें। uri_base सेट करें और openai/gpt-5-mini जैसे मॉडल id इस्तेमाल करें।

इंस्टॉल करें

gem install ruby-openai
export API14_API_KEY="sk-api14-..."

कॉन्फ़िगर करें

ruby-openai अपना API संस्करण (v1) uri_base के अंत में खुद जोड़ता है, इसलिए बेस https://api14.com/api है और अनुरोध https://api14.com/api/v1/... पर जाते हैं।

require "openai"

client = OpenAI::Client.new(
  access_token: ENV.fetch("API14_API_KEY"),
  uri_base: "https://api14.com/api",
  request_timeout: 120
)

Rails ऐप में gem को एक बार initializer में कॉन्फ़िगर करें और क्लाइंट बिना आर्गुमेंट के बनाएँ:

# config/initializers/openai.rb
OpenAI.configure do |config|
  config.access_token = ENV.fetch("API14_API_KEY")
  config.uri_base = "https://api14.com/api"
  config.request_timeout = 120 # सेकंड, डिफ़ॉल्ट 120
end

client = OpenAI::Client.new

चैट

response = client.chat(
  parameters: {
    model: "openai/gpt-5-mini",
    messages: [{ role: "user", content: "Explain vector databases in two sentences." }]
  }
)
puts response.dig("choices", 0, "message", "content")

स्ट्रीमिंग

stream के रूप में एक proc पास करें; वह हर चंक पर एक बार कॉल होता है:

client.chat(
  parameters: {
    model: "anthropic/claude-sonnet-4.5",
    messages: [{ role: "user", content: "Write a haiku about APIs." }],
    stream: proc { |chunk, _bytesize| print chunk.dig("choices", 0, "delta", "content") }
  }
)

देखें स्ट्रीमिंग।

टूल कॉलिंग

require "json"

tools = [{
  type: "function",
  function: {
    name: "get_weather",
    description: "Current weather for a city",
    parameters: { type: "object", properties: { city: { type: "string" } }, required: ["city"] }
  }
}]
messages = [{ role: "user", content: "What's the weather in Istanbul?" }]

loop do
  response = client.chat(parameters: { model: "openai/gpt-5-mini", messages: messages, tools: tools })
  message = response.dig("choices", 0, "message")
  unless message["tool_calls"]
    puts message["content"]
    break
  end

  messages << message
  message["tool_calls"].each do |call|
    args = JSON.parse(call.dig("function", "arguments"))
    messages << { role: "tool", tool_call_id: call["id"], content: get_weather(args["city"]).to_json } # आपका मेथड
  end
end

देखें टूल कॉलिंग।

स्ट्रक्चर्ड आउटपुट

response = client.chat(
  parameters: {
    model: "openai/gpt-5-mini",
    messages: [{ role: "user", content: "Extract: Ada Lovelace, born 1815 in London." }],
    response_format: {
      type: "json_schema",
      json_schema: {
        name: "person",
        strict: true,
        schema: {
          type: "object",
          properties: { name: { type: "string" }, born: { type: "integer" }, city: { type: "string" } },
          required: %w[name born city],
          additionalProperties: false
        }
      }
    }
  }
)
person = JSON.parse(response.dig("choices", 0, "message", "content"))

देखें स्ट्रक्चर्ड आउटपुट।

इमेज इनपुट

require "base64"

image = Base64.strict_encode64(File.binread("photo.png"))

client.chat(
  parameters: {
    model: "google/gemini-2.5-flash",
    messages: [{
      role: "user",
      content: [
        { type: "text", text: "What is in this picture?" },
        { type: "image_url", image_url: { url: "data:image/png;base64,#{image}" } }
      ]
    }]
  }
)

देखें विज़न और फ़ाइलें।

गेटवे पैरामीटर

gem parameters को जैसा है वैसा आगे भेजता है, इसलिए गेटवे फ़ील्ड सीधे काम करते हैं:

client.chat(
  parameters: {
    model: "openai/gpt-5-mini",
    models: ["anthropic/claude-sonnet-4.5"], # फ़ॉलबैक
    provider: { sort: "throughput" },
    reasoning: { effort: "low" },
    messages: [{ role: "user", content: "Hi" }]
  }
)

एम्बेडिंग

response = client.embeddings(
  parameters: { model: "openai/text-embedding-3-small", input: ["The quick brown fox", "Lorem ipsum"] }
)
puts response.dig("data", 0, "embedding").length

त्रुटियाँ

HTTP त्रुटियाँ Faraday::Error के रूप में उठती हैं; response hash में स्टेटस, हेडर और OpenAI संरचना वाली बॉडी होती है:

begin
  client.chat(parameters: { model: "openai/gpt-5-mini", messages: [{ role: "user", content: "Hi" }] })
rescue Faraday::Error => e
  case e.response&.dig(:status)
  when 402 then warn "Balance too low, add funds (insufficient_balance)"
  when 429 then warn "Rate limited, retry after #{e.response[:headers]['retry-after']}s"
  else raise
  end
end

हर कोड के लिए देखें त्रुटियाँ।

बैकऑफ़ के साथ रीट्राई

ruby-openai डिफ़ॉल्ट रूप से रीट्राई नहीं करता। एक छोटा रैपर rate_limited, 5xx और टाइमआउट को रीट्राई करता है, और daily_limit_reached तथा insufficient_balance को छोड़ देता है:

def with_retry(attempts: 5)
  attempts.times do |i|
    return yield
  rescue Faraday::TimeoutError, Faraday::ConnectionFailed
    raise if i == attempts - 1
    sleep([2**i, 60].min + rand)
  rescue Faraday::Error => e
    status = e.response&.dig(:status).to_i
    retryable = status >= 500 || (status == 429 && !e.response[:body].to_s.include?("daily_limit_reached"))
    raise if !retryable || i == attempts - 1
    wait = e.response.dig(:headers, "retry-after")&.to_i || 2**i
    sleep([wait, 60].min + rand)
  end
end

with_retry { client.chat(parameters: { model: "openai/gpt-5-mini", messages: [{ role: "user", content: "Hi" }] }) }

देखें रेट लिमिट।

सभी क्षमताएँ

import OpenAI from "openai";

const client = new OpenAI({
  baseURL: "https://api14.com/api/v1",
  apiKey: process.env.API14_API_KEY,
});

const result = await client.chat.completions.create({
  "model": "openai/gpt-5-mini",
  "messages": [
    {
      "role": "user",
      "content": "Explain vector databases in two sentences."
    }
  ]
});
console.log(result.choices[0].message.content);