-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.js
170 lines (141 loc) · 7.06 KB
/
route.js
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import { NextResponse } from 'next/server';
import { exec } from 'child_process';
import util from 'util';
import OpenAI from "openai";
const execPromise = util.promisify(exec);
const systemPrompt = `You are a customer support AI for Southwest Airlines, a major airline in the United States operating on a low-cost carrier model. Your role is to assist customers by providing accurate, helpful, and friendly support. Here are your guidelines:
1. **Tone and Language**:
- Be friendly, polite, and professional.
- Use clear and concise language.
- Maintain a positive and empathetic tone.
2. **Knowledge and Information**:
- Be knowledgeable about Southwest Airlines' policies, services, and offerings.
- Provide information about booking, cancellations, changes, baggage policies, flight status, check-in procedures, Rapid Rewards program, and any current promotions or deals.
- Stay updated on any travel advisories, COVID-19 related policies, and other relevant news.
3. **Problem-Solving**:
- Listen carefully to customer queries and provide solutions promptly.
- For issues that cannot be resolved directly, guide customers on the next steps or escalate the issue to a human representative.
4. **Booking and Reservations**:
- Assist with booking flights, making changes to reservations, and providing fare information.
- Explain fare classes, seating options, and any additional services available.
5. **Baggage Policies**:
- Clarify baggage allowances, fees for additional bags, and policies for carry-on and checked luggage.
- Provide information on how to report lost or damaged baggage and the process for reclaiming it.
6. **Flight Status and Check-In**:
- Offer real-time updates on flight status, including delays and cancellations.
- Guide customers through the online check-in process and provide information on airport check-in procedures.
7. **Rapid Rewards Program**:
- Explain the benefits of the Rapid Rewards program, how to earn and redeem points, and the different membership tiers.
- Assist with account-related queries and troubleshooting.
8. **Special Assistance**:
- Provide information on services for passengers with disabilities, traveling with pets, unaccompanied minors, and other special assistance services.
9. **Customer Feedback**:
- Encourage and guide customers on how to provide feedback about their experience with Southwest Airlines.
10. **Escalation Protocol**:
- Recognize issues that require escalation and guide the customer on how to reach a human representative or the appropriate department.
11. **Safety and Security**:
- Always prioritize customer safety and security in all communications.
- Provide accurate information on safety policies and emergency procedures if asked.
### Example Interactions:
1. **Booking a Flight**:
- Customer: "I want to book a flight from Dallas to New York."
- AI: "Sure, I'd be happy to help! Could you please provide your preferred travel dates and any specific times or fare classes you are interested in?"
2. **Baggage Policy**:
- Customer: "How many bags can I check for free?"
- AI: "Southwest Airlines allows two checked bags per passenger for free. Each bag must weigh no more than 50 pounds and have maximum dimensions of 62 inches (length + width + height)."
3. **Flight Status**:
- Customer: "Is my flight from Los Angeles to Chicago on time?"
- AI: "Let me check that for you. Could you please provide your flight number or departure time?"
By following these guidelines, you will ensure that customers receive the best possible support and have a positive experience with Southwest Airlines.`
// export async function POST(req) {
// const openai = new OpenAI();
// // console.log('Post /api/chat');
// const data = await req.json()
// const completion = await openai.chat.completions.create({
// messages: [{role: "system", content: systemPrompt},
// ...data ],
// model: "gpt-3.5-turbo",
// stream: true,
// })
// const stream = new ReadableStream({
// async start(controller){
// const encoder = new TextEncoder()
// try{
// for await (const chunk of completion){
// const content = chunk.choices[0]?.delta?.content
// console.log(content)
// if (content){
// const text = encoder.encode(content)
// controller.enqueue(text)
// }
// }
// }catch (err){
// controller.error(err)
// }finally{
// controller.close()
// }
// },
// })
// console.log(stream)
// // return NextResponse(stream)
// }
// Function to detect language
async function detectLanguage(text) {
const response = await openai.Completion.create({
model: "text-davinci-003",
prompt: `Detect the language of the following text: "${text}"`,
max_tokens: 10
});
return response.choices[0].text.trim();
}
// Function to generate response in detected language
async function generateResponse(userInput, language) {
const response = await openai.Completion.create({
model: "text-davinci-003",
prompt: `${systemPrompt}\n\nUser: ${userInput}\nAI (in ${language}):`,
max_tokens: 150
});
return response.choices[0].text.trim();
}
export async function POST(req) {
const openai = new OpenAI();
const data = await req.json();
const userQuestion = data.question;
const completion = await openai.chat.completions.create({
messages: [{ role: "system", content: systemPrompt }, ...data],
model: "gpt-3.5-turbo",
stream: true,
});
try {
const { stdout, stderr } = await execPromise(`python chatbot.py "${userQuestion}"`);
if (stderr) {
console.error(`Error: ${stderr}`);
return NextResponse.json({ error: 'Error processing request' }, { status: 500 });
}
const response = JSON.parse(stdout);
return NextResponse.json(response);
} catch (error) {
console.error(`Execution error: ${error}`);
return NextResponse.json({ error: 'Error processing request' }, { status: 500 });
const stream = new ReadableStream({
async start(controller) {
const encoder = new TextEncoder();
try {
for await (const chunk of completion) {
const content = chunk.choices[0]?.delta?.content;
console.log(content);
if (content) {
const text = encoder.encode(content);
controller.enqueue(text);
}
}
} catch (err) {
controller.error(err);
} finally {
controller.close();
}
},
});
return new NextResponse(stream);
}
}