หน้าแรก / คู่มือ / Free LLM API สำหรับ LangChain

Free LLM API สำหรับ LangChain

LangChain เชื่อมกับ API ที่เข้ากันได้กับ OpenAI ผ่าน ChatOpenAI และ OpenAIEmbeddings คุณเปลี่ยนเพียง base URL และคีย์ คู่มือนี้ชี้ทั้งสองคลาสไปยัง FreeLLMAPI ซึ่งรวมระดับฟรีของผู้ให้บริการ 45 ราย ไว้หลังคีย์เดียว โค้ดด้านล่างทำงานได้ทั้งใน Python และ JavaScript และจัดการขีดจำกัดอัตราให้

โค้ด ชื่อโมเดล และป้ายกำกับของแคตตาล็อกสดจะแสดงเป็นภาษาอังกฤษ

ตั้งค่าเกตเวย์และแพ็กเกจ

ติดตั้ง FreeLLMAPI และเพิ่มคีย์ฟรีจากผู้ให้บริการอย่างน้อยสองรายในหน้า Keys ของแดชบอร์ด คัดลอกคีย์รวม โค้ดของคุณใช้เพียงคีย์นี้ คีย์ของผู้ให้บริการยังคงอยู่ใน FreeLLMAPI

ติดตั้ง FreeLLMAPI และแพ็กเกจ LangChain

curl -fsSL https://freellmapi.co/install.sh | bash

# Python
pip install -U langchain langchain-openai

# JavaScript
npm install @langchain/openai @langchain/core zod

ตัวแปรสภาพแวดล้อม

export FREELLMAPI_BASE_URL=http://localhost:3001/v1
export FREELLMAPI_API_KEY=<unified-key>

ใช้ model="auto" เพื่อให้เราเตอร์เลือกโมเดล คุณใช้กลยุทธ์การกำหนดเส้นทางได้ด้วย เช่น auto:fast หรือ auto:smart โปรไฟล์ที่มีชื่อ เช่น auto:coding และ ID โมเดลคงที่จากแคตตาล็อกก็ใช้ได้เช่นกัน

หากต้องการดูว่าคีย์ของคุณเรียกโมเดลใดได้ตอนนี้ ให้ร้องขอ GET /v1/models?execution_status=ready รายการเต็มยังมีโมเดลที่ไม่มีคีย์ด้วย โมเดลที่มีสถานะ exhausted มีคีย์ แต่ทุกคีย์ถึงขีดจำกัดในขณะนี้ สถานะนี้จะหายไปเมื่อช่วงพักสิ้นสุด

แสดงรายการโมเดลที่ตอบได้ตอนนี้

curl "http://localhost:3001/v1/models?execution_status=ready" \
  -H "Authorization: Bearer <unified-key>"

แชตด้วย ChatOpenAI

ใน Python ตั้งค่า base_url และ api_key ค่า max_retries จะถูกส่งไปยังไคลเอนต์ OpenAI ซึ่งลองใหม่เมื่อเกิดข้อผิดพลาด 429 และ 5xx โดยรอแบบเพิ่มขึ้นเป็นทวีคูณ

Python: แชตและสตรีมมิง

import os
from langchain_openai import ChatOpenAI

llm = ChatOpenAI(
    model="auto",
    base_url=os.environ["FREELLMAPI_BASE_URL"],
    api_key=os.environ["FREELLMAPI_API_KEY"],
    temperature=0.2,
    max_retries=3,
    timeout=60,
    include_response_headers=True,
)

msg = llm.invoke("Explain retrieval-augmented generation in two sentences.")
print(msg.content)
print("Served by:", msg.response_metadata["headers"].get("x-routed-via"))

for chunk in llm.stream("Write a haiku about SQLite."):
    print(chunk.content, end="", flush=True)

ใน JavaScript ใส่ base URL ใน configuration.baseURL ออบเจ็กต์ configuration จะถูกส่งไปยังไคลเอนต์ SDK ของ OpenAI อย่างเป็นทางการ

JavaScript: แชตและสตรีมมิง

import { ChatOpenAI } from "@langchain/openai";

const llm = new ChatOpenAI({
  model: "auto",
  apiKey: process.env.FREELLMAPI_API_KEY,
  temperature: 0.2,
  maxRetries: 3,
  configuration: { baseURL: process.env.FREELLMAPI_BASE_URL },
});

const msg = await llm.invoke("Explain retrieval-augmented generation in two sentences.");
console.log(msg.content);

for await (const chunk of await llm.stream("Write a haiku about SQLite.")) {
  process.stdout.write(String(chunk.content));
}

header x-routed-via แสดงผู้ให้บริการและโมเดลที่ตอบ หากเราเตอร์เปลี่ยนผู้ให้บริการ x-fallback-trail จะแสดงความพยายามที่ล้มเหลวแต่ละครั้งและเหตุผล

เครื่องมือและเอเจนต์

FreeLLMAPI ส่ง tools แบบ OpenAI ไปยังแต่ละผู้ให้บริการ และส่ง tool_calls กลับในรูปแบบ OpenAI สำหรับ Gemini ระบบแปลงคำขอเป็นรูปแบบของ Google และแปลงกลับ ใช้ bind_tools สำหรับการเรียกหนึ่งครั้ง หรือ create_agent สำหรับลูปที่รันเครื่องมือ

Python: bind_tools และ create_agent

from langchain.agents import create_agent
from langchain_core.tools import tool

@tool
def get_weather(city: str) -> str:
    """Return the current weather for a city."""
    return f"It is 18 C and cloudy in {city}."

# One call: the model returns the tool call, your code runs it
msg = llm.bind_tools([get_weather]).invoke("What is the weather in Dublin?")
print(msg.tool_calls)

# Agent loop: LangChain runs the tool and sends the result back
agent = create_agent(
    model=llm,
    tools=[get_weather],
    system_prompt="You answer weather questions. Use the tool.",
)
result = agent.invoke({"messages": [{"role": "user", "content": "Weather in Dublin?"}]})
print(result["messages"][-1].content)

JavaScript: bindTools

import { tool } from "@langchain/core/tools";
import { z } from "zod";

const getWeather = tool(
  async ({ city }) => `It is 18 C and cloudy in ${city}.`,
  {
    name: "get_weather",
    description: "Return the current weather for a city.",
    schema: z.object({ city: z.string() }),
  },
);

const reply = await llm.bindTools([getWeather]).invoke("What is the weather in Dublin?");
console.log(reply.tool_calls);

มีเพียงบางโมเดลฟรีที่รองรับการเรียกใช้เครื่องมือ เมื่อใช้ auto ให้เก็บโมเดลที่เรียกใช้เครื่องมือได้ไว้ในห่วงโซ่สำรอง หรือสร้างโปรไฟล์สำหรับเอเจนต์ โมเดลฟรีเหล่านี้รองรับเครื่องมือในขณะนี้:

โมเดลฟรีที่เรียกใช้เครื่องมือได้ และมีบริบท 128K ขึ้นไป
โมเดลฟรีบนขนาดบริบทขีดจำกัดฟรี (ข้อเสนอที่ดีที่สุด)เครื่องมือการอ่านภาพ
GLM 5.3UnoRouter1M1 คำขอ/นาที
1 req/min per model
ใช่ไม่
GLM 5.3 FlashNVIDIA NIM, UnoRouter1M40 คำขอ/นาที
40 RPM
ใช่ใช่
Gemini 3.8 FlashGoogle AI Studio1MRecurring provider quotaใช่ใช่
Qwen3.8 Flash NextAMD Radeon Cloud262KRecurring daily account allowanceใช่ไม่
Qwen3.8 FlashElectronHub1M$0.25/week shared recurring creditใช่ใช่
Gemini 3.7 FlashGoogle AI Studio1M10 คำขอ/นาที
20 คำขอ/วัน
250,000 โทเคน/นาที
~3M
ใช่ใช่
GPT-5.6 LunaElectronHub1M$0.25/week shared recurring creditใช่ใช่
Dots3 Note PreviewKilo Gateway, OpenRouter512K20 คำขอ/นาที
50 คำขอ/วัน
50 req/day account-wide (1,000 after a $10 purchase)
ใช่ใช่
DeepSeek V4 FlashAMD Radeon Cloud, BazaarLink, ElectronHub1M10 คำขอ/นาที
50 คำขอ/วัน
Shared rate limit
ใช่ไม่
Qwen3.8 27BElectronHub, Groq, OVH AI Endpoints262K30 คำขอ/นาที
250 คำขอ/วัน
70,000 โทเคน/นาที
~6M
ใช่ใช่

แสดง 10 จาก 109 โมเดลฟรีที่ตรงกัน ดูโมเดลทั้งหมด.

ข้อมูลสดจากแคตตาล็อก FreeLLMAPI (เวอร์ชัน 2026.11.19 ตรวจสอบเมื่อ 2026-11-19) ขีดจำกัดฟรีเปลี่ยนบ่อย แคตตาล็อกถูกตรวจสอบทุกวัน

Embedding ด้วย OpenAIEmbeddings

โดยค่าเริ่มต้น OpenAIEmbeddings ใน Python แบ่งข้อความด้วย tokenizer ของ OpenAI และส่งเป็นโทเคน ผู้ให้บริการอื่นส่วนใหญ่ต้องการข้อความดิบ ตั้งค่า check_embedding_ctx_length=False เพื่อส่งข้อความ

Python: embedding

from langchain_openai import OpenAIEmbeddings

emb = OpenAIEmbeddings(
    model="bge-m3",  # one embedding family, 1024 dimensions
    base_url=os.environ["FREELLMAPI_BASE_URL"],
    api_key=os.environ["FREELLMAPI_API_KEY"],
    check_embedding_ctx_length=False,
)
vectors = emb.embed_documents(["FreeLLMAPI pools free tiers.", "LangChain builds LLM apps."])
print(len(vectors), len(vectors[0]))

JavaScript: embedding

import { OpenAIEmbeddings } from "@langchain/openai";

const emb = new OpenAIEmbeddings({
  model: "bge-m3",
  apiKey: process.env.FREELLMAPI_API_KEY,
  configuration: { baseURL: process.env.FREELLMAPI_BASE_URL },
});
const vector = await emb.embedQuery("What does FreeLLMAPI do?");
console.log(vector.length);

การสลับ embedding ใน FreeLLMAPI ไม่เปลี่ยนโมเดลเลย ระบบย้ายได้เฉพาะระหว่างผู้ให้บริการที่ให้บริการตระกูลเดียวกัน ซึ่งทำให้เวกเตอร์ทั้งหมดในคลังเดียวกันยังเข้ากันได้ เลือกตระกูลหนึ่งแล้วใช้ต่อไป โมเดล embedding ฟรีในขณะนี้มีดังนี้:

โมเดลผู้ให้บริการมิติอินพุตสูงสุดขีดจำกัดฟรี
Gemini Embedding
gemini-embedding-001
Google AI Studio30722048100 rpm · 1K req/day
Nemotron Embed VL 1B
nvidia/llama-nemotron-embed-vl-1b-v2
NVIDIA NIM20488192~40 rpm
Nemotron 3 Embed 1B (OR free)
nvidia/nemotron-3-embed-1b:free
OpenRouter20483276820 rpm · shared free daily cap
SEA-LION E5 Embedding 600M
aisingapore/SEA-LION-E5-Embedding-600M
SEA-LION10248192free · 10 rpm
SEA-LION ModernBERT Embedding 300M
aisingapore/SEA-LION-ModernBERT-Embedding-300M
SEA-LION10248192free · 10 rpm
SEA-LION ModernBERT Embedding 600M
aisingapore/SEA-LION-ModernBERT-Embedding-600M
SEA-LION10248192free · 10 rpm
BGE-M3
@cf/baai/bge-m3
Cloudflare Workers AI1024819210K neurons/day (shared)
EmbeddingGemma 300M
@cf/google/embeddinggemma-300m
Cloudflare Workers AI768204810K neurons/day (shared)
Qwen3 Embedding 0.6B
@cf/qwen/qwen3-embedding-0.6b
Cloudflare Workers AI1024819210K neurons/day (shared)
Cohere Embed v4
embed-v4.0
Cohere15361280001K calls/mo (shared w/ chat)
Gemini Embedding 2
gemini-embedding-2
Google AI Studio30728192free-tier rate limited
Nemotron 3 Embed 1B
nvidia/nemotron-3-embed-1b
NVIDIA NIM20484096~40 rpm
PLaMo Embedding 1B (CF)
@cf/pfnet/plamo-embedding-1b
Cloudflare Workers AI2048409610K neurons/day (shared)
BGE Small EN v1.5 (CF)
@cf/baai/bge-small-en-v1.5
Cloudflare Workers AI38451210K neurons/day (shared)
BGE Base EN v1.5 (CF)
@cf/baai/bge-base-en-v1.5
Cloudflare Workers AI76851210K neurons/day (shared)
BGE Large EN v1.5 (CF)
@cf/baai/bge-large-en-v1.5
Cloudflare Workers AI102451210K neurons/day (shared)
Mistral Embed
mistral-embed
Mistral10248192free mode · monthly cap (shared)
Codestral Embed
codestral-embed
Mistral15368192free mode · monthly cap (shared)
Llama Text Embed v2 (Pinecone)
llama-text-embed-v2
Pinecone102420485M tokens/mo per model
Multilingual E5 Large (Pinecone)
multilingual-e5-large
Pinecone10245075M tokens/mo per model
Nemotron 3 Embed 1B (Speka)
nvidia/nemotron-3-embed-1b
Speka20488192$1/month shared across Speka models
OpenAI: Text Embedding 3 Small (ElectronHub)
text-embedding-3-small
ElectronHub15368192$0.25/week shared free credits
OpenAI: Text Embedding 3 Large (ElectronHub)
text-embedding-3-large
ElectronHub30728192$0.25/week shared free credits
Nemotron 3 Embed 8B (Inferbase)
nvidia-nemotron-3-embed-8b
Inferbase409681925,000 requests/month; 100 requests and $0.50 usage/day shared
Qwen 3 Embedding 0.6B (Inferbase)
qwen3-embedding-0-6b
Inferbase102481925,000 requests/month; 100 requests and $0.50 usage/day shared
Qwen 3 Embedding 4B (Inferbase)
qwen3-embedding-4b
Inferbase256081925,000 requests/month; 100 requests and $0.50 usage/day shared
Qwen 3 Embedding 8B (Inferbase)
qwen3-embedding-8b
Inferbase409681925,000 requests/month; 100 requests and $0.50 usage/day shared
LFM2.5 Embedding 350M (LLMTR)
liquid/lfm-2.5-embedding-350m-free
LLMTR1024512Ongoing zero-price route; provider terms apply
BGE-M3 (SEA-LION)
BAAI/bge-m3
SEA-LION10248192free · 10 rpm
Cohere Embed English v3
embed-english-v3.0
Cohere10245121K calls/mo (shared w/ chat)
Qwen3 Embedding 8B (OVH)
Qwen3-Embedding-8B
OVH AI Endpoints409632768free · anonymous 2 req/min per IP
BGE-M3 (OVH)
bge-m3
OVH AI Endpoints10248192free · anonymous 2 req/min per IP
BGE Multilingual Gemma2 (OVH)
bge-multilingual-gemma2
OVH AI Endpoints35848192free · anonymous 2 req/min per IP
Mistral: Codestral Embed (ElectronHub)
codestral-embed
ElectronHub15368192$0.25/week shared free credits
Mistral: Mistral Embed (ElectronHub)
mistral-embed
ElectronHub10248192$0.25/week shared free credits
Google: Gemini Embedding 2 (ElectronHub)
gemini-embedding-2
ElectronHub30728192$0.25/week shared free credits
Google: Gemini Embedding 001 (ElectronHub)
gemini-embedding-001
ElectronHub30722048$0.25/week shared free credits
OpenAI: Text Embedding Ada 002 (ElectronHub)
text-embedding-ada-002
ElectronHub15368192$0.25/week shared free credits
Cohere Embed English Light v3
embed-english-light-v3.0
Cohere3845121K calls/mo (shared w/ chat)
Cohere Embed Multilingual v3
embed-multilingual-v3.0
Cohere10245121K calls/mo (shared w/ chat)
Cohere Embed Multilingual Light v3
embed-multilingual-light-v3.0
Cohere3845121K calls/mo (shared w/ chat)

ข้อมูลสดจากแคตตาล็อก FreeLLMAPI (เวอร์ชัน 2026.11.19 ตรวจสอบเมื่อ 2026-11-19) ขีดจำกัดฟรีเปลี่ยนบ่อย แคตตาล็อกถูกตรวจสอบทุกวัน

เอาต์พุตที่มีโครงสร้าง เซสชัน และการเลือกโมเดล

with_structured_output คืนค่าเป็นออบเจ็กต์ Pydantic แทนข้อความ โดยค่าเริ่มต้น LangChain ใช้การเรียกฟังก์ชันสำหรับสิ่งนี้ วิธีนี้ใช้ได้กับทุกโมเดลฟรีที่รองรับเครื่องมือ

Python: เอาต์พุตที่มีโครงสร้าง

from pydantic import BaseModel, Field

class Ticket(BaseModel):
    category: str = Field(description="billing, bug or other")
    urgent: bool

classifier = llm.with_structured_output(Ticket)
ticket = classifier.invoke("The app crashes when I pay. Please fix it today.")
print(ticket.category, ticket.urgent)

อย่าใช้ method="json_schema" กับ auto ผู้ให้บริการฟรีไม่ได้รองรับเอาต์พุต JSON schema แบบเข้มงวดทุกราย และเราเตอร์อาจเลือกผู้ให้บริการต่างกันในแต่ละคำขอ

FreeLLMAPI เก็บการสนทนาไว้กับโมเดลเดียวนาน 30 นาที ระบบหาการสนทนาจาก header X-Session-Id หากไม่มี header ระบบจะใช้แฮชของข้อความผู้ใช้แรก สำหรับแอปแชตที่มีผู้ใช้จำนวนมาก ให้ส่ง session ID หนึ่งรายการต่อผู้ใช้หนึ่งคน:

Python: หนึ่งเซสชันต่อผู้ใช้

def llm_for(user_id: str) -> ChatOpenAI:
    return ChatOpenAI(
        model="auto",
        base_url=os.environ["FREELLMAPI_BASE_URL"],
        api_key=os.environ["FREELLMAPI_API_KEY"],
        default_headers={"X-Session-Id": f"user-{user_id}"},
        max_retries=3,
    )

เลือกสตริงโมเดลสำหรับแต่ละส่วนของแอปของคุณ:

งานในแอปของคุณสตริงโมเดลเหตุผล
คำตอบแชตสำหรับผู้ใช้autoทำตามห่วงโซ่ที่คุณตั้งไว้ในแดชบอร์ด
เอเจนต์ที่ใช้เครื่องมือauto:<profile>โปรไฟล์ที่มีเฉพาะโมเดลที่เรียกใช้เครื่องมือได้
การจัดประเภท การกำหนดเส้นทาง และการสรุปสั้นauto:fastจัดอันดับโมเดลตามความเร็วที่วัดได้
ขั้นตอนการให้เหตุผลที่ยากauto:smartจัดอันดับโมเดลตามความฉลาด
การทดสอบที่ต้องทำซ้ำได้ID โมเดลคงที่โมเดลเดิมทุกครั้ง แต่จะหยุดเมื่อถึงขีดจำกัดของโมเดลนั้น

ระดับฟรีไม่มี SLA เวลาแฝงเปลี่ยนไปตลอดวัน และโมเดลที่แข็งแรงที่สุดจะถึงขีดจำกัดรายวันก่อน ทดสอบห่วงโซ่ของคุณด้วยโมเดลที่ห่วงโซ่จะใช้จริง

ขีดจำกัดอัตราและการลองใหม่

FreeLLMAPI ลองผู้ให้บริการรายถัดไปเมื่อรายหนึ่งส่ง 429 โค้ดของคุณจะเห็น 429 ก็ต่อเมื่อทุกคีย์ในห่วงโซ่ถึงขีดจำกัด จากนั้นการตอบกลับจะมี header Retry-After ขีดจำกัดรายวันหลายรายการรีเซ็ตเที่ยงคืนตามเวลา UTC

สำหรับงานแบบชุด ให้เพิ่มชั้นการลองใหม่ที่สองด้วย with_retry ระบบจะลองทั้ง runnable ใหม่ด้วยการรอแบบเพิ่มขึ้นเป็นทวีคูณและ jitter

Python: ลองห่วงโซ่ใหม่เมื่อเจอขีดจำกัดอัตรา

import openai
from langchain_core.prompts import ChatPromptTemplate

prompt = ChatPromptTemplate.from_messages([
    ("system", "Summarize the text in one sentence."),
    ("user", "{text}"),
])

chain = (prompt | llm).with_retry(
    retry_if_exception_type=(openai.RateLimitError, openai.APITimeoutError),
    wait_exponential_jitter=True,
    stop_after_attempt=5,
)

# batch() runs requests in parallel; max_concurrency keeps the rate low
results = chain.batch(
    [{"text": t} for t in ["First document.", "Second document."]],
    config={"max_concurrency": 4},
)
ปัญหาสาเหตุวิธีแก้
openai.AuthenticationErrorคีย์รวมผิดคัดลอกคีย์อีกครั้งจากหน้า Keys
openai.NotFoundError ทุกครั้งที่เรียกbase URL ไม่มี /v1ใช้ http://localhost:3001/v1
openai.RateLimitErrorทุกคีย์ในห่วงโซ่ถึงขีดจำกัดแล้วเพิ่มผู้ให้บริการ ลด max_concurrency หรือรอ Retry-After
Embedding ผิดพลาดเกี่ยวกับโทเคนหรือชนิดอินพุตLangChain ส่งโทเคน ไม่ใช่ข้อความตั้งค่า check_embedding_ctx_length=False
tool_calls ว่างเปล่าโมเดลไม่รองรับเครื่องมือใช้โมเดลจากตารางเครื่องมือ หรือโปรไฟล์สำหรับเครื่องมือ
ไม่มีข้อความการให้เหตุผลChatOpenAI เก็บเฉพาะฟิลด์มาตรฐานของ OpenAIอ่านคำตอบใน content ฟิลด์เพิ่มเติมของผู้ให้บริการจะไม่ถูกเก็บ
ใช้ทุกตัวในรายการนี้ผ่านคีย์เดียว FreeLLMAPI คือเราเตอร์โอเพนซอร์สที่คุณโฮสต์เอง รวมระดับฟรีทั้งหมดนี้ไว้หลังปลายทางเดียวที่เข้ากันได้กับ OpenAI และเปลี่ยนไปใช้ตัวอื่นเมื่อตัวใดตัวหนึ่งถูกจำกัดอัตรา ดูแคตตาล็อก หรือ เริ่มใช้งานจริง.

คำถามที่พบบ่อย

ฉันใช้ Free LLM API กับ LangChain ได้ไหม

ได้ API ใดก็ได้ที่เข้ากันได้กับ OpenAI ใช้กับ ChatOpenAI ได้ ตั้ง base_url เป็น http://localhost:3001/v1 และ api_key เป็นคีย์รวม FreeLLMAPI ของคุณ เราเตอร์ใช้ระดับฟรีของผู้ให้บริการ

ฉันตั้ง base URL ที่กำหนดเองใน ChatOpenAI ของ LangChain อย่างไร

ใน Python ส่ง base_url ให้ ChatOpenAI ใน JavaScript ส่ง configuration: { baseURL } ให้ constructor ตัวแปรสภาพแวดล้อม OPENAI_BASE_URL ก็ใช้ได้ใน Python เช่นกัน

เอเจนต์ของ LangChain ใช้งานกับโมเดลฟรีได้ไหม

ได้ หากโมเดลรองรับการเรียกใช้เครื่องมือ ใช้ create_agent กับโมเดล ChatOpenAI และเก็บโมเดลที่เรียกใช้เครื่องมือได้ไว้ในห่วงโซ่ FreeLLMAPI ของคุณ

ทำไม OpenAIEmbeddings ล้มเหลวกับผู้ให้บริการที่ไม่ใช่ OpenAI

โดยค่าเริ่มต้นระบบส่ง ID ของโทเคน ผู้ให้บริการอื่นส่วนใหญ่ต้องการข้อความดิบ ตั้ง check_embedding_ctx_length=False ใน Python

ฉันจัดการข้อผิดพลาด 429 ใน LangChain อย่างไร

ตั้ง max_retries บน ChatOpenAI และเพิ่ม with_retry ให้ห่วงโซ่สำหรับงานแบบชุด FreeLLMAPI ย้ายไปผู้ให้บริการรายถัดไปก่อนที่โค้ดของคุณจะเห็น 429 อยู่แล้ว

คู่มือที่เกี่ยวข้อง

แหล่งข้อมูล

คู่มือทั้งหมด · ดูโมเดลฟรีทั้งหมด