This guide walks through the steps to personalize and configure an AI in Node.js, allowing you to adjust the AI’s behavior for various scenarios, like customer support, education, or content generation.
To personalize the AI responses, we’ll create different API routes using Node.js. Each route can use custom system messages, model configurations, and message history to match specific use cases.
Here’s the basic setup for the AI chat route:
app.post('/api/chat', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: 'You are a helpful assistant.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
In this basic route, the system message is "You are a helpful assistant."
Adjusting this can shape the AI’s tone and focus.
The system message establishes the AI's role and personality in each conversation. Modifying it allows the AI to take on specific roles:
"You are a friendly customer support agent."
"You are a patient tutor who explains concepts in simple terms."
"You are a highly knowledgeable software engineer."
Setting up different routes with unique system messages lets the API handle multiple use cases. Below are examples of routes with customized system configurations.
app.post('/api/chat/customer-support', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: 'You are a friendly and empathetic customer service agent.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
app.post('/api/chat/tutor', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: 'You are an educational tutor who explains complex topics simply.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
Including a message history provides context to the AI, allowing it to build on previous conversations and maintain continuity:
const messageHistory = [ { role: "user", content: "Can you help me with my order?" }, { role: "assistant", content: "Of course! What issue are you experiencing?" }, ];
Adding messageHistory
to each request can enhance response relevance.
You can adjust the model for each route to balance accuracy and speed:
gpt-4
for complex or accuracy-focused routes.gpt-3.5-turbo
for faster responses.Here’s how to set up different routes with custom system messages and message histories.
app.post('/api/chat/support', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: 'You are a friendly customer support representative.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
app.post('/api/chat/educational', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-3.5-turbo', messages: [{ role: 'system', content: 'You are a knowledgeable tutor who explains topics clearly.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
app.post('/api/chat/technical', async (req, res) => { try { const { messages } = req.body; const completion = await openai.chat.completions.create({ model: 'gpt-4', messages: [{ role: 'system', content: 'You are a skilled software engineer providing expert advice.' }, ...messages], }); res.json(completion.choices[0].message); } catch (error) { res.status(500).send('Error generating the response.'); } });
With these route-based customizations, you can build a flexible AI API that adapts to different roles and scenarios. Tailor system messages, model choices, and message histories to provide a highly personalized experience for each user.
Subscribe to our Newsletter
To receive news, updates, code challenges, tech trends
© 2024 Codding Nutella. All rights reserved.