local-llm-server 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- local_llm_server-0.1.0/LICENSE +201 -0
- local_llm_server-0.1.0/PKG-INFO +68 -0
- local_llm_server-0.1.0/README.md +45 -0
- local_llm_server-0.1.0/local_llm_server/__init__.py +22 -0
- local_llm_server-0.1.0/local_llm_server/cli.py +282 -0
- local_llm_server-0.1.0/local_llm_server/constants.py +26 -0
- local_llm_server-0.1.0/local_llm_server/router.py +136 -0
- local_llm_server-0.1.0/local_llm_server/server.py +495 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/PKG-INFO +68 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/SOURCES.txt +16 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/dependency_links.txt +1 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/entry_points.txt +2 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/requires.txt +6 -0
- local_llm_server-0.1.0/local_llm_server.egg-info/top_level.txt +1 -0
- local_llm_server-0.1.0/pyproject.toml +51 -0
- local_llm_server-0.1.0/setup.cfg +4 -0
- local_llm_server-0.1.0/tests/test_router.py +169 -0
- local_llm_server-0.1.0/tests/test_server.py +368 -0
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: local-llm-server
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ローカルLLM(mlx / mlx-vlm / llama.cpp / router)を OpenAI 互換 API として起動・管理する軽量サーバー
|
|
5
|
+
Author: ToPo-ToPo-ToPo
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/ToPo-ToPo-ToPo/local-llm-server
|
|
8
|
+
Keywords: llm,local-llm,mlx,mlx-vlm,llama.cpp,openai,server,inference
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Topic :: Software Development
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Requires-Python: >=3.11
|
|
17
|
+
Description-Content-Type: text/markdown
|
|
18
|
+
License-File: LICENSE
|
|
19
|
+
Provides-Extra: mlx
|
|
20
|
+
Requires-Dist: mlx-lm>=0.31.3; (sys_platform == "darwin" and platform_machine == "arm64") and extra == "mlx"
|
|
21
|
+
Requires-Dist: mlx-vlm>=0.5.0; (sys_platform == "darwin" and platform_machine == "arm64") and extra == "mlx"
|
|
22
|
+
Dynamic: license-file
|
|
23
|
+
|
|
24
|
+
# local-llm-server
|
|
25
|
+
|
|
26
|
+
ローカルLLM(**mlx** / **mlx-vlm** / **llama.cpp**)を **OpenAI 互換 API** として
|
|
27
|
+
起動・管理する軽量サーバー。テキストと画像(vision)を自動で振り分ける
|
|
28
|
+
**router** モードを備え、任意の OpenAI 互換クライアントからそのまま利用できる。
|
|
29
|
+
|
|
30
|
+
- コア機能(プロセスの起動・監視・graceful shutdown・router プロキシ)は
|
|
31
|
+
**標準ライブラリのみ**で動作。
|
|
32
|
+
- 実際の推論バックエンドは extras で導入(Apple Silicon では `mlx` を自動選択)。
|
|
33
|
+
|
|
34
|
+
## インストール
|
|
35
|
+
|
|
36
|
+
```bash
|
|
37
|
+
pip install local-llm-server # コア(バックエンドは別途用意)
|
|
38
|
+
pip install "local-llm-server[mlx]" # Apple Silicon 向け mlx / mlx-vlm 同梱
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
## 使い方
|
|
42
|
+
|
|
43
|
+
```bash
|
|
44
|
+
# テキストLLMを起動(既定バックエンドは環境に応じて自動選択)
|
|
45
|
+
local-llm-server --backend mlx
|
|
46
|
+
|
|
47
|
+
# 画像入力対応(vision)
|
|
48
|
+
local-llm-server --backend mlx-vlm
|
|
49
|
+
|
|
50
|
+
# テキストLLMとVLMを同時起動し、リクエスト内容で自動振り分け
|
|
51
|
+
local-llm-server --backend router
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
起動後、表示される `base_url`(例 `http://127.0.0.1:8080/v1`)を
|
|
55
|
+
OpenAI 互換クライアントに設定する。
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import local_llm_server as srv
|
|
59
|
+
|
|
60
|
+
config = srv.ServerConfig(model="mlx-community/Qwen3.6-27B-4bit", backend="mlx")
|
|
61
|
+
with srv.LocalServer(config) as server:
|
|
62
|
+
server.wait_until_ready()
|
|
63
|
+
print(srv.list_models(server.base_url))
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## ライセンス
|
|
67
|
+
|
|
68
|
+
Apache-2.0
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
# local-llm-server
|
|
2
|
+
|
|
3
|
+
ローカルLLM(**mlx** / **mlx-vlm** / **llama.cpp**)を **OpenAI 互換 API** として
|
|
4
|
+
起動・管理する軽量サーバー。テキストと画像(vision)を自動で振り分ける
|
|
5
|
+
**router** モードを備え、任意の OpenAI 互換クライアントからそのまま利用できる。
|
|
6
|
+
|
|
7
|
+
- コア機能(プロセスの起動・監視・graceful shutdown・router プロキシ)は
|
|
8
|
+
**標準ライブラリのみ**で動作。
|
|
9
|
+
- 実際の推論バックエンドは extras で導入(Apple Silicon では `mlx` を自動選択)。
|
|
10
|
+
|
|
11
|
+
## インストール
|
|
12
|
+
|
|
13
|
+
```bash
|
|
14
|
+
pip install local-llm-server # コア(バックエンドは別途用意)
|
|
15
|
+
pip install "local-llm-server[mlx]" # Apple Silicon 向け mlx / mlx-vlm 同梱
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## 使い方
|
|
19
|
+
|
|
20
|
+
```bash
|
|
21
|
+
# テキストLLMを起動(既定バックエンドは環境に応じて自動選択)
|
|
22
|
+
local-llm-server --backend mlx
|
|
23
|
+
|
|
24
|
+
# 画像入力対応(vision)
|
|
25
|
+
local-llm-server --backend mlx-vlm
|
|
26
|
+
|
|
27
|
+
# テキストLLMとVLMを同時起動し、リクエスト内容で自動振り分け
|
|
28
|
+
local-llm-server --backend router
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
起動後、表示される `base_url`(例 `http://127.0.0.1:8080/v1`)を
|
|
32
|
+
OpenAI 互換クライアントに設定する。
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import local_llm_server as srv
|
|
36
|
+
|
|
37
|
+
config = srv.ServerConfig(model="mlx-community/Qwen3.6-27B-4bit", backend="mlx")
|
|
38
|
+
with srv.LocalServer(config) as server:
|
|
39
|
+
server.wait_until_ready()
|
|
40
|
+
print(srv.list_models(server.base_url))
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## ライセンス
|
|
44
|
+
|
|
45
|
+
Apache-2.0
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"""local-llm-server — ローカルLLMを OpenAI 互換 API として起動・管理する。
|
|
2
|
+
|
|
3
|
+
mlx / mlx-vlm / llama.cpp、および画像とテキストを自動振り分けする router を
|
|
4
|
+
サブプロセスとして起動・監視する。依存は標準ライブラリのみで、任意の
|
|
5
|
+
OpenAI 互換クライアントから利用できる。
|
|
6
|
+
|
|
7
|
+
公開 API:
|
|
8
|
+
- サーバー管理: LocalServer / ServerConfig / ServerPool / build_command など
|
|
9
|
+
- 起動状態の確認: is_ready / list_models / models_match / parse_host_port
|
|
10
|
+
- 既定値・定数: DEFAULT_MODEL / DEFAULT_VISION_MODEL / DEFAULT_BACKEND / BACKENDS
|
|
11
|
+
- router: RouterServer
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from .constants import ( # noqa: F401
|
|
16
|
+
BACKENDS,
|
|
17
|
+
DEFAULT_MODEL,
|
|
18
|
+
DEFAULT_VISION_MODEL,
|
|
19
|
+
_env_bool,
|
|
20
|
+
)
|
|
21
|
+
from .server import * # noqa: F401,F403
|
|
22
|
+
from .router import * # noqa: F401,F403
|
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import argparse
|
|
4
|
+
import os
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
|
|
8
|
+
from .constants import DEFAULT_MODEL, DEFAULT_VISION_MODEL, _env_bool
|
|
9
|
+
from .router import RouterServer
|
|
10
|
+
from .server import (
|
|
11
|
+
BACKENDS,
|
|
12
|
+
DEFAULT_BACKEND,
|
|
13
|
+
ServerConfig,
|
|
14
|
+
ServerPool,
|
|
15
|
+
build_command,
|
|
16
|
+
build_pool_configs,
|
|
17
|
+
find_pids_on_port,
|
|
18
|
+
install_shutdown_handlers,
|
|
19
|
+
parallel_supported,
|
|
20
|
+
stop_pid,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
# CLI で選べるバックエンド。router は内部でテキスト LLM と vision VLM を
|
|
24
|
+
# 同時起動し、リクエスト内容で自動振り分けする特別モード。
|
|
25
|
+
ROUTER = "router"
|
|
26
|
+
CLI_BACKENDS = (*BACKENDS, ROUTER)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _run_router(args, extra: list[str], enable_thinking: bool) -> int:
|
|
30
|
+
"""router モード: テキスト LLM と vision VLM を同時起動し、プロキシで振り分ける。
|
|
31
|
+
|
|
32
|
+
公開ポート(args.port)にルーターを立て、テキストを port+1、vision を port+2 で
|
|
33
|
+
起動する。クライアントは公開ポートの 1 つだけを base_url に指定すればよい。
|
|
34
|
+
vision バックエンドは mlx-vlm(Apple Silicon 前提)、テキストは mlx を使う。
|
|
35
|
+
"""
|
|
36
|
+
text_port = args.port + 1
|
|
37
|
+
vision_port = args.port + 2
|
|
38
|
+
text_cfg = ServerConfig(
|
|
39
|
+
backend="mlx",
|
|
40
|
+
model=args.model,
|
|
41
|
+
host=args.host,
|
|
42
|
+
port=text_port,
|
|
43
|
+
disable_thinking=not enable_thinking,
|
|
44
|
+
extra_args=extra,
|
|
45
|
+
)
|
|
46
|
+
vision_cfg = ServerConfig(
|
|
47
|
+
backend="mlx-vlm",
|
|
48
|
+
model=args.vision_model,
|
|
49
|
+
host=args.host,
|
|
50
|
+
port=vision_port,
|
|
51
|
+
)
|
|
52
|
+
pool = ServerPool([text_cfg, vision_cfg])
|
|
53
|
+
print(f"Starting (text): {' '.join(build_command(text_cfg))}", file=sys.stderr)
|
|
54
|
+
print(f"Starting (vision): {' '.join(build_command(vision_cfg))}", file=sys.stderr)
|
|
55
|
+
try:
|
|
56
|
+
pool.start()
|
|
57
|
+
pool.wait_until_ready()
|
|
58
|
+
except (RuntimeError, TimeoutError) as exc:
|
|
59
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
60
|
+
pool.stop()
|
|
61
|
+
return 1
|
|
62
|
+
|
|
63
|
+
router = RouterServer(
|
|
64
|
+
(args.host, args.port),
|
|
65
|
+
(args.host, text_port),
|
|
66
|
+
(args.host, vision_port),
|
|
67
|
+
)
|
|
68
|
+
public_url = f"http://{args.host}:{args.port}/v1"
|
|
69
|
+
print("Ready (automatic routing):", file=sys.stderr)
|
|
70
|
+
print(f" - public: {public_url}", file=sys.stderr)
|
|
71
|
+
print(f" text -> http://{args.host}:{text_port}/v1", file=sys.stderr)
|
|
72
|
+
print(f" image/media -> http://{args.host}:{vision_port}/v1", file=sys.stderr)
|
|
73
|
+
print(
|
|
74
|
+
f'In agent.toml, set only the single base_url = "{public_url}". '
|
|
75
|
+
"Text-only requests are routed to the LLM, requests with images to the VLM, automatically.",
|
|
76
|
+
file=sys.stderr,
|
|
77
|
+
)
|
|
78
|
+
print(
|
|
79
|
+
"Note: if the VLM does not support function calling (tools), "
|
|
80
|
+
'set tool_mode = "prompt" when using images.',
|
|
81
|
+
file=sys.stderr,
|
|
82
|
+
)
|
|
83
|
+
|
|
84
|
+
thread = threading.Thread(target=router.serve_forever, daemon=True)
|
|
85
|
+
thread.start()
|
|
86
|
+
try:
|
|
87
|
+
pool.wait() # 子サーバーが終了するまでブロック
|
|
88
|
+
except KeyboardInterrupt:
|
|
89
|
+
pass
|
|
90
|
+
finally:
|
|
91
|
+
router.shutdown()
|
|
92
|
+
router.server_close()
|
|
93
|
+
pool.stop()
|
|
94
|
+
return 0
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _stop_servers(ports: list[int]) -> int:
|
|
98
|
+
"""指定ポートで動いているローカルサーバーを探して停止する(--stop 用)。
|
|
99
|
+
|
|
100
|
+
各ポートを LISTEN しているプロセスを lsof で特定し、プロセスグループごと
|
|
101
|
+
SIGTERM→SIGKILL で止める。1つでも止めれば 0、見つからなければ 1 を返す。
|
|
102
|
+
"""
|
|
103
|
+
if os.name != "posix":
|
|
104
|
+
print(
|
|
105
|
+
"--stop is supported on macOS / Linux only. On Windows, stop the server "
|
|
106
|
+
"from its own window (Ctrl+C) or via Task Manager.",
|
|
107
|
+
file=sys.stderr,
|
|
108
|
+
)
|
|
109
|
+
return 1
|
|
110
|
+
stopped = False
|
|
111
|
+
for port in ports:
|
|
112
|
+
pids = find_pids_on_port(port)
|
|
113
|
+
for pid in pids:
|
|
114
|
+
print(f"Stopping the server on port {port} (pid {pid})...", file=sys.stderr)
|
|
115
|
+
if stop_pid(pid):
|
|
116
|
+
stopped = True
|
|
117
|
+
if not stopped:
|
|
118
|
+
ports_str = ", ".join(str(p) for p in ports)
|
|
119
|
+
print(f"No running server found on port(s): {ports_str}.", file=sys.stderr)
|
|
120
|
+
return 1
|
|
121
|
+
print("Stopped.", file=sys.stderr)
|
|
122
|
+
return 0
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def main(argv: list[str] | None = None) -> int:
|
|
126
|
+
parser = argparse.ArgumentParser(
|
|
127
|
+
prog="local-llm-server",
|
|
128
|
+
description=(
|
|
129
|
+
"Start a local LLM server (mlx / mlx-vlm / llama.cpp / router). "
|
|
130
|
+
"Use the vision-capable mlx-vlm for image input (--backend mlx-vlm). "
|
|
131
|
+
"With --backend router, start a text LLM and a VLM together and route requests automatically based on their content."
|
|
132
|
+
),
|
|
133
|
+
epilog="Backend-specific extra arguments can be passed after --.",
|
|
134
|
+
)
|
|
135
|
+
parser.add_argument(
|
|
136
|
+
"--backend",
|
|
137
|
+
choices=CLI_BACKENDS,
|
|
138
|
+
default=os.environ.get("CODER_BACKEND", DEFAULT_BACKEND),
|
|
139
|
+
help="Backend to use (env: CODER_BACKEND, default: %(default)s)",
|
|
140
|
+
)
|
|
141
|
+
parser.add_argument(
|
|
142
|
+
"--model",
|
|
143
|
+
default=os.environ.get("CODER_MODEL", DEFAULT_MODEL),
|
|
144
|
+
help="Model (mlx: name/path, llama-cpp: .gguf path; used as the text model in router mode). env: CODER_MODEL, default: %(default)s",
|
|
145
|
+
)
|
|
146
|
+
parser.add_argument(
|
|
147
|
+
"--vision-model",
|
|
148
|
+
default=os.environ.get("CODER_VISION_MODEL", DEFAULT_VISION_MODEL),
|
|
149
|
+
help="Vision model that handles images/media in router mode. env: CODER_VISION_MODEL, default: %(default)s",
|
|
150
|
+
)
|
|
151
|
+
parser.add_argument("--host", default="127.0.0.1", help="Host to bind to")
|
|
152
|
+
parser.add_argument("--port", type=int, default=8080, help="Port to bind to (the first port when using a pool)")
|
|
153
|
+
parser.add_argument(
|
|
154
|
+
"--stop",
|
|
155
|
+
action="store_true",
|
|
156
|
+
help="Stop a server already running on --port instead of starting one "
|
|
157
|
+
"(use the same --port / --instances / --backend router as when you started it). macOS / Linux only.",
|
|
158
|
+
)
|
|
159
|
+
parser.add_argument(
|
|
160
|
+
"--parallel",
|
|
161
|
+
type=int,
|
|
162
|
+
help="Number of concurrent processing slots (llama.cpp only; mlx processes sequentially)",
|
|
163
|
+
)
|
|
164
|
+
parser.add_argument(
|
|
165
|
+
"--instances",
|
|
166
|
+
type=int,
|
|
167
|
+
default=1,
|
|
168
|
+
help="Number of servers to start on consecutive ports (to gain parallelism with mlx; each loads its own model)",
|
|
169
|
+
)
|
|
170
|
+
parser.add_argument(
|
|
171
|
+
"--thinking",
|
|
172
|
+
action=argparse.BooleanOptionalAction,
|
|
173
|
+
default=None,
|
|
174
|
+
help="Thinking mode. Use --no-thinking to disable (env: CODER_ENABLE_THINKING)",
|
|
175
|
+
)
|
|
176
|
+
parser.add_argument(
|
|
177
|
+
"--draft-model",
|
|
178
|
+
default=os.environ.get("CODER_DRAFT_MODEL"),
|
|
179
|
+
help="MTP drafter for Gemma 4 (HF id/path, e.g. "
|
|
180
|
+
"mlx-community/gemma-4-E4B-it-qat-assistant-bf16). Use 'auto' to select automatically from the main model name. "
|
|
181
|
+
"Speeds up inference without changing the main model's output. mlx-vlm backend only. env: CODER_DRAFT_MODEL",
|
|
182
|
+
)
|
|
183
|
+
args, extra = parser.parse_known_args(argv)
|
|
184
|
+
|
|
185
|
+
if args.instances < 1:
|
|
186
|
+
parser.error("--instances must be 1 or greater")
|
|
187
|
+
|
|
188
|
+
# --stop: 起動の代わりに、起動時と同じポート割り当てを再現して停止する。
|
|
189
|
+
# router は public(port) / text(port+1) / vision(port+2)、通常は連番ポート。
|
|
190
|
+
if args.stop:
|
|
191
|
+
if args.backend == ROUTER:
|
|
192
|
+
ports = [args.port, args.port + 1, args.port + 2]
|
|
193
|
+
else:
|
|
194
|
+
ports = [args.port + i for i in range(args.instances)]
|
|
195
|
+
return _stop_servers(ports)
|
|
196
|
+
|
|
197
|
+
# kill / ターミナルを閉じる(SIGTERM・SIGHUP)でも下流の finally(pool.stop)を
|
|
198
|
+
# 必ず通し、起動したバックエンドを孫プロセスとして残さない。
|
|
199
|
+
install_shutdown_handlers()
|
|
200
|
+
|
|
201
|
+
if args.parallel is not None:
|
|
202
|
+
if args.parallel < 1:
|
|
203
|
+
parser.error("--parallel must be 1 or greater")
|
|
204
|
+
if not parallel_supported(args.backend):
|
|
205
|
+
print(
|
|
206
|
+
f"Warning: {args.backend} does not support parallel slots "
|
|
207
|
+
"(sequential processing). --parallel is ignored.",
|
|
208
|
+
file=sys.stderr,
|
|
209
|
+
)
|
|
210
|
+
|
|
211
|
+
if args.backend == "mlx-vlm":
|
|
212
|
+
print(
|
|
213
|
+
"Info: starting with the vision backend (mlx-vlm). It supports image input (image_url), "
|
|
214
|
+
"but some models do not support function calling (tools)/streaming. "
|
|
215
|
+
'In that case, set tool_mode = "prompt" in agent.toml.',
|
|
216
|
+
file=sys.stderr,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
enable_thinking = (
|
|
220
|
+
args.thinking if args.thinking is not None
|
|
221
|
+
else _env_bool("CODER_ENABLE_THINKING", False)
|
|
222
|
+
)
|
|
223
|
+
|
|
224
|
+
# argparse が拾った先頭の "--" を除去してバックエンドへ素通し
|
|
225
|
+
if extra and extra[0] == "--":
|
|
226
|
+
extra = extra[1:]
|
|
227
|
+
|
|
228
|
+
if args.backend == ROUTER:
|
|
229
|
+
if args.instances != 1:
|
|
230
|
+
parser.error("--instances cannot be used with --backend router")
|
|
231
|
+
return _run_router(args, extra, enable_thinking)
|
|
232
|
+
|
|
233
|
+
base = ServerConfig(
|
|
234
|
+
backend=args.backend,
|
|
235
|
+
model=args.model,
|
|
236
|
+
host=args.host,
|
|
237
|
+
port=args.port,
|
|
238
|
+
parallel=args.parallel,
|
|
239
|
+
disable_thinking=not enable_thinking,
|
|
240
|
+
draft_model=args.draft_model,
|
|
241
|
+
extra_args=extra,
|
|
242
|
+
)
|
|
243
|
+
configs = build_pool_configs(base, args.instances)
|
|
244
|
+
pool = ServerPool(configs)
|
|
245
|
+
|
|
246
|
+
for config in configs:
|
|
247
|
+
print(f"Starting: {' '.join(build_command(config))}", file=sys.stderr)
|
|
248
|
+
try:
|
|
249
|
+
pool.start()
|
|
250
|
+
pool.wait_until_ready()
|
|
251
|
+
except (RuntimeError, TimeoutError) as exc:
|
|
252
|
+
print(f"Error: {exc}", file=sys.stderr)
|
|
253
|
+
pool.stop()
|
|
254
|
+
return 1
|
|
255
|
+
|
|
256
|
+
print("Ready:", file=sys.stderr)
|
|
257
|
+
for url in pool.base_urls:
|
|
258
|
+
print(f" - {url}", file=sys.stderr)
|
|
259
|
+
if args.instances > 1:
|
|
260
|
+
print(
|
|
261
|
+
"Point each OpenAI-compatible client at one of the base URLs above. "
|
|
262
|
+
f'Example: base_url = "{pool.base_urls[0]}"',
|
|
263
|
+
file=sys.stderr,
|
|
264
|
+
)
|
|
265
|
+
else:
|
|
266
|
+
print(
|
|
267
|
+
"Point your OpenAI-compatible client at "
|
|
268
|
+
f'base_url = "{pool.base_urls[0]}".',
|
|
269
|
+
file=sys.stderr,
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
try:
|
|
273
|
+
pool.wait()
|
|
274
|
+
except KeyboardInterrupt:
|
|
275
|
+
pass
|
|
276
|
+
finally:
|
|
277
|
+
pool.stop()
|
|
278
|
+
return 0
|
|
279
|
+
|
|
280
|
+
|
|
281
|
+
if __name__ == "__main__":
|
|
282
|
+
raise SystemExit(main())
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"""エージェント側(agent/)とサーバー側(server/)で共有する中立な定数・ヘルパー。
|
|
2
|
+
|
|
3
|
+
どちらのサブパッケージにも依存しないため、ここを基点に server/ と agent/ を
|
|
4
|
+
完全に分離できる(server/ が agent/ を import する逆向き依存を作らないための層)。
|
|
5
|
+
"""
|
|
6
|
+
from __future__ import annotations
|
|
7
|
+
|
|
8
|
+
import os
|
|
9
|
+
|
|
10
|
+
# サーバー・モデルが明示されないときに使う既定モデル
|
|
11
|
+
DEFAULT_MODEL = "mlx-community/Qwen3.6-27B-4bit"
|
|
12
|
+
|
|
13
|
+
# 画像・メディアを処理する vision モデルの既定。
|
|
14
|
+
# 既定モデル(Qwen3.6)はマルチモーダルなので、テキストと共通のものを使う。
|
|
15
|
+
DEFAULT_VISION_MODEL = DEFAULT_MODEL
|
|
16
|
+
|
|
17
|
+
# 起動可能なローカルLLMサーバーのバックエンド一覧。
|
|
18
|
+
# server/ が実装し、agent/ は agent.toml の backend 検証に使う(共有値)。
|
|
19
|
+
BACKENDS = ("mlx", "mlx-vlm", "llama-cpp")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _env_bool(name: str, default: bool) -> bool:
|
|
23
|
+
value = os.environ.get(name)
|
|
24
|
+
if value is None:
|
|
25
|
+
return default
|
|
26
|
+
return value.strip().lower() in ("1", "true", "yes", "on")
|