Open History to see the last commit
1/**
2 * System Prompts Example
3 *
4 * This example demonstrates the new systemPrompts feature that allows you to:
5 * 1. Define default system prompts in the AI constructor
6 * 2. Override them per chat call
7 * 3. Use multiple system prompts as an array
8 */
10import { AI } from "@tanstack/ai";
11import { OpenAIAdapter } from "@tanstack/ai-openai";
13// Example 1: Constructor-level system prompts
14// ============================================
15// Define default system prompts that will be used for ALL chat calls
17const aiWithDefaultPrompts = new AI({
18 adapters: {
19 openai: new OpenAIAdapter({
20 apiKey: process.env.OPENAI_API_KEY!,
21 }),
22 },
23 // These prompts will be automatically prepended to all chat calls
24 systemPrompts: [
25 "You are a helpful AI assistant.",
26 "Always be concise and friendly in your responses.",
27 ],
28});
30// Usage: No need to manually add system messages!
31async function chatWithDefaults() {
32 const result = await aiWithDefaultPrompts.chat({
33 adapter: "openai",
34 model: "gpt-4",
35 messages: [
36 { role: "user", content: "What is the capital of France?" },
37 ],
38 });
40 console.log("Response:", result.content);
41 // System prompts were automatically prepended before the user message
42}
44// Example 2: Override system prompts per chat call
45// =================================================
46// You can override the default prompts for specific conversations
48async function chatWithCustomPrompts() {
49 const result = await aiWithDefaultPrompts.chat({
50 adapter: "openai",
51 model: "gpt-4",
52 messages: [
53 { role: "user", content: "Explain quantum physics" },
54 ],
55 // Override the default system prompts for this call only
56 systemPrompts: [
57 "You are a physics professor.",
58 "Explain concepts using simple analogies.",
59 ],
60 });
62 console.log("Response:", result.content);
63 // The custom prompts replaced the default ones for this call
64}
66// Example 3: Single system prompt
67// ================================
68// You can also use a single prompt instead of an array
70const aiWithSinglePrompt = new AI({
71 adapters: {
72 openai: new OpenAIAdapter({
73 apiKey: process.env.OPENAI_API_KEY!,
74 }),
75 },
76 systemPrompts: ["You are a coding assistant specialized in TypeScript."],
77});
79// Example 4: No system prompts (optional)
80// ========================================
81// If you don't provide systemPrompts, behavior is unchanged
83const aiWithoutPrompts = new AI({
84 adapters: {
85 openai: new OpenAIAdapter({
86 apiKey: process.env.OPENAI_API_KEY!,
87 }),
88 },
89 // No systemPrompts - you can still manually add system messages if needed
90});
92async function chatWithManualSystemMessage() {
93 const result = await aiWithoutPrompts.chat({
94 adapter: "openai",
95 model: "gpt-4",
96 messages: [
97 { role: "system", content: "You are a helpful assistant." },
98 { role: "user", content: "Hello!" },
99 ],
100 });
102 console.log("Response:", result.content);
103}
105// Example 5: Multiple system prompts for context layering
106// ========================================================
107// Use multiple prompts to layer different instructions
109const aiWithLayeredPrompts = new AI({
110 adapters: {
111 openai: new OpenAIAdapter({
112 apiKey: process.env.OPENAI_API_KEY!,
113 }),
114 },
115 systemPrompts: [
116 "You are a customer support agent for TechCorp.",
117 "Our business hours are 9 AM to 5 PM EST.",
118 "Always ask for the customer's order number before proceeding.",
119 "Be empathetic and patient with frustrated customers.",
120 ],
121});
123// Example 6: Tool-aware system prompts
124// =====================================
125// System prompts work seamlessly with tools
127const tools = {
128 getWeather: {
129 type: "function" as const,
130 function: {
131 name: "getWeather",
132 description: "Get the current weather for a location",
133 parameters: {
134 type: "object",
135 properties: {
136 location: { type: "string", description: "City name" },
137 },
138 required: ["location"],
139 },
140 },
141 execute: async ({ location }: { location: string }) => {
142 return JSON.stringify({ location, temp: 72, condition: "sunny" });
143 },
144 },
145} as const;
147const aiWithToolsAndPrompts = new AI({
148 adapters: {
149 openai: new OpenAIAdapter({
150 apiKey: process.env.OPENAI_API_KEY!,
151 }),
152 },
153 tools,
154 systemPrompts: [
155 "You are a weather assistant.",
156 "When asked about weather, use the getWeather tool.",
157 "Always include the temperature in Fahrenheit and the condition.",
158 ],
159});
161async function chatWithToolsAndPrompts() {
162 const result = await aiWithToolsAndPrompts.chat({
163 adapter: "openai",
164 model: "gpt-4",
165 messages: [
166 { role: "user", content: "What's the weather in New York?" },
167 ],
168 tools: ["getWeather"],
169 });
171 console.log("Response:", result.content);
172 // System prompts guide the AI on how to use the tools
173}
175// Example 7: Streaming with system prompts
176// =========================================
177// System prompts work with streaming too
179async function streamWithPrompts() {
180 const stream = aiWithDefaultPrompts.chat({
181 adapter: "openai",
182 model: "gpt-4",
183 messages: [
184 { role: "user", content: "Tell me a short story" },
185 ],
186 as: "stream" as const,
187 // You can still override prompts in streaming mode
188 systemPrompts: [
189 "You are a creative storyteller.",
190 "Keep stories under 100 words.",
191 ],
192 });
194 for await (const chunk of stream) {
195 if (chunk.type === "content") {
196 process.stdout.write(chunk.delta);
197 }
198 }
199}
201// Example 8: Response mode with system prompts
202// =============================================
203// System prompts work with Response mode for HTTP streaming
205async function responseWithPrompts() {
206 const response = aiWithDefaultPrompts.chat({
207 adapter: "openai",
208 model: "gpt-4",
209 messages: [
210 { role: "user", content: "Hello!" },
211 ],
212 as: "response" as const,
213 });
215 // Return this Response directly from an API endpoint
216 return response;
217}
219// Example 9: Behavior when messages already have system prompts
220// ==============================================================
221// If messages already contain system messages, they are REPLACED
223async function replacingExistingSystemMessages() {
224 const result = await aiWithDefaultPrompts.chat({
225 adapter: "openai",
226 model: "gpt-4",
227 messages: [
228 // This system message will be REPLACED by the default prompts
229 { role: "system", content: "This will be replaced" },
230 { role: "user", content: "Hello!" },
231 ],
232 });
234 console.log("Response:", result.content);
235 // The AI used the default prompts, not the one in messages
236}
238// Example 10: Empty array to skip system prompts
239// ===============================================
240// Pass an empty array to skip system prompts for a specific call
242async function skipSystemPromptsForOneCall() {
243 const result = await aiWithDefaultPrompts.chat({
244 adapter: "openai",
245 model: "gpt-4",
246 messages: [
247 { role: "user", content: "Hello!" },
248 ],
249 systemPrompts: [], // Skip system prompts for this call
250 });
252 console.log("Response:", result.content);
253 // No system prompts were used
254}
256// Run examples
257async function main() {
258 console.log("=== Example 1: Default prompts ===");
259 await chatWithDefaults();
261 console.log("\n=== Example 2: Override prompts ===");
262 await chatWithCustomPrompts();
264 console.log("\n=== Example 6: Tools + prompts ===");
265 await chatWithToolsAndPrompts();
267 console.log("\n=== Example 7: Streaming ===");
268 await streamWithPrompts();
269}
271// Uncomment to run:
272// main().catch(console.error);