Skip to main content

llmf (llmfree)

Một cú gọi duy nhất cho các LLM miễn phí, không cần API key. Không đăng ký, không khai báo nhà cung cấp, không phụ thuộc thư viện ngoài (chỉ Python stdlib, chạy tốt trên Termux).

import llmf

print(llmf.chat("Xin chào, bạn là ai?"))

Chỉ vậy thôi. llmf tự chọn endpoint đang sống, tự chuyển sang nhà cung cấp khác khi lỗi, và luôn trả về cùng một kiểu dữ liệu.

Cài đặt

pip install llmf

Yêu cầu Python ≥ 3.8. Không có dependency.

Cú pháp thống nhất

Mọi nhà cung cấp, mọi model đều dùng cùng một hàm, cùng tham số:

llmf.chat("câu hỏi")                                   # tự động chọn + fallback
llmf.chat("câu hỏi", model="best")                     # theo mục đích: fast | best | code | vision | reasoning
llmf.chat("câu hỏi", model="llama")                    # gõ tên gần đúng, không phân biệt hoa/thường
llmf.chat("câu hỏi", model="ovh/Qwen3.5-397B-A17B")    # chỉ định đích danh: nhà_cung_cấp/model
llmf.chat("câu hỏi", model=["best", "fast"])           # thử lần lượt

llmf.chat("...", system="Trả lời ngắn gọn", max_tokens=300, temperature=0.3)
llmf.chat("Mô tả ảnh này", images=["anh.jpg"])         # ảnh: đường dẫn / URL / bytes
llmf.chat("Trả JSON", json=True)
llmf.chat([{"role": "user", "content": "hi"}])         # hoặc danh sách messages kiểu OpenAI

Kết quả là Reply – một str kèm metadata:

r = llmf.chat("2+2=?")
print(r)             # "4"  (đã strip, đã bỏ phần reasoning)
r.provider, r.model  # ai đã trả lời
r.reasoning          # chain-of-thought nếu có (không lẫn vào câu trả lời)
r.usage, r.latency, r.attempts

Streaming, hội thoại, async

for piece in llmf.stream("Đếm từ 1 đến 10"):
    print(piece, end="", flush=True)

s = llmf.session(system="Bạn là trợ lý ngắn gọn")   # nhớ lịch sử
s("Tên tôi là An")
print(s("Tên tôi là gì?"))

reply = await llmf.achat("hi")                       # async
async for piece in llmf.astream("hi"): ...

Streaming chỉ fallback trước khi có chunk đầu tiên; sau đó lỗi sẽ được raise.

Dòng lệnh

llmf "Giải thích GIL trong Python"
cat code.py | llmf "Review đoạn code này" -m code
llmf -i                      # chat tương tác (/reset, /exit)
llmf --stream "kể chuyện cười"
llmf models                  # danh sách model (--live: hỏi trực tiếp từng nhà cung cấp)
llmf providers
llmf health                  # kiểm tra endpoint nào đang sống
python -m llmf "hi"

Nó hoạt động thế nào

  • Chuỗi fallback theo thứ tự nhanh/sạch nhất: ovh → kilo-openrouter → chatjimmy → kilo-gateway → pollinations → llm7 → blockrun → freeai.
  • Endpoint vừa lỗi bị hạ ưu tiên (cooldown) một lúc; lỗi 429 thì 60 giây.
  • Pollinations bị giới hạn 1 request/15s nên tự được giãn nhịp và xếp cuối.
  • Làm sạch đầu ra: bỏ reasoning, tách <think>…</think>, strip \n\n, bóc khối <|stats|> của ChatJimmy, coi câu trả lời rỗng / lỗi giấu trong HTTP 200 là thất bại để chuyển nhà khác.
  • max_tokens mặc định 1024 (model reasoning cần ≥ 200, nếu không content sẽ rỗng).
  • Không retry vô ích: lỗi tạm thời (mạng, 5xx, timeout) thử lại 1 vòng; 4xx / 429 thì bỏ qua đích đó.

Khi tất cả đều thất bại sẽ có llmf.AllProvidersFailed với danh sách .errors chi tiết từng nơi.

Tuỳ biến (không bắt buộc)

llmf.configure(timeout=30, exclude=["blockrun"])      # hoặc only=["ovh", "llm7"]

# Thêm endpoint OpenAI-compatible của riêng bạn
llmf.register("lab", "http://localhost:11434/v1", ["llama3"], front=True)

# Endpoint hardcode bị chết? Lấy danh sách còn sống từ stillworks (opt-in)
llmf.discover()

c = llmf.Client(timeout=20)       # client độc lập, không dùng chung trạng thái

Biến môi trường: LLMF_MODEL, LLMF_TIMEOUT. Debug: logging.getLogger("llmf").setLevel("DEBUG").

Lưu ý quan trọng

  • Đây là các endpoint công khai, miễn phí, không chính thức. Chúng có thể đổi, chậm, giới hạn hoặc biến mất bất cứ lúc nào – danh sách trong gói được xác minh ngày 2026-10-22. Hãy dùng llmf health và llmf.discover().
  • Đừng gửi dữ liệu nhạy cảm (mật khẩu, khoá, dữ liệu cá nhân): prompt đi tới máy chủ bên thứ ba ẩn danh, và discover() còn dùng endpoint của người lạ.
  • Tôn trọng ToS và rate limit của từng dịch vụ; không spam. Dùng thương mại cần đọc điều khoản.
  • llmf không liên kết với bất kỳ nhà cung cấp nào.
  • Streaming trên phần lớn nhà cung cấp chưa được kiểm chứng trên endpoint thật (nếu họ không hỗ trợ, llmf nhận cả câu trả lời rồi trả một lần).

Phát triển

git clone https://github.com/MoonLite-Br/llmfree && cd llmf
python -m unittest discover -s tests -v      # test chạy offline với server giả lập

Cấu trúc:

src/llmf/
  client.py        Client, Session, Stream: fallback, cooldown, rate-limit
  registry.py      danh mục provider + phân giải tên model (alias/fuzzy)
  providers/       builtin.py (catalogue), openai_compat.py, chatjimmy.py
  _messages.py     str/messages/ảnh -> messages chuẩn
  _think.py        tách <think> (hỗ trợ streaming)
  cli.py           lệnh `llmf`

Thêm nhà cung cấp mới tương thích OpenAI chỉ cần thêm một ProviderSpec vào providers/builtin.py.

License

MIT

Metadata

Release files for llmf 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for llmf 0.1.0
File Size Uploaded Provenance
llmf-0.1.0.tar.gz 32.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for llmf 0.1.0
File Interpreter ABI Platform
llmf-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 64.4 kB

Release files / llmf-0.1.0.tar.gz

Download URL llmf-0.1.0.tar.gz
Size 32.6 kB
Tags Source
SHA-256 checksum
How to use checksums
ea1d0dab45a95a30a562bbca0a80272f9cc4eafd123e41c10cdc92d0494eb3b3
BLAKE2b-256 checksum
How to use checksums
e2400b5fd4545107c987778023a93497e2f48f298db4a04473fece5356c19266
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / llmf-0.1.0-py3-none-any.whl

Download URL llmf-0.1.0-py3-none-any.whl
Size 31.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
75dce3713fc0b8aaecdc05d717acd2c570424c8783d646d698925140c4b3542b
BLAKE2b-256 checksum
How to use checksums
5c36acd889e7a31c2cb929a9855eb7950f5bbdcfba57691ced5b205c1b33ead8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for llmf 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page