local-llm-client 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_client-0.1.0/LICENSE +201 -0
- local_llm_client-0.1.0/PKG-INFO +73 -0
- local_llm_client-0.1.0/README.md +52 -0
- local_llm_client-0.1.0/local_llm_client/__init__.py +37 -0
- local_llm_client-0.1.0/local_llm_client/client.py +196 -0
- local_llm_client-0.1.0/local_llm_client.egg-info/PKG-INFO +73 -0
- local_llm_client-0.1.0/local_llm_client.egg-info/SOURCES.txt +11 -0
- local_llm_client-0.1.0/local_llm_client.egg-info/dependency_links.txt +1 -0
- local_llm_client-0.1.0/local_llm_client.egg-info/requires.txt +1 -0
- local_llm_client-0.1.0/local_llm_client.egg-info/top_level.txt +1 -0
- local_llm_client-0.1.0/pyproject.toml +42 -0
- local_llm_client-0.1.0/setup.cfg +4 -0
- local_llm_client-0.1.0/tests/test_client.py +166 -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,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: local-llm-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ローカル LLM ゲートウェイ(local-llm-server / OpenAI 互換)に繋ぐ高レベルクライアント。エージェント共通のクライアントで、openai のボイラープレートを再実装しない。
|
|
5
|
+
Author: ToPo-ToPo-ToPo
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/ToPo-ToPo-ToPo/local-llm-client
|
|
8
|
+
Keywords: llm,openai,client,local-llm,gateway,agent
|
|
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
|
+
Requires-Dist: openai>=1.0
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# local-llm-client
|
|
23
|
+
|
|
24
|
+
ローカル LLM ゲートウェイ([local-llm-server](https://github.com/ToPo-ToPo-ToPo/local-llm-server) / OpenAI 互換)に繋ぐ**高レベルクライアント**。
|
|
25
|
+
|
|
26
|
+
- サーバー(ゲートウェイ)は別パッケージ。これは**接続する側**(エージェント共通のクライアント)。
|
|
27
|
+
- 各エージェントが openai のボイラープレート(メッセージ整形・画像入力・thinking 切替・ストリーム)を
|
|
28
|
+
再実装しなくて済む。
|
|
29
|
+
- 依存は公式 `openai` SDK のみ。
|
|
30
|
+
|
|
31
|
+
## インストール
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
uv add local-llm-client
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 使い方
|
|
38
|
+
|
|
39
|
+
ゲートウェイ(`local-llm-server`)を起動しておき、公開ポートに繋ぐだけ。
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from local_llm_client import LLMClient
|
|
43
|
+
|
|
44
|
+
llm = LLMClient(
|
|
45
|
+
model="mlx-community/Qwen3.6-27B-4bit",
|
|
46
|
+
base_url="http://127.0.0.1:8799/v1",
|
|
47
|
+
)
|
|
48
|
+
print(llm.respond("ローカル LLM の利点を3つ。"))
|
|
49
|
+
|
|
50
|
+
# 画像入力・ストリーム
|
|
51
|
+
print(llm.respond("これは何?", images=["photo.jpg"]))
|
|
52
|
+
for piece in llm.respond("長い説明を", stream=True):
|
|
53
|
+
print(piece, end="", flush=True)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
起動確認付きのワンライナー(未起動なら親切なエラー。サーバーは起動しない):
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from local_llm_client import connect, ServerNotRunningError
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
llm = connect(model="mlx-community/Qwen3.6-27B-4bit",
|
|
63
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
64
|
+
except ServerNotRunningError:
|
|
65
|
+
print("先にゲートウェイ(local-llm-server)を起動してください")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
高度な操作(embeddings / tool calling / 構造化出力 / async など)は、土台の openai クライアントに
|
|
69
|
+
`llm.openai` で直接アクセスできる。素の `openai` SDK で `base_url` を指してもよい。
|
|
70
|
+
|
|
71
|
+
## ライセンス
|
|
72
|
+
|
|
73
|
+
Apache-2.0
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# local-llm-client
|
|
2
|
+
|
|
3
|
+
ローカル LLM ゲートウェイ([local-llm-server](https://github.com/ToPo-ToPo-ToPo/local-llm-server) / OpenAI 互換)に繋ぐ**高レベルクライアント**。
|
|
4
|
+
|
|
5
|
+
- サーバー(ゲートウェイ)は別パッケージ。これは**接続する側**(エージェント共通のクライアント)。
|
|
6
|
+
- 各エージェントが openai のボイラープレート(メッセージ整形・画像入力・thinking 切替・ストリーム)を
|
|
7
|
+
再実装しなくて済む。
|
|
8
|
+
- 依存は公式 `openai` SDK のみ。
|
|
9
|
+
|
|
10
|
+
## インストール
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
uv add local-llm-client
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
## 使い方
|
|
17
|
+
|
|
18
|
+
ゲートウェイ(`local-llm-server`)を起動しておき、公開ポートに繋ぐだけ。
|
|
19
|
+
|
|
20
|
+
```python
|
|
21
|
+
from local_llm_client import LLMClient
|
|
22
|
+
|
|
23
|
+
llm = LLMClient(
|
|
24
|
+
model="mlx-community/Qwen3.6-27B-4bit",
|
|
25
|
+
base_url="http://127.0.0.1:8799/v1",
|
|
26
|
+
)
|
|
27
|
+
print(llm.respond("ローカル LLM の利点を3つ。"))
|
|
28
|
+
|
|
29
|
+
# 画像入力・ストリーム
|
|
30
|
+
print(llm.respond("これは何?", images=["photo.jpg"]))
|
|
31
|
+
for piece in llm.respond("長い説明を", stream=True):
|
|
32
|
+
print(piece, end="", flush=True)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
起動確認付きのワンライナー(未起動なら親切なエラー。サーバーは起動しない):
|
|
36
|
+
|
|
37
|
+
```python
|
|
38
|
+
from local_llm_client import connect, ServerNotRunningError
|
|
39
|
+
|
|
40
|
+
try:
|
|
41
|
+
llm = connect(model="mlx-community/Qwen3.6-27B-4bit",
|
|
42
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
43
|
+
except ServerNotRunningError:
|
|
44
|
+
print("先にゲートウェイ(local-llm-server)を起動してください")
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
高度な操作(embeddings / tool calling / 構造化出力 / async など)は、土台の openai クライアントに
|
|
48
|
+
`llm.openai` で直接アクセスできる。素の `openai` SDK で `base_url` を指してもよい。
|
|
49
|
+
|
|
50
|
+
## ライセンス
|
|
51
|
+
|
|
52
|
+
Apache-2.0
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""local-llm-client — ローカル LLM ゲートウェイ(OpenAI 互換)に繋ぐ高レベルクライアント。
|
|
2
|
+
|
|
3
|
+
サーバー(ゲートウェイ)は別パッケージ `local-llm-server`。本パッケージは**接続する側**
|
|
4
|
+
(エージェント共通のクライアント)だけを提供し、各エージェントが openai のボイラープレートを
|
|
5
|
+
再実装しなくて済むようにする。依存は公式 `openai` SDK のみ。
|
|
6
|
+
|
|
7
|
+
from local_llm_client import LLMClient, connect
|
|
8
|
+
|
|
9
|
+
llm = LLMClient(model="mlx-community/Qwen3.6-27B-4bit",
|
|
10
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
11
|
+
print(llm.respond("こんにちは", images=["photo.jpg"]))
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
from .client import (
|
|
16
|
+
DEFAULT_BASE_URL,
|
|
17
|
+
DEFAULT_MODEL,
|
|
18
|
+
LLMClient,
|
|
19
|
+
ServerNotRunningError,
|
|
20
|
+
build_user_content,
|
|
21
|
+
connect,
|
|
22
|
+
is_ready,
|
|
23
|
+
thinking_extra_body,
|
|
24
|
+
to_image_url,
|
|
25
|
+
)
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"LLMClient",
|
|
29
|
+
"connect",
|
|
30
|
+
"ServerNotRunningError",
|
|
31
|
+
"is_ready",
|
|
32
|
+
"to_image_url",
|
|
33
|
+
"build_user_content",
|
|
34
|
+
"thinking_extra_body",
|
|
35
|
+
"DEFAULT_MODEL",
|
|
36
|
+
"DEFAULT_BASE_URL",
|
|
37
|
+
]
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
"""ローカル LLM ゲートウェイ(OpenAI 互換)へ繋ぐ高レベルクライアント。
|
|
2
|
+
|
|
3
|
+
各エージェントが個別に書いていた「system / user / 画像をまとめて chat.completions に
|
|
4
|
+
投げ、テキスト(or ストリーム)を受け取る」ラッパー。公式 `openai` SDK を土台にするため
|
|
5
|
+
自動リトライ・型付きレスポンス・ツール呼び出し/構造化出力などの高度機能もそのまま使える。
|
|
6
|
+
|
|
7
|
+
from local_llm_client import LLMClient
|
|
8
|
+
|
|
9
|
+
llm = LLMClient(model="mlx-community/Qwen3.6-27B-4bit",
|
|
10
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
11
|
+
print(llm.respond("俳句を一つ詠んでください。"))
|
|
12
|
+
|
|
13
|
+
起動中のゲートウェイに繋ぐワンライナー(未起動なら親切なエラー。サーバーは起動しない)
|
|
14
|
+
なら connect():
|
|
15
|
+
|
|
16
|
+
from local_llm_client import connect
|
|
17
|
+
|
|
18
|
+
llm = connect(model="mlx-community/Qwen3.6-27B-4bit",
|
|
19
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
20
|
+
for piece in llm.respond("ローカル LLM の利点は?", stream=True):
|
|
21
|
+
print(piece, end="", flush=True)
|
|
22
|
+
|
|
23
|
+
より高度な操作(embeddings / tool calling / 構造化出力 / async など)は、`LLMClient.openai`
|
|
24
|
+
で土台の openai クライアントに直接アクセスできる。
|
|
25
|
+
"""
|
|
26
|
+
from __future__ import annotations
|
|
27
|
+
|
|
28
|
+
import base64
|
|
29
|
+
import mimetypes
|
|
30
|
+
import urllib.error
|
|
31
|
+
import urllib.request
|
|
32
|
+
from pathlib import Path
|
|
33
|
+
from typing import Any, Iterator
|
|
34
|
+
|
|
35
|
+
from openai import OpenAI
|
|
36
|
+
|
|
37
|
+
# ゲートウェイ(local-llm-server)の既定値。クライアントは公開ポートへ繋ぐだけ。
|
|
38
|
+
DEFAULT_MODEL = "mlx-community/Qwen3.6-27B-4bit"
|
|
39
|
+
DEFAULT_BASE_URL = "http://127.0.0.1:8799/v1"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class ServerNotRunningError(RuntimeError):
|
|
43
|
+
"""接続先(ゲートウェイ)が応答しなかった。"""
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def is_ready(base_url: str = DEFAULT_BASE_URL, timeout: float = 1.0) -> bool:
|
|
47
|
+
"""OpenAI 互換サーバー(ゲートウェイ)が応答可能かを判定する。"""
|
|
48
|
+
try:
|
|
49
|
+
with urllib.request.urlopen(f"{base_url}/models", timeout=timeout) as resp:
|
|
50
|
+
return resp.status == 200
|
|
51
|
+
except (urllib.error.URLError, OSError):
|
|
52
|
+
return False
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
def _is_url(ref: str) -> bool:
|
|
56
|
+
return ref.startswith(("http://", "https://", "data:"))
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def to_image_url(ref: str) -> str:
|
|
60
|
+
"""画像参照(ローカルパス or URL)を OpenAI 互換の image_url 文字列に変換する。
|
|
61
|
+
|
|
62
|
+
URL / データURI はそのまま、ローカルファイルは base64 のデータURIにする。
|
|
63
|
+
"""
|
|
64
|
+
if _is_url(ref):
|
|
65
|
+
return ref
|
|
66
|
+
path = Path(ref)
|
|
67
|
+
if not path.exists():
|
|
68
|
+
raise FileNotFoundError(f"Image not found: {ref}")
|
|
69
|
+
mime = mimetypes.guess_type(path.name)[0] or "image/png"
|
|
70
|
+
data = base64.b64encode(path.read_bytes()).decode("ascii")
|
|
71
|
+
return f"data:{mime};base64,{data}"
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def build_user_content(
|
|
75
|
+
text: str, images: list[str] | None = None
|
|
76
|
+
) -> str | list[dict[str, Any]]:
|
|
77
|
+
"""テキスト(+画像)を OpenAI 互換の user メッセージ content に組み立てる。
|
|
78
|
+
|
|
79
|
+
画像が無ければ素の文字列、あれば text パート+image_url パートの配列を返す。
|
|
80
|
+
"""
|
|
81
|
+
if not images:
|
|
82
|
+
return text
|
|
83
|
+
parts: list[dict[str, Any]] = [{"type": "text", "text": text}]
|
|
84
|
+
for ref in images:
|
|
85
|
+
parts.append({"type": "image_url", "image_url": {"url": to_image_url(ref)}})
|
|
86
|
+
return parts
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def thinking_extra_body(enable: bool) -> dict[str, Any]:
|
|
90
|
+
"""思考(thinking)モードの ON/OFF をサーバーへ渡す extra_body を作る。
|
|
91
|
+
|
|
92
|
+
バックエンドによって解釈するキーが異なるため両形式を併記する(未知キーは無視される):
|
|
93
|
+
- mlx-vlm … トップレベル enable_thinking
|
|
94
|
+
- mlx_lm/llama … chat_template_kwargs.enable_thinking
|
|
95
|
+
|
|
96
|
+
chat.completions.create(..., extra_body=thinking_extra_body(False)) のように渡す。
|
|
97
|
+
"""
|
|
98
|
+
return {
|
|
99
|
+
"enable_thinking": enable,
|
|
100
|
+
"chat_template_kwargs": {"enable_thinking": enable},
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class LLMClient:
|
|
105
|
+
"""OpenAI 互換エンドポイント(ゲートウェイ)へ繋ぐクライアント。
|
|
106
|
+
|
|
107
|
+
respond() は非ストリームでは生成テキスト(str)を、stream=True ではテキスト断片の
|
|
108
|
+
Iterator[str] を返す。マルチモーダルは images にローカルパス/URL を渡す。土台の
|
|
109
|
+
openai クライアントは ``self.openai`` で直接使える(embeddings / tool calling など)。
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
def __init__(
|
|
113
|
+
self,
|
|
114
|
+
model: str = DEFAULT_MODEL,
|
|
115
|
+
*,
|
|
116
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
117
|
+
api_key: str = "not-needed",
|
|
118
|
+
temperature: float = 0.0,
|
|
119
|
+
max_tokens: int | None = None,
|
|
120
|
+
timeout: Any = None,
|
|
121
|
+
) -> None:
|
|
122
|
+
self.model = model
|
|
123
|
+
self.base_url = base_url
|
|
124
|
+
self.temperature = float(temperature)
|
|
125
|
+
self.max_tokens = max_tokens
|
|
126
|
+
# timeout は float / httpx.Timeout / None。None のときは openai の既定に任せる
|
|
127
|
+
# (ローカルの巨大モデルは初回応答が遅いので、長め/無制限を渡せるようにする)。
|
|
128
|
+
client_kwargs: dict[str, Any] = {"base_url": base_url, "api_key": api_key}
|
|
129
|
+
if timeout is not None:
|
|
130
|
+
client_kwargs["timeout"] = timeout
|
|
131
|
+
self.openai = OpenAI(**client_kwargs)
|
|
132
|
+
|
|
133
|
+
def respond(
|
|
134
|
+
self,
|
|
135
|
+
user_text: str,
|
|
136
|
+
*,
|
|
137
|
+
system_prompt: str | None = None,
|
|
138
|
+
images: list[str] | None = None,
|
|
139
|
+
stream: bool = False,
|
|
140
|
+
**kwargs: Any,
|
|
141
|
+
) -> str | Iterator[str]:
|
|
142
|
+
"""1 ターン生成する。stream=True なら断片の Iterator[str] を返す。"""
|
|
143
|
+
messages: list[dict[str, Any]] = []
|
|
144
|
+
if system_prompt:
|
|
145
|
+
messages.append({"role": "system", "content": system_prompt})
|
|
146
|
+
messages.append({"role": "user", "content": build_user_content(user_text, images)})
|
|
147
|
+
|
|
148
|
+
params: dict[str, Any] = dict(
|
|
149
|
+
model=self.model, messages=messages, temperature=self.temperature, **kwargs
|
|
150
|
+
)
|
|
151
|
+
if self.max_tokens is not None:
|
|
152
|
+
params["max_tokens"] = self.max_tokens
|
|
153
|
+
|
|
154
|
+
if stream:
|
|
155
|
+
return self._stream(params)
|
|
156
|
+
resp = self.openai.chat.completions.create(**params)
|
|
157
|
+
return resp.choices[0].message.content or ""
|
|
158
|
+
|
|
159
|
+
def _stream(self, params: dict[str, Any]) -> Iterator[str]:
|
|
160
|
+
for chunk in self.openai.chat.completions.create(stream=True, **params):
|
|
161
|
+
if not chunk.choices:
|
|
162
|
+
continue
|
|
163
|
+
delta = chunk.choices[0].delta.content
|
|
164
|
+
if delta:
|
|
165
|
+
yield delta
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def connect(
|
|
169
|
+
model: str = DEFAULT_MODEL,
|
|
170
|
+
*,
|
|
171
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
172
|
+
temperature: float = 0.0,
|
|
173
|
+
max_tokens: int | None = None,
|
|
174
|
+
timeout: Any = None,
|
|
175
|
+
) -> LLMClient:
|
|
176
|
+
"""**起動中のゲートウェイ**に繋いだ LLMClient を返す(サーバーは起動しない)。
|
|
177
|
+
|
|
178
|
+
挙動は 2 つだけ:
|
|
179
|
+
1. base_url が応答する … そのゲートウェイに繋いだ LLMClient を返す。
|
|
180
|
+
2. 応答しない … ServerNotRunningError を投げる(先にゲートウェイを起動して)。
|
|
181
|
+
|
|
182
|
+
繋ぐ前に死活確認したいときの薄いワンライナー。挙動は `LLMClient(model, base_url=...)` と
|
|
183
|
+
同じだが、未起動なら早めに親切なエラーを出す。
|
|
184
|
+
"""
|
|
185
|
+
if not is_ready(base_url):
|
|
186
|
+
raise ServerNotRunningError(
|
|
187
|
+
f"No gateway responding at {base_url}. Start it first (the local-llm-server "
|
|
188
|
+
"gateway / its app), then connect again."
|
|
189
|
+
)
|
|
190
|
+
return LLMClient(
|
|
191
|
+
model,
|
|
192
|
+
base_url=base_url,
|
|
193
|
+
temperature=temperature,
|
|
194
|
+
max_tokens=max_tokens,
|
|
195
|
+
timeout=timeout,
|
|
196
|
+
)
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: local-llm-client
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: ローカル LLM ゲートウェイ(local-llm-server / OpenAI 互換)に繋ぐ高レベルクライアント。エージェント共通のクライアントで、openai のボイラープレートを再実装しない。
|
|
5
|
+
Author: ToPo-ToPo-ToPo
|
|
6
|
+
License-Expression: Apache-2.0
|
|
7
|
+
Project-URL: Repository, https://github.com/ToPo-ToPo-ToPo/local-llm-client
|
|
8
|
+
Keywords: llm,openai,client,local-llm,gateway,agent
|
|
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
|
+
Requires-Dist: openai>=1.0
|
|
20
|
+
Dynamic: license-file
|
|
21
|
+
|
|
22
|
+
# local-llm-client
|
|
23
|
+
|
|
24
|
+
ローカル LLM ゲートウェイ([local-llm-server](https://github.com/ToPo-ToPo-ToPo/local-llm-server) / OpenAI 互換)に繋ぐ**高レベルクライアント**。
|
|
25
|
+
|
|
26
|
+
- サーバー(ゲートウェイ)は別パッケージ。これは**接続する側**(エージェント共通のクライアント)。
|
|
27
|
+
- 各エージェントが openai のボイラープレート(メッセージ整形・画像入力・thinking 切替・ストリーム)を
|
|
28
|
+
再実装しなくて済む。
|
|
29
|
+
- 依存は公式 `openai` SDK のみ。
|
|
30
|
+
|
|
31
|
+
## インストール
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
uv add local-llm-client
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 使い方
|
|
38
|
+
|
|
39
|
+
ゲートウェイ(`local-llm-server`)を起動しておき、公開ポートに繋ぐだけ。
|
|
40
|
+
|
|
41
|
+
```python
|
|
42
|
+
from local_llm_client import LLMClient
|
|
43
|
+
|
|
44
|
+
llm = LLMClient(
|
|
45
|
+
model="mlx-community/Qwen3.6-27B-4bit",
|
|
46
|
+
base_url="http://127.0.0.1:8799/v1",
|
|
47
|
+
)
|
|
48
|
+
print(llm.respond("ローカル LLM の利点を3つ。"))
|
|
49
|
+
|
|
50
|
+
# 画像入力・ストリーム
|
|
51
|
+
print(llm.respond("これは何?", images=["photo.jpg"]))
|
|
52
|
+
for piece in llm.respond("長い説明を", stream=True):
|
|
53
|
+
print(piece, end="", flush=True)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
起動確認付きのワンライナー(未起動なら親切なエラー。サーバーは起動しない):
|
|
57
|
+
|
|
58
|
+
```python
|
|
59
|
+
from local_llm_client import connect, ServerNotRunningError
|
|
60
|
+
|
|
61
|
+
try:
|
|
62
|
+
llm = connect(model="mlx-community/Qwen3.6-27B-4bit",
|
|
63
|
+
base_url="http://127.0.0.1:8799/v1")
|
|
64
|
+
except ServerNotRunningError:
|
|
65
|
+
print("先にゲートウェイ(local-llm-server)を起動してください")
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
高度な操作(embeddings / tool calling / 構造化出力 / async など)は、土台の openai クライアントに
|
|
69
|
+
`llm.openai` で直接アクセスできる。素の `openai` SDK で `base_url` を指してもよい。
|
|
70
|
+
|
|
71
|
+
## ライセンス
|
|
72
|
+
|
|
73
|
+
Apache-2.0
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
pyproject.toml
|
|
4
|
+
local_llm_client/__init__.py
|
|
5
|
+
local_llm_client/client.py
|
|
6
|
+
local_llm_client.egg-info/PKG-INFO
|
|
7
|
+
local_llm_client.egg-info/SOURCES.txt
|
|
8
|
+
local_llm_client.egg-info/dependency_links.txt
|
|
9
|
+
local_llm_client.egg-info/requires.txt
|
|
10
|
+
local_llm_client.egg-info/top_level.txt
|
|
11
|
+
tests/test_client.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
openai>=1.0
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
local_llm_client
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "local-llm-client"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "ローカル LLM ゲートウェイ(local-llm-server / OpenAI 互換)に繋ぐ高レベルクライアント。エージェント共通のクライアントで、openai のボイラープレートを再実装しない。"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.11"
|
|
7
|
+
license = "Apache-2.0"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
authors = [{ name = "ToPo-ToPo-ToPo" }]
|
|
10
|
+
keywords = ["llm", "openai", "client", "local-llm", "gateway", "agent"]
|
|
11
|
+
classifiers = [
|
|
12
|
+
"Development Status :: 3 - Alpha",
|
|
13
|
+
"Intended Audience :: Developers",
|
|
14
|
+
"Programming Language :: Python :: 3",
|
|
15
|
+
"Programming Language :: Python :: 3.11",
|
|
16
|
+
"Programming Language :: Python :: 3.12",
|
|
17
|
+
"Topic :: Software Development",
|
|
18
|
+
"Operating System :: OS Independent",
|
|
19
|
+
]
|
|
20
|
+
# 公式 openai SDK を土台にする(自動リトライ・型付き応答・ツール呼び出し/構造化出力)。
|
|
21
|
+
# サーバー(ゲートウェイ)は別パッケージ local-llm-server。本パッケージは接続のみ。
|
|
22
|
+
dependencies = [
|
|
23
|
+
"openai>=1.0",
|
|
24
|
+
]
|
|
25
|
+
|
|
26
|
+
[dependency-groups]
|
|
27
|
+
dev = [
|
|
28
|
+
"pytest>=8.0",
|
|
29
|
+
]
|
|
30
|
+
|
|
31
|
+
[project.urls]
|
|
32
|
+
Repository = "https://github.com/ToPo-ToPo-ToPo/local-llm-client"
|
|
33
|
+
|
|
34
|
+
[build-system]
|
|
35
|
+
requires = ["setuptools>=77"]
|
|
36
|
+
build-backend = "setuptools.build_meta"
|
|
37
|
+
|
|
38
|
+
[tool.setuptools.packages.find]
|
|
39
|
+
include = ["local_llm_client*"]
|
|
40
|
+
|
|
41
|
+
[tool.pytest.ini_options]
|
|
42
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"""LLMClient / connect / 整形ヘルパのテスト(openai をモック、実サーバーには繋がない)。"""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import base64
|
|
5
|
+
|
|
6
|
+
import pytest
|
|
7
|
+
|
|
8
|
+
from local_llm_client import ServerNotRunningError
|
|
9
|
+
from local_llm_client import client as client_mod
|
|
10
|
+
from local_llm_client.client import (
|
|
11
|
+
LLMClient,
|
|
12
|
+
build_user_content,
|
|
13
|
+
connect,
|
|
14
|
+
thinking_extra_body,
|
|
15
|
+
to_image_url,
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
# --- マルチモーダル content 構築 -------------------------------------------
|
|
20
|
+
def test_build_user_content_text_only():
|
|
21
|
+
assert build_user_content("hello") == "hello"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_build_user_content_with_image_passthrough_url():
|
|
25
|
+
content = build_user_content("見て", images=["https://example.com/a.png"])
|
|
26
|
+
assert content[0] == {"type": "text", "text": "見て"}
|
|
27
|
+
assert content[1]["image_url"]["url"] == "https://example.com/a.png"
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def test_to_image_url_local_file_becomes_data_uri(tmp_path):
|
|
31
|
+
p = tmp_path / "pix.png"
|
|
32
|
+
p.write_bytes(b"\x89PNG\r\n")
|
|
33
|
+
url = to_image_url(str(p))
|
|
34
|
+
assert url.startswith("data:image/png;base64,")
|
|
35
|
+
assert base64.b64decode(url.split(",", 1)[1]) == b"\x89PNG\r\n"
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def test_to_image_url_missing_file_raises():
|
|
39
|
+
with pytest.raises(FileNotFoundError):
|
|
40
|
+
to_image_url("/no/such/file.png")
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
# --- respond(openai クライアントをフェイクに差し替え) --------------------
|
|
44
|
+
class _FakeMessage:
|
|
45
|
+
def __init__(self, content):
|
|
46
|
+
self.content = content
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class _FakeChoice:
|
|
50
|
+
def __init__(self, content):
|
|
51
|
+
self.message = _FakeMessage(content)
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
class _FakeResp:
|
|
55
|
+
def __init__(self, content):
|
|
56
|
+
self.choices = [_FakeChoice(content)]
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
class _FakeDelta:
|
|
60
|
+
def __init__(self, content):
|
|
61
|
+
self.content = content
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
class _FakeStreamChoice:
|
|
65
|
+
def __init__(self, content):
|
|
66
|
+
self.delta = _FakeDelta(content)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
class _FakeStreamChunk:
|
|
70
|
+
def __init__(self, content):
|
|
71
|
+
self.choices = [_FakeStreamChoice(content)]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class _FakeCompletions:
|
|
75
|
+
def __init__(self):
|
|
76
|
+
self.calls = []
|
|
77
|
+
|
|
78
|
+
def create(self, **kwargs):
|
|
79
|
+
self.calls.append(kwargs)
|
|
80
|
+
if kwargs.get("stream"):
|
|
81
|
+
return iter([_FakeStreamChunk("こん"), _FakeStreamChunk("にちは")])
|
|
82
|
+
return _FakeResp("done")
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
class _FakeChat:
|
|
86
|
+
def __init__(self):
|
|
87
|
+
self.completions = _FakeCompletions()
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class _FakeOpenAI:
|
|
91
|
+
def __init__(self, *a, **k):
|
|
92
|
+
self.init_kwargs = k
|
|
93
|
+
self.chat = _FakeChat()
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
@pytest.fixture
|
|
97
|
+
def fake_openai(monkeypatch):
|
|
98
|
+
monkeypatch.setattr(client_mod, "OpenAI", _FakeOpenAI)
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_respond_non_stream_returns_text(fake_openai):
|
|
102
|
+
llm = LLMClient(model="m")
|
|
103
|
+
assert llm.respond("hi") == "done"
|
|
104
|
+
sent = llm.openai.chat.completions.calls[0]
|
|
105
|
+
assert sent["messages"][-1] == {"role": "user", "content": "hi"}
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_respond_includes_system_prompt(fake_openai):
|
|
109
|
+
llm = LLMClient(model="m")
|
|
110
|
+
llm.respond("hi", system_prompt="be brief")
|
|
111
|
+
msgs = llm.openai.chat.completions.calls[0]["messages"]
|
|
112
|
+
assert msgs[0] == {"role": "system", "content": "be brief"}
|
|
113
|
+
|
|
114
|
+
|
|
115
|
+
def test_respond_stream_yields_pieces(fake_openai):
|
|
116
|
+
llm = LLMClient(model="m")
|
|
117
|
+
assert list(llm.respond("hi", stream=True)) == ["こん", "にちは"]
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_respond_passes_images(fake_openai):
|
|
121
|
+
llm = LLMClient(model="m")
|
|
122
|
+
llm.respond("見て", images=["https://example.com/a.png"])
|
|
123
|
+
content = llm.openai.chat.completions.calls[0]["messages"][-1]["content"]
|
|
124
|
+
assert isinstance(content, list) and content[1]["type"] == "image_url"
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
def test_max_tokens_forwarded(fake_openai):
|
|
128
|
+
LLMClient(model="m", max_tokens=128).respond("hi")
|
|
129
|
+
assert LLMClient(model="m", max_tokens=128).max_tokens == 128
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def test_openai_client_accessible(fake_openai):
|
|
133
|
+
# 土台の openai クライアントに直接アクセスできる(高度操作用)。
|
|
134
|
+
llm = LLMClient(model="m", base_url="http://127.0.0.1:8080/v1")
|
|
135
|
+
assert llm.openai.init_kwargs["base_url"] == "http://127.0.0.1:8080/v1"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_timeout_passed_to_openai(fake_openai):
|
|
139
|
+
# timeout を渡したときだけ openai クライアントへ伝える(None なら既定に任せる)。
|
|
140
|
+
assert "timeout" not in LLMClient(model="m").openai.init_kwargs
|
|
141
|
+
assert LLMClient(model="m", timeout=42.0).openai.init_kwargs["timeout"] == 42.0
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
# --- connect(起動中ゲートウェイに繋ぐだけ。自動起動しない) ----------------
|
|
145
|
+
def test_connect_returns_client_when_gateway_ready(fake_openai, monkeypatch):
|
|
146
|
+
monkeypatch.setattr(client_mod, "is_ready", lambda url, *a, **k: True)
|
|
147
|
+
llm = connect(model="m", base_url="http://127.0.0.1:8799/v1")
|
|
148
|
+
assert isinstance(llm, LLMClient)
|
|
149
|
+
assert llm.base_url == "http://127.0.0.1:8799/v1"
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def test_connect_raises_when_gateway_down(monkeypatch):
|
|
153
|
+
# 未起動なら自前で立てず、親切なエラーを投げる。
|
|
154
|
+
monkeypatch.setattr(client_mod, "is_ready", lambda url, *a, **k: False)
|
|
155
|
+
with pytest.raises(ServerNotRunningError):
|
|
156
|
+
connect(model="m", base_url="http://127.0.0.1:8799/v1")
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# --- thinking_extra_body(バックエンド protocol ヘルパ) --------------------
|
|
160
|
+
def test_thinking_extra_body_emits_both_forms():
|
|
161
|
+
on = thinking_extra_body(True)
|
|
162
|
+
assert on["enable_thinking"] is True # mlx-vlm 形式
|
|
163
|
+
assert on["chat_template_kwargs"]["enable_thinking"] is True # mlx_lm/llama 形式
|
|
164
|
+
off = thinking_extra_body(False)
|
|
165
|
+
assert off["enable_thinking"] is False
|
|
166
|
+
assert off["chat_template_kwargs"]["enable_thinking"] is False
|