Merge branch 'main' into chat-box

This commit is contained in:
andzejsp 2023-05-29 21:28:53 +03:00
commit ac21d758a7
17 changed files with 757 additions and 147 deletions

View File

@ -368,7 +368,10 @@ class ModelProcess:
return output
def _callback(self, text, text_type=0):
print(text,end="", flush=True)
try:
print(str(text),end="", flush=True)
except:
print(".")
self.curent_text += text
detected_anti_prompt = False
anti_prompt_to_remove=""
@ -501,7 +504,10 @@ class GPT4AllAPI():
def callback(progress):
socketio.emit('install_progress',{'status': 'progress', 'progress': progress})
self.download_file(model_path, installation_path, callback)
if hasattr(self.binding, "download_model"):
self.binding.download_model(model_path, installation_path, callback)
else:
self.download_file(model_path, installation_path, callback)
socketio.emit('install_progress',{'status': 'succeeded', 'error': ''})
tpe = threading.Thread(target=install_model_, args=())
tpe.start()

View File

@ -274,6 +274,36 @@ class DiscussionsDB:
)
discussions.append(discussion)
return discussions
def import_from_json(self, json_data):
discussions = []
data = json_data
for discussion_data in data:
discussion_id = discussion_data.get("id")
discussion_title = discussion_data.get("title")
messages_data = discussion_data.get("messages", [])
discussion = {"id": discussion_id, "title": discussion_title, "messages": []}
# Insert discussion into the database
self.insert("INSERT INTO discussion (id, title) VALUES (?, ?)", (discussion_id, discussion_title))
for message_data in messages_data:
sender = message_data.get("sender")
content = message_data.get("content")
content_type = message_data.get("type")
rank = message_data.get("rank")
parent = message_data.get("parent")
discussion["messages"].append(
{"sender": sender, "content": content, "type": content_type, "rank": rank, "parent": parent}
)
# Insert message into the database
self.insert("INSERT INTO message (sender, content, type, rank, parent, discussion_id) VALUES (?, ?, ?, ?, ?, ?)",
(sender, content, content_type, rank, parent, discussion_id))
discussions.append(discussion)
return discussions
class Discussion:
def __init__(self, discussion_id, discussions_db:DiscussionsDB):

View File

@ -1 +1,84 @@
# TODO : implement
try:
from langchain.llms.base import LLM
except ImportError:
raise ImportError(
'To use the ctransformers.langchain module, please install the '
'`langchain` python package: `pip install langchain`')
from typing import Any, Dict, Optional, Sequence
from pydantic import root_validator
from langchain.callbacks.manager import CallbackManagerForLLMRun
from api.binding import LLMBinding
class GenericBinding(LLM):
"""Wrapper around All compatible LLM interfaces.
Thanks to Marella for providing the base for this work.
To follow him, here is his github profile:
To use, you should have the `langchain` python package installed.
"""
client: Any #: :meta private:
model: str
"""The path to a model file or directory or the name of a Hugging Face Hub
model repo."""
model_type: Optional[str] = None
"""The model type."""
model_file: Optional[str] = None
"""The name of the model file in repo or directory."""
config: Optional[Dict[str, Any]] = None
"""The config parameters."""
lib: Optional[Any] = None
"""The path to a shared library or one of `avx2`, `avx`, `basic`."""
@property
def _identifying_params(self) -> Dict[str, Any]:
"""Get the identifying parameters."""
return {
'model': self.model,
'model_type': self.model_type,
'model_file': self.model_file,
'config': self.config,
}
@property
def _llm_type(self) -> str:
"""Return type of llm."""
return 'generic_binding'
@root_validator()
def validate_environment(cls, values: Dict) -> Dict:
"""Validate and load model from a local file or remote repo."""
config = values['config'] or {}
values['client'] = LLMBinding(config, True)
return values
def _call(
self,
prompt: str,
stop: Optional[Sequence[str]] = None,
run_manager: Optional[CallbackManagerForLLMRun] = None,
) -> str:
"""Generate text from a prompt.
Args:
prompt: The prompt to generate text from.
stop: A list of sequences to stop generation when encountered.
Returns:
The generated text.
"""
text = []
for chunk in self.client(prompt, stop=stop, stream=True):
text.append(chunk)
if run_manager:
run_manager.on_llm_new_token(chunk, verbose=self.verbose)
return ''.join(text)

9
app.py
View File

@ -232,6 +232,11 @@ class Gpt4AllWebUI(GPT4AllAPI):
self.add_endpoint(
"/export_multiple_discussions", "export_multiple_discussions", self.export_multiple_discussions, methods=["POST"]
)
self.add_endpoint(
"/import_multiple_discussions", "import_multiple_discussions", self.import_multiple_discussions, methods=["POST"]
)
def export_multiple_discussions(self):
data = request.get_json()
@ -239,6 +244,10 @@ class Gpt4AllWebUI(GPT4AllAPI):
discussions = self.db.export_discussions_to_json(discussion_ids)
return jsonify(discussions)
def import_multiple_discussions(self):
data = request.get_json()
discussions = data
return jsonify(discussions)
def reset(self):
os.kill(os.getpid(), signal.SIGINT) # Send the interrupt signal to the current process

View File

@ -24,6 +24,7 @@ __copyright__ = "Copyright 2023, "
__license__ = "Apache 2.0"
binding_name = "GPT4ALL"
from gpt4all import GPT4All
class GPT4ALL(LLMBinding):
file_extension='*.bin'
@ -64,6 +65,8 @@ class GPT4ALL(LLMBinding):
str: The detokenized text as a string.
"""
return None
def generate(self,
prompt:str,
n_predict: int = 128,
@ -79,8 +82,19 @@ class GPT4ALL(LLMBinding):
verbose (bool, optional): If true, the code will spit many informations about the generation process. Defaults to False.
"""
try:
output = ""
for tok in self.model.generate(prompt,
response_tokens = []
def local_callback(token_id, response):
decoded_token = response.decode('utf-8')
response_tokens.append( decoded_token );
if new_text_callback is not None:
if not new_text_callback(''.join(response_tokens)):
return False
# Do whatever you want with decoded_token here.
return True
self.model._response_callback = local_callback
self.model.generate(prompt,
n_predict=n_predict,
temp=self.config['temperature'],
top_k=self.config['top_k'],
@ -88,15 +102,11 @@ class GPT4ALL(LLMBinding):
repeat_penalty=self.config['repeat_penalty'],
repeat_last_n = self.config['repeat_last_n'],
# n_threads=self.config['n_threads'],
streaming=True,
):
output += tok
if new_text_callback is not None:
if not new_text_callback(tok):
return output
streaming=False,
)
except Exception as ex:
print(ex)
return output
return ''.join(response_tokens)
@staticmethod
def get_available_models():

View File

@ -32,7 +32,7 @@ class Install:
subprocess.run(["pip", "install", "--no-cache-dir", "-r", str(requirements_file)])
# Create ther models folder
models_folder = Path("./models/c_transformers")
models_folder = Path("./models/gpt_4all")
models_folder.mkdir(exist_ok=True, parents=True)
#Create the install file

View File

@ -14,6 +14,15 @@ from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
from api.binding import LLMBinding
import torch
import yaml
import requests
from tqdm import tqdm
import os
import requests
from tqdm import tqdm
from bs4 import BeautifulSoup
import concurrent.futures
import wget
__author__ = "parisneo"
__github__ = "https://github.com/ParisNeo/GPTQ_binding"
@ -33,10 +42,14 @@ class GPTQ(LLMBinding):
super().__init__(config, False)
self.model_dir = f'{config["model"]}'
pretrained_model_dir = "facebook/opt-125m"
quantized_model_dir = "opt-125m-4bit"
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True)
# load quantized model to the first GPU
self.model = AutoGPTQForCausalLM.from_quantized(self.model_dir)
# load quantized model, currently only support cpu or single gpu
self.model = AutoGPTQForCausalLM.from_pretrained(self.model_dir, BaseQuantizeConfig())
self.tokenizer = AutoTokenizer.from_pretrained(self.model_dir, use_fast=True )
def tokenize(self, prompt):
"""
Tokenizes the given prompt using the model's tokenizer.
@ -47,7 +60,7 @@ class GPTQ(LLMBinding):
Returns:
list: A list of tokens representing the tokenized prompt.
"""
return None
return self.tokenizer.tokenize(prompt)
def detokenize(self, tokens_list):
"""
@ -59,7 +72,7 @@ class GPTQ(LLMBinding):
Returns:
str: The detokenized text as a string.
"""
return None
return self.tokenizer.decode(tokens_list)
def generate(self,
prompt:str,
n_predict: int = 128,
@ -82,7 +95,7 @@ class GPTQ(LLMBinding):
"""
self.model.reset()
for tok in self.model.generate(prompt,
n_predict=n_predict,
n_predict=n_predict,
temp=self.config['temp'],
top_k=self.config['top_k'],
top_p=self.config['top_p'],
@ -96,7 +109,59 @@ class GPTQ(LLMBinding):
except Exception as ex:
print(ex)
return output
def download_model(self, repo, base_folder, callback=None):
"""
Downloads a folder from a Hugging Face repository URL, reports the download progress using a callback function,
and displays a progress bar.
Args:
repo (str): The name of the Hugging Face repository.
base_folder (str): The base folder where the repository should be saved.
installation_path (str): The path where the folder should be saved.
callback (function, optional): A callback function to be called during the download
with the progress percentage as an argument. Defaults to None.
"""
dont_download = [".gitattributes"]
url = f"https://huggingface.co/{repo}/tree/main"
response = requests.get(url)
html_content = response.text
soup = BeautifulSoup(html_content, 'html.parser')
file_names = []
for a_tag in soup.find_all('a', {'class': 'group'}):
span_tag = a_tag.find('span', {'class': 'truncate'})
if span_tag:
file_name = span_tag.text
if file_name not in dont_download:
file_names.append(file_name)
print(f"Repo: {repo}")
print("Found files:")
for file in file_names:
print(" ", file)
dest_dir = Path(base_folder) / repo.replace("/", "_")
dest_dir.mkdir(parents=True, exist_ok=True)
os.chdir(dest_dir)
def download_file(get_file):
filename = f"https://huggingface.co/{repo}/resolve/main/{get_file}"
print(f"\nDownloading {filename}")
wget.download(filename, out=str(dest_dir), bar=callback)
with concurrent.futures.ThreadPoolExecutor() as executor:
executor.map(download_file, file_names)
os.chdir(base_folder)
installation_path = Path(installation_path)
installation_path.parent.mkdir(parents=True, exist_ok=True)
dest_dir.rename(installation_path)
print("Done")
@staticmethod
def list_models(config:dict):
"""Lists the models for this binding

View File

@ -0,0 +1,6 @@
name: AutoGPTQ
author: PanQiWei
version: 1.0
link: https://github.com/PanQiWei/AutoGPTQ
description: 'Python bindings for the GPTQ type of files
'

View File

@ -15,7 +15,6 @@ class Install:
print("This is the first time you are using this binding.")
print("Installing ...")
# Example of installing py torche
"""
try:
print("Checking pytorch")
import torch
@ -27,7 +26,6 @@ class Install:
self.reinstall_pytorch_with_cuda()
except Exception as ex:
self.reinstall_pytorch_with_cuda()
"""
# Step 2: Install dependencies using pip from requirements.txt
requirements_file = current_dir / "requirements.txt"

View File

@ -1,72 +1,8 @@
- bestGPTJ: 'true'
description: Current best commercially licensable model based on GPT-J and trained
by Nomic AI on the latest curated GPT4All dataset.
filename: ggml-gpt4all-j-v1.3-groovy.bin
filesize: '3785248281'
isDefault: 'true'
md5sum: 81a09a0ddf89690372fc296ff7f625af
- bestLlama: 'true'
description: Current best non-commercially licensable model based on Llama 13b and
trained by Nomic AI on the latest curated GPT4All dataset.
filename: ggml-gpt4all-l13b-snoozy.bin
filesize: '8136770688'
md5sum: 91f886b68fbce697e9a3cd501951e455
- bestMPT: 'true'
description: Current best non-commercially licensable chat model based on MPT and
trained by Mosaic ML.
filename: ggml-mpt-7b-chat.bin
filesize: '4854401050'
isDefault: 'true'
md5sum: 756249d3d6abe23bde3b1ae272628640
requires: 2.4.1
- description: A commercially licensable model based on GPT-J and trained by Nomic
AI on the v2 GPT4All dataset.
filename: ggml-gpt4all-j-v1.2-jazzy.bin
filesize: '3785248281'
md5sum: 879344aaa9d62fdccbda0be7a09e7976
- description: A commercially licensable model based on GPT-J and trained by Nomic
AI on the v1 GPT4All dataset.
filename: ggml-gpt4all-j-v1.1-breezy.bin
filesize: '3785248281'
md5sum: 61d48a82cb188cceb14ebb8082bfec37
- description: A commercially licensable model based on GPT-J and trained by Nomic
AI on the v0 GPT4All dataset.
filename: ggml-gpt4all-j.bin
filesize: '3785248281'
md5sum: 5b5a3f9b858d33b29b52b89692415595
- description: A non-commercially licensable model based on Llama 7b and trained by
teams from UC Berkeley, CMU, Stanford, MBZUAI, and UC San Diego.
filename: ggml-vicuna-7b-1.1-q4_2.bin
filesize: '4212859520'
md5sum: 29119f8fa11712704c6b22ac5ab792ea
- description: A non-commercially licensable model based on Llama 13b and trained
by teams from UC Berkeley, CMU, Stanford, MBZUAI, and UC San Diego.
filename: ggml-vicuna-13b-1.1-q4_2.bin
filesize: '8136770688'
md5sum: 95999b7b0699e2070af63bf5d34101a8
- description: A non-commercially licensable model based on Llama 7b and trained by
Microsoft and Peking University.
filename: ggml-wizardLM-7B.q4_2.bin
filesize: '4212864640'
md5sum: 99e6d129745a3f1fb1121abed747b05a
- description: A non-commercially licensable model based on Llama 13b and RLHF trained
by Stable AI.
filename: ggml-stable-vicuna-13B.q4_2.bin
filesize: '8136777088'
md5sum: 6cb4ee297537c9133bddab9692879de0
- description: A commercially licensable model base pre-trained by Mosaic ML.
filename: ggml-mpt-7b-base.bin
filesize: '4854401028'
md5sum: 120c32a51d020066288df045ef5d52b9
requires: 2.4.1
- description: A non-commercially licensable model based on Vicuna 13b, fine-tuned
on ~180,000 instructions, trained by Nous Research.
filename: ggml-nous-gpt4-vicuna-13b.bin
filesize: '8136777088'
md5sum: d5eafd5b0bd0d615cfd5fd763f642dfe
- description: A commericially licensable instruct model based on MPT and trained
by Mosaic ML.
filename: ggml-mpt-7b-instruct.bin
filesize: '4854401028'
md5sum: 1cfa4958f489f0a0d1ffdf6b37322809
requires: 2.4.1
- description: GGML format model files for the original LLaMa
icon : https://aeiljuispo.cloudimg.io/v7/https://s3.amazonaws.com/moonup/production/uploads/6426d3f3a7723d62b53c259b/tvPikpAzKTKGN5wrpadOJ.jpeg?w=200&h=200&f=face
filename: Samantha-7B-GPTQ
license: Non commercial
owner_link: https://huggingface.co/TheBloke/
owner: TheBloke
server: Samantha-7B-GPTQ
sha256: ec2f2d1f0dfb73b72a4cbac7fa121abbe04c37ab327125a38248f930c0f09ddf

View File

@ -1 +1 @@
gptq
auto-gptq

View File

@ -0,0 +1,468 @@
# Flask Backend API Documentation
This documentation provides an overview of the endpoints available in the Flask backend API.
## Introduction
The Flask backend API exposes various endpoints to interact with the application. Each endpoint performs a specific function and supports different HTTP methods. The following sections describe each endpoint along with their parameters and expected outputs.
## Endpoints
### Endpoint: /disk_usage (GET)
**Description**: Retrieves the disk usage of the system.
**Parameters**: None
**Output**: Returns the disk usage information.
---
### Endpoint: /list_bindings (GET)
**Description**: Lists the available bindings.
**Parameters**: None
**Output**: Returns a list of available bindings.
---
### Endpoint: /list_models (GET)
**Description**: Lists the available models.
**Parameters**: None
**Output**: Returns a list of available models.
---
### Endpoint: /list_personalities_languages (GET)
**Description**: Lists the languages supported by personalities.
**Parameters**: None
**Output**: Returns a list of languages supported by personalities.
---
### Endpoint: /list_personalities_categories (GET)
**Description**: Lists the categories of personalities.
**Parameters**: None
**Output**: Returns a list of personality categories.
---
### Endpoint: /list_personalities (GET)
**Description**: Lists the available personalities.
**Parameters**: None
**Output**: Returns a list of available personalities.
---
### Endpoint: /list_languages (GET)
**Description**: Lists the available languages.
**Parameters**: None
**Output**: Returns a list of available languages.
---
### Endpoint: /list_discussions (GET)
**Description**: Lists the discussions.
**Parameters**: None
**Output**: Returns a list of discussions.
---
### Endpoint: /set_personality (GET)
**Description**: Sets the active personality.
**Parameters**: None
**Output**: Sets the active personality.
---
### Endpoint: /delete_personality (GET)
**Description**: Deletes a personality.
**Parameters**: None
**Output**: Deletes the specified personality.
---
### Endpoint: / (GET)
**Description**: Returns the index page.
**Parameters**: None
**Output**: Returns the index page.
---
### Endpoint: /<path:filename> (GET)
**Description**: Serves static files.
**Parameters**: `filename` - The path to the static file.
**Output**: Returns the requested static file.
---
### Endpoint: /personalities/<path:filename> (GET)
**Description**: Serves personality files.
**Parameters**: `filename` - The path to the personality file.
**Output**: Returns the requested personality file.
---
### Endpoint: /outputs/<path:filename> (GET)
**Description**: Serves output files.
**Parameters**: `filename` - The path to the output file.
**Output**: Returns the requested output file.
---
### Endpoint: /export_discussion (GET)
**Description**: Exports a discussion.
**Parameters**: None
**Output**: Exports the specified discussion.
---
### Endpoint: /export (GET)
**Description**: Exports data.
**Parameters**: None
**Output**: Exports the specified data.
---
### Endpoint: /new_discussion (GET)
**Description**: Creates a new discussion.
**Parameters**: None
**Output**: Creates a new discussion.
---
### Endpoint: /stop_gen (GET)
**Description**: Stops the generation process.
**Parameters**: None
**Output**: Stops the generation process.
---
### Endpoint: /rename (POST)
**Description**: Renames a resource.
**Parameters**: None
**Output**: Renames the specified resource.
---
### Endpoint: /edit_title (POST)
**Description**: Edits the title of a resource.
**Parameters**: None
**Output**: Edits the title of the specified resource.
---
### Endpoint: /load_discussion (POST)
**Description**: Loads a discussion.
**Parameters**: None
**Output**: Loads the specified discussion.
---
### Endpoint: /delete_discussion (POST)
**Description**: Deletes a discussion.
**Parameters**: None
**Output**: Deletes the specified discussion.
---
### Endpoint: /update_message (GET)
**Description**: Updates a message.
**Parameters**: None
**Output**: Updates the specified message.
---
### Endpoint: /message_rank_up (GET)
**Description**: Increases the rank of a message.
**Parameters**: None
**Output**: Increases the rank of the specified message.
---
### Endpoint: /message_rank_down (GET)
**Description**: Decreases the rank of a message.
**Parameters**: None
**Output**: Decreases the rank of the specified message.
---
### Endpoint: /delete_message (GET)
**Description**: Deletes a message.
**Parameters**: None
**Output**: Deletes the specified message.
---
### Endpoint: /set_binding (POST)
**Description**: Sets a binding.
**Parameters**: None
**Output**: Sets the specified binding.
---
### Endpoint: /set_model (POST)
**Description**: Sets a model.
**Parameters**: None
**Output**: Sets the specified model.
---
### Endpoint: /update_model_params (POST)
**Description**: Updates model parameters.
**Parameters**: None
**Output**: Updates the specified model parameters.
---
### Endpoint: /get_config (GET)
**Description**: Retrieves the configuration.
**Parameters**: None
**Output**: Returns the configuration.
---
### Endpoint: /get_available_models (GET)
**Description**: Retrieves the available models.
**Parameters**: None
**Output**: Returns a list of available models.
---
### Endpoint: /extensions (GET)
**Description**: Retrieves the extensions.
**Parameters**: None
**Output**: Returns the extensions.
---
### Endpoint: /training (GET)
**Description**: Performs training.
**Parameters**: None
**Output**: Performs the training process.
---
### Endpoint: /main (GET)
**Description**: Returns the main page.
**Parameters**: None
**Output**: Returns the main page.
---
### Endpoint: /settings (GET)
**Description**: Returns the settings page.
**Parameters**: None
**Output**: Returns the settings page.
---
### Endpoint: /help (GET)
**Description**: Returns the help page.
**Parameters**: None
**Output**: Returns the help page.
---
### Endpoint: /get_generation_status (GET)
**Description**: Retrieves the generation status.
**Parameters**: None
**Output**: Returns the generation status.
---
### Endpoint: /update_setting (POST)
**Description**: Updates a setting.
**Parameters**: None
**Output**: Updates the specified setting.
---
### Endpoint: /apply_settings (POST)
**Description**: Applies the settings.
**Parameters**: None
**Output**: Applies the specified settings.
---
### Endpoint: /save_settings (POST)
**Description**: Saves the settings.
**Parameters**: None
**Output**: Saves the specified settings.
---
### Endpoint: /get_current_personality (GET)
**Description**: Retrieves the current personality.
**Parameters**: None
**Output**: Returns the current personality.
---
### Endpoint: /get_all_personalities (GET)
**Description**: Retrieves all personalities.
**Parameters**: None
**Output**: Returns a list of all personalities.
---
### Endpoint: /get_personality (GET)
**Description**: Retrieves a specific personality.
**Parameters**: None
**Output**: Returns the specified personality.
---
### Endpoint: /reset (GET)
**Description**: Resets the system.
**Parameters**: None
**Output**: Resets the system.
---
### Endpoint: /export_multiple_discussions (POST)
**Description**: Exports multiple discussions.
**Parameters**: None
**Output**: Exports the specified discussions.
---
### Endpoint: /import_multiple_discussions (POST)
**Description**: Imports multiple discussions.
**Parameters**: None
**Output**: Imports the specified discussions.

File diff suppressed because one or more lines are too long

1
web/dist/assets/index-86ec2b95.css vendored Normal file

File diff suppressed because one or more lines are too long

48
web/dist/assets/index-9deab5e4.js vendored Normal file

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

4
web/dist/index.html vendored
View File

@ -6,8 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>GPT4All - WEBUI</title>
<script type="module" crossorigin src="/assets/index-0e9c5983.js"></script>
<link rel="stylesheet" href="/assets/index-c9ce79a4.css">
<script type="module" crossorigin src="/assets/index-9deab5e4.js"></script>
<link rel="stylesheet" href="/assets/index-86ec2b95.css">
</head>
<body>
<div id="app"></div>