-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
73 lines (60 loc) · 2.62 KB
/
app.py
File metadata and controls
73 lines (60 loc) · 2.62 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
# app.py
import os
import openai
import csv
from dotenv import load_dotenv
from flask import Flask, request, jsonify, render_template
# Load environment variables from .env file
load_dotenv()
# Get the API key from environment variables
api_key = os.getenv("OPENAI_API_KEY")
# Use the API key with OpenAI
openai.api_key = api_key
app = Flask(__name__)
# Function to handle the interaction with GPT-4
def chat_with_gpt4(messages):
response = openai.ChatCompletion.create(
model="gpt-4",
messages=messages
)
return response['choices'][0]['message']['content']
# Initialize the conversation
conversation = [
{"role": "system", "content": "You are a customer service representative for HongQi Automotive repair shop. Your task is to get the customer's car information (year, make, model, reason for the visit) and the customer's personal information (first name, last name, email, phone number) and make an appointment for any time between 9am to 4pm."}
]
@app.route('/')
def home():
return render_template('index.html')
@app.route('/chat', methods=['POST'])
def chat():
user_message = request.json['message']
conversation.append({"role": "user", "content": user_message})
bot_response = chat_with_gpt4(conversation)
conversation.append({"role": "assistant", "content": bot_response})
# Extract and save data if user provides all necessary details
if "First Name" in user_message and "Last Name" in user_message and "Email" in user_message and "Phone Number" in user_message:
parsed_data = parse_user_input(user_message)
save_to_csv(parsed_data)
return jsonify({'message': bot_response})
# Parse the user input (assuming the format is known)
def parse_user_input(user_input):
lines = user_input.strip().split("\n")
car_year = lines[0].split(": ")[1]
car_make = lines[1].split(": ")[1]
car_model = lines[2].split(": ")[1]
reason = lines[3].split(": ")[1]
first_name = lines[5].split(": ")[1]
last_name = lines[6].split(": ")[1]
email = lines[7].split(": ")[1]
phone_number = lines[8].split(": ")[1]
return [car_year, car_make, car_model, reason, first_name, last_name, email, phone_number]
# Save the parsed data to a CSV file
def save_to_csv(data):
file_exists = os.path.isfile('appointments.csv')
with open('appointments.csv', mode='a', newline='') as file:
writer = csv.writer(file)
if not file_exists:
writer.writerow(["Year", "Make", "Model", "Reason for the visit", "First Name", "Last Name", "Email", "Phone Number"])
writer.writerow(data)
if __name__ == '__main__':
app.run(debug=True)