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

PHP

SDK गाइड

PHP

api14 के साथ openai-php/client इस्तेमाल करें: इंस्टॉल, क्लाइंट कॉन्फ़िगरेशन, चैट, स्ट्रीमिंग, टूल कॉलिंग, एम्बेडिंग, त्रुटियाँ और बैकऑफ़ के साथ रीट्राई।

api14 के साथ openai-php/client इस्तेमाल करें। क्लाइंट को factory से बनाएँ, बेस URI को api14 पर सेट करें, और मॉडल id provider/model फ़ॉर्मेट में रखें।

इंस्टॉल करें

composer require openai-php/client guzzlehttp/guzzle
export API14_API_KEY="sk-api14-..."

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

<?php
require __DIR__ . '/vendor/autoload.php';

$client = OpenAI::factory()
    ->withApiKey(getenv('API14_API_KEY'))
    ->withBaseUri(getenv('API14_BASE_URL') ?: 'https://api14.com/api/v1')
    ->withHttpClient(new \GuzzleHttp\Client(['timeout' => 120, 'connect_timeout' => 5]))
    ->make();

कुंजी को एनवायरनमेंट (या अपने फ़्रेमवर्क की .env) में रखें, सोर्स कोड में कभी नहीं। Guzzle का timeout सेकंड में है और पूरे अनुरोध को कवर करता है; लंबी रीज़निंग या स्ट्रीमिंग कॉल के लिए इसे बढ़ाएँ।

चैट

$result = $client->chat()->create([
    'model' => 'openai/gpt-5-mini',
    'messages' => [
        ['role' => 'user', 'content' => 'Explain vector databases in two sentences.'],
    ],
]);

echo $result->choices[0]->message->content;

स्ट्रीमिंग

$stream = $client->chat()->createStreamed([
    'model' => 'anthropic/claude-sonnet-4.5',
    'messages' => [['role' => 'user', 'content' => 'Write a haiku about APIs.']],
]);

foreach ($stream as $response) {
    echo $response->choices[0]->delta->content;
}

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

टूल कॉलिंग

$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?"]];

while (true) {
    $response = $client->chat()->create(['model' => 'openai/gpt-5-mini', 'messages' => $messages, 'tools' => $tools]);
    $message = $response->choices[0]->message;
    if (empty($message->toolCalls)) {
        echo $message->content;
        break;
    }
    $messages[] = $message->toArray();
    foreach ($message->toolCalls as $call) {
        $args = json_decode($call->function->arguments, true);
        $messages[] = [
            'role' => 'tool',
            'tool_call_id' => $call->id,
            'content' => json_encode(getWeather($args['city'])), // आपका फ़ंक्शन
        ];
    }
}

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

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

$result = $client->chat()->create([
    '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' => ['name', 'born', 'city'],
                'additionalProperties' => false,
            ],
        ],
    ],
]);

$person = json_decode($result->choices[0]->message->content, true, flags: JSON_THROW_ON_ERROR);

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

इमेज इनपुट

$image = base64_encode(file_get_contents('photo.png'));

$result = $client->chat()->create([
    '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}"]],
        ],
    ]],
]);

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

एम्बेडिंग

$result = $client->embeddings()->create([
    'model' => 'openai/text-embedding-3-small',
    'input' => ['The quick brown fox', 'Lorem ipsum'],
]);

echo count($result->embeddings[0]->embedding);

स्पीच

$audio = $client->audio()->speech([
    'model' => 'openai/gpt-4o-mini-tts',
    'input' => 'Hello from api14!',
    'voice' => 'alloy',
]);

file_put_contents('speech.mp3', $audio);

त्रुटियाँ

गेटवे की त्रुटियाँ OpenAI की संरचना में होती हैं, इसलिए क्लाइंट स्टेटस और मशीन कोड के साथ OpenAI\Exceptions\ErrorException उठाता है:

use OpenAI\Exceptions\ErrorException;

try {
    $client->chat()->create([
        'model' => 'openai/gpt-5-mini',
        'messages' => [['role' => 'user', 'content' => 'Hi']],
    ]);
} catch (ErrorException $e) {
    match ($e->getStatusCode()) {
        402 => error_log('Balance too low, add funds: ' . $e->getErrorCode()), // insufficient_balance
        429 => error_log('Rate limited, retry in 60s: ' . $e->getErrorCode()),  // rate_limited
        default => throw $e,
    };
}

गेटवे rate_limited के साथ Retry-After: 60 भेजता है। देखें रेट लिमिट और त्रुटियाँ।

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

openai-php/client अपने आप रीट्राई नहीं करता। कॉल को एक छोटे लूप में लपेटें जो rate_limited और 5xx को रीट्राई करे और बाकी सब पर हार मान ले:

use OpenAI\Exceptions\ErrorException;
use OpenAI\Exceptions\TransporterException;

function withRetry(callable $fn, int $attempts = 5): mixed
{
    for ($i = 0; ; $i++) {
        try {
            return $fn();
        } catch (ErrorException $e) {
            $retryable = $e->getErrorCode() === 'rate_limited' || $e->getStatusCode() >= 500;
            if (!$retryable || $i + 1 >= $attempts) throw $e;
        } catch (TransporterException $e) { // नेटवर्क त्रुटि या टाइमआउट
            if ($i + 1 >= $attempts) throw $e;
        }
        usleep((min(60, 2 ** $i) * 1000 + random_int(0, 1000)) * 1000); // बैकऑफ़ + jitter
    }
}

$result = withRetry(fn () => $client->chat()->create([
    'model' => 'openai/gpt-5-mini',
    'messages' => [['role' => 'user', 'content' => 'Hi']],
]));

daily_limit_reached और insufficient_balance को रीट्राई नहीं किया जाता, क्योंकि कुछ सेकंड रुकने से वे ठीक नहीं होते।

सभी क्षमताएँ

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);