From bdae0d5344c39b6ccc81a53fa3207d55e893552f Mon Sep 17 00:00:00 2001
From: Patrick_Pluto <patrick_pluto@noreply.codeberg.org>
Date: Wed, 18 Sep 2024 14:52:04 +0200
Subject: [PATCH] backend preps

---
 .gitignore                                    |  2 +
 ...versation.tsx => ConversationFrontend.tsx} |  4 +-
 app/InputBackend.tsx                          | 76 +++++++++++++++++++
 app/InputForm.tsx                             | 21 -----
 app/InputFrontend.tsx                         | 37 +++++++++
 app/backend/ai_api.ts                         | 12 ---
 app/page.tsx                                  | 34 +--------
 package-lock.json                             | 26 +++++++
 package.json                                  |  2 +
 9 files changed, 147 insertions(+), 67 deletions(-)
 rename app/{Conversation.tsx => ConversationFrontend.tsx} (91%)
 create mode 100644 app/InputBackend.tsx
 delete mode 100644 app/InputForm.tsx
 create mode 100644 app/InputFrontend.tsx
 delete mode 100644 app/backend/ai_api.ts

diff --git a/.gitignore b/.gitignore
index fd3dbb5..1de8943 100644
--- a/.gitignore
+++ b/.gitignore
@@ -34,3 +34,5 @@ yarn-error.log*
 # typescript
 *.tsbuildinfo
 next-env.d.ts
+
+api_key.txt
\ No newline at end of file
diff --git a/app/Conversation.tsx b/app/ConversationFrontend.tsx
similarity index 91%
rename from app/Conversation.tsx
rename to app/ConversationFrontend.tsx
index 1caa836..8b3e560 100644
--- a/app/Conversation.tsx
+++ b/app/ConversationFrontend.tsx
@@ -7,7 +7,7 @@ interface ConversationProps {
   onCopyClick: () => void;
 }
 
-const Conversation = React.forwardRef<HTMLDivElement, ConversationProps>(
+const ConversationFrontend = React.forwardRef<HTMLDivElement, ConversationProps>(
   ({ messages, onResendClick, onEditClick, onCopyClick }, ref: ForwardedRef<HTMLDivElement>) => {
     return (
       <div className="output">
@@ -40,4 +40,4 @@ const Conversation = React.forwardRef<HTMLDivElement, ConversationProps>(
   }
 );
 
-export default Conversation;
+export default ConversationFrontend;
diff --git a/app/InputBackend.tsx b/app/InputBackend.tsx
new file mode 100644
index 0000000..9576490
--- /dev/null
+++ b/app/InputBackend.tsx
@@ -0,0 +1,76 @@
+import React from 'react';
+import InputFrontend from './InputFrontend';
+import ConversationFrontend from './ConversationFrontend';
+import { Mistral } from '@mistralai/mistralai';
+
+async function prompt_mistral(model: string, prompt: string, system: string) {
+  const apiKey = "m3kZRjN8DRSIo88r8Iti9hmKGWIklrLY";
+
+  const client = new Mistral({ apiKey: apiKey });
+
+  var chatResponse = await client.chat.complete({
+    model: model,
+    messages: [{ role: 'user', content: prompt }, { role: 'system', content: system, }],
+  });
+
+  if (chatResponse && chatResponse.choices && chatResponse.choices.length > 0) {
+    if (chatResponse.choices[0].message.content) {
+      messages.push('AI: ', chatResponse.choices[0].message.content);
+      console.error('Error: Brain Not Found');
+    }
+  } else {
+    console.error('Error: Unexpected API response:', chatResponse);
+  }
+}
+
+const handleSendClick = (message: string) => {
+  messages.push('User: ', message);
+  prompt_mistral("mistral-large-latest", message, "You are a helpful assistant.")
+};
+
+const handleMicClick = () => {
+  console.log('Mic clicked!');
+  // Do something when the mic button is clicked
+};
+
+var messages = [
+  'User: Hello!',
+  'AI: Hi there!',
+  'User: How are you?',
+  'AI: I’m good, thank you!'
+];
+
+const handleResendClick = () => {
+  console.log('Resend button clicked');
+  // Handle resend action
+};
+
+const handleEditClick = () => {
+  console.log('Edit button clicked');
+  // Handle edit action
+};
+
+const handleCopyClick = () => {
+  console.log('Copy button clicked');
+  // Handle copy action
+};
+
+const InputBackend = () => {
+  return (
+    <div>
+      <ConversationFrontend
+        messages={messages}
+        onResendClick={handleResendClick}
+        onEditClick={handleEditClick}
+        onCopyClick={handleCopyClick}
+      />
+      <InputFrontend
+        message=""
+        onSendClick={handleSendClick}
+        onMicClick={handleMicClick}
+      />
+    </div>
+  );
+};
+
+export default InputBackend;
diff --git a/app/InputForm.tsx b/app/InputForm.tsx
deleted file mode 100644
index 6abfc64..0000000
--- a/app/InputForm.tsx
+++ /dev/null
@@ -1,21 +0,0 @@
-import React from 'react';
-
-const InputForm: React.FC = () => {
-  return (
-    <form className="input" method="POST" action="" id="inputForm">
-      <input
-        type="text"
-        name="user_message"
-        placeholder="Type your message here..."
-      />
-      <button type="submit" name="option" value="chat">
-        <img src="/img/send.svg" alt="send" />
-      </button>
-      <button type="submit" name="option" value="voice">
-        <img src="/img/microphone.svg" alt="microphone" />
-      </button>
-    </form>
-  );
-};
-
-export default InputForm;
diff --git a/app/InputFrontend.tsx b/app/InputFrontend.tsx
new file mode 100644
index 0000000..c3cdea8
--- /dev/null
+++ b/app/InputFrontend.tsx
@@ -0,0 +1,37 @@
+import React, { useState, ForwardedRef } from 'react';
+
+interface InputProps {
+  message: string;
+  onSendClick: (message: string) => void;
+  onMicClick: () => void;
+}
+
+const InputFrontend = React.forwardRef<HTMLDivElement, InputProps>(
+  ({ message, onSendClick, onMicClick }, ref: ForwardedRef<HTMLDivElement>) => {
+    const [inputValue, setInputValue] = useState('');
+
+    const handleInputChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+      setInputValue(e.target.value);
+    };
+
+    return (
+      <div className="input" id="inputForm">
+        <input
+          type="text"
+          name="user_message"
+          placeholder="Type your message here..."
+          value={inputValue}
+          onChange={handleInputChange}
+        />
+        <button type="submit" onClick={() => onSendClick(inputValue)}>
+          <img src="/img/send.svg" alt="send" />
+        </button>
+        <button type="submit" onClick={onMicClick}>
+          <img src="/img/microphone.svg" alt="microphone" />
+        </button>
+      </div>
+    );
+  }
+);
+
+export default InputFrontend;
diff --git a/app/backend/ai_api.ts b/app/backend/ai_api.ts
deleted file mode 100644
index 5cafa54..0000000
--- a/app/backend/ai_api.ts
+++ /dev/null
@@ -1,12 +0,0 @@
-import ollama from 'ollama'
-
-async function name(model: string, prompt: string, system: string,) {
-  var message = [{ role: 'user', content: prompt }, { role: 'system', content: system }]
-  var response = await ollama.chat({ model: model, messages: message, stream: true })
-  for await (const part of response) {
-    process.stdout.write(part.message.content)
-  }
-}
-
-
-
diff --git a/app/page.tsx b/app/page.tsx
index a53edbb..05030cd 100644
--- a/app/page.tsx
+++ b/app/page.tsx
@@ -4,8 +4,7 @@ import React, { useState, useEffect, useRef } from 'react';
 import Header from './Header';
 import History from './History';
 import Models from './Models';
-import Conversation from './Conversation';
-import InputForm from './InputForm';
+import InputBackend from './InputBackend';
 import './styles/master.css';
 
 const LandingPage: React.FC = () => {
@@ -41,28 +40,6 @@ const LandingPage: React.FC = () => {
     setShowDivs(prevState => !prevState);
   };
 
-  const messages = [
-    'User: Hello!',
-    'AI: Hi there!',
-    'User: How are you?',
-    'AI: I’m good, thank you!'
-  ];
-
-  const handleResendClick = () => {
-    console.log('Resend button clicked');
-    // Handle resend action
-  };
-
-  const handleEditClick = () => {
-    console.log('Edit button clicked');
-    // Handle edit action
-  };
-
-  const handleCopyClick = () => {
-    console.log('Copy button clicked');
-    // Handle copy action
-  };
-
   return (
     <div className="App">
       <Header toggleDivs={toggleDivs} showDivs={showDivs} />
@@ -72,14 +49,7 @@ const LandingPage: React.FC = () => {
           <Models />
         </div>
         <div className={`conversation-container ${showDivs ? 'expanded' : 'collapsed'}`}>
-          <Conversation
-            ref={conversationRef}
-            messages={messages}
-            onResendClick={handleResendClick}
-            onEditClick={handleEditClick}
-            onCopyClick={handleCopyClick}
-          />
-          <InputForm />
+          <InputBackend />
         </div>
       </div>
     </div>
diff --git a/package-lock.json b/package-lock.json
index ec2f85c..64403cd 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -8,6 +8,8 @@
       "name": "interstellar_ai",
       "version": "0.1.0",
       "dependencies": {
+        "@mistralai/mistralai": "^1.0.4",
+        "fs": "^0.0.1-security",
         "next": "14.2.12",
         "ollama": "^0.5.9",
         "react": "^18",
@@ -235,6 +237,14 @@
         "@jridgewell/sourcemap-codec": "^1.4.14"
       }
     },
+    "node_modules/@mistralai/mistralai": {
+      "version": "1.0.4",
+      "resolved": "https://registry.npmjs.org/@mistralai/mistralai/-/mistralai-1.0.4.tgz",
+      "integrity": "sha512-fLFBD8r4KvITCkKlKcq2ievnNyLd7Oob4xMY7MkY04BqR4nffkTS49DqapnVkemuldtrmHidwPzwD7UT+yFC4A==",
+      "peerDependencies": {
+        "zod": ">= 3"
+      }
+    },
     "node_modules/@next/env": {
       "version": "14.2.12",
       "resolved": "https://registry.npmjs.org/@next/env/-/env-14.2.12.tgz",
@@ -2323,6 +2333,12 @@
         "url": "https://github.com/sponsors/isaacs"
       }
     },
+    "node_modules/fs": {
+      "version": "0.0.1-security",
+      "resolved": "https://registry.npmjs.org/fs/-/fs-0.0.1-security.tgz",
+      "integrity": "sha512-3XY9e1pP0CVEUCdj5BmfIZxRBTSDycnbqhIOGec9QYtmVH2fbLpj86CFWkrNOkt/Fvty4KZG5lTglL9j/gJ87w==",
+      "license": "ISC"
+    },
     "node_modules/fs.realpath": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz",
@@ -5319,6 +5335,16 @@
       "funding": {
         "url": "https://github.com/sponsors/sindresorhus"
       }
+    },
+    "node_modules/zod": {
+      "version": "3.23.8",
+      "resolved": "https://registry.npmjs.org/zod/-/zod-3.23.8.tgz",
+      "integrity": "sha512-XBx9AXhXktjUqnepgTiE5flcKIYWi/rme0Eaj+5Y0lftuGBq+jyRu/md4WnuxqgP1ubdpNCsYEYPxrzVHD8d6g==",
+      "license": "MIT",
+      "peer": true,
+      "funding": {
+        "url": "https://github.com/sponsors/colinhacks"
+      }
     }
   }
 }
diff --git a/package.json b/package.json
index 2aea3c5..b1b9f3c 100644
--- a/package.json
+++ b/package.json
@@ -9,6 +9,8 @@
     "lint": "next lint"
   },
   "dependencies": {
+    "@mistralai/mistralai": "^1.0.4",
+    "fs": "^0.0.1-security",
     "next": "14.2.12",
     "ollama": "^0.5.9",
     "react": "^18",