local-llm-client
ローカル LLM ゲートウェイ(local-llm-server / OpenAI 互換)に繋ぐ高レベルクライアント。
- サーバー(ゲートウェイ)は別パッケージ。これは接続する側(エージェント共通のクライアント)。
- 各エージェントが openai のボイラープレート(メッセージ整形・画像入力・thinking 切替・ストリーム)を 再実装しなくて済む。
- 依存は公式
openaiSDK のみ。
インストール
uv add local-llm-client
使い方
ゲートウェイ(local-llm-server)を起動しておき、公開ポートに繋ぐだけ。
from local_llm_client import LLMClient
llm = LLMClient(
model="mlx-community/Qwen3.6-27B-4bit",
base_url="http://127.0.0.1:8799/v1",
)
print(llm.respond("ローカル LLM の利点を3つ。"))
# 画像入力・ストリーム
print(llm.respond("これは何?", images=["photo.jpg"]))
for piece in llm.respond("長い説明を", stream=True):
print(piece, end="", flush=True)
起動確認付きのワンライナー(未起動なら親切なエラー。サーバーは起動しない):
from local_llm_client import connect, ServerNotRunningError
try:
llm = connect(model="mlx-community/Qwen3.6-27B-4bit",
base_url="http://127.0.0.1:8799/v1")
except ServerNotRunningError:
print("先にゲートウェイ(local-llm-server)を起動してください")
高度な操作(embeddings / tool calling / 構造化出力 / async など)は、土台の openai クライアントに
llm.openai で直接アクセスできる。素の openai SDK で base_url を指してもよい。
在席セッション(使い終わったら即メモリ解放)
LLMClient は既定で、ゲートウェイに「このモデルを使う」と登録し、定期ハートビートを送る。
クライアントを破棄(close() / with ブロック終了 / プロセス終了)すると利用終了を通知し、
そのモデルを使うエージェントが他に居なければ、ゲートウェイがそのモデルを即アンロードして
メモリを解放する(idle_timeout の20分を待たない)。GPU/RAM が逼迫する環境で、使い終わった
モデルをすぐ片付けたいときに効く。
確実に即解放させるには with で囲むか、使い終わりに close() を呼ぶ:
with LLMClient(model="mlx-community/Qwen3.6-27B-4bit",
base_url="http://127.0.0.1:8799/v1") as llm:
print(llm.respond("..."))
# ブロックを抜けた瞬間、他に同モデル利用者が居なければメモリが即解放される
- 明示しなくても安全:
close()を呼ばずに落ちても、ゲートウェイ側がハートビート途絶を 検出して回収する(gateway.tomlのsession_ttl、既定90秒)。with/close()はそれを 待たず即座に解放するための最短手段。 - オフにする:
LLMClient(..., session=False)で完全に無効化(従来どおりidle_timeoutまかせ)。ゲートウェイが未対応/未起動でも自動で無効化されるだけで、エラーにはならない。 - 任意指定:
agent_id(既定は自動採番)、heartbeat_interval(既定30秒)。
サーバー側の仕組みは local-llm-server の 在席ベースの即時アンロード を参照。
ライセンス
Apache-2.0
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file local_llm_client-0.3.0.tar.gz.
File metadata
- Download URL: local_llm_client-0.3.0.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f4754211447e2fff8bd6d590d5b8c52c281037c7f3c25e6c408a11079f7f06db
|
|
| MD5 |
70ee27e436ee242d0d0512525dadae60
|
|
| BLAKE2b-256 |
9edbce4983fd8df6381121c4337a9063933a2dff913bfe42af50caf242753b6c
|
File details
Details for the file local_llm_client-0.3.0-py3-none-any.whl.
File metadata
- Download URL: local_llm_client-0.3.0-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3de0b798b84fac0ad2532758a2ce00104f56ec80d1ab0cb2ede2a1a79301f625
|
|
| MD5 |
770418529bfd6fb29ebbbf4e938e7c61
|
|
| BLAKE2b-256 |
881c51d869a843471dd837ff93076f33678fed0f18561dd1ae833643eb49ee9d
|