const { streamText } = require('ai');
const { createOpenAI } = require('@ai-sdk/openai');

class AIService {
  async chat(req, res) {
    const { messages } = req.body;

    try {
      const deepseek = createOpenAI({
        baseURL: 'https://api.deepseek.com',
        apiKey: process.env.DEEPSEEK_API_KEY,
      });

      const result = await streamText({
        "model": deepseek.chat("deepseek-chat"),
        messages,
        temperature: 0.5,
        maxTokens: 1024,
      });

      result.pipeDataStreamToResponse(res);

    } catch (error) {
      console.error(error);
      res.status(500).send(JSON.stringify({ error: error.message }));
    }
    
  }
}

module.exports = new AIService();