Skip to content

Commit bd66f4a

Browse files
committed
8 Sep 2023 - Optional user checks for AI Chatbot
1 parent b2e5145 commit bd66f4a

File tree

7 files changed

+55
-18
lines changed

7 files changed

+55
-18
lines changed

ai chatbot/chatbot/0-setup.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
php 0-setup.php
22
virtualenv venv
33
call venv\Scripts\activate
4-
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask
4+
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask pyjwt
55
if "%1"=="GPU" (
66
pip install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cu117
77
) else (

ai chatbot/chatbot/0-setup.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
// (D) COPY HOST SETTINGS FROM CORE-CONFIG.PHP TO SETTINGS.PY
1414
$replace = [
1515
"http_allow" => "[\"http://".HOST_NAME."\", \"https://".HOST_NAME."\"]",
16-
"http_host" => "\"".HOST_NAME."\""
16+
"http_host" => "\"".HOST_NAME."\"",
17+
"jwt_algo" => "\"".JWT_ALGO."\"",
18+
"jwt_secret" => "\"".JWT_SECRET."\""
1719
];
1820
$cfg = file(PATH_CHATBOT . "settings.py") or exit("Cannot read". PATH_CHATBOT ."settings.py");
1921
foreach ($cfg as $j=>$line) { foreach ($replace as $k=>$v) { if (strpos($line, $k) !== false) {

ai chatbot/chatbot/0-setup.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
php 0-setup.php
22
virtualenv venv
33
source "venv/bin/activate"
4-
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask
4+
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask pyjwt
55
if [[ $1 == "GPU" ]]
66
then
77
pip3 install torch torchvision torchaudio --force-reinstall

ai chatbot/chatbot/bot.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# (A) LOAD SETTINGS & MODULES
22
import settings as set
33
from flask import Flask, Response, request
4-
import torch
4+
import torch, jwt
55
from transformers import AutoTokenizer, AutoModelForCausalLM, pipeline
66
from langchain import PromptTemplate, HuggingFacePipeline
77
from langchain.vectorstores import Chroma
@@ -19,8 +19,6 @@
1919
persist_directory = set.path_db,
2020
embedding_function = HuggingFaceEmbeddings()
2121
)
22-
pipe = ""
23-
chain = ""
2422

2523
# (C) PIPE + CHAIN
2624
pipe = pipeline(
@@ -50,24 +48,53 @@
5048
verbose = set.chain_verbose
5149
)
5250

53-
# (D) FLASK
51+
""" @TODO - ENABLE THIS TO OPEN FOR REGISTERED USERS ONLY
52+
# (D) VERIFY USER
53+
def jwtVerify(cookies):
54+
try:
55+
token = jwt.decode(
56+
jwt = cookies.get("cbsess"),
57+
key = set.jwt_secret,
58+
audience = set.http_host,
59+
algorithms = [set.jwt_algo]
60+
)
61+
# DO WHATEVER YOU WANT WITH THE DECODED USER TOKEN
62+
# print(token)
63+
return True
64+
except Exception as error:
65+
# print(error)
66+
return False
67+
"""
68+
69+
# (E) FLASK
5470
app = Flask(__name__)
55-
@app.route("/", methods=["POST"])
71+
@app.route("/", methods = ["POST"])
5672
def bot():
73+
# (E1) CORS
5774
if "HTTP_ORIGIN" in request.environ and request.environ["HTTP_ORIGIN"] in set.http_allow:
75+
# (E2-1) ALLOW ONLY REGISTERED USERS
76+
""" @TODO - ENABLE THIS TO OPEN FOR REGISTERED USERS ONLY
77+
if jwtVerify(request.cookies) is False:
78+
return Response("Not Allowed", status = 405)
79+
"""
80+
81+
# (E2-2) ANSWER THE QUESTION
5882
data = dict(request.form)
5983
if "query" in data:
6084
ans = chain(data["query"])
6185
ans = ans["result"]
6286
else:
6387
ans = "Where's the question, yo?"
64-
response = Response(ans, status=200)
88+
response = Response(ans, status = 200)
6589
response.headers.add("Access-Control-Allow-Origin", request.environ["HTTP_ORIGIN"] )
6690
response.headers.add("Access-Control-Allow-Credentials", "true")
91+
92+
# (E2) ORIGIN NOT ALLOWED
6793
else:
68-
response = Response("Not Allowed", status=405)
94+
response = Response("Not Allowed", status = 405)
6995
return response
7096

97+
# (F) GO!
7198
if __name__ == "__main__":
7299
app.run(
73100
host = set.http_host,

ai chatbot/chatbot/settings.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,8 @@
3535
# (F) HTTP ENDPOINT
3636
http_allow = ["http://localhost"]
3737
http_host = "localhost"
38-
http_port = 8008
38+
http_port = 8008
39+
40+
# (G) JWT
41+
jwt_algo = ""
42+
jwt_secret = ""

ai chatbot/pages/PAGE-ai.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,22 @@
11
<?php
2-
// (A) PAGE META
2+
// (A) FOR REGISTERED USERS ONLY
3+
// @TODO - ENABLE THIS TO OPEN FOR REGISTERED USERS ONLY
4+
// $_CORE->ucheck();
5+
6+
// (B) PAGE META
37
$_PMETA = ["load" => [
48
["l", HOST_ASSETS."PAGE-ai.css"],
59
["s", HOST_ASSETS."PAGE-ai.js", "defer"]
610
]];
711

8-
// (B) HTML PAGE
12+
// (C) HTML PAGE
913
require PATH_PAGES . "TEMPLATE-top.php"; ?>
1014
<script>const AIEP = "<?=HOST_CHATBOT?>";</script>
1115
<div id="ai-wrap">
12-
<!-- (B1) CHAT HISTORY -->
16+
<!-- (C1) CHAT HISTORY -->
1317
<div id="ai-chat"></div>
1418

15-
<!-- (B2) QUERY -->
19+
<!-- (C2) QUERY -->
1620
<form id="ai-query" class="d-flex align-items-stretch head border p-2 w-100" onsubmit="return chat.send()">
1721
<input type="text" id="ai-txt" placeholder="Question"
1822
class="form-control form-control-sm" autocomplete="off" required disabled>

core/lib/LIB-Session.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ class Session extends Core {
55
"domain" => HOST_NAME,
66
"path" => "/",
77
"httponly" => true,
8-
"expires" => 0
9-
// "secure" => true,
10-
// "samesite" => "None"
8+
"expires" => 0,
9+
"samesite" => "Lax",
10+
// "secure" => true
1111
];
1212

1313
// (B) CONSTRUCTOR - AUTO VALIDATE JWT COOKIE & RESTORE SESSION DATA

0 commit comments

Comments
 (0)