You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 04-PracticalSamples/README.md
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,7 +26,7 @@ This chapter showcases **sample projects** that demonstrate generative AI develo
26
26
27
27
### Foundry Local Spring Boot Demo
28
28
29
-
The **[Foundry Local Spring Boot Demo](foundrylocal/README.md)** demonstrates how to integrate with local AI models using the **OpenAI Java SDK**. It showcases connecting to the **Phi-3.5-mini** model running on Foundry Local, allowing you to run AI applications without relying on cloud services.
29
+
The **[Foundry Local Spring Boot Demo](foundrylocal/README.md)** demonstrates how to integrate with local AI models using the **OpenAI Java SDK**. It showcases connecting to models running on Foundry Local (e.g., **Phi-4-mini**), with automatic model detection, allowing you to run AI applications without relying on cloud services.
Copy file name to clipboardExpand all lines: 04-PracticalSamples/foundrylocal/README.md
+79-51Lines changed: 79 additions & 51 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,12 +27,20 @@ Before starting this tutorial, make sure you have:
27
27
28
28
### **Install Foundry Local:**
29
29
30
+
> **Note:** Foundry Local CLI is available on **Windows** and **macOS** only. Linux is supported via the [Foundry Local SDKs](https://github.com/microsoft/Foundry-Local) (Python, JavaScript, C#, Rust).
31
+
30
32
```bash
31
33
# Windows
32
34
winget install Microsoft.FoundryLocal
33
35
34
-
# macOS (after installing)
35
-
foundry model run phi-3.5-mini
36
+
# macOS
37
+
brew tap microsoft/foundrylocal
38
+
brew install foundrylocal
39
+
```
40
+
41
+
Verify the installation:
42
+
```bash
43
+
foundry --version
36
44
```
37
45
38
46
## Project Overview
@@ -52,12 +60,13 @@ This project consists of four main components:
-**base-url**: Specifies where Foundry Local is running, including the `/v1` path for OpenAI API compatibility. **Note**: Foundry Local dynamically assigns a port, so check your actual port using `foundry service status`
60
-
-**model**: Names the AI model to use for text generation, including the version number (e.g., `:1`). Use `foundry model list`to see available models with their exact IDs
68
+
-**base-url**: Specifies where Foundry Local is running, including the `/v1` path for OpenAI API compatibility. The default port is `5273`. If the port differs, check it with `foundry service status`.
69
+
-**model** (optional): Names the AI model to use for text generation. **By default, the application auto-detects the model** by querying the Foundry Local `/v1/models` endpoint at startup, so you don't need to set this. You can still set it explicitly to override auto-detection if needed.
61
70
62
71
**Key concept:** Spring Boot automatically loads these properties and makes them available to your application using the `@Value` annotation.
63
72
@@ -117,19 +126,24 @@ public class FoundryLocalService {
- `@Service` tells Spring this classprovides business logic
126
135
- `@Value` injects configuration values from application.properties
127
-
- The `:default-value` syntax provides fallback values if properties aren't set
136
+
- The model defaults to empty, which triggers **auto-detection** from Foundry Local at startup. This means the app works with any model loaded in Foundry Local without manual configuration.
128
137
129
138
#### Client Initialization:
130
139
```java
131
140
@PostConstruct
132
141
public void init() {
142
+
// Auto-detect the model from Foundry Local if not explicitly configured
143
+
if (model == null || model.isBlank()) {
144
+
model = detectModel();
145
+
}
146
+
133
147
this.openAIClient = OpenAIOkHttpClient.builder()
134
148
.baseUrl(baseUrl) // Base URL already includes /v1 from configuration
135
149
.apiKey("not-needed") // Local server doesn't need real API key
@@ -139,6 +153,7 @@ public void init() {
139
153
140
154
**Whatthis does:**
141
155
- `@PostConstruct` runs this method after Spring creates the service
156
+
-If no model is configured, it queries FoundryLocal's `/v1/models` endpoint and picks the first loaded model
142
157
- Creates an OpenAI client that points to your local Foundry Local instance
143
158
- The base URL from `application.properties` already includes `/v1` for OpenAI API compatibility
144
159
- API key is set to "not-needed" because local development doesn't require authentication
@@ -216,52 +231,70 @@ Here's the complete flow when you run the application:
216
231
217
232
1.**Startup**:SpringBoot starts and reads `application.properties`
218
233
2.**ServiceCreation**:Spring creates `FoundryLocalService` and injects configuration values
219
-
3.**ClientSetup**: `@PostConstruct` initializes the OpenAI client to connect to FoundryLocal
220
-
4.**DemoExecution**: `CommandLineRunner` executes after startup
221
-
5.**AICall**:The demo calls `foundryLocalService.chat()` with a test message
222
-
6.**APIRequest**:Service builds and sends OpenAI-compatible request to FoundryLocal
223
-
7.**ResponseProcessing**:Service extracts and returns the AI's response
224
-
8. **Display**: Application prints the response and exits
234
+
3.**ModelDetection**:If no model is configured, the service queries FoundryLocal's `/v1/models` endpoint and uses the first available model automatically
235
+
4. **Client Setup**: `@PostConstruct` initializes the OpenAI client to connect to Foundry Local
236
+
5. **Demo Execution**: `CommandLineRunner` executes after startup
237
+
6. **AI Call**: The demo calls `foundryLocalService.chat()` with a test message
238
+
7. **API Request**: Service builds and sends OpenAI-compatible request to Foundry Local
239
+
8. **Response Processing**: Service extracts and returns the AI's response
240
+
9.**Display**:Application prints the response and exits
225
241
226
242
## SettingUpFoundryLocal
227
243
228
-
To set up Foundry Local, follow these steps:
229
-
230
244
1.**InstallFoundryLocal** using the instructions in the [Prerequisites](#prerequisites) section.
231
245
232
-
2. **Check the dynamically assigned port**. Foundry Local automatically assigns a port when it starts. Find your port with:
246
+
2.**Start the service** (if not already running):
247
+
```bash
248
+
foundry service start
249
+
```
250
+
251
+
3.**Check the service status** to confirm it is running and note the port:
233
252
```bash
234
253
foundry service status
235
254
```
236
-
237
-
**Optional**: If you prefer to use a specific port (e.g., 5273), you can configure it manually:
255
+
256
+
4.**Download and run a model** (downloads on first run, cached for subsequent runs):
238
257
```bash
239
-
foundry service set --port 5273
258
+
foundry model run phi-4-mini
240
259
```
260
+
This opens an interactive chat session. You can exit with `Ctrl+C`.The model stays loaded in the service.
261
+
262
+
>**Tip:**Run `foundry model list` to see all available models. Replace `phi-4-mini` with any alias from the catalog (e.g., `qwen2.5-0.5b` for a smaller/faster model).
241
263
242
-
3. **Download the AI model** you want to use, for example, `phi-3.5-mini`, with the following command:
264
+
5.**Verify the model is loaded:**
243
265
```bash
244
-
foundry model run phi-3.5-mini
266
+
foundry service ps
245
267
```
246
268
247
-
4. **Configure the application.properties** file to match your Foundry Local settings:
248
-
- Update the port in `base-url` (from step 2), ensuring it includes `/v1` at the end
249
-
- Update the model name to include the version number (check with `foundry model list`)
250
-
251
-
Example:
269
+
6.**Update `application.properties`**if needed:
270
+
-Thedefault `base-url` (`http://localhost:5273/v1`) matches the default CLI port. Update only if `foundry service status` shows a different port.
271
+
-The model is **auto-detected** at startup — no configuration needed.
0 commit comments