Skip to content

Commit 9b5e227

Browse files
committed
9 Sep 2023
Installer default to "GPU", "borrowed" accelerate snippet from Oobabooga. Should load and run a little faster now.
1 parent 11dfdcf commit 9b5e227

File tree

12 files changed

+144
-98
lines changed

12 files changed

+144
-98
lines changed

ai chatbot/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,12 @@ https://code-boxx.com/core-boxx-ai-chatbot/
55
* [Core Boxx](https://github.com/code-boxx/Core-Boxx-PHP-Framework/tree/main/core)
66
* [Python](https://www.python.org/) At the time of writing, 3.9~3.10 works fine.
77
* [Microsoft C++ Build Tools](https://visualstudio.microsoft.com/downloads/?q=build+tools)
8+
* A decent graphics card. Even if you tweak and run with CPU-only, it will be painfully slow...
89

910
## INSTALLATION
1011
* Copy/unzip this module into your existing Core Boxx project folder.
1112
* Put documents you want the AI to "learn" into `chatbot/docs`, accepted file types - `csv pdf txt epub html md odt doc docx ppt pptx`.
12-
* Run setup - *BE WARNED, SEVERAL GIGABYTES WORTH OF DOWNLOAD!*
13-
* Windows - Run `0-setup.bat` for "CPU only", or `0-setup.bat GPU` if you have an Nvidia graphics card.
14-
* Linux - Run `0-setup.sh` for "CPU only", or `0-setup.sh GPU` if you have an Nvidia graphics card.
13+
* Run `0-setup.bat` (Windows) `0-setup.sh` (Linux) - *BE WARNED, SEVERAL GIGABYTES WORTH OF DOWNLOAD!*
1514
* Access `http://your-site.com/ai/` for the demo.
1615

1716
## NOTES

ai chatbot/chatbot/0-setup.bat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
php 0-setup.php %1
1+
php 0-setup.php
22
virtualenv venv
33
call venv\Scripts\activate
44
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask pyjwt
5-
if "%1"=="GPU" (
6-
pip install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cu117
7-
) else (
5+
if "%1"=="CPU" (
86
pip install torch torchvision torchaudio --force-reinstall
7+
) else (
8+
pip install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cu117
99
)
10-
python create.py
11-
python bot.py
10+
python b_create.py
11+
python d_bot.py

ai chatbot/chatbot/0-setup.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,30 @@
11
<?php
2-
// (A) KOA SUTATO
3-
require dirname(__DIR__) . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "CORE-Go.php";
2+
// (A) RODO KOA KONFIGU
3+
require dirname(__DIR__) . DIRECTORY_SEPARATOR . "lib" . DIRECTORY_SEPARATOR . "CORE-Config.php";
44

55
// (B) NEW CHATBOT PATH
66
define("PATH_CHATBOT", PATH_BASE . "chatbot" . DIRECTORY_SEPARATOR);
77

8-
// (C) BACKUP CHATBOT/SETTINGS.PY
9-
if (!copy(PATH_CHATBOT . "settings.py", PATH_CHATBOT . "settings.old")) {
10-
exit("Failed to backup settings file - " . PATH_CHATBOT . "settings.old");
8+
// (C) BACKUP CHATBOT/A_SETTINGS.PY
9+
if (!copy(PATH_CHATBOT . "a_settings.py", PATH_CHATBOT . "a_settings.old")) {
10+
exit("Failed to backup settings file - " . PATH_CHATBOT . "a_settings.old");
1111
}
1212

13-
// (D) COPY HOST SETTINGS FROM CORE-CONFIG.PHP TO SETTINGS.PY
13+
// (D) COPY SETTINGS FROM CORE-CONFIG.PHP TO A_SETTINGS.PY
1414
$replace = [
15-
"model_name" => isset($argv[1]) && $argv[1]=="GPU"
16-
? '"TheBloke/Wizard-Vicuna-7B-Uncensored-GPTQ"'
17-
: '"TheBloke/Wizard-Vicuna-7B-Uncensored-GGML"',
1815
"http_allow" => "[\"http://".HOST_NAME."\", \"https://".HOST_NAME."\"]",
1916
"http_host" => "\"".HOST_NAME."\"",
2017
"jwt_algo" => "\"".JWT_ALGO."\"",
2118
"jwt_secret" => "\"".JWT_SECRET."\""
2219
];
23-
$cfg = file(PATH_CHATBOT . "settings.py") or exit("Cannot read". PATH_CHATBOT ."settings.py");
20+
$cfg = file(PATH_CHATBOT . "a_settings.py") or exit("Cannot read". PATH_CHATBOT ."a_settings.py");
2421
foreach ($cfg as $j=>$line) { foreach ($replace as $k=>$v) { if (strpos($line, $k) !== false) {
2522
$cfg[$j] = "$k = $v # CHANGED BY INSTALLER\r\n";
2623
unset($replace[$k]);
2724
if (count($replace)==0) { break; }
2825
}}}
29-
try { file_put_contents(PATH_CHATBOT . "settings.py", implode("", $cfg)); }
30-
catch (Exception $ex) { exit("Error writing to ". PATH_CHATBOT . "settings.py"); }
26+
try { file_put_contents(PATH_CHATBOT . "a_settings.py", implode("", $cfg)); }
27+
catch (Exception $ex) { exit("Error writing to ". PATH_CHATBOT . "a_settings.py"); }
3128

3229
// (E) ADD AI TO CORE-CONFIG.PHP
3330
try {

ai chatbot/chatbot/0-setup.sh

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
php 0-setup.php $1
1+
php 0-setup.php
22
virtualenv venv
33
source "venv/bin/activate"
44
pip install langchain transformers optimum auto-gptq chromadb sentence_transformers Flask pyjwt
5-
if [[ $1 == "GPU" ]]
5+
if [[ $1 == "CPU" ]]
66
then
7-
pip3 install torch torchvision torchaudio --force-reinstall
7+
pip install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cpu
88
else
9-
pip3 install torch torchvision torchaudio --force-reinstall --index-url https://download.pytorch.org/whl/cpu
9+
pip install torch torchvision torchaudio --force-reinstall
1010
fi
11-
python create.py
12-
python bot.py
11+
python b_create.py
12+
python d_bot.py

ai chatbot/chatbot/1-create.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@echo off
22
call venv\Scripts\activate
3-
python create.py
3+
python b_create.py
44
deactivate

ai chatbot/chatbot/1-create.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
source "venv/bin/activate"
2-
python create.py
2+
python b_create.py
33
deactivate

ai chatbot/chatbot/2-bot.bat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
@echo off
22
call venv\Scripts\activate
3-
python bot.py
3+
python d_bot.py
44
deactivate

ai chatbot/chatbot/2-bot.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
source "venv/bin/activate"
2-
python bot.py
2+
python d_bot.py
33
deactivate
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,50 @@
1-
# (A) PATHS
1+
# (A) PATH
22
import os
33
path_base = os.path.dirname(os.path.realpath(__file__))
44
path_models = os.path.join(path_base, "models")
55
path_db = os.path.join(path_base, "db")
66
path_docs = os.path.join(path_base, "docs")
7+
8+
# (B) ENVIRONMENT VARIABLES
9+
os.environ["HF_HUB_DISABLE_SYMLINKS_WARNING"] = "true"
710
os.environ["TRANSFORMERS_CACHE"] = path_models
811

9-
# (B) MODEL
10-
model_name = "TheBloke/Wizard-Vicuna-7B-Uncensored-GPTQ"
11-
model_sample = True
12-
model_max_tokens = 1024
13-
model_batch_size = 1
14-
model_temperature = 0.7
15-
model_top_p = 1
16-
model_top_k = 40
17-
model_sequences = 1
12+
# (C) MODEL SETTINGS
13+
model_name = "TheBloke/vicuna-7B-v1.5-GPTQ"
14+
model_args = {
15+
"do_sample" : True,
16+
"max_new_tokens" : 3000,
17+
"batch_size" : 1,
18+
"temperature" : 0.7,
19+
"top_k" : 40,
20+
"top_p" : 1,
21+
"num_return_sequences" : 1
22+
}
23+
24+
# (D) CHAIN SETTINGS
25+
chain_args = {
26+
"chain_type" : "stuff",
27+
"return_source_documents" : True,
28+
"verbose" : True
29+
}
1830

19-
# (C) PROMPT TEMPLATE
31+
# (E) PROMPT TEMPLATE
2032
prompt_template = """SYSTEM: Use the following context section and only that context to answer the question at the end. Do not use your internal knowledge. If you don't know the answer, just say that you don't know, don't try to make up an answer.
2133
CONTEXT: {context}
2234
USER: {question}
2335
ANSWER:"""
2436

25-
# (D) CHAIN
26-
chain_verbose = True
27-
chain_type = "stuff"
28-
chain_kwargs = 4
29-
chain_source = True
30-
31-
# (E) DATABASE
32-
doc_chunks = 512
33-
doc_overlap = 30
37+
# (F) DATABASE - DOCUMENT SPLITTER
38+
db_split = {
39+
"chunk_size" : 512,
40+
"chunk_overlap" : 30
41+
}
3442

35-
# (F) HTTP ENDPOINT
43+
# (G) HTTP ENDPOINT
3644
http_allow = ["http://localhost"]
3745
http_host = "localhost"
3846
http_port = 8008
3947

40-
# (G) JWT
48+
# (H) JWT
4149
jwt_algo = ""
4250
jwt_secret = ""
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# (A) LOAD SETTINGS & MODULES
2-
import settings as set
2+
import a_settings as set
33
import os, glob
44
from pathlib import Path
55
from langchain.vectorstores import Chroma
@@ -65,9 +65,7 @@ def rmdir(folder):
6565
db.persist()
6666

6767
# (D2) ADD DOCUMENTS
68-
splitter = RecursiveCharacterTextSplitter(
69-
chunk_size = set.doc_chunks, chunk_overlap = set.doc_overlap
70-
)
68+
splitter = RecursiveCharacterTextSplitter(**set.db_split)
7169
for doc in all:
7270
print("Adding - " + doc)
7371
name, ext = os.path.splitext(doc)

0 commit comments

Comments
 (0)