Skip to content

Instantly share code, notes, and snippets.

@iceener
Created May 1, 2024 13:28
Show Gist options
  • Save iceener/fd676ef67d0930f4104fb836f9ac7d40 to your computer and use it in GitHub Desktop.
Save iceener/fd676ef67d0930f4104fb836f9ac7d40 to your computer and use it in GitHub Desktop.
heyalice.app — Node.js server example
import express from 'express';
import { OpenAI } from 'openai';
const app = express();
const openai = new OpenAI({
apiKey: 'API_KEY'
});
app.use(express.json());
const getFormattedTimestamp = () => new Date().toISOString();
app.post('/api/chat', async (req, res) => {
res.writeHead(200, {
'Content-Type': 'application/x-ndjson',
'Transfer-Encoding': 'chunked'
});
try {
const stream = await openai.chat.completions.create({
model: 'gpt-4',
messages: req.body.messages,
stream: true,
});
for await (const chunk of stream) {
const response = {
model: "gpt-4",
created_at: getFormattedTimestamp(),
message: { content: chunk.choices[0]?.delta?.content || '' },
done: chunk.choices[0]?.delta?.index === null
};
res.write(JSON.stringify(response) + '\n');
}
} catch (error) {
console.error('Error processing OpenAI API request:', error);
res.end();
}
res.end();
});
app.listen(3005, () => {
console.log('Server running on http://localhost:3005');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment