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..6eb7cc0 --- /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=['GET']) + 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