કન્ટેન્ટ પર જાઓ
ઝડપી શરૂઆત

ઝડપી શરૂઆત

પહેલી રિક્વેસ્ટ કરો: તમારી કી મેળવો, એક મેસેજ મોકલો અને જવાબ વાંચો. API OpenAI અને Anthropic ફોર્મેટ બોલે છે, તેથી તમે પહેલેથી વાપરો છો તે SDK તેનું base URL અને કી બદલ્યા પછી કામ કરે છે.

1. તમારી કી મેળવો

દરેક એકાઉન્ટ પાસે એક API કી હોય છે. સાઇન ઇન કરો, Keys & usage ખોલો અને તેને કોપી કરો. કી દરેક એન્ડપોઇન્ટ અને દરેક મોડેલ પર કામ કરે છે. Keys & usage

2. તમારા ક્લાયન્ટને API તરફ દોરો

બધા એન્ડપોઇન્ટ્સ એક base URL હેઠળ છે. OpenAI SDK તેને અંતે /v1 સાથે લે છે; Anthropic SDK તેના વિના લે છે, કારણ કે તે /v1/messages જાતે ઉમેરે છે.

ક્લાયન્ટ Base URL
OpenAI SDKs અને OpenAI-સુસંગત ક્લાયન્ટ્સ https://api.shannon-ai.com/v1
Anthropic SDKs https://api.shannon-ai.com

ત્રણ એન્ડપોઇન્ટ્સ ટેક્સ્ટ બનાવે છે. તેઓ સમાન મોડેલ્સ સુધી પહોંચે છે; જેનું ફોર્મેટ તમારો કોડ પહેલેથી વાપરે છે તે પસંદ કરો.

એન્ડપોઇન્ટ ફોર્મેટ
POST /v1/chat/completions OpenAI Chat Completions
POST /v1/messages Anthropic Messages
POST /v1/responses OpenAI Responses

3. પહેલી રિક્વેસ્ટ મોકલો

Chat Completions એન્ડપોઇન્ટ પર shannon-3 ને એક user મેસેજ મોકલો. YOUR_API_KEY ની જગ્યાએ તમારી કી મૂકો.

POST https://api.shannon-ai.com/v1/chat/completions

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.shannon-ai.com/v1"
)

response = client.chat.completions.create(
    model="shannon-3",
    messages=[
        {"role": "user", "content": "Hello, Shannon!"}
    ],
    max_tokens=1024
)

print(response.choices[0].message.content)
ફીલ્ડ પ્રકાર ડિફોલ્ટ વર્ણન
model string જવાબ આપનાર મોડેલનો id. id Models & pricing પર યાદી થયેલા છે.
messages array અત્યાર સુધીની વાતચીત: role અને content વાળા ઓબ્જેક્ટ્સ. છેલ્લો user મેસેજ prompt છે. જરૂરી.
max_tokens integer 4096 રિક્વેસ્ટનું આઉટપુટ બજેટ, 1 થી 65,536. રિક્વેસ્ટ આ રકમ તમારા બેલેન્સમાંથી અલગ રાખે છે અને તેણે ખરેખર વાપરેલાનો ચાર્જ લાગે છે.
stream boolean false true જવાબ લખાતો હોય ત્યારે ટુકડે ટુકડે મોકલે છે.

રિક્વેસ્ટ બોડી JSON છે અને તેને Content-Type: application/json હેડર જોઈએ. ફીલ્ડ્સની સંપૂર્ણ યાદી એન્ડપોઇન્ટ પેજ પર છે. Chat Completions

જવાબ

stream વિનાનો કોલ જવાબ પૂરો થાય ત્યારે એક JSON ઓબ્જેક્ટ પાછો આપે છે. નીચેના ટેક્સ્ટ અને સંખ્યાઓ ઉદાહરણો છે.

200 JSON
{
  "id": "chatcmpl-9b2f6c1e4a7d4f0f8a3c5d2e1b6a7c90",
  "object": "chat.completion",
  "created": 1791590400,
  "model": "shannon-3",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": "Hello! What would you like to work on?",
        "reasoning_content": "A greeting. A short, friendly reply fits."
      },
      "finish_reason": "stop"
    }
  ],
  "usage": {
    "prompt_tokens": 1386,
    "completion_tokens": 74,
    "total_tokens": 1460
  }
}
ફીલ્ડ પ્રકાર વર્ણન
id string chatcmpl- પછી 32 હેક્સાડેસિમલ અક્ષરો.
object string હંમેશા chat.completion.
created integer જવાબનો સમય Unix સેકન્ડમાં.
model string જવાબ આપનાર મોડેલનો પ્રકાશિત id, તમે alias અથવા બીજી જોડણી મોકલી હોય ત્યારે પણ.
choices array હંમેશા એક એન્ટ્રી, index 0 સાથે.
choices[0].message.role string હંમેશા assistant.
choices[0].message.content string | null જવાબ. જવાબ tool_calls લઈ જાય ત્યારે તે null હોઈ શકે.
choices[0].message.reasoning_content string | null જવાબ પહેલાંનું મોડેલનું reasoning, અથવા કોઈ ન હોય ત્યારે null.
choices[0].message.tool_calls array મોડેલ તમે જાહેર કરેલા ટૂલ્સમાંથી કોઈ એકને કોલ કરે ત્યારે જ હાજર.
choices[0].finish_reason string જવાબ માટે stop, મોડેલ ટૂલ કોલ કરે ત્યારે tool_calls.
usage.prompt_tokens integer આ જવાબ માટે મોડેલે વાંચેલા ટોકન્સ. shannon-2-* અને Shannon 3 ફેમિલી પર આ તમે મોકલેલા ટેક્સ્ટ કરતાં વધુ હોઈ શકે.
usage.completion_tokens integer મોડેલે લખેલા ટોકન્સ.
usage.total_tokens integer કુલ. Shannon મોડેલનો ચાર્જ આ સંખ્યા પરથી ગણાય છે.
  • usage માં ટોકન ગણતરી હોય છે, પૈસા નહીં. કોલે તમારા બેલેન્સમાંથી શું લીધું તે Keys & usage પર દરેક રિક્વેસ્ટ મુજબ બતાવાય છે; તે કેવી રીતે ગણાય છે તે Limits and balance પર છે. મર્યાદાઓ અને બેલેન્સ
  • દરેક જવાબ, એરર અને સ્ટ્રીમ પણ, x-request-id હેડર ધરાવે છે. જો તમારી રિક્વેસ્ટ x-request-id હેડર લઈ જાય, તો તેની કિંમત પાછી આવે છે; અન્યથા કિંમત 12 હેક્સાડેસિમલ અક્ષરો હોય છે. તેને તમારા લોગમાં રાખો: તે આ એક રિક્વેસ્ટને ઓળખાવે છે.

એ જ કોલ સ્ટ્રીમ તરીકે

જવાબ લખાતો હોય ત્યારે વાંચવા માટે stream ને true સેટ કરો. રિક્વેસ્ટમાં બાકી બધું સમાન રહે છે.

from openai import OpenAI

client = OpenAI(
    api_key="YOUR_API_KEY",
    base_url="https://api.shannon-ai.com/v1"
)

stream = client.chat.completions.create(
    model="shannon-3",
    messages=[
        {"role": "user", "content": "Hello, Shannon!"}
    ],
    max_tokens=1024,
    stream=True
)

for chunk in stream:
    if chunk.choices and chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="", flush=True)

સ્ટ્રીમની દરેક ફ્રેમ data: થી શરૂ થતી અને JSON ઓબ્જેક્ટ ધરાવતી એક લાઇન છે. એક ફ્રેમ, ઘણી લાઇનોમાં છાપેલી:

એક ફ્રેમ
{
  "id": "chatcmpl-9b2f6c1e4a7d4f0f8a3c5d2e1b6a7c90",
  "object": "chat.completion.chunk",
  "created": 1791590400,
  "model": "shannon-3",
  "choices": [
    {
      "index": 0,
      "delta": {
        "content": "Hello! "
      },
      "finish_reason": null
    }
  ]
}
ફ્રેમ્સ, ક્રમમાં delta finish_reason
મોડેલ reasoning કરતું હોય ત્યારે. શૂન્ય કે વધુ ફ્રેમ્સ. {"reasoning_content": "…"} null
જવાબ, ટુકડે ટુકડે. શૂન્ય કે વધુ ફ્રેમ્સ. {"content": "…"} null
છેલ્લી JSON ફ્રેમ. તે usage પણ લઈ જાય છે; તમારે તે માંગવું પડતું નથી. {} "stop"
સ્ટ્રીમનો અંત: લાઇન data: [DONE].
  • જવાબનું સ્ટેટસ 200 અને content-type: text/event-stream હોય છે. દરેક ફ્રેમ data: <json> લાઇન છે, જેની પછી ખાલી લાઇન આવે છે.
  • દર 15 સેકન્ડે : ping લાઇન આવે છે. તે કનેક્શન ખુલ્લું રાખતી કોમેન્ટ છે; SSE ક્લાયન્ટ્સ તેને છોડી દે છે.
  • સ્ટ્રીમ ખુલ્યા પછીની નિષ્ફળતા data: {"error": {…}} ફ્રેમ તરીકે આવે છે. સ્ટેટસ 200 જ રહે છે, તેથી ફ્રેમ્સ તપાસો.

સ્ટ્રીમિંગ

જો કોલ નિષ્ફળ જાય

એરર એટલે type અને message ધરાવતો error ધરાવતો JSON ઓબ્જેક્ટ. પહેલા કોલને સૌથી વધુ વાર મળતા જવાબો આ છે.

સ્ટેટસ પ્રકાર મેસેજ તેનો અર્થ
401 authentication_error Missing authentication કોઈ કી API સુધી પહોંચી નથી. Authorization: Bearer પછી તમારી કી મોકલો.
401 authentication_error Invalid API key કી એકાઉન્ટની હાલની કી નથી. તેને Keys & usage પરથી ફરી કોપી કરો.
400 invalid_request_error unknown model: <id> model પ્રકાશિત id નથી. Models & pricing માંથી id વાપરો.
422 invalid_request_error Failed to deserialize the JSON body into the target type… કોઈ ફીલ્ડનો JSON પ્રકાર ખોટો છે, અથવા messages ખૂટે છે.
429 rate_limit_error Quota exceeded. Upgrade your plan at shannon-ai.com/plan રિક્વેસ્ટનું આઉટપુટ બજેટ તમારા બેલેન્સમાં સમાતું નથી.

ભૂલ સંભાળ

આગળ ક્યાં જવું

  • મોડેલ્સ અને કિંમત — મોડેલ પસંદ કરો: id, કોન્ટેક્સ્ટ વિન્ડો અને કિંમતો.
  • Chat Completions — તમે કોલ કરેલા એન્ડપોઇન્ટનું દરેક રિક્વેસ્ટ ફીલ્ડ, જવાબ ફીલ્ડ અને સ્ટ્રીમ ઇવેન્ટ.
  • સ્ટ્રીમિંગ — સ્ટ્રીમની ફ્રેમ્સ, keep-alive લાઇન્સ અને સ્ટ્રીમની અંદરની એરર્સ.
  • ફંક્શન કોલિંગ — મોડેલને તમારા ફંક્શન્સ કોલ કરવા દો.
  • મર્યાદાઓ અને બેલેન્સ — રિક્વેસ્ટ શું અલગ રાખે છે અને તેનો શું ખર્ચ છે, અને તમે કેટલી રિક્વેસ્ટ મોકલી શકો છો.
  • API પ્લેગ્રાઉન્ડ — તમારી કી સાથે બ્રાઉઝરમાં રિક્વેસ્ટ અજમાવો.