From 7010be84ea7cb1569105b5e27e616c23d0524398 Mon Sep 17 00:00:00 2001
From: Patrick_Pluto <patrick_pluto@noreply.codeberg.org>
Date: Tue, 24 Sep 2024 09:24:31 +0200
Subject: [PATCH 1/2] Uhmm fixed

---
 py/ai.py            | 29 +++++++++++++++++++++++++++++
 py/api.py           | 13 ++++++++++---
 py/requirements.txt |  3 +--
 py/run.py           |  7 -------
 py/webapp.py        |  1 -
 5 files changed, 40 insertions(+), 13 deletions(-)
 delete mode 100644 py/run.py
 delete mode 100644 py/webapp.py

diff --git a/py/ai.py b/py/ai.py
index a8c5537..f78b3b7 100644
--- a/py/ai.py
+++ b/py/ai.py
@@ -1,5 +1,6 @@
 from mistralai import Mistral
 from openai import OpenAI
+import google.generativeai as genai
 import anthropic
 import ollama
 
@@ -72,3 +73,31 @@ class AI:
             for text in stream.text_stream:
                 with return_class.ai_response_lock:
                     return_class.ai_response[access_token] += text
+
+    @staticmethod
+    def process_google(model, messages, return_class, access_token, api_key):
+
+        message = messages[-1]['content']
+        messages.pop()
+
+        system = None
+        if messages and messages[0]['role'] == 'system':
+            system = messages[0]['content']
+            messages.pop(0)
+
+        for msg in messages:
+            msg['parts'] = msg.pop('content')
+
+        for msg in messages:
+            if msg['role'] == 'assistant':
+                msg['role'] = 'model'
+
+        genai.configure(api_key=api_key)
+        model = genai.GenerativeModel("gemini-1.5-flash")
+        chat = model.start_chat(
+            system_instruction=system,
+            history=[
+                {"role": "user", "parts": "Hello"},
+                {"role": "model", "parts": "Great to meet you. What would you like to know?"},
+            ]
+        )
diff --git a/py/api.py b/py/api.py
index be44561..6c2d243 100644
--- a/py/api.py
+++ b/py/api.py
@@ -42,19 +42,22 @@ class API:
                 return jsonify({'status': 200})
             elif model_type == "mistral":
                 api_key = data.get('api_key')
-                thread = threading.Thread(target=self.ai.process_mistralai, args=(ai_model, messages, self, access_token, api_key))
+                thread = threading.Thread(target=self.ai.process_mistralai,
+                                          args=(ai_model, messages, self, access_token, api_key))
                 thread.start()
                 thread.join()
                 return jsonify({'status': 200})
             elif model_type == "openai":
                 api_key = data.get('api_key')
-                thread = threading.Thread(target=self.ai.process_openai, args=(ai_model, messages, self, access_token, api_key))
+                thread = threading.Thread(target=self.ai.process_openai,
+                                          args=(ai_model, messages, self, access_token, api_key))
                 thread.start()
                 thread.join()
                 return jsonify({'status': 200})
             elif model_type == "anthropic":
                 api_key = data.get('api_key')
-                thread = threading.Thread(target=self.ai.process_anthropic, args=(ai_model, messages, self, access_token, api_key))
+                thread = threading.Thread(target=self.ai.process_anthropic,
+                                          args=(ai_model, messages, self, access_token, api_key))
                 thread.start()
                 thread.join()
                 return jsonify({'status': 200})
@@ -118,3 +121,7 @@ class API:
 
         ssl_context = ("cert.pem", "key.pem")
         self.app.run(debug=True, host='0.0.0.0', port=5000)
+
+
+api = API()
+api.run()
diff --git a/py/requirements.txt b/py/requirements.txt
index 8a79dc1..bd93ad7 100644
--- a/py/requirements.txt
+++ b/py/requirements.txt
@@ -4,5 +4,4 @@ ollama
 mistralai
 openai
 anthropic
-pyOpenSSL
-pywebview
\ No newline at end of file
+pyOpenSSL
\ No newline at end of file
diff --git a/py/run.py b/py/run.py
deleted file mode 100644
index 612d72e..0000000
--- a/py/run.py
+++ /dev/null
@@ -1,7 +0,0 @@
-import os
-import webview
-
-os.system("python api.py")
-webview.create_window('Hello world', 'http://localhost:3000')
-webview.start()
-
diff --git a/py/webapp.py b/py/webapp.py
deleted file mode 100644
index 8b13789..0000000
--- a/py/webapp.py
+++ /dev/null
@@ -1 +0,0 @@
-

From 778bfd632e8a7ae4d1595237d88edd7d33780716 Mon Sep 17 00:00:00 2001
From: Patrick_Pluto <patrick_pluto@noreply.codeberg.org>
Date: Tue, 24 Sep 2024 09:27:36 +0200
Subject: [PATCH 2/2] Thanks Google for not using standards.

---
 py/ai.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/py/ai.py b/py/ai.py
index f78b3b7..5879e9d 100644
--- a/py/ai.py
+++ b/py/ai.py
@@ -93,11 +93,14 @@ class AI:
                 msg['role'] = 'model'
 
         genai.configure(api_key=api_key)
-        model = genai.GenerativeModel("gemini-1.5-flash")
+
+        model = genai.GenerativeModel(model)
+
         chat = model.start_chat(
             system_instruction=system,
-            history=[
-                {"role": "user", "parts": "Hello"},
-                {"role": "model", "parts": "Great to meet you. What would you like to know?"},
-            ]
+            history=messages
         )
+
+        response = chat.send_message(message, stream=True)
+        for chunk in response:
+            return_class.ai_response[access_token] += chunk.text