Spaces:
Sleeping
Sleeping
Download server/test_nvidia.js from Shivam4096/Carpool: direct link, hf CLI and curl.
- Browser
- Download file 5.33 kB
-
https://huggingface.co/spaces/Shivam4096/Carpool/resolve/main/server/test_nvidia.js
- Command line
-
hf download hf://spaces/Shivam4096/Carpool/server/test_nvidia.js
-
curl -L -o test_nvidia.js https://huggingface.co/spaces/Shivam4096/Carpool/resolve/main/server/test_nvidia.js
5.33 kB
| 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(); | |