Skip to content

MortBot Documentation for Mati #6

@BobTheFarmer

Description

@BobTheFarmer

To use MortBot locally

  1. Update dependences
    ./mvnw run:spring-boot
  2. Create .env file with key. Create a new file called '.env' in the base of the repo. Copy this into it:
    SECRET=Bearer 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
  3. Run backend locally and make request as below
Screenshot 2024-03-15 at 11 30 35 AM Screenshot 2024-03-15 at 11 30 55 AM

How MortBot works

MortBot takes the user input and makes another request to a different API huggingface mistralai. It also gets the API key from .env. This API is kind of bad and has a low world limit I don't know how to fix.

    @PostMapping("/post")
    public ResponseEntity<Object> postPerson(
    @RequestParam("title") String title,
    @RequestParam("desc") String desc,
    @RequestParam("username") String username) throws IOException {
    
        if(title.length() < 3 || title.length() > 100) {
            return new ResponseEntity<>("Title is less than 3 or longer than 100 characters", HttpStatus.BAD_REQUEST);
        }
        if(desc.length() < 3 || desc.length() > 50000) {
            return new ResponseEntity<>("Desc is less than 3 or longer than 500000 characters", HttpStatus.BAD_REQUEST);
        }
        //Get secret api key from .env file
        Dotenv dotenv = Dotenv.load();
        String secretKey = dotenv.get("SECRET");
        
        OkHttpClient client = new OkHttpClient();
        String json = "{\"inputs\":\"<s> [INST] You are MortBot, an experienced code helper. If it fits, you like to say 'Code! Code! Code!' and variations of it by changing Code to another word in certain contexts. Your job is to respond to a issue a student is having, but you will only have one message to respond so avoid asking follow up questions, instead answer to the best of your ability. A student, " + username + " who submitted this issue for you: Title: " + title + ". Description: " + desc + "  [/INST] Model answer</s>\"}";
        okhttp3.RequestBody body = okhttp3.RequestBody.create(
                okhttp3.MediaType.parse("application/json; charset=utf-8"), json);
    
        Request request = new Request.Builder()
                .url("https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1")
                .post(body)
                .addHeader("Authorization", secretKey)
                .build();
    
        String responseString;
        try (Response response = client.newCall(request).execute()) {
            responseString = response.body().string();
        }

        
        // Use regex to remove content between <s> and </s>
        String patternString = "<s>.*</s>";
        Pattern pattern = Pattern.compile(patternString, Pattern.DOTALL);
        Matcher matcher = pattern.matcher(responseString);
    
        // If the matcher finds a match
        if (matcher.find()) {
            responseString = matcher.replaceAll("");
        }

        if (responseString.startsWith("[{\"generated_text\":\"")) {
            responseString = responseString.substring(20); // Remove the leading part
        }
        if (responseString.endsWith("\"}")) {
            responseString = responseString.substring(0, responseString.length() - 2); // Remove the trailing part
        }

        repository.save(new Issue(title, desc, username, responseString));

        return new ResponseEntity<>("Created successfully", HttpStatus.CREATED);
    }

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions