From ece46c505e0d72ebefd699557f4f0b2fbca2bad5 Mon Sep 17 00:00:00 2001 From: ParisNeo Date: Thu, 6 Apr 2023 21:47:46 +0200 Subject: [PATCH 1/4] updated gitignore and added a README.md to the models folder --- .gitignore | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index e402fa61..193d14c7 100644 --- a/.gitignore +++ b/.gitignore @@ -130,4 +130,6 @@ dmypy.json # Database *.db -/data \ No newline at end of file +/data +models/ +!models/README.md \ No newline at end of file From 864c5769e9f18a30aec02cfcac23571fdee472f8 Mon Sep 17 00:00:00 2001 From: ParisNeo Date: Thu, 6 Apr 2023 22:07:20 +0200 Subject: [PATCH 2/4] Upgraded install batch files --- .gitignore | 9 +++++- .vscode/settings.json | 3 ++ install.bat | 68 +++++++++++++++++++++++++++++++------------ uninstall.bat | 2 +- 4 files changed, 62 insertions(+), 20 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.gitignore b/.gitignore index 193d14c7..2654392b 100644 --- a/.gitignore +++ b/.gitignore @@ -130,6 +130,13 @@ dmypy.json # Database *.db + +# Docker files /data + +# models models/ -!models/README.md \ No newline at end of file +!models/README.md + +# Temporary files +tmp/ \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 00000000..5d7d7306 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "ros.distro": "noetic" +} \ No newline at end of file diff --git a/install.bat b/install.bat index 1388eb6a..e7ba234e 100644 --- a/install.bat +++ b/install.bat @@ -1,12 +1,47 @@ @echo off +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHH .HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHH. ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHH.## HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHH#.HHHHH/*,*,*,*,*,*,*,*,***,*,**#HHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHH.*,,***,***,***,***,***,***,*******HHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*,,,,,HHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH.,,,***,***,***,***,***,***,***,***,***,***/HHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH*,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHH#,***,***,***,***,***,***,***,***,***,***,***,**HHHHHHHHHHHHHHHHH +echo HHHHHHHHHH..HHH,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*#HHHHHHHHHHHHHHHH +echo HHHHHHH,,,**,/H*,***,***,***,,,*,***,***,***,**,,,**,***,***,***H,,*,***HHHHHHHH +echo HHHHHH.*,,,*,,,,,*,*,*,***#HHHHH.,,*,*,*,*,**/HHHHH.,*,*,*,*,*,*,*,*****HHHHHHHH +echo HHHHHH.*,***,*,*,***,***,.HHHHHHH/**,***,****HHHHHHH.***,***,***,*******HHHHHHHH +echo HHHHHH.,,,,,,,,,,,,,,,,,,,.HHHHH.,,,,,,,,,,,,.HHHHHH,,,,,,,,,,,,,,,,,***HHHHHHHH +echo HHHHHH.,,,,,,/H,,,**,***,***,,,*,***,***,***,**,,,,*,***,***,***H***,***HHHHHHHH +echo HHHHHHH.,,,,*.H,,,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,***H*,,,,/HHHHHHHHH +echo HHHHHHHHHHHHHHH*,***,***,**,,***,***,***,***,***,***,***,***,**.HHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH,,,,,,,,*,,#H#,,,,,*,,,*,,,,,,,,*#H*,,,,,,,,,**HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH,,*,***,***,**/.HHHHHHHHHHHHH#*,,,*,***,***,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH**,***,***,***,***,***,***,***,***,***,***,*.HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH**,***,***,*******/..HHHHHHHHH.#/*,*,,,***,***HHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH*,*,*,******#HHHHHHHHHHHHHHHHHHHHHHHHHHHH./**,,,.HHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH.,,*,***.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH.*#HHHHHHHHHHHH +echo HHHHHHHHHHHHHHH/,,,*.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHH,,#HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHH.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH + + REM Check if Python is installed set /p="Checking for python..." nul 2>&1 -if %ERRORLEVEL% neq 0 ( - echo Python is not installed. Would you like to install Python? [Y/N] - set /p choice= - if /i "%choice%" equ "Y" ( +where python >nul 2>&1 +if %errorlevel% neq 0 ( + set /p choice=Python is not installed. Would you like to install Python? [Y/N] + if /i ".choice." equ "Y" ( REM Download Python installer echo Downloading Python installer... powershell -Command "Invoke-WebRequest -Uri 'https://www.python.org/ftp/python/3.10.0/python-3.10.0-amd64.exe' -OutFile 'python.exe'" @@ -22,20 +57,20 @@ if %ERRORLEVEL% neq 0 ( echo OK ) + REM Check if pip is installed set /p="Checking for pip..." nul 2>&1 -if %ERRORLEVEL% neq 0 ( - echo Pip is not installed. Would you like to install pip? [Y/N] - set /p choice= - if /i "%choice%" equ "Y" ( +if %errorlevel% neq 0 ( + set /p choice=Pip is not installed. Would you like to install pip? [Y/N] + if /i ".choice." equ "Y" ( REM Download get-pip.py echo Downloading get-pip.py... powershell -Command "Invoke-WebRequest -Uri 'https://bootstrap.pypa.io/get-pip.py' -OutFile 'get-pip.py'" REM Install pip echo Installing pip... python get-pip.py - ) else ( + ) else . echo Please install pip and try again. pause exit /b 1 @@ -47,10 +82,9 @@ if %ERRORLEVEL% neq 0 ( REM Check if venv module is available set /p="Checking for venv..." nul 2>&1 -if %ERRORLEVEL% neq 0 ( - echo venv module is not available. Would you like to upgrade Python to the latest version? [Y/N] - set /p choice= - if /i "%choice%" equ "Y" ( +if %errorlevel% neq 0 ( + set /p choice=venv module is not available. Would you like to upgrade Python to the latest version? [Y/N] + if /i ".choice." equ "Y" ( REM Upgrade Python echo Upgrading Python... python -m pip install --upgrade pip setuptools wheel @@ -67,7 +101,7 @@ if %ERRORLEVEL% neq 0 ( REM Create a new virtual environment set /p="Creating virtual environment ..." Date: Thu, 6 Apr 2023 22:16:52 +0200 Subject: [PATCH 3/4] Upgraded batch file --- install.bat | 17 ++++++++++++----- run.bat | 0 2 files changed, 12 insertions(+), 5 deletions(-) create mode 100644 run.bat diff --git a/install.bat b/install.bat index e7ba234e..799fc4fb 100644 --- a/install.bat +++ b/install.bat @@ -35,6 +35,7 @@ echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +if not exist "./tmp" mkdir "./tmp" REM Check if Python is installed set /p="Checking for python..." Date: Thu, 6 Apr 2023 23:15:01 +0200 Subject: [PATCH 4/4] Moved to the new python Bindings from nomic-AI --- app.py | 115 ++++++++++++++++++++++++++++---------------------------- run.bat | 39 +++++++++++++++++++ 2 files changed, 96 insertions(+), 58 deletions(-) diff --git a/app.py b/app.py index 4be17f7b..5c10d1ea 100644 --- a/app.py +++ b/app.py @@ -1,5 +1,5 @@ from flask import Flask, jsonify, request, render_template, Response, stream_with_context -from nomic.gpt4all import GPT4All +from pyllamacpp.model import Model import argparse import threading from io import StringIO @@ -148,7 +148,6 @@ class Gpt4AllWebUI(): self.app=app self.db_path= db_path self.add_endpoint('/', '', self.index, methods=['GET']) - self.add_endpoint('/stream', 'stream', self.stream, methods=['GET']) self.add_endpoint('/export', 'export', self.export, methods=['GET']) self.add_endpoint('/new_discussion', 'new_discussion', self.new_discussion, methods=['GET']) self.add_endpoint('/bot', 'bot', self.bot, methods=['POST']) @@ -159,12 +158,44 @@ class Gpt4AllWebUI(): self.add_endpoint('/update_message', 'update_message', self.update_message, methods=['GET']) - - + conditionning_message=""" +Instruction: Act as GPT4All. A kind and helpful AI bot built to help users solve problems. +Start by welcoming the user then stop sending text. +GPT4All:""" + self.prepare_query(conditionning_message) + chatbot_bindings.generate(conditionning_message, n_predict=55, new_text_callback=self.new_text_callback, n_threads=8) + print(f"Bot said:{self.bot_says}") # Chatbot conditionning # response = self.chatbot_bindings.prompt("This is a discussion between A user and an AI. AI responds to user questions in a helpful manner. AI is not allowed to lie or deceive. AI welcomes the user\n### Response:") # print(response) + def prepare_query(self, message): + self.bot_says='' + self.full_text='' + self.is_bot_text_started=False + self.current_message = message + + + def new_text_callback(self, text: str): + print(text, end="") + self.full_text += text + if self.is_bot_text_started: + self.bot_says += text + if self.current_message in self.full_text: + self.is_bot_text_started=True + + def new_text_callback_with_yield(self, text: str): + """ + To do , fix the problem with yield to be able to show interactive response as text comes + """ + print(text, end="") + self.full_text += text + if self.is_bot_text_started: + self.bot_says += text + if self.current_message in self.full_text: + self.is_bot_text_started=True + yield text + def add_endpoint(self, endpoint=None, endpoint_name=None, handler=None, methods=['GET'], *args, **kwargs): self.app.add_url_rule(endpoint, endpoint_name, handler, methods=methods, *args, **kwargs) @@ -184,63 +215,29 @@ class Gpt4AllWebUI(): # Return the formatted message return message - - def stream(self): - def generate(): - # Replace this with your text-generating code - for i in range(10): - yield f'This is line {i+1}\n' - time.sleep(1) - - return Response(stream_with_context(generate())) - def export(self): return jsonify(export_to_json(self.db_path)) - @stream_with_context def parse_to_prompt_stream(self, message, message_id): - bot_says = [''] - point = b'' - bot = self.chatbot_bindings.bot + bot_says = '' self.stop=False - # very important. This is the maximum time we wait for the model - wait_val = 15.0 # At the beginning the server may need time to send data. we wait 15s - # send the message to the bot print(f"Received message : {message}") - bot = self.chatbot_bindings.bot - bot.stdin.write(message.encode('utf-8')) - bot.stdin.write(b"\n") - bot.stdin.flush() - # First we need to send the new message ID to the client response_id = self.current_discussion.add_message("GPT4All",'') # first the content is empty, but we'll fill it at the end yield(json.dumps({'type':'input_message_infos','message':message, 'id':message_id, 'response_id':response_id})) - #Now let's wait for the bot to answer - while not self.stop: - readable, _, _ = select.select([bot.stdout], [], [], wait_val) - wait_val = 4.0 # Once started, the process doesn't take that much so we reduce the wait - if bot.stdout in readable: - point += bot.stdout.read(1) - try: - character = point.decode("utf-8") - if character == "\n": - bot_says.append('\n') - yield '\n' - else: - bot_says[-1] += character - yield character - point = b'' + self.current_message = "User: "+message+"\nGPT4All:" + self.prepare_query(self.current_message) + chatbot_bindings.generate(self.current_message, n_predict=55, new_text_callback=self.new_text_callback, n_threads=8) - except UnicodeDecodeError: - if len(point) > 4: - point = b'' - else: - self.current_discussion.update_message(response_id,bot_says) - return "\n".join(bot_says) + self.current_discussion.update_message(response_id,self.bot_says) + yield self.bot_says + # TODO : change this to use the yield version in order to send text word by word + + return "\n".join(bot_says) def bot(self): self.stop=True @@ -335,17 +332,19 @@ if __name__ == '__main__': args = parser.parse_args() - chatbot_bindings = GPT4All(decoder_config = { - 'temp': args.temp, - 'n_predict':args.n_predict, - 'top_k':args.top_k, - 'top_p':args.top_p, - #'color': True,#"## Instruction", - 'repeat_penalty': args.repeat_penalty, - 'repeat_last_n':args.repeat_last_n, - 'ctx_size': args.ctx_size - }) - chatbot_bindings.open() + chatbot_bindings = Model(ggml_model='./models/gpt4all-converted.bin', n_ctx=512) + + # Old Code + # GPT4All(decoder_config = { + # 'temp': args.temp, + # 'n_predict':args.n_predict, + # 'top_k':args.top_k, + # 'top_p':args.top_p, + # #'color': True,#"## Instruction", + # 'repeat_penalty': args.repeat_penalty, + # 'repeat_last_n':args.repeat_last_n, + # 'ctx_size': args.ctx_size + # }) check_discussion_db(args.db_path) bot = Gpt4AllWebUI(chatbot_bindings, app, args.db_path) diff --git a/run.bat b/run.bat index e69de29b..a86428ba 100644 --- a/run.bat +++ b/run.bat @@ -0,0 +1,39 @@ +echo off + +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHH .HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHH. ,HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHH.## HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHH#.HHHHH/*,*,*,*,*,*,*,*,***,*,**#HHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHH.*,,***,***,***,***,***,***,*******HHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*,,,,,HHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH.,,,***,***,***,***,***,***,***,***,***,***/HHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH*,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHH#,***,***,***,***,***,***,***,***,***,***,***,**HHHHHHHHHHHHHHHHH +echo HHHHHHHHHH..HHH,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*#HHHHHHHHHHHHHHHH +echo HHHHHHH,,,**,/H*,***,***,***,,,*,***,***,***,**,,,**,***,***,***H,,*,***HHHHHHHH +echo HHHHHH.*,,,*,,,,,*,*,*,***#HHHHH.,,*,*,*,*,**/HHHHH.,*,*,*,*,*,*,*,*****HHHHHHHH +echo HHHHHH.*,***,*,*,***,***,.HHHHHHH/**,***,****HHHHHHH.***,***,***,*******HHHHHHHH +echo HHHHHH.,,,,,,,,,,,,,,,,,,,.HHHHH.,,,,,,,,,,,,.HHHHHH,,,,,,,,,,,,,,,,,***HHHHHHHH +echo HHHHHH.,,,,,,/H,,,**,***,***,,,*,***,***,***,**,,,,*,***,***,***H***,***HHHHHHHH +echo HHHHHHH.,,,,*.H,,,,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,***H*,,,,/HHHHHHHHH +echo HHHHHHHHHHHHHHH*,***,***,**,,***,***,***,***,***,***,***,***,**.HHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH,,,,,,,,*,,#H#,,,,,*,,,*,,,,,,,,*#H*,,,,,,,,,**HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH,,*,***,***,**/.HHHHHHHHHHHHH#*,,,*,***,***,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*,*HHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH**,***,***,***,***,***,***,***,***,***,***,*.HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*HHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHH**,***,***,*******/..HHHHHHHHH.#/*,*,,,***,***HHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHH*,*,*,******#HHHHHHHHHHHHHHHHHHHHHHHHHHHH./**,,,.HHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHH.,,*,***.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH.*#HHHHHHHHHHHH +echo HHHHHHHHHHHHHHH/,,,*.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHH,,#HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHH.HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH +echo HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH + +env/Scripts/activate.bat +python app.py \ No newline at end of file