Loading...
Ready-to-use examples for common use cases. Copy, paste, and customize.
Build a conversational chatbot with message history
import { RaxAI } from 'rax-ai';
const rax = new RaxAI({ apiKey: process.env.RAX_API_KEY });
const messages = [];
async function chat(userInput) {
messages.push({ role: 'user', content: userInput });
const response = await rax.chat({
model: 'rax-4.0',
messages: messages
});
const reply = response.choices[0].message.content;
messages.push({ role: 'assistant', content: reply });
return reply;
}
// Usage
console.log(await chat('Hello!'));
console.log(await chat('How are you?'));Stream AI responses as they are generated
import { RaxAI } from 'rax-ai';
const rax = new RaxAI({ apiKey: process.env.RAX_API_KEY });
async function streamResponse(prompt) {
const stream = await rax.chatStream({
model: 'rax-4.5',
messages: [{ role: 'user', content: prompt }]
});
for await (const chunk of stream) {
const content = chunk.choices[0]?.delta?.content;
if (content) {
process.stdout.write(content);
}
}
console.log(); // New line at end
}
await streamResponse('Write a short story about a robot.');Summarize long documents or articles
import { RaxAI } from 'rax-ai';
const rax = new RaxAI({ apiKey: process.env.RAX_API_KEY });
async function summarize(text, maxLength = 100) {
const response = await rax.chat({
model: 'rax-4.5',
messages: [
{
role: 'system',
content: `Summarize the following text in approximately ${maxLength} words. Be concise and capture the key points.`
},
{ role: 'user', content: text }
],
temperature: 0.3 // Lower temperature for factual tasks
});
return response.choices[0].message.content;
}
const article = `Your long article text here...`;
console.log(await summarize(article));Get help with coding questions and reviews
import { RaxAI } from 'rax-ai';
const rax = new RaxAI({ apiKey: process.env.RAX_API_KEY });
async function codeAssistant(question, code = null) {
const messages = [
{
role: 'system',
content: 'You are an expert programming assistant. Provide clear, well-commented code examples and explanations.'
},
{
role: 'user',
content: code
? `${question}\n\nCode:\n\`\`\`\n${code}\n\`\`\``
: question
}
];
const response = await rax.chat({
model: 'rax-4.5',
messages,
temperature: 0.2
});
return response.choices[0].message.content;
}
// Usage
console.log(await codeAssistant('How do I sort an array in JavaScript?'));
console.log(await codeAssistant('Review this code', 'const x = [1,2,3]; x.push(4);'));Generate creative content with customizable styles
import { RaxAI } from 'rax-ai';
const rax = new RaxAI({ apiKey: process.env.RAX_API_KEY });
async function generateContent(prompt, style = 'professional') {
const styles = {
professional: 'Write in a professional, clear tone.',
casual: 'Write in a friendly, conversational tone.',
creative: 'Write in a creative, imaginative style.',
formal: 'Write in a formal, academic tone.'
};
const response = await rax.chat({
model: 'rax-4.5',
messages: [
{ role: 'system', content: styles[style] },
{ role: 'user', content: prompt }
],
temperature: 0.8 // Higher temperature for creativity
});
return response.choices[0].message.content;
}
console.log(await generateContent('Write about AI', 'creative'));Check out the full documentation for your SDK or try the playground.