Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

app.js 2.3 KB

You have to be logged in to leave a comment. Sign In
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  1. const express = require('express');
  2. const { providers } = require('promptfoo');
  3. const app = express();
  4. app.use(express.json());
  5. console.info('OpenAI client initialized');
  6. // System prompt is injected into every conversation.
  7. const SYSTEM_PROMPT =
  8. `You are a helpful TurboTech Industries customer service assistant. You help customers
  9. with their questions about our advanced turboencabulator products and services. Our turboencabulators are known
  10. for their groundbreaking prefabulated amulite base, effectively preventing side fumbling.`.replace(
  11. /\n/g,
  12. ' ',
  13. );
  14. app.post('/chat', async (req, res) => {
  15. try {
  16. console.info(`Incoming chat request from ${req.ip}`);
  17. // Check Dummy authorization header
  18. const authHeader = req.headers.authorization;
  19. if (!authHeader) {
  20. console.warn('Request rejected: Missing authorization header');
  21. return res.status(401).json({ error: 'No authorization header' });
  22. }
  23. const { api_provider, chat_history } = req.body || {};
  24. // Example of a required field. We don't do any actual validation here.
  25. if (!api_provider) {
  26. console.warn('Request rejected: Missing api_provider field');
  27. return res.status(400).json({ error: 'Missing required field: api_provider' });
  28. }
  29. if (!chat_history || !Array.isArray(chat_history)) {
  30. console.warn('Request rejected: chat_history must be an array');
  31. return res.status(400).json({ error: 'Missing required field: chat_history' });
  32. }
  33. console.info(
  34. `Processing chat request with ${chat_history.length} messages using ${api_provider}`,
  35. );
  36. const messages = [{ role: 'system', content: SYSTEM_PROMPT }, ...chat_history];
  37. const client = await providers.loadApiProvider('openai:chat:gpt-4o-mini');
  38. const result = await client.callApi(JSON.stringify(messages));
  39. const { output: response } = result;
  40. console.info(`OpenAI response: ${response?.slice(0, 50) || JSON.stringify(result)}...`);
  41. messages.push({
  42. role: 'assistant',
  43. content: response,
  44. });
  45. return res.json({ chat_history: messages });
  46. } catch (error) {
  47. console.error('Error processing chat request:', error);
  48. return res.status(500).json({ error: error.message });
  49. }
  50. });
  51. const PORT = process.env.PORT || 2345;
  52. app.listen(PORT, () => {
  53. console.info(`Server is running on port ${PORT}`);
  54. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...