File size: 921 Bytes
da1be96
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require('dotenv').config();
const express = require('express');
const { InferenceClient } = require('@huggingface/inference');
const app = express();
const port = 3000;

const client = new InferenceClient(process.env.HF_TOKEN);

app.use(express.json());
app.use(express.static('public'));

app.post('/chat', async (req, res) => {
    try {
        const messages = req.body.messages;
        const chatCompletion = await client.chatCompletion({
            provider: "hf-inference",
            model: "facebook/blenderbot-400M-distill",
            messages: messages,
        });
        const response = chatCompletion.choices[0].message;
        res.json({ response });
    } catch (error) {
        console.error(error);
        res.status(500).json({ error: 'An error occurred while processing your request.' });
    }
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});