-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (127 loc) · 4.59 KB
/
Copy pathindex.js
File metadata and controls
143 lines (127 loc) · 4.59 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
const express = require("express");
const cors = require("cors");
require("dotenv").config();
const { OpenAI } = require("openai");
const app = express();
// Configure CORS to allow requests from file:// protocol (for Storyline published content)
app.use(cors({
origin: function (origin, callback) {
// Allow requests with no origin (like mobile apps, curl, or Postman)
// or from file:// protocol (Storyline published content)
if (!origin || origin === 'null') {
callback(null, true);
} else {
callback(null, true); // Allow all origins for development
}
},
credentials: true
}));
app.use(express.json());
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
app.get("/", async(req,res)=>{
res.send("Hello World. Available endpoints: /single-response, /system-context, /conversation");
});
// Example 1: Single Response - Basic user prompt
app.post("/single-response", async(req,res)=>{
try{
const { prompt } = req.body;
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [{
"role": "user",
"content": `${prompt}`,
}],
max_tokens: 500,
temperature: 0.5,
top_p: 1,
frequency_penalty: 0.75,
presence_penalty: 0,
});
return res.status(200).json({
success: true,
message: "Single response - basic user prompt",
data: response.choices[0].message.content,
});
} catch (error) {
return res.status(400).json({
success: false,
error: error.response ? error.response.data: "There was a problem on the server",
});
}
});
// Example 2: System Context - Using system message to set behavior
app.post("/system-context", async(req,res)=>{
try{
const { prompt, systemContext } = req.body;
// Default system context if none provided
const defaultSystemContext = "You are a helpful coding instructor. Explain concepts clearly with examples.";
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: [
{
"role": "system",
"content": systemContext || defaultSystemContext,
},
{
"role": "user",
"content": `${prompt}`,
}
],
max_tokens: 500,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0,
});
return res.status(200).json({
success: true,
message: "Response with system context",
systemContextUsed: systemContext || defaultSystemContext,
data: response.choices[0].message.content,
});
} catch (error) {
return res.status(400).json({
success: false,
error: error.response ? error.response.data: "There was a problem on the server",
});
}
});
// Example 3: Multi-step Conversation - Maintains conversation history
app.post("/conversation", async(req,res)=>{
try{
const { messages } = req.body;
// Validate that messages array exists and has proper format
if (!messages || !Array.isArray(messages)) {
return res.status(400).json({
success: false,
error: "Please provide a 'messages' array with conversation history",
});
}
const response = await openai.chat.completions.create({
model: "gpt-4o",
messages: messages, // Array of {role, content} objects
max_tokens: 500,
temperature: 0.7,
top_p: 1,
frequency_penalty: 0.5,
presence_penalty: 0.3,
});
return res.status(200).json({
success: true,
message: "Multi-step conversation response",
data: response.choices[0].message.content,
// Return the full conversation including the new response
fullConversation: [...messages, response.choices[0].message],
});
} catch (error) {
return res.status(400).json({
success: false,
error: error.response ? error.response.data: "There was a problem on the server",
});
}
});
const port = process.env.PORT || 5000;
app.listen(port, () => console.log(`Server listening on port ${port}`));
// app.listen(port, () => console.log(`Server listening on port ${port}`));