-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
135 lines (108 loc) · 4.6 KB
/
Copy pathapp.py
File metadata and controls
135 lines (108 loc) · 4.6 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
from flask import Flask, request, render_template, jsonify, session, redirect, url_for
import json
from openai import AzureOpenAI
app = Flask(__name__)
app.secret_key = "secret_key@#123456789"
with open("ChatSetup.json", "r") as file:
chat_setup = json.load(file)
search_endpoint = "https://ehb-search.search.windows.net"
search_key = chat_setup["searchKey"]
search_index_name = "ehb"
master_password = chat_setup["masterPassword"]
client = AzureOpenAI(
azure_endpoint="https://ehb-france-central.openai.azure.com/",
api_key=chat_setup["api_key"],
api_version="2024-02-01",
)
users = {chat_setup["username"]: chat_setup["password"]}
conversations = {}
@app.route("/clear_chat", methods=["POST"])
def clear_chat():
if "username" in session:
username = session["username"]
conversations[username] = []
return jsonify({"success": True}), 200
return jsonify({"error": "Unauthorized"}), 401
@app.route("/")
def index():
if "username" in session:
username = session["username"]
old_messages = conversations.get(username, [])
return render_template("index.html", conversation_log=old_messages)
return render_template("index.html") # Render the index.html template without old messages
@app.route("/login", methods=["POST"])
def login():
if request.method == "POST":
username = request.json.get("username")
password = request.json.get("password")
if username in users and users[username] == password:
session["username"] = username
old_messages = conversations.get(username, [])
return jsonify({"success": True, "old_messages": old_messages}), 200
return jsonify({"success": False}), 401
@app.route("/register", methods=["POST"])
def register():
if request.method == "POST":
master_password_input = request.json.get("master_password")
username = request.json.get("username")
password = request.json.get("password")
if master_password_input != master_password:
return jsonify({"success": False, "error": "Invalid master password"}), 401
if username in users:
return jsonify({"success": False, "error": "Username already exists"}), 409
users[username] = password
# Log in the user after successful registration
session["username"] = username
# Redirect to the chat screen
return redirect(url_for("index"))
# Return a response even if the method is not POST
return jsonify({"success": False, "error": "Method not allowed"}), 405
@app.route("/chat", methods=["POST"])
def chat():
if "username" not in session:
return jsonify({"error": "Unauthorized"}), 401
user_message = request.json.get("message")
username = session["username"]
if username not in conversations:
conversations[username] = []
conversation = conversations[username]
conversation.append({"role": "user", "content": user_message})
response = client.chat.completions.create(
model=chat_setup["chatParameters"]["chatParameters"],
messages=conversation,
extra_body={
"data_sources": [
{
"type": "azure_search",
"parameters": {
"endpoint": search_endpoint,
"index_name": search_index_name,
"semantic_configuration": "default",
"query_type": "simple",
"fields_mapping": {},
"role_information": chat_setup["systemPrompt"],
"strictness": 3,
"top_n_documents": 5,
"authentication": {"type": "api_key", "key": search_key},
"key": search_key,
},
}
],
},
)
system_response = response.choices[0].message.content
conversation.append({"role": "assistant", "content": system_response})
conversations[username] = conversation
old_messages = conversations.get(username, [])
session["old_messages"] = old_messages
return jsonify({"response": system_response})
@app.route("/logout", methods=["GET", "POST"])
def logout():
if request.method == "POST" or request.method == "GET": # Corrected the condition here
username = session.get("username")
if username:
session.pop("username")
return redirect(url_for("index")) # Redirect to the index/login page
return jsonify({"error": "Method not allowed"}), 405
if __name__ == "__main__":
app.run(host="0.0.0.0", debug=False)