Spaces:
Sleeping
Sleeping
File size: 5,332 Bytes
d381a4c | 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 | require('dotenv').config();
const OpenAI = require('openai');
const client = new OpenAI({
apiKey: process.env.NVIDIA_API_KEY,
baseURL: 'https://integrate.api.nvidia.com/v1',
});
const model = process.env.NVIDIA_MODEL || 'meta/llama-3.3-70b-instruct';
const TOOLS = [
{
type: 'function',
function: {
name: 'draft_booking',
description: 'Drafts a ride booking when user wants to book a ride.',
parameters: {
type: 'object',
properties: {
origin: { type: 'string', description: 'Pickup location' },
destination: { type: 'string', description: 'Drop-off location' },
time: { type: 'string', description: 'Departure time HH:MM' }
},
required: ['origin', 'destination', 'time']
}
}
}
];
async function runTests() {
console.log('\nββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log('β NVIDIA NIM β Carpool AI Agent Test Suite β');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(` Model : ${model}`);
console.log(` Key : ${process.env.NVIDIA_API_KEY.slice(0, 14)}...`);
console.log('');
// ββ Test 1: Plain text response βββββββββββββββββββββββββββββββββββββββββββββ
console.log('TEST 1 βΆ Plain text (policy question)');
try {
const res1 = await client.chat.completions.create({
model,
messages: [
{ role: 'system', content: 'You are Commute Copilot for Reliance Carpool. Answer briefly.' },
{ role: 'user', content: 'What is the cancellation policy for rides?' }
],
max_tokens: 150,
temperature: 0.5,
});
const text = res1.choices[0].message.content;
console.log(' β
Response received');
console.log(` π¬ "${text.slice(0, 120)}..."`);
console.log(` π Tokens used: ${res1.usage.total_tokens} (prompt: ${res1.usage.prompt_tokens}, completion: ${res1.usage.completion_tokens})`);
} catch (err) {
console.error(' β FAILED:', err.message);
process.exit(1);
}
console.log('');
// ββ Test 2: Tool calling (draft_booking) ββββββββββββββββββββββββββββββββββββ
console.log('TEST 2 βΆ Tool calling β draft_booking');
try {
const res2 = await client.chat.completions.create({
model,
messages: [
{ role: 'system', content: 'You are Commute Copilot for Reliance Carpool. Use tools when the user wants to book a ride.' },
{ role: 'user', content: 'I need a ride from Kharghar to RCP Twin Towers at 9am tomorrow.' }
],
tools: TOOLS,
tool_choice: 'auto',
max_tokens: 256,
temperature: 0.3,
});
const msg = res2.choices[0].message;
if (msg.tool_calls && msg.tool_calls.length > 0) {
const call = msg.tool_calls[0];
const args = JSON.parse(call.function.arguments);
console.log(' β
Tool call triggered correctly');
console.log(` π§ Function: ${call.function.name}`);
console.log(` π Origin : ${args.origin}`);
console.log(` π Destination: ${args.destination}`);
console.log(` π Time : ${args.time}`);
} else {
console.log(' β οΈ No tool call β model responded with text instead:');
console.log(` π¬ "${msg.content}"`);
}
} catch (err) {
console.error(' β FAILED:', err.message);
process.exit(1);
}
console.log('');
// ββ Test 3: Context injection (user stats) ββββββββββββββββββββββββββββββββββ
console.log('TEST 3 βΆ Context injection (user stats query)');
try {
const fakeStats = { rides_taken: 14, avg_cost: 48, co2_saved: 44.1 };
const res3 = await client.chat.completions.create({
model,
messages: [
{ role: 'system', content: 'You are Commute Copilot for Reliance Carpool. Answer briefly using the stats in the System Note.' },
{ role: 'user', content: `[System Note: The current user is a Passenger. Stats: ${JSON.stringify(fakeStats)}]\n\nUser Query: How many rides have I taken and how much CO2 did I save?` }
],
max_tokens: 100,
temperature: 0.5,
});
const text3 = res3.choices[0].message.content;
console.log(' β
Context-aware response received');
console.log(` π¬ "${text3}"`);
} catch (err) {
console.error(' β FAILED:', err.message);
process.exit(1);
}
console.log('');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ');
console.log(' π ALL TESTS PASSED β NVIDIA NIM agent is ready!');
console.log('ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ\n');
}
runTests();
|