From 7b63b35d6be358acff08740e62c836f58ff1667b Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Thu, 19 Sep 2024 09:41:00 +0200 Subject: [PATCH 1/2] Backend WOW. --- .gitignore | 4 +- py/.idea/.gitignore | 3 ++ .../inspectionProfiles/profiles_settings.xml | 6 +++ py/.idea/misc.xml | 10 ++++ py/.idea/modules.xml | 8 +++ py/.idea/py.iml | 13 +++++ py/.idea/vcs.xml | 6 +++ py/api.py | 51 +++++++++++++++++++ py/install.sh | 4 ++ py/requirements.txt | 2 + 10 files changed, 106 insertions(+), 1 deletion(-) create mode 100644 py/.idea/.gitignore create mode 100644 py/.idea/inspectionProfiles/profiles_settings.xml create mode 100644 py/.idea/misc.xml create mode 100644 py/.idea/modules.xml create mode 100644 py/.idea/py.iml create mode 100644 py/.idea/vcs.xml create mode 100644 py/api.py create mode 100644 py/install.sh create mode 100644 py/requirements.txt diff --git a/.gitignore b/.gitignore index 1de8943..e28c376 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,6 @@ yarn-error.log* *.tsbuildinfo next-env.d.ts -api_key.txt \ No newline at end of file + +.idea/ +venv/ diff --git a/py/.idea/.gitignore b/py/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/py/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/py/.idea/inspectionProfiles/profiles_settings.xml b/py/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/py/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/py/.idea/misc.xml b/py/.idea/misc.xml new file mode 100644 index 0000000..3671ece --- /dev/null +++ b/py/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + \ No newline at end of file diff --git a/py/.idea/modules.xml b/py/.idea/modules.xml new file mode 100644 index 0000000..3a65488 --- /dev/null +++ b/py/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/py/.idea/py.iml b/py/.idea/py.iml new file mode 100644 index 0000000..49f4c24 --- /dev/null +++ b/py/.idea/py.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/py/.idea/vcs.xml b/py/.idea/vcs.xml new file mode 100644 index 0000000..6c0b863 --- /dev/null +++ b/py/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/py/api.py b/py/api.py new file mode 100644 index 0000000..ad52667 --- /dev/null +++ b/py/api.py @@ -0,0 +1,51 @@ +from flask import Flask, request, jsonify +import ollama + + +class AI: + @staticmethod + def process_local(model, message, system, return_class, access_token): + stream = ollama.chat( + model=model, + messages=[{'role': 'user', 'content': message}, {'role': 'system', 'content': system}], + stream=True, + ) + + for chunk in stream: + print(chunk['message']['content']) + return_class.ai_response[access_token] += chunk['message']['content'] + + +class API: + def __init__(self): + self.app = Flask(__name__) + self.ai_response = [] + self.ai = AI() + + def run(self): + @self.app.route('/interstellar/api/ai_create', methods=['POST']) + def create_ai(): + self.ai_response.append("") + return jsonify({'status': 200, 'access_token': len(self.ai_response) - 1}) + + @self.app.route('/interstellar/api/ai_send', methods=['POST']) + def send_ai(): + data = request.get_json() + message = data.get('message') + ai_model = data.get('ai_model') + system_prompt = data.get('system_prompt') + access_token = data.get('access_token') + self.ai.process_local(ai_model, message, system_prompt, self, access_token) + return jsonify({'status': 200}) + + @self.app.route('/interstellar/api/ai_get', methods=['GET']) + def get_ai(): + data = request.args.get('access_token') + return jsonify({'status': 200, 'response': self.ai_response[int(data)]}) + + self.app.run(debug=True) + + +if __name__ == '__main__': + api = API() + api.run() diff --git a/py/install.sh b/py/install.sh new file mode 100644 index 0000000..1fbdcba --- /dev/null +++ b/py/install.sh @@ -0,0 +1,4 @@ +python -m venv venv +source venv/bin/activate +pip install -r requirements.txt +deactivate \ No newline at end of file diff --git a/py/requirements.txt b/py/requirements.txt new file mode 100644 index 0000000..731af42 --- /dev/null +++ b/py/requirements.txt @@ -0,0 +1,2 @@ +flask +ollama \ No newline at end of file From fcc147f8ae7ae1154a08dada6da10917b0481c18 Mon Sep 17 00:00:00 2001 From: Patrick_Pluto Date: Thu, 19 Sep 2024 09:44:54 +0200 Subject: [PATCH 2/2] Backend WOW. Pt.2 --- py/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/py/api.py b/py/api.py index ad52667..6eb7cc0 100644 --- a/py/api.py +++ b/py/api.py @@ -23,7 +23,7 @@ class API: self.ai = AI() def run(self): - @self.app.route('/interstellar/api/ai_create', methods=['POST']) + @self.app.route('/interstellar/api/ai_create', methods=['GET']) def create_ai(): self.ai_response.append("") return jsonify({'status': 200, 'access_token': len(self.ai_response) - 1})