Open History to see the last commit
1/**
2 * Unified Chat API Example
3 *
4 * Demonstrates the new unified chat() method with three modes:
5 * - "promise" (default): Returns Promise<ChatCompletionResult>
6 * - "stream": Returns AsyncIterable<StreamChunk>
7 * - "response": Returns Response with SSE headers
8 */
10import { AI } from "@ts-poc/ai";
11import { OpenAIAdapter } from "@ts-poc/ai-openai";
12import { OllamaAdapter } from "@ts-poc/ai-ollama";
14// Initialize AI with multiple adapters
15const ai = new AI({
16 adapters: {
17 openai: new OpenAIAdapter({
18 apiKey: process.env.OPENAI_API_KEY || "",
19 }),
20 ollama: new OllamaAdapter({
21 baseURL: "http://localhost:11434",
22 }),
23 },
24});
26// 1. PROMISE MODE (DEFAULT) - Standard non-streaming chat
27async function promiseMode() {
28 console.log("\n=== Promise Mode ===\n");
30 const result = await ai.chat({
31 adapter: "openai",
32 model: "gpt-4",
33 messages: [
34 { role: "system", content: "You are a helpful assistant." },
35 { role: "user", content: "What is TypeScript in one sentence?" },
36 ],
37 temperature: 0.7,
38 // as: "promise" is the default, so we can omit it
39 });
41 console.log("Response:", result.content);
42 console.log(`Tokens used: ${result.usage.totalTokens}`);
43}
45// 2. STREAM MODE - Manual streaming with custom handling
46async function streamMode() {
47 console.log("\n=== Stream Mode ===\n");
49 const stream = ai.chat({
50 adapter: "openai",
51 model: "gpt-4",
52 messages: [
53 { role: "user", content: "Count from 1 to 5 slowly." },
54 ],
55 as: "stream", // ← Returns AsyncIterable<StreamChunk>
56 });
58 process.stdout.write("Streaming: ");
60 for await (const chunk of stream) {
61 if (chunk.type === "content") {
62 process.stdout.write(chunk.delta);
63 } else if (chunk.type === "done") {
64 console.log(`\n\nFinish reason: ${chunk.finishReason}`);
65 console.log(`Tokens used: ${chunk.usage?.totalTokens}`);
66 }
67 }
68}
70// 3. RESPONSE MODE - HTTP Response with SSE headers
71async function responseMode() {
72 console.log("\n=== Response Mode ===\n");
74 // This is what you'd return from an API endpoint
75 const response = ai.chat({
76 adapter: "openai",
77 model: "gpt-4",
78 messages: [
79 { role: "user", content: "Say hello!" },
80 ],
81 as: "response", // ← Returns Response object
82 });
84 console.log("Response type:", response.constructor.name);
85 console.log("Headers:");
86 console.log(" Content-Type:", response.headers.get("Content-Type"));
87 console.log(" Cache-Control:", response.headers.get("Cache-Control"));
88 console.log(" Connection:", response.headers.get("Connection"));
90 // Read the stream
91 const reader = response.body!.getReader();
92 const decoder = new TextDecoder();
94 console.log("\nStreaming content:");
95 while (true) {
96 const { done, value } = await reader.read();
97 if (done) break;
99 const text = decoder.decode(value);
100 const lines = text.split("\n\n");
102 for (const line of lines) {
103 if (line.startsWith("data: ")) {
104 const data = line.slice(6);
105 if (data === "[DONE]") {
106 console.log("\n[Stream ended]");
107 continue;
108 }
110 try {
111 const chunk = JSON.parse(data);
112 if (chunk.type === "content") {
113 process.stdout.write(chunk.delta);
114 }
115 } catch (e) {
116 // Skip invalid JSON
117 }
118 }
119 }
120 }
121}
123// 4. PROMISE MODE WITH FALLBACKS
124async function promiseWithFallbacks() {
125 console.log("\n=== Promise Mode with Fallbacks ===\n");
127 try {
128 const result = await ai.chat({
129 adapter: "openai",
130 model: "gpt-4",
131 messages: [
132 { role: "user", content: "What's 2+2?" },
133 ],
134 as: "promise", // or omit - it's the default
135 fallbacks: [
136 { adapter: "ollama", model: "llama2" },
137 ],
138 });
140 console.log("Response:", result.content);
141 } catch (error: any) {
142 console.error("All adapters failed:", error.message);
143 }
144}
146// 5. STREAM MODE WITH FALLBACKS
147async function streamWithFallbacks() {
148 console.log("\n=== Stream Mode with Fallbacks ===\n");
150 const stream = ai.chat({
151 adapter: "openai",
152 model: "gpt-4",
153 messages: [
154 { role: "user", content: "Tell me a short joke." },
155 ],
156 as: "stream",
157 fallbacks: [
158 { adapter: "ollama", model: "llama2" },
159 ],
160 });
162 process.stdout.write("Response: ");
164 for await (const chunk of stream) {
165 if (chunk.type === "content") {
166 process.stdout.write(chunk.delta);
167 } else if (chunk.type === "done") {
168 console.log("\n");
169 }
170 }
171}
173// 6. TOOL EXECUTION IN PROMISE MODE
174async function toolExecutionPromise() {
175 console.log("\n=== Tool Execution (Promise Mode) ===\n");
177 const tools = [
178 {
179 type: "function" as const,
180 function: {
181 name: "get_weather",
182 description: "Get current weather for a location",
183 parameters: {
184 type: "object",
185 properties: {
186 location: { type: "string", description: "City name" },
187 },
188 required: ["location"],
189 },
190 },
191 execute: async (args: { location: string }) => {
192 console.log(` → Executing get_weather for: ${args.location}`);
193 return JSON.stringify({
194 location: args.location,
195 temperature: 72,
196 condition: "sunny",
197 });
198 },
199 },
200 ];
202 const result = await ai.chat({
203 adapter: "openai",
204 model: "gpt-4",
205 messages: [
206 { role: "user", content: "What's the weather like in San Francisco?" },
207 ],
208 tools,
209 toolChoice: "auto",
210 maxIterations: 5,
211 as: "promise",
212 });
214 console.log("Final response:", result.content);
215}
217// 7. TOOL EXECUTION IN STREAM MODE
218async function toolExecutionStream() {
219 console.log("\n=== Tool Execution (Stream Mode) ===\n");
221 const tools = [
222 {
223 type: "function" as const,
224 function: {
225 name: "calculate",
226 description: "Perform a mathematical calculation",
227 parameters: {
228 type: "object",
229 properties: {
230 expression: { type: "string", description: "Math expression to evaluate" },
231 },
232 required: ["expression"],
233 },
234 },
235 execute: async (args: { expression: string }) => {
236 console.log(` → Calculating: ${args.expression}`);
237 // In real app, use a safe math evaluator
238 const result = eval(args.expression);
239 return JSON.stringify({ result });
240 },
241 },
242 ];
244 const stream = ai.chat({
245 adapter: "openai",
246 model: "gpt-4",
247 messages: [
248 { role: "user", content: "What's 123 * 456?" },
249 ],
250 tools,
251 toolChoice: "auto",
252 maxIterations: 5,
253 as: "stream",
254 });
256 for await (const chunk of stream) {
257 if (chunk.type === "content") {
258 process.stdout.write(chunk.delta);
259 } else if (chunk.type === "tool_call") {
260 console.log(`\n→ Tool call: ${chunk.toolCall.function.name}`);
261 } else if (chunk.type === "done") {
262 console.log("\n");
263 }
264 }
265}
267// 8. FALLBACK-ONLY MODE (No primary adapter)
268async function fallbackOnlyMode() {
269 console.log("\n=== Fallback-Only Mode ===\n");
271 // Try multiple adapters in order until one succeeds
272 const result = await ai.chat({
273 messages: [
274 { role: "user", content: "What's the capital of France?" },
275 ],
276 fallbacks: [
277 { adapter: "openai", model: "gpt-4" },
278 { adapter: "ollama", model: "llama2" },
279 ],
280 as: "promise",
281 });
283 console.log("Response:", result.content);
284}
286// Run examples
287async function main() {
288 console.log("=".repeat(60));
289 console.log(" UNIFIED CHAT API EXAMPLES");
290 console.log("=".repeat(60));
292 try {
293 await promiseMode();
294 await streamMode();
295 await responseMode();
296 await promiseWithFallbacks();
297 await streamWithFallbacks();
298 await toolExecutionPromise();
299 await toolExecutionStream();
300 await fallbackOnlyMode();
301 } catch (error: any) {
302 console.error("\nError:", error.message);
303 }
305 console.log("\n" + "=".repeat(60));
306 console.log(" ALL EXAMPLES COMPLETED");
307 console.log("=".repeat(60) + "\n");
308}
310main();