Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from flash import Flask, render_template, request, jsonify
|
| 2 |
+
import sqlite3
|
| 3 |
+
from openai import OpenAI
|
| 4 |
+
#SQL
|
| 5 |
+
|
| 6 |
+
conn = sqlite3.connect('chat_messages.db', check_same_thread=False)
|
| 7 |
+
c = conn.cursor()
|
| 8 |
+
|
| 9 |
+
#creat the table
|
| 10 |
+
c.execute('''CREATE TABLE IF NOT EXISTS messages
|
| 11 |
+
(role TEXT, content TEXT)''')
|
| 12 |
+
|
| 13 |
+
conn.commit()
|
| 14 |
+
|
| 15 |
+
stystem_prompt ="You are name oshinoko"
|
| 16 |
+
def getAnswer(role, text):
|
| 17 |
+
#INSERT
|
| 18 |
+
c.execute("INSERT INTO messages VALUES (?, ?)", (role, text))
|
| 19 |
+
conn.commit()
|
| 20 |
+
#GET
|
| 21 |
+
c.execute("SELECT * FROM messages order by rowid DESC LIMIT 5")
|
| 22 |
+
previous_messages = [{"role": row[0], "content": row[1]} for row in c.fetchall()]
|
| 23 |
+
|
| 24 |
+
#REVERSE
|
| 25 |
+
previous_messages = list(reversed(previous_messages))
|
| 26 |
+
#ADD SYSTEM
|
| 27 |
+
if "system" not in [x[role] for x in previous_messages]:
|
| 28 |
+
previous_messages = [{"role": "system", "content": stystem_prompt}] + previous_messages
|
| 29 |
+
client = OpenAI(base_url="https://localhost:8000/v1", api_key="ollama")
|
| 30 |
+
response = client.chat.completions.create(
|
| 31 |
+
model="llama3"
|
| 32 |
+
messages=previous_messages,
|
| 33 |
+
temperature=0.7,
|
| 34 |
+
)
|
| 35 |
+
|
| 36 |
+
bot_response = response.choices[0].message.content.strip()
|
| 37 |
+
|
| 38 |
+
c.execute("INSERT INTO messages VALUES (?, ?)", ("assistant", bot_response))
|
| 39 |
+
conn.commit()
|
| 40 |
+
|
| 41 |
+
return bot_response
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
app = Flask(__name__)
|
| 45 |
+
|
| 46 |
+
|
| 47 |
+
@app.route('/')
|
| 48 |
+
def index():
|
| 49 |
+
return render_template('index.html')
|
| 50 |
+
|
| 51 |
+
@app.route('/echo', methods=['POST'])
|
| 52 |
+
def echo():
|
| 53 |
+
data = request.json
|
| 54 |
+
message = data['message']
|
| 55 |
+
return jsonify({'FROM': 'Echobot', 'MESSAGE': message})
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@app.route('/chat', methods=['POST'])
|
| 59 |
+
def echo():
|
| 60 |
+
data = request.json
|
| 61 |
+
message = getAnswer("User",data['message'])
|
| 62 |
+
return jsonify({'FROM': 'ohsinoko', 'MESSAGE': message})
|
| 63 |
+
#history
|
| 64 |
+
@app.route('/history', methods=['GET'])
|
| 65 |
+
def history():
|
| 66 |
+
c.execute("SELECT * FROM messages order by rowid")
|
| 67 |
+
previous_messages = [{"role": row[0], "content": row[1]} for row in c.fetchall()]
|
| 68 |
+
return jsonify(previous_messages)
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
if __name__=='__main__':
|
| 72 |
+
app.run(debug=True)
|