Python-ൽ സൗജന്യ LLM API
Official openai Python പാക്കേജ് ഏത് OpenAI-compatible API-യോടും പ്രവർത്തിക്കുന്നു. നിങ്ങൾ base URL-ഉം key-ഉം മാറ്റുന്നു. ഈ ഗൈഡ് FreeLLMAPI ഉപയോഗിക്കുന്നു. ഇത് ഒരു key-ക്ക് പിന്നിൽ 45 സൗജന്യ provider tier-കൾ ചേർക്കുന്നു, ഒരു പരിധി request തടയുമ്പോൾ അടുത്ത provider-ലേക്ക് മാറുന്നു. താഴെയുള്ള ഓരോ ഉദാഹരണവും പൂർണ്ണമാണ്, അതേപടി പ്രവർത്തിക്കുന്നു.
കോഡ്, മോഡൽ പേരുകളും തത്സമയ കാറ്റലോഗ് ലേബലുകളും ഇംഗ്ലീഷിൽ കാണിക്കുന്നു.
സജ്ജീകരണം
FreeLLMAPI ഇൻസ്റ്റാൾ ചെയ്യുക. ഇത് Docker-ൽ പ്രവർത്തിക്കുന്നു. http://localhost:3001-ൽ ഡാഷ്ബോർഡ് തുറക്കുക, Keys പേജിൽ രണ്ടോ അതിലധികമോ provider-കളിൽ നിന്നുള്ള സൗജന്യ key-കൾ ചേർക്കുക, unified key പകർത്തുക. തുടർന്ന് Python പാക്കേജ് ഇൻസ്റ്റാൾ ചെയ്യുക.
നിങ്ങളുടെ Python code unified key മാത്രം ഉപയോഗിക്കുന്നു. Provider keys FreeLLMAPI-യിൽ തന്നെ നിൽക്കുന്നു, അതിന്റെ database-ൽ എൻക്രിപ്റ്റ് ചെയ്ത്. Provider key മാറ്റുകയോ ചേർക്കുകയോ ചെയ്താൽ നിങ്ങളുടെ code മാറ്റേണ്ടതില്ല.
ഇൻസ്റ്റാൾ ചെയ്യുക
curl -fsSL https://freellmapi.co/install.sh | bash
python3 -m pip install -U openai
export FREELLMAPI_API_KEY=<unified-key>client.py
import os
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:3001/v1",
api_key=os.environ["FREELLMAPI_API_KEY"],
max_retries=3, # the SDK retries 429, 408, 409 and 5xx errors
timeout=60,
)
resp = client.chat.completions.create(
model="auto",
messages=[{"role": "user", "content": "Name three uses of Python in one line."}],
)
print(resp.choices[0].message.content)auto എന്ന മോഡൽ ഡാഷ്ബോർഡിലെ നിങ്ങളുടെ fallback chain പിന്തുടരുന്നു. auto:fast, auto:smart അല്ലെങ്കിൽ auto:reliable, auto:coding പോലുള്ള ഒരു profile, അല്ലെങ്കിൽ ഒരു നിശ്ചിത model ID എന്നിവയും ഉപയോഗിക്കാം.
Streaming ഉം response headers ഉം
മറുപടി stream ചെയ്യുക
from client import client
stream = client.chat.completions.create(
model="auto:fast",
messages=[{"role": "user", "content": "Write a haiku about Python."}],
stream=True,
)
for chunk in stream:
if chunk.choices:
print(chunk.choices[0].delta.content or "", end="", flush=True)
print()ഓരോ response-ലും മറുപടി നൽകിയ provider-ന്റെയും മോഡലിന്റെയും X-Routed-Via header ഉണ്ട്. SDK ഉപയോഗിച്ച് headers വായിക്കാൻ with_raw_response ഉപയോഗിക്കുക, സാധാരണ object ലഭിക്കാൻ parse() വിളിക്കുക.
Routing headers വായിക്കുക
from client import client
raw = client.chat.completions.with_raw_response.create(
model="auto",
messages=[{"role": "user", "content": "hi"}],
)
print("Served by:", raw.headers.get("x-routed-via"))
print("Failed attempts:", raw.headers.get("x-fallback-attempts", "0"))
print(raw.parse().choices[0].message.content)Tool calling
OpenAI ശൈലിയിലുള്ള tools അയയ്ക്കുക. Router അവയെ OpenAI-compatible providers-ലേക്ക് കൈമാറുന്നു, Gemini-ക്കായി അവയെ പരിവർത്തനം ചെയ്യുന്നു. ഓരോ provider-നും മറുപടി ഒരേ ഫോർമാറ്റിൽ തിരികെ വരുന്നു.
ഒരു tool call-ഉം അന്തിമ ഉത്തരവും
import json
from client import client
tools = [{
"type": "function",
"function": {
"name": "get_exchange_rate",
"description": "Get the exchange rate between two currencies.",
"parameters": {
"type": "object",
"properties": {
"base": {"type": "string"},
"quote": {"type": "string"},
},
"required": ["base", "quote"],
},
},
}]
messages = [{"role": "user", "content": "How many euros is 250 US dollars?"}]
first = client.chat.completions.create(model="auto", messages=messages, tools=tools)
msg = first.choices[0].message
if msg.tool_calls:
messages.append(msg)
for call in msg.tool_calls:
args = json.loads(call.function.arguments)
result = {"base": args["base"], "quote": args["quote"], "rate": 0.92} # your real lookup
messages.append({"role": "tool", "tool_call_id": call.id, "content": json.dumps(result)})
final = client.chat.completions.create(model="auto", messages=messages, tools=tools)
print(final.choices[0].message.content)
else:
print(msg.content)ചില providers tool call-നെ സാധാരണ ടെക്സ്റ്റായി തിരികെ നൽകുന്നു. FreeLLMAPI ഈ മറുപടികളെ യഥാർത്ഥ tool_calls ആക്കി മാറ്റുന്നു, അതിനാൽ നിങ്ങളുടെ code ഒരു ഫോർമാറ്റ് മാത്രം വായിക്കുന്നു. എപ്പോഴും msg.tool_calls പരിശോധിക്കുക, msg.content വായിക്കുന്നതിന് മുൻപ്, കാരണം ഒരു മോഡലിന് tool ഉപയോഗിക്കാതെയും മറുപടി നൽകാം.
Tool calling ഉള്ള ഒരു മോഡൽ ഉപയോഗിക്കുക. Tools പിന്തുണയ്ക്കുന്നതും ഇന്ന് ഏറ്റവും വേഗത്തിൽ അളന്ന സൗജന്യ മോഡലുകൾ ഇവയാണ്:
| മോഡൽ | സൗജന്യമായി ലഭ്യം | കോൺടെക്സ്റ്റ് | സൗജന്യ പരിധികൾ (ഏറ്റവും മികച്ച ഓഫർ) | ടൂളുകൾ | ദൃശ്യ ശേഷി |
|---|---|---|---|---|---|
| Qwen3.8 27B | ElectronHub, Groq, OVH AI Endpoints | 262K | 30 അഭ്യർഥന/മിനിറ്റ് 250 അഭ്യർഥന/ദിവസം 70,000 ടോക്കൺ/മിനിറ്റ് ~6M | അതെ | അതെ |
| Nemotron 3.5 Lightning | ElectronHub, Kilo Gateway, OpenRouter | 1M | 20 അഭ്യർഥന/മിനിറ്റ് 50 അഭ്യർഥന/ദിവസം 50 req/day account-wide (1,000 after a $10 purchase) | അതെ | ഇല്ല |
| GPT-OSS 120B | Cloudflare Workers AI, ElectronHub, Groq +1 കൂടി | 131K | 30 അഭ്യർഥന/മിനിറ്റ് 1,000 അഭ്യർഥന/ദിവസം 8,000 ടോക്കൺ/മിനിറ്റ് 200,000 ടോക്കൺ/ദിവസം ~6M | അതെ | ഇല്ല |
| GPT-OSS 20B | Cloudflare Workers AI, Groq, NVIDIA NIM +1 കൂടി | 131K | 30 അഭ്യർഥന/മിനിറ്റ് 1,000 അഭ്യർഥന/ദിവസം 8,000 ടോക്കൺ/മിനിറ്റ് 200,000 ടോക്കൺ/ദിവസം ~6M | അതെ | ഇല്ല |
| GPT-OSS Safeguard 20B | Groq | 131K | 30 അഭ്യർഥന/മിനിറ്റ് 1,000 അഭ്യർഥന/ദിവസം 8,000 ടോക്കൺ/മിനിറ്റ് 200,000 ടോക്കൺ/ദിവസം ~6M | അതെ | ഇല്ല |
| Gemini 3.5 Flash Lite | ElectronHub, Google AI Studio | 1M | 15 അഭ്യർഥന/മിനിറ്റ് 20 അഭ്യർഥന/ദിവസം 250,000 ടോക്കൺ/മിനിറ്റ് ~3M | അതെ | അതെ |
| Step 3.7 Flash | ElectronHub | 256K | $0.25/week shared recurring credit | അതെ | അതെ |
| Gemini 3.1 Flash-Lite | DreamPrompting, Google AI Studio | 1M | 100 അഭ്യർഥന/മിനിറ്റ് 5,000 അഭ്യർഥന/ദിവസം 500,000 ടോക്കൺ/ദിവസം 500K tokens/day · 5,000 req/day | അതെ | അതെ |
| Nemotron 3.5 Lightning 30B | Requesty | 1M | 200 അഭ്യർഥന/ദിവസം 200 rpd | അതെ | ഇല്ല |
| Gemini 2.5 Flash-Lite | ElectronHub, Google AI Studio | 1M | 15 അഭ്യർഥന/മിനിറ്റ് 20 അഭ്യർഥന/ദിവസം 250,000 ടോക്കൺ/മിനിറ്റ് ~3M | അതെ | അതെ |
പൊരുത്തപ്പെടുന്ന 146 സൗജന്യ മോഡലുകളിൽ 10 എണ്ണം കാണിക്കുന്നു. എല്ലാ മോഡലുകളും കാണുക.
FreeLLMAPI കാറ്റലോഗിൽ നിന്നുള്ള തത്സമയ ഡാറ്റ (പതിപ്പ് 2026.11.19, 2026-11-19 ൽ പരിശോധിച്ചത്). സൗജന്യ പരിധികൾ പലപ്പോഴും മാറുന്നു. കാറ്റലോഗ് എല്ലാ ദിവസവും പരിശോധിക്കുന്നു.
സൗജന്യ പരിധികൾക്കുള്ളിൽ async batches
AsyncOpenAI ഒരേ സമയം നിരവധി request-കൾ അയയ്ക്കുന്നു. സൗജന്യ tier-കൾ മിനിറ്റിൽ request-കൾ പരിമിതപ്പെടുത്തുന്നതിനാൽ, semaphore ഉപയോഗിച്ച് concurrency പരിമിതപ്പെടുത്തുക. എല്ലാ providers-ഉം പരിധിയിലാകുമ്പോൾ ഉദാഹരണം വീണ്ടും ശ്രമിക്കുന്നു.
batch.py
import asyncio
import os
import openai
from openai import AsyncOpenAI
client = AsyncOpenAI(
base_url="http://localhost:3001/v1",
api_key=os.environ["FREELLMAPI_API_KEY"],
max_retries=2,
)
limit = asyncio.Semaphore(4) # at most 4 requests at a time
async def summarize(text: str) -> str:
async with limit:
for attempt in range(5):
try:
resp = await client.chat.completions.create(
model="auto:fast",
messages=[
{"role": "system", "content": "Summarize in one sentence."},
{"role": "user", "content": text},
],
)
return resp.choices[0].message.content
except openai.RateLimitError as err:
header = err.response.headers.get("retry-after", "")
wait = int(header) if header.isdigit() else 2 ** attempt
await asyncio.sleep(min(wait, 60))
return "ERROR: rate limited"
async def main():
docs = [f"Document {i}: Python is a programming language." for i in range(20)]
results = await asyncio.gather(*(summarize(d) for d in docs))
for r in results:
print(r)
await client.close()
asyncio.run(main())Chain-ലെ എല്ലാ key-കളും പരിധിയിലാകുമ്പോൾ മാത്രമേ router 429 നൽകുന്നുള്ളൂ. അപ്പോൾ response-ൽ Retry-After header ഉണ്ടാകും. ചെറിയ semaphore മൂല്യവും കൂടുതൽ provider keys-ഉം വലിയ batch-കൾക്ക് ഏറ്റവും നല്ല ഫലം നൽകുന്നു.
ഓരോ ജോലിക്കും model string തിരഞ്ഞെടുക്കുക
Model string router-നോട് മോഡൽ എങ്ങനെ തിരഞ്ഞെടുക്കണമെന്ന് പറയുന്നു. ഓരോ call-ലും വ്യത്യസ്ത string ഉപയോഗിക്കാം. ഡാഷ്ബോർഡ് മാറ്റേണ്ടതില്ല.
| ജോലി | Model string | കാരണം |
|---|---|---|
| ഉപയോക്താക്കൾക്കുള്ള chat മറുപടികൾ | auto | ഡാഷ്ബോർഡിൽ നിങ്ങൾ സജ്ജമാക്കിയ fallback chain പിന്തുടരുന്നു |
| വർഗ്ഗീകരണം, extraction, ചെറിയ സംഗ്രഹങ്ങൾ | auto:fast | പ്രവർത്തനക്ഷമമായ മോഡലുകളെ അളന്ന വേഗത അനുസരിച്ച് റാങ്ക് ചെയ്യുന്നു |
| കഠിനമായ ചോദ്യങ്ങൾ, code review, ആസൂത്രണം | auto:smart | പ്രവർത്തനക്ഷമമായ മോഡലുകളെ ബുദ്ധിശക്തി അനുസരിച്ച് റാങ്ക് ചെയ്യുന്നു |
| പൂർത്തിയാകണമെന്ന് നിർബന്ധമുള്ള ഷെഡ്യൂൾ ചെയ്ത ജോലികൾ | auto:reliable | പ്രവർത്തനക്ഷമമായ മോഡലുകളെ സമീപകാല വിജയ നിരക്ക് അനുസരിച്ച് റാങ്ക് ചെയ്യുന്നു |
| ഒരു ആപ്പിന് പ്രത്യേക chain | auto:<profile> | പേരുള്ള ഒരു fallback chain ഉപയോഗിക്കുന്നു |
| ആവർത്തിക്കാവുന്നതായിരിക്കണം എന്ന പരീക്ഷകൾ | ഒരു നിശ്ചിത model ID | ഓരോ തവണയും ഒരേ മോഡൽ, എന്നാൽ ആ മോഡലിന്റെ പരിധിയിൽ എത്തുമ്പോൾ നിൽക്കുന്നു |
നിങ്ങളുടെ keys ഇപ്പോൾ വിളിക്കാൻ കഴിയുന്ന മോഡലുകൾ കാണാൻ, ഒരു filter ഉപയോഗിച്ച് model list ആവശ്യപ്പെടുക. മുഴുവൻ പട്ടികയിൽ ഇതുവരെ key ഇല്ലാത്ത മോഡലുകളും ഉണ്ട്.
ഇപ്പോൾ മറുപടി നൽകാൻ കഴിയുന്ന മോഡലുകൾ പട്ടികപ്പെടുത്തുക
import os
import httpx
r = httpx.get(
"http://localhost:3001/v1/models",
params={"execution_status": "ready"},
headers={"Authorization": f"Bearer {os.environ['FREELLMAPI_API_KEY']}"},
timeout=30,
)
r.raise_for_status()
for m in r.json()["data"]:
print(m["id"])exhausted സ്റ്റാറ്റസുള്ള ഒരു മോഡലിന് keys ഉണ്ട്, പക്ഷേ അവയെല്ലാം ഇപ്പോൾ ഒരു പരിധിയിലാണ്. Cooldown-കൾ തീരുമ്പോൾ സ്റ്റാറ്റസ് മാറും. needsKey സ്റ്റാറ്റസുള്ള ഒരു മോഡലിന് സജീവമായ key ഇല്ല.
ഒരു സംഭാഷണത്തിന്, X-Session-Id header extra_headers-ഓടെ അയയ്ക്കുക. അപ്പോൾ FreeLLMAPI ആ സംഭാഷണത്തെ 30 മിനിറ്റ് ഒരേ മോഡലിൽ നിലനിർത്തുന്നു. Header ഇല്ലെങ്കിൽ, ആദ്യ user സന്ദേശത്തിന്റെ hash session key ആയി ഉപയോഗിക്കുന്നു.
Embeddings
ടെക്സ്റ്റ് embed ചെയ്യുക
from client import client
resp = client.embeddings.create(
model="bge-m3", # one embedding family
input=["Python is a programming language.", "A snake is a reptile."],
)
print(len(resp.data), "vectors of", len(resp.data[0].embedding), "dimensions")Embedding failover ഒരിക്കലും മോഡൽ മാറ്റുന്നില്ല. ഒരേ family നൽകുന്ന providers-ക്കിടയിൽ മാത്രമേ അത് നീങ്ങുന്നുള്ളൂ, അതിനാൽ നിങ്ങളുടെ vectors പൊരുത്തപ്പെടുന്നതായി തുടരുന്നു. auto മൂല്യം ഡാഷ്ബോർഡിൽ നിങ്ങൾ സജ്ജമാക്കിയ സ്ഥിരസ്ഥിതി family ഉപയോഗിക്കുന്നു.
ഒരു input പട്ടികയിൽ നിരവധി ടെക്സ്റ്റുകൾ അയയ്ക്കുക. 32 ടെക്സ്റ്റുകളുള്ള ഒരു request സൗജന്യ പരിധിയിൽ നിന്ന് ഒരു request മാത്രമേ ഉപയോഗിക്കുന്നുള്ളൂ, 32 അല്ല. ഓരോ ടെക്സ്റ്റും family-യുടെ പരമാവധി input-ന് താഴെ നിർത്തുക. Family പേര് നിങ്ങളുടെ vectors-നൊപ്പം സൂക്ഷിക്കുക, അതിലൂടെ പിന്നീട് queries ഒരേ family ഉപയോഗിച്ച് embed ചെയ്യാം.
പിശകുകളും പരിഹാരങ്ങളും
| Exception | കാരണം | പരിഹാരം |
|---|---|---|
openai.AuthenticationError | തെറ്റായ unified key | Keys പേജിൽ നിന്ന് key വീണ്ടും പകർത്തുക |
openai.APIConnectionError | FreeLLMAPI പ്രവർത്തിക്കുന്നില്ല, അല്ലെങ്കിൽ port തെറ്റാണ് | ബ്രൗസറിൽ http://localhost:3001 തുറക്കുക |
openai.NotFoundError | Base URL-ൽ /v1 ഇല്ല | http://localhost:3001/v1 ഉപയോഗിക്കുക |
openai.RateLimitError | Chain-ലെ എല്ലാ key-കളും അവയുടെ പരിധിയിലാണ് | Providers ചേർക്കുക, concurrency കുറയ്ക്കുക, Retry-After വരെ കാത്തിരിക്കുക |
openai.BadRequestError, auto:name-നൊപ്പം | Profile പേര് നിലവിലില്ല | ഡാഷ്ബോർഡിൽ profile പേര് പരിശോധിക്കുക |
ചിത്രങ്ങളോടെ openai.UnprocessableEntityError (422) | നിങ്ങളുടെ chain-ൽ vision മോഡൽ ഇല്ല | Fallback chain-ൽ ഒരു vision മോഡൽ ഓൺ ചെയ്യുക |
സൗജന്യ tier-കൾക്ക് SLA ഇല്ല. ദിവസത്തിനിടയിൽ latency മാറുന്നു. ഏറ്റവും ശക്തമായ മോഡലുകൾ ആദ്യം ദിവസ പരിധിയിൽ എത്തുന്നു, പല പരിധികളും UTC അർധരാത്രിയിൽ പുനഃസജ്ജമാകുന്നു. നിശ്ചിത സമയത്തിനുള്ളിൽ മറുപടി നൽകേണ്ട ഒരു സേവനത്തിന് സൗജന്യ gateway ഉപയോഗിക്കരുത്.
പതിവ് ചോദ്യങ്ങൾ
Python-നായി സൗജന്യ LLM API ഉണ്ടോ?
ഉണ്ട്. പല providers-നും openai Python പാക്കേജിനൊപ്പം പ്രവർത്തിക്കുന്ന സൗജന്യ tier-കളുണ്ട്. FreeLLMAPI അവയെ ഒരു base URL-ഉം ഒരു key-ഉം പിന്നിൽ ചേർക്കുന്നു, providers-ക്കിടയിൽ failover-ഓടെ.
വ്യത്യസ്ത base URL-ഓടെ openai Python library എങ്ങനെ ഉപയോഗിക്കാം?
OpenAI() അല്ലെങ്കിൽ AsyncOpenAI()-ന് base_url നൽകുക, ഉദാഹരണത്തിന് base_url="http://localhost:3001/v1". നിങ്ങളുടെ gateway key api_key ആയി നൽകുക.
Python-ൽ rate limit പിശകുകൾ എങ്ങനെ കൈകാര്യം ചെയ്യാം?
Client-ൽ max_retries സജ്ജമാക്കുക. openai.RateLimitError പിടിക്കുക, Retry-After header വായിച്ച് കാത്തിരിക്കുക. asyncio semaphore ഉപയോഗിച്ച് concurrency പരിമിതപ്പെടുത്തുക.
Python-ൽ സൗജന്യ LLM API-യോടെ streaming പ്രവർത്തിക്കുമോ?
ഉവ്വ്. stream=True നൽകി chunk.choices[0].delta.content വായിക്കുക. FreeLLMAPI ഓരോ provider-നും OpenAI ഫോർമാറ്റിൽ streaming നൽകുന്നു.
സൗജന്യ LLM API-യോടെ async Python ഉപയോഗിക്കാമോ?
ഉപയോഗിക്കാം. അതേ base_url-ഉം key-ഉം ഉപയോഗിച്ച് AsyncOpenAI ഉപയോഗിക്കുക. സമാന്തര request-കളുടെ എണ്ണം ചെറുതായി നിലനിർത്തുക, കാരണം സൗജന്യ tier-കൾ മിനിറ്റിൽ request-കൾ പരിമിതപ്പെടുത്തുന്നു.
അനുബന്ധ ഗൈഡുകൾ
- സൗജന്യ OpenAI-compatible API base URL-കൾ
- LangChain-നായി സൗജന്യ LLM API
- AI ഏജന്റുകൾക്കുള്ള സൗജന്യ LLM API
- Function calling ഉള്ള സൗജന്യ LLM API
- സൗജന്യ LLM API റേറ്റ് പരിധികൾ
ഉറവിടങ്ങൾ
- OpenAI Python SDK README (async, retries, raw responses, errors) (20 November 2026 ൽ പരിശോധിച്ചത്)
- FreeLLMAPI docs: REST API (chat, streaming, tool calling, embeddings, headers) (20 November 2026 ൽ പരിശോധിച്ചത്)
- FreeLLMAPI docs: degraded mode and failover (20 November 2026 ൽ പരിശോധിച്ചത്)