Salt
commited on
Commit
·
5828964
1
Parent(s):
eb03bf9
Delete server.py
Browse files
server.py
DELETED
@@ -1,157 +0,0 @@
|
|
1 |
-
from flask import (
|
2 |
-
Flask,
|
3 |
-
jsonify,
|
4 |
-
request,
|
5 |
-
render_template_string,
|
6 |
-
abort,
|
7 |
-
)
|
8 |
-
from flask_cors import CORS
|
9 |
-
import unicodedata
|
10 |
-
import markdown
|
11 |
-
import time
|
12 |
-
import os
|
13 |
-
import gc
|
14 |
-
import base64
|
15 |
-
from io import BytesIO
|
16 |
-
from random import randint
|
17 |
-
import hashlib
|
18 |
-
from colorama import Fore, Style, init as colorama_init
|
19 |
-
import chromadb
|
20 |
-
import posthog
|
21 |
-
from chromadb.config import Settings
|
22 |
-
from sentence_transformers import SentenceTransformer
|
23 |
-
from werkzeug.middleware.proxy_fix import ProxyFix
|
24 |
-
|
25 |
-
colorama_init()
|
26 |
-
|
27 |
-
port = 7860
|
28 |
-
host = "0.0.0.0"
|
29 |
-
|
30 |
-
embedding_model = 'sentence-transformers/all-mpnet-base-v2'
|
31 |
-
|
32 |
-
print("Initializing ChromaDB")
|
33 |
-
|
34 |
-
# disable chromadb telemetry
|
35 |
-
posthog.capture = lambda *args, **kwargs: None
|
36 |
-
chromadb_client = chromadb.Client(Settings(anonymized_telemetry=False))
|
37 |
-
chromadb_embedder = SentenceTransformer(embedding_model)
|
38 |
-
chromadb_embed_fn = chromadb_embedder.encode
|
39 |
-
|
40 |
-
# Flask init
|
41 |
-
app = Flask(__name__)
|
42 |
-
CORS(app) # allow cross-domain requests
|
43 |
-
app.config["MAX_CONTENT_LENGTH"] = 100 * 1024 * 1024
|
44 |
-
|
45 |
-
app.wsgi_app = ProxyFix(
|
46 |
-
app.wsgi_app, x_for=2, x_proto=1, x_host=1, x_prefix=1
|
47 |
-
)
|
48 |
-
|
49 |
-
def get_real_ip():
|
50 |
-
return request.remote_addr
|
51 |
-
|
52 |
-
@app.route("/", methods=["GET"])
|
53 |
-
def index():
|
54 |
-
with open("./README.md", "r", encoding="utf8") as f:
|
55 |
-
content = f.read()
|
56 |
-
return render_template_string(markdown.markdown(content, extensions=["tables"]))
|
57 |
-
|
58 |
-
|
59 |
-
@app.route("/api/modules", methods=["GET"])
|
60 |
-
def get_modules():
|
61 |
-
return jsonify({"modules": ['chromadb']})
|
62 |
-
|
63 |
-
@app.route("/api/chromadb", methods=["POST"])
|
64 |
-
def chromadb_add_messages():
|
65 |
-
data = request.get_json()
|
66 |
-
if "chat_id" not in data or not isinstance(data["chat_id"], str):
|
67 |
-
abort(400, '"chat_id" is required')
|
68 |
-
if "messages" not in data or not isinstance(data["messages"], list):
|
69 |
-
abort(400, '"messages" is required')
|
70 |
-
|
71 |
-
ip = get_real_ip()
|
72 |
-
chat_id_md5 = hashlib.md5(f'{ip}-{data["chat_id"]}'.encode()).hexdigest()
|
73 |
-
collection = chromadb_client.get_or_create_collection(
|
74 |
-
name=f"chat-{chat_id_md5}", embedding_function=chromadb_embed_fn
|
75 |
-
)
|
76 |
-
|
77 |
-
documents = [m["content"] for m in data["messages"]]
|
78 |
-
ids = [m["id"] for m in data["messages"]]
|
79 |
-
metadatas = [
|
80 |
-
{"role": m["role"], "date": m["date"], "meta": m.get("meta", "")}
|
81 |
-
for m in data["messages"]
|
82 |
-
]
|
83 |
-
|
84 |
-
if len(ids) > 0:
|
85 |
-
collection.upsert(
|
86 |
-
ids=ids,
|
87 |
-
documents=documents,
|
88 |
-
metadatas=metadatas,
|
89 |
-
)
|
90 |
-
|
91 |
-
return jsonify({"count": len(ids)})
|
92 |
-
|
93 |
-
|
94 |
-
@app.route("/api/chromadb/query", methods=["POST"])
|
95 |
-
def chromadb_query():
|
96 |
-
data = request.get_json()
|
97 |
-
if "chat_id" not in data or not isinstance(data["chat_id"], str):
|
98 |
-
abort(400, '"chat_id" is required')
|
99 |
-
if "query" not in data or not isinstance(data["query"], str):
|
100 |
-
abort(400, '"query" is required')
|
101 |
-
|
102 |
-
if "n_results" not in data or not isinstance(data["n_results"], int):
|
103 |
-
n_results = 1
|
104 |
-
else:
|
105 |
-
n_results = data["n_results"]
|
106 |
-
|
107 |
-
ip = get_real_ip()
|
108 |
-
chat_id_md5 = hashlib.md5(f'{ip}-{data["chat_id"]}'.encode()).hexdigest()
|
109 |
-
collection = chromadb_client.get_or_create_collection(
|
110 |
-
name=f"chat-{chat_id_md5}", embedding_function=chromadb_embed_fn
|
111 |
-
)
|
112 |
-
|
113 |
-
n_results = min(collection.count(), n_results)
|
114 |
-
|
115 |
-
messages = []
|
116 |
-
if n_results > 0:
|
117 |
-
query_result = collection.query(
|
118 |
-
query_texts=[data["query"]],
|
119 |
-
n_results=n_results,
|
120 |
-
)
|
121 |
-
|
122 |
-
documents = query_result["documents"][0]
|
123 |
-
ids = query_result["ids"][0]
|
124 |
-
metadatas = query_result["metadatas"][0]
|
125 |
-
distances = query_result["distances"][0]
|
126 |
-
|
127 |
-
messages = [
|
128 |
-
{
|
129 |
-
"id": ids[i],
|
130 |
-
"date": metadatas[i]["date"],
|
131 |
-
"role": metadatas[i]["role"],
|
132 |
-
"meta": metadatas[i]["meta"],
|
133 |
-
"content": documents[i],
|
134 |
-
"distance": distances[i],
|
135 |
-
}
|
136 |
-
for i in range(len(ids))
|
137 |
-
]
|
138 |
-
|
139 |
-
return jsonify(messages)
|
140 |
-
|
141 |
-
@app.route("/api/chromadb/purge", methods=["POST"])
|
142 |
-
def chromadb_purge():
|
143 |
-
data = request.get_json()
|
144 |
-
if "chat_id" not in data or not isinstance(data["chat_id"], str):
|
145 |
-
abort(400, '"chat_id" is required')
|
146 |
-
|
147 |
-
ip = get_real_ip()
|
148 |
-
chat_id_md5 = hashlib.md5(f'{ip}-{data["chat_id"]}'.encode()).hexdigest()
|
149 |
-
collection = chromadb_client.get_or_create_collection(
|
150 |
-
name=f"chat-{chat_id_md5}", embedding_function=chromadb_embed_fn
|
151 |
-
)
|
152 |
-
|
153 |
-
deleted = collection.delete()
|
154 |
-
print("ChromaDB embeddings deleted", len(deleted))
|
155 |
-
|
156 |
-
return 'Ok', 200
|
157 |
-
app.run(host=host, port=port)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|