-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
108 lines (89 loc) · 3.59 KB
/
app.py
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
from flask import Flask, jsonify, request
from flask_pymongo import PyMongo
from config import Config
from dotenv import load_dotenv
import openai
from openai import OpenAI
import os
from flask_cors import CORS
import string
load_dotenv()
openai.api_key = os.getenv('OPENAI_API_KEY')
app = Flask(__name__)
app.config.from_object(Config)
CORS(app)
client = OpenAI(
api_key=os.getenv('OPEN_API_KEY')
)
# Initialize MongoDB connection using Flask-PyMongo
mongo = PyMongo(app)
customerCollection = mongo.db.customers # Use 'customers' collection
petCollection = mongo.db.pets
# Routes
@app.route('/api/customers', methods=['GET'])
def get_customers():
customers = list(customerCollection.find({}, {'_id': 0})) # Get all customers, hide MongoDB `_id`
return jsonify(customers)
@app.route('/api/customers', methods=['POST'])
def create_customer():
data = request.json
result = customerCollection.insert_one({
'first_name': data['first_name'],
'last_name': data['last_name'],
'date_of_birth': data['date_of_birth'],
'insurance_provider': data['insurance_provider'],
'policy_number': data['policy_number'],
})
#print(result.inserted_id)
return jsonify({"message": "customer created!", "id": str(result.inserted_id)}), 201
@app.route('/api/customers/<name>', methods=['DELETE'])
def delete_customer(name):
result = customerCollection.delete_one({'name': name})
if result.deleted_count == 0:
return jsonify({"error": "customer not found"}), 404
return jsonify({"message": "customer deleted!"})
@app.route('/api/pets', methods=['GET'])
def get_pets():
pets = list(petCollection.find({}, {'_id': 0})) # Get all pets, hide MongoDB `_id`
return jsonify(pets)
@app.route('/api/pets', methods=['POST'])
def create_pet():
data = request.json
petCollection.insert_one({
'name': data['name'],
'owner': data['owner'],
'age': data['age'],
'weight': data['weight'],
})
return jsonify({"message": "pet created!"}), 201
@app.route('/api/pets/<name>', methods=['DELETE'])
def delete_pet(name):
result = petCollection.delete_one({'name': name})
if result.deleted_count == 0:
return jsonify({"error": "pet not found"}), 404
return jsonify({"message": "pet deleted!"})
@app.route('/api/calculatecost', methods=['POST'])
def calculate_cost():
try:
data = request.json
prompt = "<cost settings>Checkups are $10, vaccinations $20, blood tests $30, oral health exams $40 and orthopedic surgeries are $50. "
prompt += "All insurance providers have a deductible of $10 except for united healthcare having one of $30. All providers will refuse to pay for pets above 12 years old </cost settings>"
prompt += f"You are an insurance adjuster. Given the following information about the pet, the treatment and the customers provider, please give a cost minus the reimbursed amount. Please confirm this addition. Return only a number"
prompt += f"<provider>{data['provider']}</provider><treatment>{data['treatment']}</treatment><age>{data['age']}</age>"
response = client.chat.completions.create(
model="gpt-3.5-turbo",
messages=[
{
"role": "user",
"content": prompt,
}
]
)
answer = response.choices[0].message.content
return jsonify({
"chatgpt_confirmation": answer
})
except Exception as e:
return jsonify({"error": str(e)}), 500
if __name__ == '__main__':
app.run(debug=True, port=5000)