-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·209 lines (176 loc) · 7.9 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/usr/bin/env python3
from flask import Flask, request, jsonify, render_template, Response, stream_with_context
from flask_sqlalchemy import SQLAlchemy
import openai
import os, sys, re
import logging
from sqlalchemy.exc import IntegrityError
import json
from requests.adapters import HTTPAdapter
from sqlalchemy.orm import sessionmaker
import requests
logging.basicConfig(level=logging.DEBUG)
app = Flask(__name__)
# Database connection info from environment variables
hostname = os.getenv('DB_HOST', 'localhost')
databasename = os.getenv('DB_NAME', 'chatdb')
username = os.getenv('DB_USER', 'chatuser')
password = os.getenv('DB_PASS', 'yourpassword')
port = os.getenv('DB_PORT', '5432')
app.config['SQLALCHEMY_DATABASE_URI'] = f'postgresql://{username}:{password}@{hostname}:{port}/{databasename}'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
# OpenAI API settings
openai.api_key = os.getenv("OPENAI_API_KEY")
# Database models
class Chat(db.Model):
id = db.Column(db.Integer, primary_key=True)
question = db.Column(db.String, nullable=False)
answer = db.Column(db.String, nullable=False)
feedback = db.Column(db.Boolean)
class KnowledgeBase(db.Model):
__tablename__ = 'KnowledgeBase'
id = db.Column(db.Integer, primary_key=True)
question = db.Column(db.String(450), unique=True, nullable=False)
answer = db.Column(db.String, nullable=False)
# Initialize session within an application context
with app.app_context():
Session = sessionmaker(bind=db.engine)
class DataEnrichmentAdapter(HTTPAdapter):
def send(self, request, **kwargs):
"""Modify request to enhance with external data."""
session = Session()
results = session.query(KnowledgeBase).filter(KnowledgeBase.question.like('%data%')).all()
data_json = [{'question': row.question, 'answer': row.answer} for row in results]
request.headers['X-Data-Reference'] = str(data_json)
return super(DataEnrichmentAdapter, self).send(request, **kwargs)
# Setup the session with the custom adapter
session = requests.Session()
adapter = DataEnrichmentAdapter()
session.mount('https://api.openai.com', adapter)
openai.requestssession = session # Ensure OpenAI uses the customized session
@app.route('/')
def home():
return render_template('chat.html')
@app.route('/chat', methods=['POST', 'GET'])
def chat():
if request.method == 'POST':
user_input = request.json['question']
return jsonify(process_post(user_input))
elif request.method == 'GET':
user_input = request.args.get('question')
return Response(stream_with_context(stream_response(user_input)), mimetype='text/event-stream')
else:
return jsonify({'error': 'Method not allowed'}), 405
@app.route('/feedback', methods=['POST'])
def feedback():
data = request.get_json()
chat_id = data.get('chat_id')
if not chat_id:
return jsonify({'message': 'Chat ID is missing!'}), 400
chat = Chat.query.get(chat_id)
if not chat:
return jsonify({'message': 'Chat not found!'}), 404
chat.feedback = data.get('feedback', True)
if data.get('correct_answer'):
chat.answer = data['correct_answer']
db.session.commit()
return jsonify({'message': 'Feedback received and chat updated!'})
db.session.commit()
return jsonify({'message': 'Feedback received!'})
def process_post(user_input):
knowledge = KnowledgeBase.query.filter_by(question=user_input).first()
if knowledge:
return {'answer': knowledge.answer, 'chat_id': knowledge.id}
response = openai.chat.completions.create(
model="gpt-4",
max_tokens=1500,
messages=[
{"role": "system", "content": "You are a skillful Technology Specialist. This is your blog called auroranrunner. Your name is Tapio Vaattanen."},
{"role": "user", "content": user_input}
]
)
answer = response.choices[0].message.content.strip()
answer = format_response(answer)
try:
new_knowledge = KnowledgeBase(question=user_input, answer=answer)
db.session.add(new_knowledge)
db.session.commit()
except IntegrityError:
db.session.rollback()
new_knowledge = KnowledgeBase.query.filter_by(question=user_input).first()
answer = new_knowledge.answer # Use existing answer if question was a duplicate
new_chat = Chat(question=user_input, answer=answer, feedback=None)
db.session.add(new_chat)
db.session.commit()
return {'answer': answer, 'chat_id': new_chat.id}
def stream_response(user_input):
with app.app_context():
yield "data: Checking for cached answers...\n\n"
knowledge = KnowledgeBase.query.filter_by(question=user_input).first()
if knowledge:
yield f"data: {json.dumps({'answer': knowledge.answer, 'chat_id': knowledge.id})}\n\n"
else:
response = openai.chat.completions.create(
model="gpt-4",
max_tokens=1500,
messages=[
{"role": "system", "content": "You are a skillful Technology Specialist. This is your blog called auroranrunner. Your name is Tapio Vaattanen."},
{"role": "user", "content": user_input}
]
)
answer = response.choices[0].message.content.strip()
answer = format_response(answer)
new_knowledge = KnowledgeBase(question=user_input, answer=answer)
db.session.add(new_knowledge)
db.session.commit()
new_chat = Chat(question=user_input, answer=answer, feedback=None)
db.session.add(new_chat)
db.session.commit()
yield f"data: {json.dumps({'answer': answer, 'chat_id': new_chat.id})}\n\n"
def format_response(text):
"""
Formats text by handling bold within numbered list items correctly,
applying general text formatting, managing code blocks, and styling comments with # retained.
"""
formatted_text = ""
in_code_block = False # Flag to check if inside a code block
buffer = "" # Buffer to store text for code blocks
lines = text.split('\n')
for line in lines:
if line.strip().startswith("```"): # Check for code block toggle
in_code_block = not in_code_block
if not in_code_block:
formatted_text += f"<pre><code>{buffer}</code></pre>"
buffer = ""
continue
if in_code_block:
buffer += line + '\n'
continue
# Handling comments in the code specifically
if line.strip().startswith("#"):
formatted_text += f"<p><strong>{line.strip()}</strong></p>"
else:
# Handling bold within numbered list items
search_result = re.search(r'^(\d+)\.\s+\*\*(.*?)\*\*:\s*(.*)$', line)
if search_result:
number, bold_text, the_rest = search_result.groups()
formatted_text += f"<p>{number}. <strong>{bold_text}</strong>: {the_rest}</p>"
else:
# Apply bold text formatting for other lines
line = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', line)
formatted_text += f"<p>{line}</p>"
# Ensure closing code block if text ends within one
if in_code_block:
formatted_text += f"<pre><code>{buffer}</code></pre>"
return formatted_text
@app.after_request
def apply_cors(response):
response.headers['Access-Control-Allow-Origin'] = 'https://www.auroranrunner.com'
response.headers['Access-Control-Allow-Methods'] = 'GET, POST'
response.headers['Access-Control-Allow-Headers'] = 'Content-Type, Authorization'
response.headers['Access-Control-Allow-Credentials'] = 'true'
return response
if __name__ == '__main__':
context = ('/etc/letsencrypt/live/vauva.ampiainen.net/fullchain.pem', '/etc/letsencrypt/live/vauva.ampiainen.net/privkey.pem')
app.run(debug=True, host='0.0.0.0', port=5004, ssl_context=context)