Hi everybody!
I have a prompt which works on Playground but not in my application.
I have this code
import OpenAI from 'openai'
import express from 'express'
import cors from 'cors'
import bodyParser from 'body-parser'
import { Client } from "@googlemaps/google-maps-services-js";
const app = express()
const PORT = process.env.PORT || 3001
const client = new Client({});
app.use(bodyParser.json());
const openai = new OpenAI({
apiKey: '■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■8LIiIzmi'
})
app.use(cors())
app.use(express.json())
let welcomeMessage = ""
// Express.js endpoint to get a welcome message
app.get('/api/welcome', async (req, res) => {
try {
const response = await openai.chat.completions.create({
model: "gpt-4", // Use an appropriate model
messages: [{ // Assuming a start of a conversation with no prior context
role: "system",
content: "Start by saying 'Hello!Welcome to geography lesson.Do you want start learning geogrpahy?'."}],//"Generate a short friendly and engaging welcome message for a geography discussion chatbot."
max_tokens: 4096,
temperature:1,
top_p:1,
frequency_penalty:0,
presence_penalty:0
});
welcomeMessage = response.choices[0].message.content.trim(); // Store the welcome message
res.json({ message: welcomeMessage });
//res.json({ message: response.choices[0].message.content.trim() });
} catch (error) {
console.error('Error fetching welcome message:', error);
res.status(500).json({ error: 'Failed to fetch welcome message' });
}
});
// Assuming you already have the '/api/welcome' endpoint
// Endpoint to handle user messages and respond
app.post('/api/conversation', async (req, res) => {
const { userMessage } = req.body; // Get the user message from the request
try {
// Send the user message to OpenAI's API
const response = await openai.chat.completions.create({
model: "gpt-4", // Adjust according to the available models
messages: [{role: "system", content: welcomeMessage}, // Use the stored welcome message as the first message
{role: "user", content: userMessage},
{role: "assistant", content: ""}, // Leave the assistant's response empty for now
{role:"system",content:"Ask question related to geography"},
{role:"system",content:"If user answers correctly, reply with 'Excellent job!!!'"},
//{"role":"system","content":"You are a geography teacher.You ask random geography questions.When user answers correctly you give feedback by saying 'Excellent job!!!'.When student answers incorrectly you provide hints or the correct answer."},
//{"role":"system","content":"Ask random geography questions"},
// {"role":"system","content":"When student answers correctly give feedback by saying 'Excellent job!!!'."},
// {"role":"system","content":"When student answers incorrectly provide hints or the correct answer."},
{"role":"user","content":userMessage}], // Send the user's message for processing
max_tokens: 4096,
temperature:1,
top_p:1,
frequency_penalty:0,
presence_penalty:0
});
// Return the generated response to the frontend
res.json({ botResponse: response.choices[0].message.content });
} catch (error) {
console.error('Error in conversation handling:', error);
res.status(500).json({ error: 'Failed to process the conversation' });
}
});
app.post('/api/city-info', async (req, res) => {
const { cityName } = req.body;
try {
const prompt = `Provide detailed information about ${cityName}.`;
const response = await openai.chat.completions.create({
model: "gpt-4",
messages: [{"role":"system","content": prompt}],
max_tokens: 150,
});
res.json({ info: response.choices[0].message.content });
} catch (error) {
console.error('Error fetching city information:', error);
res.status(500).json({ error: 'Failed to fetch city information' });
}
});
app.post('/api/location', async (req, res) => {
const { location } = req.body;
try {
const response = await client.geocode({
params: {
address: location,
key: apiKey,
},
});
const { lat, lng } = response.data.results[0].geometry.location;
res.json({ lat, lng });
} catch (error) {
console.error(error);
res.status(500).send('Error retrieving location data');
}
});
app.listen(PORT, () => console.log(`Server started on http://localhost:${PORT}`))
and I get this