Browse Source

LLamaCCP to run Mistral

tbvns 5 tháng trước cách đây
mục cha
commit
a8ff03710e

+ 29 - 0
pom.xml

@@ -35,12 +35,41 @@
 			<artifactId>spring-boot-starter</artifactId>
 		</dependency>
 
+		<dependency>
+			<groupId>org.springframework.boot</groupId>
+			<artifactId>spring-boot-starter-web</artifactId>
+		</dependency>
+
+		<dependency>
+			<groupId>org.apache.commons</groupId>
+			<artifactId>commons-lang3</artifactId>
+			<version>3.17.0</version>
+		</dependency>
+
 		<dependency>
 			<groupId>org.springframework.boot</groupId>
 			<artifactId>spring-boot-starter-test</artifactId>
 			<scope>test</scope>
 		</dependency>
 
+		<dependency>
+			<groupId>at.favre.lib</groupId>
+			<artifactId>bcrypt</artifactId>
+			<version>0.10.2</version>
+		</dependency>
+
+		<dependency>
+			<groupId>mysql</groupId>
+			<artifactId>mysql-connector-java</artifactId>
+			<version>8.0.23</version>
+		</dependency>
+
+		<dependency>
+			<groupId>de.kherud</groupId>
+			<artifactId>llama</artifactId>
+			<version>3.3.0</version>
+		</dependency>
+
 		<dependency>
 			<groupId>commons-io</groupId>
 			<artifactId>commons-io</artifactId>

+ 16 - 0
src/main/java/xyz/prismix/OPCAI_server/Account.java

@@ -1,5 +1,21 @@
 package xyz.prismix.OPCAI_server;
 
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+import org.springframework.web.bind.annotation.RestController;
+import xyz.prismix.OPCAI_server.DataBase.SQLDatabase;
+
+import java.sql.SQLException;
+
+@RestController
 public class Account {
+    @RequestMapping("/account/create")
+    public String createAccount(@RequestParam String username, @RequestParam String password) throws SQLException {
+        return SQLDatabase.createAccount(username, password);
+    }
 
+    @RequestMapping("/account/login")
+    public String login(@RequestParam String username, @RequestParam String password) throws SQLException {
+        return SQLDatabase.getTokenForUser(username, password);
+    }
 }

+ 26 - 0
src/main/java/xyz/prismix/OPCAI_server/Ai/Text/Mistral.java

@@ -0,0 +1,26 @@
+package xyz.prismix.OPCAI_server.Ai.Text;
+
+
+import java.io.IOException;
+
+public class Mistral {
+
+    public static String prompt(String prompt) throws IOException {
+        Process process = new ProcessBuilder()
+                .command(
+                    "C:\\Users\\Tbvns\\Downloads\\llama-b4094-bin-win-cuda-cu12.2.0-x64\\llama-cli.exe",
+                    "-m", "C:\\Users\\Tbvns\\Downloads\\Mistral-7B-Instruct-v0.3.Q5_K_S.gguf",
+                    "-p", "\"" + prompt + "\"",
+                    "-ngl", "100", "--no-display-prompt"
+                )
+                .start();
+        String data = "";
+        while (process.isAlive()) {
+            System.out.print(new String(process.getErrorStream().readAllBytes()));
+            data = new String(process.getInputStream().readAllBytes());
+            System.out.println(data);
+        }
+        return data
+                .replace("[end of text]", "");
+    }
+}

+ 78 - 0
src/main/java/xyz/prismix/OPCAI_server/DataBase/SQLDatabase.java

@@ -0,0 +1,78 @@
+package xyz.prismix.OPCAI_server.DataBase;
+
+import at.favre.lib.crypto.bcrypt.BCrypt;
+import org.apache.commons.lang3.RandomStringUtils;
+
+import java.sql.*;
+
+public class SQLDatabase {
+    static Connection connection;
+    static {
+        try {
+            String url = "jdbc:mysql://192.168.1.42/OPCAI";
+            String user = "OPCAI";
+            String password = "OPCAI1234!?";
+            connection = DriverManager.getConnection(url, user, password);
+        } catch (SQLException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    public static String createAccount(String username, String password) throws SQLException {
+        Statement statement = SQLDatabase.connection.createStatement();
+        ResultSet set = statement.executeQuery("SELECT * FROM Users WHERE Username='" + username + "';"); //TODO: Fix sql injection
+        if (set.next()) {
+            return "This username is already taken !";
+        }
+
+        String salt = RandomStringUtils.randomAlphanumeric(10);
+        password = BCrypt.with(BCrypt.Version.VERSION_2X).hashToString(12, (password + salt).toCharArray());
+
+        statement.executeUpdate("INSERT INTO Users (Username, Password, Salt) VALUES ('" + username + "', '" + password + "', '" + salt + "');");
+        return "Success";
+    }
+
+
+    public static boolean verifyAccount(String username, String password) throws SQLException {
+        String sql = "SELECT Password, Salt FROM Users WHERE Username = ?";
+        PreparedStatement preparedStatement = SQLDatabase.connection.prepareStatement(sql);
+        preparedStatement.setString(1, username);
+        ResultSet resultSet = preparedStatement.executeQuery();
+
+        if (!resultSet.next()) {
+            return false;
+        }
+
+        String hashedPassword = resultSet.getString("Password");
+        String salt = resultSet.getString("Salt");
+
+        BCrypt.Result result = BCrypt.verifyer().verify((password + salt).toCharArray(), hashedPassword);
+
+        return result.verified;
+    }
+
+
+    public static String getTokenForUser(String username, String password) throws SQLException {
+        if (!verifyAccount(username, password)) {
+            return "null";
+        }
+
+        String sql = "SELECT Token FROM Tokens WHERE Username = ?";
+        PreparedStatement preparedStatement = SQLDatabase.connection.prepareStatement(sql);
+        preparedStatement.setString(1, username);
+        ResultSet resultSet = preparedStatement.executeQuery();
+
+        if (resultSet.next()) {
+            return resultSet.getString("Token");
+        }
+
+        String newToken = RandomStringUtils.randomAlphanumeric(50);
+
+        PreparedStatement insertion = SQLDatabase.connection.prepareStatement("INSERT INTO Tokens (Token, Username) VALUES (?, ?)");
+        insertion.setString(1, newToken);
+        insertion.setString(2, username);
+        insertion.executeUpdate();
+
+        return newToken;
+    }
+}

+ 20 - 1
src/main/java/xyz/prismix/OPCAI_server/OpcaiServerApplication.java

@@ -1,12 +1,31 @@
 package xyz.prismix.OPCAI_server;
 
+
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
+import xyz.prismix.OPCAI_server.Ai.Text.Mistral;
+
+import java.io.IOException;
 
 @SpringBootApplication
 public class OpcaiServerApplication {
 
-	public static void main(String[] args) {
+	public static void main(String[] args) throws IOException {
+		System.out.println(Mistral.prompt("""
+		Only reply with the JSON.
+		The type of the pokemon is Light
+		Generate a new imaginary pokemon by completing the following JSON
+		{
+			'name': [ENTER NAME HERE]
+			'atk 1': [ENTER ATK 1 HERE]
+			'atk desc 1': [ENTER ATK DESC 1 HERE]
+			'atk 2': [ENTER ATK 2 HERE]
+			'atk desc 2': [ENTER ATK DESC 2 HERE]
+			'weakness': [ENTER WEAKNESS HERE]
+			'visual desc': [ENTER VISUAL DESC HERE]
+		}
+		"""
+		));
 		SpringApplication.run(OpcaiServerApplication.class, args);
 	}