Skip to content

Commit

Permalink
Version 3.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
Davidasx committed Oct 28, 2024
0 parents commit c0bea4f
Show file tree
Hide file tree
Showing 18 changed files with 1,596 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.vscode/
__pycache__/
.termdbms/
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# flask-gpt

ChatGPT Web UI implemented with Flask, HTML, Javascript and CSS.

Fill in your own API to use.

## Installation

See [install.md](install.md) for installation instructions.

## Implementation

Frontend: HTML, CSS, and Javascript to receive user messages and send them to the backend.

Backend: Python Flask to receive messages from the frontend and call the API.

Search: Free DuckDuckGo search API to search for information. You can set `DUCK_PROXY` environment variable to use a proxy for DuckDuckGo.

All chat data is stored in an SQL database, with UUIDs stored in cookies.

We will not use your data for any purpose other than to provide the service. The SQL database will never be viewed by anyone other than the program.

The log of the python program will hide all personal information. The only thing that will be shown in the log is the web requests.

## Branches

[Main branch](https://github.com/Davidasx/flask-gpt/tree/main): Latest tested stable version, with the most stable features and good experience.

[Dev branch](https://github.com/Davidasx/flask-gpt/tree/dev): Latest features, may be unstable. Several features may be incomplete.

## Demo site (low performance, Dev branch)

[http://chat.davidx.us.kg/](http://chat.davidx.us.kg/)
270 changes: 270 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
from flask import Flask, send_from_directory, request, jsonify, Response
import os
from openai import OpenAI
import json
import logging
from duckduckgo_search import DDGS
import time
import os
from database import init_db, get_chat_log, save_chat_log, delete_chat_log, db, ChatLog

class SensitiveInfoFilter(logging.Filter):
def filter(self, record):
message = record.getMessage()
if 'GET' in message or 'POST' in message:
parts = message.split(' ')
for i, part in enumerate(parts):
if part.startswith('/'):
url = part
if '?' in url:
url = url.split('?')[0]
parts[i] = url
record.msg = ' '.join(parts)
record.args = () # Clear args to avoid formatting errors
return True

app = Flask(__name__)
init_db(app)

pre_prompt = []

def load_prompt(file_path):
global pre_prompt
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
content = ''.join(line.strip() for line in lines)
pre_prompt.append({"role": "system", "content": content})

def load_all_prompts(directory):
for filename in os.listdir(directory):
filepath = os.path.join(directory, filename)
if os.path.isfile(filepath):
load_prompt(filepath)

load_all_prompts('prompts')

class ChatBot:
def __init__(self, api_key, base_url, model, messages):
self.client = OpenAI(api_key=api_key, base_url=base_url)
self.model = model
self.messages = messages

def reply(self):
try:
completion = self.client.chat.completions.create(
model=self.model,
messages=self.messages,
stream=True # Enable streaming output
)
except:
return None
return completion

@app.route('/')
def index():
return send_from_directory('.', 'index.html')

@app.route('/chat', methods=['POST', 'GET'])
def chat():
user_id = request.args.get('user_id')
api_key = request.args.get('api_key')
base_url = request.args.get('base_url')
hidden = request.args.get('hidden', 'false').lower() == 'true'
time_param = request.args.get('time')

if request.method == 'POST':
data = request.get_json()
message = data.get('message')

if not user_id:
return jsonify({'status': 'error', 'message': 'Missing user_id'}), 400

if not api_key or not base_url:
return jsonify({'status': 'error', 'message': 'Missing required parameters'}), 400

chat_log_entry = get_chat_log(user_id)
if not chat_log_entry:
chat_log_entry = ChatLog(user_id=user_id, chat_log=json.dumps([]))

chat_log = json.loads(chat_log_entry.chat_log)

chat_log.append({'role': 'user', 'content': message})

save_chat_log(user_id, chat_log)

return jsonify({'status': 'success', 'message': 'Message sent to bot'}), 200

elif request.method == 'GET':
chat_log_entry = get_chat_log(user_id)

if not chat_log_entry:
return jsonify({'status': 'error', 'message': 'No chat log found for user'}), 404

chat_log = json.loads(chat_log_entry.chat_log)

if "HTTP_PROXY" in os.environ:
os.environ["ALL_PROXY"] = os.environ["HTTPS_PROXY"]
os.environ["all_proxy"] = os.environ["https_proxy"]

model = "gpt-4o"
order = [0, 1]
if time_param:
timedate=f"{time_param[:4]}-{time_param[4:6]}-{time_param[6:8]} {time_param[8:10]}:{time_param[10:12]}"
else:
timedate = time.strftime("%Y-%m-%d %H:%M")
timeprompt={"role":"system","content":"The current datetime is \
"+ timedate + ". Use this information as if you can access the real \
datetime. Note that as the time can change, always use the time in \
this message as the current time. Do not use the time from any other \
messages because they can be outdated."}
full_log = pre_prompt + [timeprompt] + chat_log
bot = ChatBot(api_key=api_key, base_url=base_url, model=model, messages=full_log)
if hidden:
original_chat_log = chat_log[:-1]
save_chat_log(user_id, original_chat_log)
completion = bot.reply()
if completion is None:
return jsonify({'status': 'error', 'message': 'Bot response error'}), 404
def generate():
for chunk in completion:
if not chunk.choices:
continue
if chunk.choices[0].delta.content is None:
break
yield f"data: {json.dumps({'content': str(chunk.choices[0].delta.content), 'end': False})}\n\n"
yield f"data: {json.dumps({'content': '', 'end': True})}\n\n"

return Response(generate(), content_type='text/event-stream')

@app.route('/clear_memory', methods=['POST'])
def clear_memory():
try:
user_id = request.args.get('user_id')
if not user_id:
return jsonify({'status': 'error', 'message': 'User ID is required'}), 400

num_rows_deleted = delete_chat_log(user_id)
return jsonify({'status': 'success', 'message': f'{num_rows_deleted} records deleted for user {user_id}'}), 200
except Exception as e:
db.session.rollback()
return jsonify({'status': 'error', 'message': str(e)}), 500

@app.route('/bot_message', methods=['POST'])
def bot():
try:
user_id=request.args.get('user_id')
data = request.get_json()
message = data.get('message')

if not user_id:
return jsonify({'status': 'error', 'message': 'Missing user_id'}), 400

chat_log_entry = get_chat_log(user_id)
if not chat_log_entry:
return jsonify({'status': 'error', 'message': 'No chat log found for user'}), 404

chat_log = json.loads(chat_log_entry.chat_log)
chat_log.append({'role': 'assistant', 'content': message})

save_chat_log(user_id, chat_log)

return jsonify({'status': 'success', 'message': 'Bot message saved'}), 200
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500

@app.route('/sync_messages', methods=['GET'])
def sync_messages():
try:
user_id = request.args.get('user_id')
if not user_id:
return jsonify({'status': 'error', 'message': 'Missing user_id'}), 400

chat_log_entry = get_chat_log(user_id)
if not chat_log_entry:
return jsonify({'status': 'error', 'message': 'No chat log found for user'}), 404

chat_log = json.loads(chat_log_entry.chat_log)
return jsonify({'status': 'success', 'messages': chat_log}), 200
except Exception as e:
return jsonify({'status': 'error', 'message': str(e)}), 500

@app.route('/delete_message', methods=['POST'])
def delete_message():
try:
user_id = request.args.get('user_id')
if not user_id:
return jsonify({'status': 'error', 'message': 'Missing user_id'}), 400

chat_log_entry = get_chat_log(user_id)
if not chat_log_entry:
return jsonify({'status': 'error', 'message': 'No chat log found for user'}), 404

chat_log = json.loads(chat_log_entry.chat_log)
if not chat_log:
return jsonify({'status': 'error', 'message': 'Chat log is empty'}), 400

chat_log.pop() # Remove the last message
save_chat_log(user_id, chat_log)

return jsonify({'status': 'success', 'message': 'Last message deleted'}), 200
except Exception as e:
db.session.rollback()
return jsonify({'status': 'error', 'message': str(e)}), 500

@app.route('/validate_uuid', methods=['GET'])
def validate_uuid():
user_id = request.args.get('user_id')
if not user_id:
return jsonify({'status': 'error', 'message': 'Missing user_id'}), 400

chat_log_entry = get_chat_log(user_id)
if chat_log_entry:
return jsonify({'status': 'success', 'message': 'UUID is valid'}), 200
else:
return jsonify({'status': 'error', 'message': 'UUID not found'}), 404

@app.route('/search_chat', methods=['POST'])
def search_chat():
data = request.get_json()

if not data or 'message' not in data or 'prompt' not in data:
return jsonify({'status': 'error', 'message': 'Missing Data'}), 400

prompts = data.get('prompt')
prompts = prompts.split('\n')
prompts = [prompt[4:] for prompt in prompts]

concats = []
for i in range(0, len(prompts)):
prompt=prompts[i]
if "DUCK_PROXY" in os.environ:
duck=DDGS(proxy=os.environ["DUCK_PROXY"])
else:
duck=DDGS()
results = duck.text(prompt, max_results=10)
bodies = [result['body'] for result in results]
titles = [result['title'] for result in results]
concat = ''.join(f"{id} -- {title}:{body}\n\n" for id, title, body in zip(range(1, len(bodies)+1), titles, bodies))
concat= "Search query " + prompt + ":\n\n" + concat
concats.append(concat)

full= '\n\n'.join(concats)
full_escaped = full.replace('"', '\\"')

answer = (
"This is a system message sent in the user's name. "
"The user's last message requires internet search. "
"The system has searched and got the following results. "
"Answer the user's message with the searched information: "
f"{full_escaped} Please do not reply to this message "
"but to the previous message by the user. Reply in the"
"user's language."
)

return jsonify({'status': 'success','answer': answer}), 200


if __name__ == '__main__':
log = logging.getLogger('werkzeug')
log.addFilter(SensitiveInfoFilter())
app.run(host="0.0.0.0", port=7788)
55 changes: 55 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
## Current Version:

[![](https://img.shields.io/badge/Dev-3.2.3-blue)](https://github.com/Davidasx/flask-gpt/tree/dev)
[![](https://img.shields.io/badge/Main-3.2.3-red)](https://github.com/Davidasx/flask-gpt/tree/main)

## Update Log (only after 3.1.0):

### 3.2.3

#### [![](https://img.shields.io/badge/Dev/Main-20241028-purple)](https://github.com/Davidasx/flask-gpt/)

Merged with main branch. Tested OK.

Fixed bug when searching. Previously, all searches will fail after a single failed search.

### 3.2.2

#### ![](https://img.shields.io/badge/Dev-20241028-blue)
Broke system prompts into separate files. You can edit them yourself to customize the chatbot.

### 3.2.1

#### ![](https://img.shields.io/badge/Dev-20241028-blue)

Improved language support and local time support.

Now the chatbot will tell you the local time, not the UTC time!

### 3.2.0

#### ![](https://img.shields.io/badge/Dev-20241028-blue)

Added multiple languages. Improved system prompt.

### 3.1.2

#### ![](https://img.shields.io/badge/Dev/Main-20241028-purple)

Fixed system message hiding bug. Fixed system prompt.

### 3.1.1

#### ![](https://img.shields.io/badge/Dev/Main-20241027-purple)

Added timedate support.

### 3.1.0

#### ![](https://img.shields.io/badge/Dev/Main-20241027-purple)

Fixes search feature. Now you can set `DUCK_PROXY` environment variable to use proxy for DuckDuckGo.

Set default language to English to allow for international users.

Will add more languages soon.
Loading

0 comments on commit c0bea4f

Please sign in to comment.