llm-microsoft-foundry 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.
- llm_microsoft_foundry-0.1.0/LICENSE +201 -0
- llm_microsoft_foundry-0.1.0/PKG-INFO +150 -0
- llm_microsoft_foundry-0.1.0/README.md +133 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/PKG-INFO +150 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/SOURCES.txt +10 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/dependency_links.txt +1 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/entry_points.txt +2 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/requires.txt +5 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.egg-info/top_level.txt +1 -0
- llm_microsoft_foundry-0.1.0/llm_microsoft_foundry.py +389 -0
- llm_microsoft_foundry-0.1.0/pyproject.toml +28 -0
- llm_microsoft_foundry-0.1.0/setup.cfg +4 -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,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llm-microsoft-foundry
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LLM plugin for Microsoft Foundry models, authenticated via Azure CLI
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Project-URL: Homepage, https://github.com/stefanstranger/llm-microsoft-foundry
|
|
7
|
+
Project-URL: Issues, https://github.com/stefanstranger/llm-microsoft-foundry/issues
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: llm>=0.19
|
|
12
|
+
Requires-Dist: httpx>=0.27
|
|
13
|
+
Requires-Dist: azure-identity>=1.16
|
|
14
|
+
Requires-Dist: pydantic>=2
|
|
15
|
+
Requires-Dist: pyyaml>=6
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# llm-microsoft-foundry
|
|
19
|
+
|
|
20
|
+
LLM plugin for [Microsoft Foundry](https://learn.microsoft.com/azure/ai-foundry/) models.
|
|
21
|
+
|
|
22
|
+
Authenticates transparently using `az login` (no API key needed) and lets you
|
|
23
|
+
talk to any model deployed in a Foundry resource directly from the
|
|
24
|
+
[llm](https://llm.datasette.io/) CLI.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```powershell
|
|
29
|
+
llm install llm-microsoft-foundry
|
|
30
|
+
# or, from a local clone:
|
|
31
|
+
llm install -e .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Dependencies (`azure-identity`, `httpx`) are installed automatically.
|
|
35
|
+
|
|
36
|
+
## Authenticate
|
|
37
|
+
|
|
38
|
+
```powershell
|
|
39
|
+
az login
|
|
40
|
+
az account set --subscription "<subscription-id-or-name>"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The plugin uses `DefaultAzureCredential`, so any credential that works with the
|
|
44
|
+
Azure SDK (az cli cache, managed identity, env vars) works here too.
|
|
45
|
+
|
|
46
|
+
## Configure a model
|
|
47
|
+
|
|
48
|
+
```powershell
|
|
49
|
+
llm foundry add-model \
|
|
50
|
+
--model-id foundry-gpt-5.4 \
|
|
51
|
+
--api-base https://<your-resource>.services.ai.azure.com \
|
|
52
|
+
--deployment gpt-5.4 \
|
|
53
|
+
--tools \
|
|
54
|
+
--alias gpt54
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Option | Description |
|
|
58
|
+
|--------|-------------|
|
|
59
|
+
| `--model-id` | Unique local identifier (used with `llm -m`) |
|
|
60
|
+
| `--api-base` | Foundry resource root URL (the **resource root**, not a project URL) |
|
|
61
|
+
| `--deployment` | Exact deployment name in Foundry (defaults to `--model-id`) |
|
|
62
|
+
| `--tools` / `--no-tools` | Enable tool calling support |
|
|
63
|
+
| `--vision` / `--no-vision` | Enable image attachment support |
|
|
64
|
+
| `--alias` | Short alias(es), repeatable |
|
|
65
|
+
|
|
66
|
+
Settings are stored in `<llm user dir>/foundry_models.yaml` — same folder as llm's own config files (`C:\Users\<you>\AppData\Roaming\io.datasette.llm\` on Windows). You can edit it directly:
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
models:
|
|
70
|
+
- model_id: foundry-gpt-5.4
|
|
71
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
72
|
+
deployment: gpt-5.4
|
|
73
|
+
supports_tools: true
|
|
74
|
+
supports_vision: false
|
|
75
|
+
|
|
76
|
+
- model_id: foundry-deepseek-v3.2
|
|
77
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
78
|
+
deployment: DeepSeek-V3.2
|
|
79
|
+
supports_tools: false
|
|
80
|
+
supports_vision: false
|
|
81
|
+
aliases:
|
|
82
|
+
- deepseek
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
> **Migrating from an older version?** If you have a `foundry_config.json`, it is migrated to `foundry_models.yaml` automatically on the next `llm` invocation.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
## Use
|
|
89
|
+
|
|
90
|
+
```powershell
|
|
91
|
+
# List configured models
|
|
92
|
+
llm foundry list-models
|
|
93
|
+
|
|
94
|
+
# Prompt a model
|
|
95
|
+
llm -m foundry-gpt-5.4 "What is the capital of France?"
|
|
96
|
+
|
|
97
|
+
# Or using an alias
|
|
98
|
+
llm -m gpt54 "Explain quantum entanglement in one sentence"
|
|
99
|
+
|
|
100
|
+
# Set as default model
|
|
101
|
+
llm models default foundry-gpt-5.4
|
|
102
|
+
|
|
103
|
+
# Check which Azure identity is active
|
|
104
|
+
llm foundry whoami
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Options
|
|
108
|
+
|
|
109
|
+
```powershell
|
|
110
|
+
llm -m foundry-gpt-5.4 -o temperature 0.2 -o max_tokens 512 "Write a haiku"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Option | Default | Description |
|
|
114
|
+
|--------|---------|-------------|
|
|
115
|
+
| `temperature` | model default | Sampling temperature (0–2) |
|
|
116
|
+
| `max_tokens` | model default | Max output tokens |
|
|
117
|
+
| `top_p` | model default | Nucleus sampling probability |
|
|
118
|
+
|
|
119
|
+
## Remove a model
|
|
120
|
+
|
|
121
|
+
```powershell
|
|
122
|
+
llm foundry remove-model foundry-gpt-5.4
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Required Azure role
|
|
126
|
+
|
|
127
|
+
The identity must have the **Cognitive Services User** role on the Foundry
|
|
128
|
+
resource (Owner/Contributor alone does not grant inference access).
|
|
129
|
+
|
|
130
|
+
## How it works
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
llm CLI
|
|
134
|
+
└─ llm-microsoft-foundry plugin
|
|
135
|
+
├─ DefaultAzureCredential (azure-identity) ← uses az login cache
|
|
136
|
+
│ └─ Entra token for https://ai.azure.com/.default
|
|
137
|
+
└─ POST <api_base>/openai/deployments/<deployment>/chat/completions?api-version=v1
|
|
138
|
+
Authorization: Bearer <token>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
No proxy, no extra service, no master key — direct to Foundry.
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
```powershell
|
|
146
|
+
git clone https://github.com/stefanstranger/llm-microsoft-foundry
|
|
147
|
+
cd llm-microsoft-foundry
|
|
148
|
+
llm install -e .
|
|
149
|
+
```
|
|
150
|
+
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
# llm-microsoft-foundry
|
|
2
|
+
|
|
3
|
+
LLM plugin for [Microsoft Foundry](https://learn.microsoft.com/azure/ai-foundry/) models.
|
|
4
|
+
|
|
5
|
+
Authenticates transparently using `az login` (no API key needed) and lets you
|
|
6
|
+
talk to any model deployed in a Foundry resource directly from the
|
|
7
|
+
[llm](https://llm.datasette.io/) CLI.
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
```powershell
|
|
12
|
+
llm install llm-microsoft-foundry
|
|
13
|
+
# or, from a local clone:
|
|
14
|
+
llm install -e .
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Dependencies (`azure-identity`, `httpx`) are installed automatically.
|
|
18
|
+
|
|
19
|
+
## Authenticate
|
|
20
|
+
|
|
21
|
+
```powershell
|
|
22
|
+
az login
|
|
23
|
+
az account set --subscription "<subscription-id-or-name>"
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
The plugin uses `DefaultAzureCredential`, so any credential that works with the
|
|
27
|
+
Azure SDK (az cli cache, managed identity, env vars) works here too.
|
|
28
|
+
|
|
29
|
+
## Configure a model
|
|
30
|
+
|
|
31
|
+
```powershell
|
|
32
|
+
llm foundry add-model \
|
|
33
|
+
--model-id foundry-gpt-5.4 \
|
|
34
|
+
--api-base https://<your-resource>.services.ai.azure.com \
|
|
35
|
+
--deployment gpt-5.4 \
|
|
36
|
+
--tools \
|
|
37
|
+
--alias gpt54
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
| Option | Description |
|
|
41
|
+
|--------|-------------|
|
|
42
|
+
| `--model-id` | Unique local identifier (used with `llm -m`) |
|
|
43
|
+
| `--api-base` | Foundry resource root URL (the **resource root**, not a project URL) |
|
|
44
|
+
| `--deployment` | Exact deployment name in Foundry (defaults to `--model-id`) |
|
|
45
|
+
| `--tools` / `--no-tools` | Enable tool calling support |
|
|
46
|
+
| `--vision` / `--no-vision` | Enable image attachment support |
|
|
47
|
+
| `--alias` | Short alias(es), repeatable |
|
|
48
|
+
|
|
49
|
+
Settings are stored in `<llm user dir>/foundry_models.yaml` — same folder as llm's own config files (`C:\Users\<you>\AppData\Roaming\io.datasette.llm\` on Windows). You can edit it directly:
|
|
50
|
+
|
|
51
|
+
```yaml
|
|
52
|
+
models:
|
|
53
|
+
- model_id: foundry-gpt-5.4
|
|
54
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
55
|
+
deployment: gpt-5.4
|
|
56
|
+
supports_tools: true
|
|
57
|
+
supports_vision: false
|
|
58
|
+
|
|
59
|
+
- model_id: foundry-deepseek-v3.2
|
|
60
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
61
|
+
deployment: DeepSeek-V3.2
|
|
62
|
+
supports_tools: false
|
|
63
|
+
supports_vision: false
|
|
64
|
+
aliases:
|
|
65
|
+
- deepseek
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
> **Migrating from an older version?** If you have a `foundry_config.json`, it is migrated to `foundry_models.yaml` automatically on the next `llm` invocation.
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
## Use
|
|
72
|
+
|
|
73
|
+
```powershell
|
|
74
|
+
# List configured models
|
|
75
|
+
llm foundry list-models
|
|
76
|
+
|
|
77
|
+
# Prompt a model
|
|
78
|
+
llm -m foundry-gpt-5.4 "What is the capital of France?"
|
|
79
|
+
|
|
80
|
+
# Or using an alias
|
|
81
|
+
llm -m gpt54 "Explain quantum entanglement in one sentence"
|
|
82
|
+
|
|
83
|
+
# Set as default model
|
|
84
|
+
llm models default foundry-gpt-5.4
|
|
85
|
+
|
|
86
|
+
# Check which Azure identity is active
|
|
87
|
+
llm foundry whoami
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Options
|
|
91
|
+
|
|
92
|
+
```powershell
|
|
93
|
+
llm -m foundry-gpt-5.4 -o temperature 0.2 -o max_tokens 512 "Write a haiku"
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
| Option | Default | Description |
|
|
97
|
+
|--------|---------|-------------|
|
|
98
|
+
| `temperature` | model default | Sampling temperature (0–2) |
|
|
99
|
+
| `max_tokens` | model default | Max output tokens |
|
|
100
|
+
| `top_p` | model default | Nucleus sampling probability |
|
|
101
|
+
|
|
102
|
+
## Remove a model
|
|
103
|
+
|
|
104
|
+
```powershell
|
|
105
|
+
llm foundry remove-model foundry-gpt-5.4
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## Required Azure role
|
|
109
|
+
|
|
110
|
+
The identity must have the **Cognitive Services User** role on the Foundry
|
|
111
|
+
resource (Owner/Contributor alone does not grant inference access).
|
|
112
|
+
|
|
113
|
+
## How it works
|
|
114
|
+
|
|
115
|
+
```
|
|
116
|
+
llm CLI
|
|
117
|
+
└─ llm-microsoft-foundry plugin
|
|
118
|
+
├─ DefaultAzureCredential (azure-identity) ← uses az login cache
|
|
119
|
+
│ └─ Entra token for https://ai.azure.com/.default
|
|
120
|
+
└─ POST <api_base>/openai/deployments/<deployment>/chat/completions?api-version=v1
|
|
121
|
+
Authorization: Bearer <token>
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
No proxy, no extra service, no master key — direct to Foundry.
|
|
125
|
+
|
|
126
|
+
## Development
|
|
127
|
+
|
|
128
|
+
```powershell
|
|
129
|
+
git clone https://github.com/stefanstranger/llm-microsoft-foundry
|
|
130
|
+
cd llm-microsoft-foundry
|
|
131
|
+
llm install -e .
|
|
132
|
+
```
|
|
133
|
+
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: llm-microsoft-foundry
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: LLM plugin for Microsoft Foundry models, authenticated via Azure CLI
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Project-URL: Homepage, https://github.com/stefanstranger/llm-microsoft-foundry
|
|
7
|
+
Project-URL: Issues, https://github.com/stefanstranger/llm-microsoft-foundry/issues
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Description-Content-Type: text/markdown
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Requires-Dist: llm>=0.19
|
|
12
|
+
Requires-Dist: httpx>=0.27
|
|
13
|
+
Requires-Dist: azure-identity>=1.16
|
|
14
|
+
Requires-Dist: pydantic>=2
|
|
15
|
+
Requires-Dist: pyyaml>=6
|
|
16
|
+
Dynamic: license-file
|
|
17
|
+
|
|
18
|
+
# llm-microsoft-foundry
|
|
19
|
+
|
|
20
|
+
LLM plugin for [Microsoft Foundry](https://learn.microsoft.com/azure/ai-foundry/) models.
|
|
21
|
+
|
|
22
|
+
Authenticates transparently using `az login` (no API key needed) and lets you
|
|
23
|
+
talk to any model deployed in a Foundry resource directly from the
|
|
24
|
+
[llm](https://llm.datasette.io/) CLI.
|
|
25
|
+
|
|
26
|
+
## Install
|
|
27
|
+
|
|
28
|
+
```powershell
|
|
29
|
+
llm install llm-microsoft-foundry
|
|
30
|
+
# or, from a local clone:
|
|
31
|
+
llm install -e .
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
Dependencies (`azure-identity`, `httpx`) are installed automatically.
|
|
35
|
+
|
|
36
|
+
## Authenticate
|
|
37
|
+
|
|
38
|
+
```powershell
|
|
39
|
+
az login
|
|
40
|
+
az account set --subscription "<subscription-id-or-name>"
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The plugin uses `DefaultAzureCredential`, so any credential that works with the
|
|
44
|
+
Azure SDK (az cli cache, managed identity, env vars) works here too.
|
|
45
|
+
|
|
46
|
+
## Configure a model
|
|
47
|
+
|
|
48
|
+
```powershell
|
|
49
|
+
llm foundry add-model \
|
|
50
|
+
--model-id foundry-gpt-5.4 \
|
|
51
|
+
--api-base https://<your-resource>.services.ai.azure.com \
|
|
52
|
+
--deployment gpt-5.4 \
|
|
53
|
+
--tools \
|
|
54
|
+
--alias gpt54
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
| Option | Description |
|
|
58
|
+
|--------|-------------|
|
|
59
|
+
| `--model-id` | Unique local identifier (used with `llm -m`) |
|
|
60
|
+
| `--api-base` | Foundry resource root URL (the **resource root**, not a project URL) |
|
|
61
|
+
| `--deployment` | Exact deployment name in Foundry (defaults to `--model-id`) |
|
|
62
|
+
| `--tools` / `--no-tools` | Enable tool calling support |
|
|
63
|
+
| `--vision` / `--no-vision` | Enable image attachment support |
|
|
64
|
+
| `--alias` | Short alias(es), repeatable |
|
|
65
|
+
|
|
66
|
+
Settings are stored in `<llm user dir>/foundry_models.yaml` — same folder as llm's own config files (`C:\Users\<you>\AppData\Roaming\io.datasette.llm\` on Windows). You can edit it directly:
|
|
67
|
+
|
|
68
|
+
```yaml
|
|
69
|
+
models:
|
|
70
|
+
- model_id: foundry-gpt-5.4
|
|
71
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
72
|
+
deployment: gpt-5.4
|
|
73
|
+
supports_tools: true
|
|
74
|
+
supports_vision: false
|
|
75
|
+
|
|
76
|
+
- model_id: foundry-deepseek-v3.2
|
|
77
|
+
api_base: https://my-resource.services.ai.azure.com
|
|
78
|
+
deployment: DeepSeek-V3.2
|
|
79
|
+
supports_tools: false
|
|
80
|
+
supports_vision: false
|
|
81
|
+
aliases:
|
|
82
|
+
- deepseek
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
> **Migrating from an older version?** If you have a `foundry_config.json`, it is migrated to `foundry_models.yaml` automatically on the next `llm` invocation.
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
## Use
|
|
89
|
+
|
|
90
|
+
```powershell
|
|
91
|
+
# List configured models
|
|
92
|
+
llm foundry list-models
|
|
93
|
+
|
|
94
|
+
# Prompt a model
|
|
95
|
+
llm -m foundry-gpt-5.4 "What is the capital of France?"
|
|
96
|
+
|
|
97
|
+
# Or using an alias
|
|
98
|
+
llm -m gpt54 "Explain quantum entanglement in one sentence"
|
|
99
|
+
|
|
100
|
+
# Set as default model
|
|
101
|
+
llm models default foundry-gpt-5.4
|
|
102
|
+
|
|
103
|
+
# Check which Azure identity is active
|
|
104
|
+
llm foundry whoami
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
### Options
|
|
108
|
+
|
|
109
|
+
```powershell
|
|
110
|
+
llm -m foundry-gpt-5.4 -o temperature 0.2 -o max_tokens 512 "Write a haiku"
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
| Option | Default | Description |
|
|
114
|
+
|--------|---------|-------------|
|
|
115
|
+
| `temperature` | model default | Sampling temperature (0–2) |
|
|
116
|
+
| `max_tokens` | model default | Max output tokens |
|
|
117
|
+
| `top_p` | model default | Nucleus sampling probability |
|
|
118
|
+
|
|
119
|
+
## Remove a model
|
|
120
|
+
|
|
121
|
+
```powershell
|
|
122
|
+
llm foundry remove-model foundry-gpt-5.4
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Required Azure role
|
|
126
|
+
|
|
127
|
+
The identity must have the **Cognitive Services User** role on the Foundry
|
|
128
|
+
resource (Owner/Contributor alone does not grant inference access).
|
|
129
|
+
|
|
130
|
+
## How it works
|
|
131
|
+
|
|
132
|
+
```
|
|
133
|
+
llm CLI
|
|
134
|
+
└─ llm-microsoft-foundry plugin
|
|
135
|
+
├─ DefaultAzureCredential (azure-identity) ← uses az login cache
|
|
136
|
+
│ └─ Entra token for https://ai.azure.com/.default
|
|
137
|
+
└─ POST <api_base>/openai/deployments/<deployment>/chat/completions?api-version=v1
|
|
138
|
+
Authorization: Bearer <token>
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
No proxy, no extra service, no master key — direct to Foundry.
|
|
142
|
+
|
|
143
|
+
## Development
|
|
144
|
+
|
|
145
|
+
```powershell
|
|
146
|
+
git clone https://github.com/stefanstranger/llm-microsoft-foundry
|
|
147
|
+
cd llm-microsoft-foundry
|
|
148
|
+
llm install -e .
|
|
149
|
+
```
|
|
150
|
+
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
README.md
|
|
3
|
+
llm_microsoft_foundry.py
|
|
4
|
+
pyproject.toml
|
|
5
|
+
llm_microsoft_foundry.egg-info/PKG-INFO
|
|
6
|
+
llm_microsoft_foundry.egg-info/SOURCES.txt
|
|
7
|
+
llm_microsoft_foundry.egg-info/dependency_links.txt
|
|
8
|
+
llm_microsoft_foundry.egg-info/entry_points.txt
|
|
9
|
+
llm_microsoft_foundry.egg-info/requires.txt
|
|
10
|
+
llm_microsoft_foundry.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
llm_microsoft_foundry
|
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
"""
|
|
2
|
+
llm-microsoft-foundry — LLM plugin for Microsoft Foundry models.
|
|
3
|
+
|
|
4
|
+
Authentication is handled transparently via azure-identity
|
|
5
|
+
(DefaultAzureCredential), which picks up an `az login` session.
|
|
6
|
+
No API key is required; bearer tokens are fetched and cached automatically.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import json
|
|
11
|
+
import time
|
|
12
|
+
from typing import Iterator, Optional
|
|
13
|
+
|
|
14
|
+
import click
|
|
15
|
+
import httpx
|
|
16
|
+
import llm
|
|
17
|
+
import yaml
|
|
18
|
+
from pydantic import Field
|
|
19
|
+
|
|
20
|
+
# ---------------------------------------------------------------------------
|
|
21
|
+
# Token helpers
|
|
22
|
+
# ---------------------------------------------------------------------------
|
|
23
|
+
|
|
24
|
+
_FOUNDRY_SCOPE = "https://ai.azure.com/.default"
|
|
25
|
+
_FOUNDRY_API_VERSION = "v1"
|
|
26
|
+
|
|
27
|
+
_token_cache: dict = {} # {"token": str, "expires_on": float}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def _get_access_token() -> str:
|
|
31
|
+
"""Return a cached (or freshly fetched) Entra access token."""
|
|
32
|
+
now = time.time()
|
|
33
|
+
if _token_cache.get("token") and _token_cache.get("expires_on", 0) > now + 60:
|
|
34
|
+
return _token_cache["token"]
|
|
35
|
+
|
|
36
|
+
try:
|
|
37
|
+
from azure.identity import DefaultAzureCredential
|
|
38
|
+
except ImportError as exc:
|
|
39
|
+
raise click.ClickException(
|
|
40
|
+
"azure-identity is required. Install it with: pip install azure-identity"
|
|
41
|
+
) from exc
|
|
42
|
+
|
|
43
|
+
credential = DefaultAzureCredential()
|
|
44
|
+
token = credential.get_token(_FOUNDRY_SCOPE)
|
|
45
|
+
_token_cache["token"] = token.token
|
|
46
|
+
_token_cache["expires_on"] = token.expires_on
|
|
47
|
+
return token.token
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# ---------------------------------------------------------------------------
|
|
51
|
+
# Plugin registration
|
|
52
|
+
# ---------------------------------------------------------------------------
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
@llm.hookimpl
|
|
56
|
+
def register_models(register):
|
|
57
|
+
config = _load_config()
|
|
58
|
+
for entry in config.get("models", []):
|
|
59
|
+
model = FoundryModel(
|
|
60
|
+
model_id=entry["model_id"],
|
|
61
|
+
deployment=entry.get("deployment", entry["model_id"]),
|
|
62
|
+
api_base=entry["api_base"],
|
|
63
|
+
supports_tools=entry.get("supports_tools", False),
|
|
64
|
+
supports_vision=entry.get("supports_vision", False),
|
|
65
|
+
)
|
|
66
|
+
aliases = entry.get("aliases", [])
|
|
67
|
+
register(model, aliases=aliases)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@llm.hookimpl
|
|
71
|
+
def register_commands(cli):
|
|
72
|
+
@cli.group(name="foundry")
|
|
73
|
+
def foundry_group():
|
|
74
|
+
"Commands for the llm-microsoft-foundry plugin"
|
|
75
|
+
|
|
76
|
+
@foundry_group.command(name="list-models")
|
|
77
|
+
def list_models():
|
|
78
|
+
"List configured Microsoft Foundry models"
|
|
79
|
+
config = _load_config()
|
|
80
|
+
models = config.get("models", [])
|
|
81
|
+
if not models:
|
|
82
|
+
click.echo(
|
|
83
|
+
"No models configured. Run 'llm foundry add-model' or edit "
|
|
84
|
+
+ str(_config_path())
|
|
85
|
+
)
|
|
86
|
+
return
|
|
87
|
+
for m in models:
|
|
88
|
+
click.echo(
|
|
89
|
+
f"{m['model_id']!r} -> {m['api_base']} "
|
|
90
|
+
f"(deployment: {m.get('deployment', m['model_id'])})"
|
|
91
|
+
)
|
|
92
|
+
|
|
93
|
+
@foundry_group.command(name="add-model")
|
|
94
|
+
@click.option("--model-id", required=True, help="Local model identifier (e.g. foundry-gpt-5.4)")
|
|
95
|
+
@click.option(
|
|
96
|
+
"--api-base",
|
|
97
|
+
required=True,
|
|
98
|
+
help="Foundry resource root URL, e.g. https://my-resource.services.ai.azure.com",
|
|
99
|
+
)
|
|
100
|
+
@click.option("--deployment", default=None, help="Exact deployment name (defaults to model-id)")
|
|
101
|
+
@click.option("--tools/--no-tools", default=False, help="Whether the model supports tool calling")
|
|
102
|
+
@click.option("--vision/--no-vision", default=False, help="Whether the model supports image input")
|
|
103
|
+
@click.option("--alias", "aliases", multiple=True, help="Short alias(es) for the model")
|
|
104
|
+
def add_model(model_id, api_base, deployment, tools, vision, aliases):
|
|
105
|
+
"Add a Microsoft Foundry model to the configuration"
|
|
106
|
+
config = _load_config()
|
|
107
|
+
models = config.setdefault("models", [])
|
|
108
|
+
# Replace existing entry with same model_id
|
|
109
|
+
models = [m for m in models if m["model_id"] != model_id]
|
|
110
|
+
entry: dict = {
|
|
111
|
+
"model_id": model_id,
|
|
112
|
+
"api_base": api_base.rstrip("/"),
|
|
113
|
+
"deployment": deployment or model_id,
|
|
114
|
+
"supports_tools": tools,
|
|
115
|
+
"supports_vision": vision,
|
|
116
|
+
}
|
|
117
|
+
if aliases:
|
|
118
|
+
entry["aliases"] = list(aliases)
|
|
119
|
+
models.append(entry)
|
|
120
|
+
config["models"] = models
|
|
121
|
+
_save_config(config)
|
|
122
|
+
click.echo(f"Added model {model_id!r}. Restart llm or reload models to use it.")
|
|
123
|
+
|
|
124
|
+
@foundry_group.command(name="remove-model")
|
|
125
|
+
@click.argument("model_id")
|
|
126
|
+
def remove_model(model_id):
|
|
127
|
+
"Remove a Microsoft Foundry model from the configuration"
|
|
128
|
+
config = _load_config()
|
|
129
|
+
before = len(config.get("models", []))
|
|
130
|
+
config["models"] = [m for m in config.get("models", []) if m["model_id"] != model_id]
|
|
131
|
+
_save_config(config)
|
|
132
|
+
removed = before - len(config["models"])
|
|
133
|
+
click.echo(f"Removed {removed} model(s).")
|
|
134
|
+
|
|
135
|
+
@foundry_group.command(name="whoami")
|
|
136
|
+
def whoami():
|
|
137
|
+
"Show the current Azure identity used for Foundry authentication"
|
|
138
|
+
try:
|
|
139
|
+
token = _get_access_token()
|
|
140
|
+
# Decode the JWT claims (informational only, no verification)
|
|
141
|
+
import base64
|
|
142
|
+
|
|
143
|
+
parts = token.split(".")
|
|
144
|
+
if len(parts) >= 2:
|
|
145
|
+
padding = "=" * (4 - len(parts[1]) % 4)
|
|
146
|
+
claims = json.loads(base64.urlsafe_b64decode(parts[1] + padding))
|
|
147
|
+
upn = (
|
|
148
|
+
claims.get("upn")
|
|
149
|
+
or claims.get("unique_name")
|
|
150
|
+
or claims.get("oid", "unknown")
|
|
151
|
+
)
|
|
152
|
+
tenant = claims.get("tid", "unknown")
|
|
153
|
+
click.echo(f"Authenticated as: {upn}")
|
|
154
|
+
click.echo(f"Tenant: {tenant}")
|
|
155
|
+
else:
|
|
156
|
+
click.echo("Token obtained but could not decode claims.")
|
|
157
|
+
except Exception as exc:
|
|
158
|
+
raise click.ClickException(f"Authentication failed: {exc}") from exc
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
# ---------------------------------------------------------------------------
|
|
162
|
+
# Config helpers
|
|
163
|
+
# ---------------------------------------------------------------------------
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _config_path():
|
|
167
|
+
return llm.user_dir() / "foundry_models.yaml"
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _legacy_json_path():
|
|
171
|
+
return llm.user_dir() / "foundry_config.json"
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def _load_config() -> dict:
|
|
175
|
+
path = _config_path()
|
|
176
|
+
legacy = _legacy_json_path()
|
|
177
|
+
|
|
178
|
+
# Migrate old JSON config to YAML on first load
|
|
179
|
+
if not path.exists() and legacy.exists():
|
|
180
|
+
old = json.loads(legacy.read_text())
|
|
181
|
+
_save_config(old)
|
|
182
|
+
legacy.unlink()
|
|
183
|
+
return old
|
|
184
|
+
|
|
185
|
+
if path.exists():
|
|
186
|
+
return yaml.safe_load(path.read_text()) or {"models": []}
|
|
187
|
+
|
|
188
|
+
return {"models": []}
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _save_config(config: dict):
|
|
192
|
+
path = _config_path()
|
|
193
|
+
path.write_text(yaml.dump(config, default_flow_style=False, allow_unicode=True))
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
# ---------------------------------------------------------------------------
|
|
197
|
+
# Model implementation
|
|
198
|
+
# ---------------------------------------------------------------------------
|
|
199
|
+
|
|
200
|
+
|
|
201
|
+
class FoundryModel(llm.Model):
|
|
202
|
+
can_stream = True
|
|
203
|
+
|
|
204
|
+
class Options(llm.Options):
|
|
205
|
+
temperature: Optional[float] = Field(
|
|
206
|
+
description="Sampling temperature (0–2). Higher = more random.",
|
|
207
|
+
ge=0,
|
|
208
|
+
le=2,
|
|
209
|
+
default=None,
|
|
210
|
+
)
|
|
211
|
+
max_tokens: Optional[int] = Field(
|
|
212
|
+
description="Maximum number of tokens to generate.",
|
|
213
|
+
ge=1,
|
|
214
|
+
default=None,
|
|
215
|
+
)
|
|
216
|
+
top_p: Optional[float] = Field(
|
|
217
|
+
description="Nucleus sampling probability mass.",
|
|
218
|
+
ge=0,
|
|
219
|
+
le=1,
|
|
220
|
+
default=None,
|
|
221
|
+
)
|
|
222
|
+
|
|
223
|
+
def __init__(
|
|
224
|
+
self,
|
|
225
|
+
model_id: str,
|
|
226
|
+
deployment: str,
|
|
227
|
+
api_base: str,
|
|
228
|
+
supports_tools: bool = False,
|
|
229
|
+
supports_vision: bool = False,
|
|
230
|
+
):
|
|
231
|
+
self.model_id = model_id
|
|
232
|
+
self.deployment = deployment
|
|
233
|
+
self.api_base = api_base.rstrip("/")
|
|
234
|
+
self.supports_tools = supports_tools
|
|
235
|
+
if supports_vision:
|
|
236
|
+
self.attachment_types = {
|
|
237
|
+
"image/jpeg",
|
|
238
|
+
"image/png",
|
|
239
|
+
"image/gif",
|
|
240
|
+
"image/webp",
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
def _chat_url(self) -> str:
|
|
244
|
+
return f"{self.api_base}/openai/v1/chat/completions"
|
|
245
|
+
|
|
246
|
+
def _build_messages(self, prompt, conversation) -> list[dict]:
|
|
247
|
+
messages: list[dict] = []
|
|
248
|
+
|
|
249
|
+
if conversation:
|
|
250
|
+
for prev in conversation.responses:
|
|
251
|
+
if prev.prompt.system and not any(
|
|
252
|
+
m.get("role") == "system" for m in messages
|
|
253
|
+
):
|
|
254
|
+
messages.append({"role": "system", "content": prev.prompt.system})
|
|
255
|
+
if prev.prompt.prompt:
|
|
256
|
+
messages.append(
|
|
257
|
+
{
|
|
258
|
+
"role": "user",
|
|
259
|
+
"content": _msg_content(
|
|
260
|
+
prev.prompt.prompt, prev.prompt.attachments
|
|
261
|
+
),
|
|
262
|
+
}
|
|
263
|
+
)
|
|
264
|
+
messages.append({"role": "assistant", "content": prev.text()})
|
|
265
|
+
|
|
266
|
+
if prompt.system and not any(m.get("role") == "system" for m in messages):
|
|
267
|
+
messages.append({"role": "system", "content": prompt.system})
|
|
268
|
+
|
|
269
|
+
if prompt.prompt:
|
|
270
|
+
messages.append(
|
|
271
|
+
{
|
|
272
|
+
"role": "user",
|
|
273
|
+
"content": _msg_content(prompt.prompt, prompt.attachments),
|
|
274
|
+
}
|
|
275
|
+
)
|
|
276
|
+
|
|
277
|
+
return messages
|
|
278
|
+
|
|
279
|
+
def _build_body(self, prompt, conversation) -> dict:
|
|
280
|
+
body: dict = {
|
|
281
|
+
"model": self.deployment,
|
|
282
|
+
"messages": self._build_messages(prompt, conversation),
|
|
283
|
+
"stream": True,
|
|
284
|
+
}
|
|
285
|
+
opts = prompt.options
|
|
286
|
+
if opts.temperature is not None:
|
|
287
|
+
body["temperature"] = opts.temperature
|
|
288
|
+
if opts.max_tokens is not None:
|
|
289
|
+
body["max_tokens"] = opts.max_tokens
|
|
290
|
+
if opts.top_p is not None:
|
|
291
|
+
body["top_p"] = opts.top_p
|
|
292
|
+
return body
|
|
293
|
+
|
|
294
|
+
def execute(self, prompt, stream, response, conversation) -> Iterator[str]:
|
|
295
|
+
token = _get_access_token()
|
|
296
|
+
headers = {
|
|
297
|
+
"Authorization": f"Bearer {token}",
|
|
298
|
+
"Content-Type": "application/json",
|
|
299
|
+
}
|
|
300
|
+
body = self._build_body(prompt, conversation)
|
|
301
|
+
|
|
302
|
+
if stream:
|
|
303
|
+
yield from self._stream(headers, body, response)
|
|
304
|
+
else:
|
|
305
|
+
body["stream"] = False
|
|
306
|
+
with httpx.Client() as client:
|
|
307
|
+
r = client.post(
|
|
308
|
+
self._chat_url(), headers=headers, json=body, timeout=120
|
|
309
|
+
)
|
|
310
|
+
_raise_for_status(r)
|
|
311
|
+
data = r.json()
|
|
312
|
+
choice = data["choices"][0]
|
|
313
|
+
text = choice["message"]["content"] or ""
|
|
314
|
+
response.response_json = data
|
|
315
|
+
yield text
|
|
316
|
+
|
|
317
|
+
def _stream(self, headers: dict, body: dict, response) -> Iterator[str]:
|
|
318
|
+
chunks: list = []
|
|
319
|
+
with httpx.Client() as client:
|
|
320
|
+
with client.stream(
|
|
321
|
+
"POST",
|
|
322
|
+
self._chat_url(),
|
|
323
|
+
headers=headers,
|
|
324
|
+
json=body,
|
|
325
|
+
timeout=120,
|
|
326
|
+
) as r:
|
|
327
|
+
if r.status_code >= 400:
|
|
328
|
+
r.read() # must consume body before accessing it in stream context
|
|
329
|
+
_raise_for_status(r)
|
|
330
|
+
for line in r.iter_lines():
|
|
331
|
+
if not line.startswith("data: "):
|
|
332
|
+
continue
|
|
333
|
+
payload = line[len("data: "):]
|
|
334
|
+
if payload.strip() == "[DONE]":
|
|
335
|
+
break
|
|
336
|
+
try:
|
|
337
|
+
chunk = json.loads(payload)
|
|
338
|
+
except json.JSONDecodeError:
|
|
339
|
+
continue
|
|
340
|
+
chunks.append(chunk)
|
|
341
|
+
choices = chunk.get("choices")
|
|
342
|
+
if not choices:
|
|
343
|
+
continue
|
|
344
|
+
delta = choices[0].get("delta", {})
|
|
345
|
+
text = delta.get("content") or ""
|
|
346
|
+
if text:
|
|
347
|
+
yield text
|
|
348
|
+
if chunks:
|
|
349
|
+
response.response_json = chunks[-1]
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
# ---------------------------------------------------------------------------
|
|
353
|
+
# Utilities
|
|
354
|
+
# ---------------------------------------------------------------------------
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def _msg_content(text: str, attachments=None):
|
|
358
|
+
"""Build message content; wraps text + image attachments into a list."""
|
|
359
|
+
if not attachments:
|
|
360
|
+
return text
|
|
361
|
+
content = [{"type": "text", "text": text or ""}]
|
|
362
|
+
for att in attachments:
|
|
363
|
+
content.append(
|
|
364
|
+
{
|
|
365
|
+
"type": "image_url",
|
|
366
|
+
"image_url": {
|
|
367
|
+
"url": att.url
|
|
368
|
+
or f"data:{att.resolve_type()};base64,{att.base64_content()}"
|
|
369
|
+
},
|
|
370
|
+
}
|
|
371
|
+
)
|
|
372
|
+
return content
|
|
373
|
+
|
|
374
|
+
|
|
375
|
+
def _raise_for_status(r: httpx.Response):
|
|
376
|
+
if r.status_code >= 400:
|
|
377
|
+
try:
|
|
378
|
+
# Streaming responses need read() before accessing body
|
|
379
|
+
if not r.is_stream_consumed:
|
|
380
|
+
r.read()
|
|
381
|
+
detail = r.json()
|
|
382
|
+
except Exception:
|
|
383
|
+
try:
|
|
384
|
+
detail = r.text
|
|
385
|
+
except Exception:
|
|
386
|
+
detail = f"HTTP {r.status_code}"
|
|
387
|
+
raise click.ClickException(
|
|
388
|
+
f"Foundry API error {r.status_code}: {json.dumps(detail, indent=2)}"
|
|
389
|
+
)
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=68"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "llm-microsoft-foundry"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "LLM plugin for Microsoft Foundry models, authenticated via Azure CLI"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "Apache-2.0" }
|
|
11
|
+
requires-python = ">=3.10"
|
|
12
|
+
dependencies = [
|
|
13
|
+
"llm>=0.19",
|
|
14
|
+
"httpx>=0.27",
|
|
15
|
+
"azure-identity>=1.16",
|
|
16
|
+
"pydantic>=2",
|
|
17
|
+
"pyyaml>=6",
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[project.entry-points."llm"]
|
|
21
|
+
microsoft_foundry = "llm_microsoft_foundry"
|
|
22
|
+
|
|
23
|
+
[project.urls]
|
|
24
|
+
Homepage = "https://github.com/stefanstranger/llm-microsoft-foundry"
|
|
25
|
+
Issues = "https://github.com/stefanstranger/llm-microsoft-foundry/issues"
|
|
26
|
+
|
|
27
|
+
[tool.setuptools]
|
|
28
|
+
py-modules = ["llm_microsoft_foundry"]
|