Update app.py
Browse files
app.py
CHANGED
|
@@ -1,72 +1,74 @@
|
|
| 1 |
-
from
|
| 2 |
-
import sqlite3
|
| 3 |
-
from openai import OpenAI
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
conn.commit()
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
previous_messages =
|
| 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 |
-
@app.route('/
|
| 52 |
-
def
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from flask import Flask, render_template, request, jsonify
|
| 2 |
+
import sqlite3
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
|
| 5 |
+
# Kết nối SQLite
|
| 6 |
+
db_path = "chat_messages.db"
|
| 7 |
+
conn = sqlite3.connect(db_path, check_same_thread=False)
|
| 8 |
+
c = conn.cursor()
|
| 9 |
+
|
| 10 |
+
# Tạo bảng nếu chưa tồn tại
|
| 11 |
+
c.execute('''CREATE TABLE IF NOT EXISTS messages
|
| 12 |
+
(role TEXT, content TEXT)''')
|
| 13 |
+
conn.commit()
|
| 14 |
+
|
| 15 |
+
# Lời nhắc hệ thống
|
| 16 |
+
system_prompt = "You are name oshinoko"
|
| 17 |
+
|
| 18 |
+
def getAnswer(role, text):
|
| 19 |
+
# Lưu tin nhắn vào database
|
| 20 |
+
c.execute("INSERT INTO messages VALUES (?, ?)", (role, text))
|
| 21 |
+
conn.commit()
|
| 22 |
+
|
| 23 |
+
# Lấy 5 tin nhắn gần nhất
|
| 24 |
+
c.execute("SELECT * FROM messages ORDER BY rowid DESC LIMIT 5")
|
| 25 |
+
previous_messages = [{"role": row[0], "content": row[1]} for row in c.fetchall()]
|
| 26 |
+
previous_messages.reverse()
|
| 27 |
+
|
| 28 |
+
# Thêm system prompt nếu chưa có
|
| 29 |
+
if not any(msg["role"] == "system" for msg in previous_messages):
|
| 30 |
+
previous_messages.insert(0, {"role": "system", "content": system_prompt})
|
| 31 |
+
|
| 32 |
+
# Gọi API OpenAI
|
| 33 |
+
client = OpenAI(base_url="https://localhost:8000/v1", api_key="ollama")
|
| 34 |
+
response = client.chat.completions.create(
|
| 35 |
+
model="llama3",
|
| 36 |
+
messages=previous_messages,
|
| 37 |
+
temperature=0.7,
|
| 38 |
+
)
|
| 39 |
+
|
| 40 |
+
bot_response = response.choices[0].message.content.strip()
|
| 41 |
+
|
| 42 |
+
# Lưu phản hồi của bot
|
| 43 |
+
c.execute("INSERT INTO messages VALUES (?, ?)", ("assistant", bot_response))
|
| 44 |
+
conn.commit()
|
| 45 |
+
|
| 46 |
+
return bot_response
|
| 47 |
+
|
| 48 |
+
# Khởi tạo Flask
|
| 49 |
+
app = Flask(__name__)
|
| 50 |
+
|
| 51 |
+
@app.route('/')
|
| 52 |
+
def index():
|
| 53 |
+
return render_template('index.html')
|
| 54 |
+
|
| 55 |
+
@app.route('/echo', methods=['POST'])
|
| 56 |
+
def echo():
|
| 57 |
+
data = request.json
|
| 58 |
+
message = data['message']
|
| 59 |
+
return jsonify({'FROM': 'Echobot', 'MESSAGE': message})
|
| 60 |
+
|
| 61 |
+
@app.route('/chat', methods=['POST'])
|
| 62 |
+
def chat():
|
| 63 |
+
data = request.json
|
| 64 |
+
message = getAnswer("User", data['message'])
|
| 65 |
+
return jsonify({'FROM': 'oshinoko', 'MESSAGE': message})
|
| 66 |
+
|
| 67 |
+
@app.route('/history', methods=['GET'])
|
| 68 |
+
def history():
|
| 69 |
+
c.execute("SELECT * FROM messages ORDER BY rowid")
|
| 70 |
+
previous_messages = [{"role": row[0], "content": row[1]} for row in c.fetchall()]
|
| 71 |
+
return jsonify(previous_messages)
|
| 72 |
+
|
| 73 |
+
if __name__ == '__main__':
|
| 74 |
+
app.run(host='0.0.0.0', port=7860, debug=True)
|