Repository navigation
Expand file tree
/
Copy pathexample-foundation-models.ts
More file actions
259 lines (215 loc) · 9.03 KB
/
Copy pathexample-foundation-models.ts
File metadata and controls
259 lines (215 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#!/usr/bin/env node
/**
* SAP AI Provider - Foundation Models API Example
*
* This example demonstrates direct Azure OpenAI access through the Foundation
* Models API, including logprobs, seed, and stop-sequence request parameters.
* Orchestration can also forward model-specific parameters; support depends on the model.
*
* When to use Foundation Models API:
* - You want to configure Azure OpenAI-specific request parameters
* - You want best-effort reproducibility with seed (not guaranteed determinism)
* - You need custom stop sequences
* - You want direct model access without orchestration features
*
* When to use Orchestration API (default):
* - You need data masking/anonymization
* - You need content filtering
* - You need document grounding
* - You need prompt templating
*
* Authentication:
* - On SAP BTP: Automatically uses service binding (VCAP_SERVICES)
* - Locally: Set AICORE_SERVICE_KEY environment variable with your service key JSON
*/
// Load environment variables
import "dotenv/config";
import { APICallError, LoadAPIKeyError, NoSuchModelError } from "@ai-sdk/provider";
import { embed, generateText, streamText } from "ai";
// NOTE: In production, use: import { createSAPAIProvider, SAP_AI_PROVIDER_NAME, UnsupportedFeatureError } from "@jerome-benoit/sap-ai-provider/v4";
import {
createSAPAIProvider,
SAP_AI_PROVIDER_NAME,
UnsupportedFeatureError,
} from "../src/index-v4";
import { parseSAPErrorResponseBody } from "./parse-sap-error-response-body.js";
/**
* Demonstrates Foundation Models API features
*/
async function foundationModelsExample() {
console.log("🔧 SAP AI Foundation Models API Example\n");
// Verify AICORE_SERVICE_KEY is set for local development
if (!process.env.AICORE_SERVICE_KEY && !process.env.VCAP_SERVICES) {
console.warn("⚠️ Warning: AICORE_SERVICE_KEY environment variable not set.");
console.warn(" Set it in your .env file or environment for local development.\n");
}
try {
console.log("📌 1. Provider-level API Selection\n");
// Create a provider that defaults to Foundation Models API
const fmProvider = createSAPAIProvider({
api: "foundation-models",
});
// All models from this provider use Foundation Models API by default
const result1 = await generateText({
model: fmProvider("gpt-4.1"),
prompt: "What is 2 + 2? Reply with just the number.",
});
console.log(" 📄 Response:", result1.text);
console.log(
" 📊 Usage:",
`${String(result1.usage.inputTokens)} input + ${String(result1.usage.outputTokens)} output`,
);
console.log("\n📌 2. Model-level API Selection\n");
// Create a default provider (uses Orchestration by default)
const provider = createSAPAIProvider();
// Override to Foundation Models for specific model
const fmModel = provider("gpt-4.1", { api: "foundation-models" });
const result2 = await generateText({
model: fmModel,
prompt: "What is the capital of France? Reply in one word.",
});
console.log(" 📄 Response:", result2.text);
console.log("\n3. Configuring Logprobs\n");
const result3 = await generateText({
model: provider("gpt-4.1", {
api: "foundation-models",
modelParams: {
logprobs: true,
maxTokens: 5,
top_logprobs: 3, // Get top 3 alternative tokens
},
}),
prompt: "The quick brown",
});
console.log(" 📄 Generated:", result3.text);
// The request enables logprobs, but this provider does not expose them in
// providerMetadata or response.body. Use the SAP SDK directly to inspect them.
console.log(" Logprobs requested; token probabilities are not exposed by this provider.");
console.log("\n4. Seed - Best-Effort Reproducibility\n");
const seedValue = 12345;
// Generate twice with same seed
const deterministicModel = provider("gpt-4.1", {
api: "foundation-models",
modelParams: {
max_tokens: 10,
seed: seedValue,
temperature: 0.7, // Seed requests repeatable sampling; identical output is not guaranteed.
},
});
const result4a = await generateText({
model: deterministicModel,
prompt: "Generate a random 4-digit number.",
});
const result4b = await generateText({
model: deterministicModel,
prompt: "Generate a random 4-digit number.",
});
console.log(` 📄 First call (seed=${String(seedValue)}):`, result4a.text);
console.log(` 📄 Second call (seed=${String(seedValue)}):`, result4b.text);
console.log(" 🔍 Same output?", result4a.text === result4b.text ? "Yes ✅" : "No ❌");
console.log("\n📌 5. Stop Sequences\n");
const result5 = await generateText({
model: provider("gpt-4.1", {
api: "foundation-models",
modelParams: {
max_tokens: 50,
stop: [".", "!"], // Stop at first period or exclamation
},
}),
prompt: "Write a sentence about the ocean",
});
console.log(" 📄 Response (stopped at '.' or '!'):", result5.text);
console.log("\n📌 6. Streaming with Foundation Models\n");
let streamError: unknown;
const stream = streamText({
model: provider("gpt-4.1", {
api: "foundation-models",
modelParams: { max_tokens: 50 },
}),
onError: ({ error }) => {
streamError = error;
},
prompt: "Count from 1 to 5, one number per line.",
});
process.stdout.write(" 📡 Streaming: ");
for await (const chunk of stream.textStream) {
process.stdout.write(chunk);
}
// textStream omits error events; let the common handler report the original error.
if (streamError !== undefined) {
throw streamError instanceof Error
? streamError
: new Error("Streaming failed", { cause: streamError });
}
console.log("\n");
console.log("📌 7. Embeddings with Foundation Models\n");
const { embedding } = await embed({
model: fmProvider.embedding("text-embedding-3-small"),
value: "Hello, world!",
});
console.log(` 📊 Embedding dimensions: ${String(embedding.length)}`);
console.log(
` 📄 First 5 values: [${embedding
.slice(0, 5)
.map((v) => v.toFixed(6))
.join(", ")}...]`,
);
console.log("\n📌 8. Call-level API Override\n");
// Even with a provider defaulting to Orchestration,
// you can override at the call level
const orchProvider = createSAPAIProvider({ api: "orchestration" });
const result8 = await generateText({
model: orchProvider("gpt-4.1"),
prompt: "Say hello",
providerOptions: {
[SAP_AI_PROVIDER_NAME]: {
api: "foundation-models", // Override for this call only
modelParams: {
max_tokens: 10,
seed: 42,
},
},
},
});
console.log(" 📄 Response:", result8.text);
console.log(" 📌 (Used Foundation Models API despite provider default)");
console.log("\n📌 9. Feature Validation\n");
// Note: UnsupportedFeatureError is thrown at runtime when you try
// to use Orchestration-only features with Foundation Models API
// (e.g., masking, filtering, grounding)
console.log(" 🔧 Foundation Models API supports: logprobs, seed, stop, logit_bias, user");
console.log(" 🔧 Orchestration API supports: masking, filtering, grounding, templating");
console.log(" ⚠️ Using incompatible features throws UnsupportedFeatureError");
console.log("\n✅ All Foundation Models examples completed!");
} catch (error: unknown) {
process.exitCode = 1;
if (error instanceof LoadAPIKeyError) {
console.error("❌ Authentication Error:", error.name);
} else if (error instanceof NoSuchModelError) {
console.error("❌ Model Not Found:", error.modelId);
} else if (error instanceof UnsupportedFeatureError) {
console.error("❌ Unsupported Feature:", error.message);
console.error(" 💡 Use API:", error.suggestedApi);
} else if (error instanceof APICallError) {
console.error("❌ API Call Error:", error.statusCode, error.name);
// Parse SAP-specific metadata
const sapError = parseSAPErrorResponseBody(error.responseBody);
if (sapError?.error.request_id) {
console.error(" SAP Request ID:", sapError.error.request_id);
}
} else {
const errorName = error instanceof Error ? error.name : "Unknown error";
console.error("❌ Example failed:", errorName);
}
console.error("\n💡 Troubleshooting tips:");
console.error(" - Ensure AICORE_SERVICE_KEY is set with valid credentials");
console.error(" - Check that your SAP AI Core instance is accessible");
console.error(" - Verify the model is available in your deployment");
console.error(" - Foundation Models API requires direct model deployments");
}
}
foundationModelsExample().catch((error: unknown) => {
process.exitCode = 1;
console.error("Example failed:", error instanceof Error ? error.name : "Unknown error");
});
export { foundationModelsExample };