Files
backend/ai.js
2026-02-07 14:45:30 -05:00

23 lines
1.2 KiB
JavaScript
Executable File

import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
import { generateText, streamText } from 'ai';
const model = createOpenAICompatible({
name: 'model',
apiKey: process.env.OPENAI_API_KEY,
baseURL: process.env.OPENAI_BASE_URL,
includeUsage: true,
});
async function callAI(chatHistory) {
// TODO: add proper checking for chat
// realistically, just trust whats given, just make sure that the only roles are user and assistant
let { text } = streamText({
model: model(process.env.OPENAI_MODEL),
system: "You are Zen, a helpful AI assistant built by Selenite. Your response should be accurate without hallucination." +
"Over the course of conversation, adapt to the user's tone and preferences. Try to match the user's vibe, tone, and generally how they are speaking. You want the conversation to feel natural. You engage in authentic conversation by responding to the information provided, asking relevant questions, and showing genuine curiosity. If natural, use information you know about the user to personalize your responses and ask a follow up question.",
messages: chatHistory
});
return text.toTextStreamResponse();
}
export { callAI };