1 import { MeshAPI } from "meshapi-node-sdk"; 2 3 const client = new MeshAPI({ 4 baseUrl: "https://api.yourdomain.com", 5 token: "rsk_01JXXXXXXXXXXXXXXXXXXXXXXXXX", 6 }); 7 8 const response = await client.chat.completions.create({ 9 model: "openai/gpt-4o-mini", 10 messages: [ 11 { role: "system", content: "You are a helpful assistant." }, 12 { role: "user", content: "What is the capital of France?" }, 13 ], 14 temperature: 0.7, 15 max_tokens: 256, 16 }); 17 18 console.log(response.choices[0]?.message.content); 19 // → "The capital of France is Paris." 20 21 console.log(`Tokens used: ${response.usage?.total_tokens}`);
1 const stream = client.chat.completions.create({ 2 model: "openai/gpt-4o-mini", 3 messages: [{ role: "user", content: "Write a haiku about TypeScript." }], 4 stream: true, 5 }); 6 7 let fullText = ""; 8 9 for await (const chunk of stream) { 10 const delta = chunk.choices[0]?.delta.content ?? ""; 11 process.stdout.write(delta); 12 fullText += delta; 13 } 14 15 console.log("\n--- Full response:", fullText);
1 const stream = client.chat.completions.create({ 2 model: "openai/gpt-4o", 3 messages: [{ role: "user", content: "What's the weather in Paris?" }], 4 tools: [ 5 { 6 type: "function", 7 function: { 8 name: "get_weather", 9 description: "Get current weather for a city", 10 parameters: { 11 type: "object", 12 properties: { 13 city: { type: "string" }, 14 }, 15 required: ["city"], 16 }, 17 }, 18 }, 19 ], 20 tool_choice: "auto", 21 stream: true, 22 }); 23 24 for await (const chunk of stream) { 25 // handle delta content or tool_calls 26 console.log(JSON.stringify(chunk.choices[0]?.delta)); 27 }
1 const controller = new AbortController(); 2 3 // Cancel after 5 seconds 4 setTimeout(() => controller.abort(), 5_000); 5 6 const stream = client.chat.completions.create( 7 { model: "openai/gpt-4o-mini", messages: [...], stream: true }, 8 { signal: controller.signal }, 9 ); 10 11 try { 12 for await (const chunk of stream) { 13 process.stdout.write(chunk.choices[0]?.delta.content ?? ""); 14 } 15 } catch (err) { 16 if ((err as Error).name === "AbortError") { 17 console.log("Stream cancelled."); 18 } 19 }