sessions/fall24/books-genai-vertex-langchain4j/src/main/java/services/web/DocumentEmbeddingController.java [42:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@RestController
@RequestMapping("/document")
public class DocumentEmbeddingController {

  private static final Logger logger = LoggerFactory.getLogger(DocumentEmbeddingController.class);

  BooksService booksService;
  CloudStorageService cloudStorageService;

  VertexAIClient vertexAIClient;

  @Value("${prompts.promptTransformTF}")
  private String promptTransformTF;

  @Value("${spring.ai.vertex.ai.gemini.chat.options.model}")
  private String model;

  @Value("classpath:/bashscripts/provision-cloud-infra.sh")
  private Resource bashscript;

  public DocumentEmbeddingController(BooksService booksService,
      CloudStorageService cloudStorageService, VertexAIClient vertexAIClient) {
    this.booksService = booksService;
    this.cloudStorageService = cloudStorageService;
    this.vertexAIClient = vertexAIClient;
  }

  @PostConstruct
  public void init() {
    logger.info("BookImagesApplication: DocumentEmbeddingController Post Construct Initializer "
        + new SimpleDateFormat("HH:mm:ss.SSS").format(
        new java.util.Date(System.currentTimeMillis())));
    logger.info(
        "BookImagesApplication: DocumentEmbeddingController Post Construct - StartupCheck can be enabled");

    StartupCheck.up();
  }

  @GetMapping("start")
  String start() {
    logger.info(
        "BookImagesApplication: DocumentEmbeddingController - Executed start endpoint request "
            + new SimpleDateFormat("HH:mm:ss.SSS").format(
            new java.util.Date(System.currentTimeMillis())));
    return "DocumentEmbeddingController started";
  }

  // used for testing
  @RequestMapping(value = "/category/books", method = RequestMethod.GET)
  public ResponseEntity<List<Map<String, Object>>> getTable(
      @RequestParam(name = "prompt") String prompt,
      @RequestParam(name = "contentCharactersLimit", defaultValue = "2000") String contentCharactersLimit) {
    return new ResponseEntity<>(
        booksService.prompt(prompt, Integer.parseInt(contentCharactersLimit)), HttpStatus.OK);
  }

  // used for testing
  @RequestMapping(value = "/category/books", method = RequestMethod.POST)
  public ResponseEntity<Integer> insertTable(@RequestBody Map<String, Object> body) {
    String fileName = (String) body.get("fileName");
    booksService.insertBook(fileName);
    return new ResponseEntity<>(HttpStatus.OK);
  }

  @RequestMapping(value = "/terraform", method = RequestMethod.POST)
  public ResponseEntity<String> receiveMessageTransform(
          @RequestBody Map<String, Object> body, @RequestHeader Map<String, String> headers) {
    String errorMsg = RequestValidationUtility.validateRequest(body,headers);
    if (!errorMsg.isBlank()) {
      return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
    }

    // get document name and bucket
    String fileName = (String) body.get("name");
    String bucketName = (String) body.get("bucket");

    logger.info("New script to transform:" + fileName);

    // read file from Cloud Storage
    BufferedReader br = cloudStorageService.readFile(bucketName, fileName);

    String response = tfTransform(br.toString());

    // success
    return new ResponseEntity<>(response, HttpStatus.OK);
  }

  @CrossOrigin
  @RequestMapping(value = "/bash/to-terraform", method = RequestMethod.POST)
  public ResponseEntity<String> tfTransformTransform(
          @RequestBody Map<String, Object> body) {

    String script = (String) body.get("script");
    String response = tfTransform(script);

    // success
    return new ResponseEntity<>(response, HttpStatus.OK);
  }

  public String tfTransform(String script) {
    logger.info("tf transform flow -");
    long start = System.currentTimeMillis();
    List<Map<String, Object>> responseDoc = booksService.prompt("Find paragraphs mentioning Terraform best practices for general style, structure, and dependency management", 6000);
    String transformScriptPrompt =  PromptUtility.formatPromptTF(responseDoc, promptTransformTF, script);
    logger.info("TF transform: prompt LLM: " + (System.currentTimeMillis() - start) + "ms");
    String response = vertexAIClient.promptModel(transformScriptPrompt);
    logger.info("TF transform flow: " + (System.currentTimeMillis() - start) + "ms");
    return response;
  }

  @RequestMapping(value = "/embeddings", method = RequestMethod.POST)
  public ResponseEntity<String> receiveMessage(
      @RequestBody Map<String, Object> body, @RequestHeader Map<String, String> headers) {
    String errorMsg = RequestValidationUtility.validateRequest(body,headers);
    if (!errorMsg.isBlank()) {
      return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
    }

    // get document name and bucket
    String fileName = (String) body.get("name");
    String bucketName = (String) body.get("bucket");

    logger.info("New book uploaded for embedding:" + fileName);

    // read file from Cloud Storage
    long start = System.currentTimeMillis();
    BufferedReader br = cloudStorageService.readFile(bucketName, fileName);
    logger.info("Embedding flow - read book: " + (System.currentTimeMillis() - start) + "ms");

    // add embedding functionality here
    // persist book and pages to AlloyDB
    start = System.currentTimeMillis();
    Integer bookId = booksService.insertBook(fileName);
    booksService.insertPagesBook(br, bookId);
    logger.info("Embedding flow - insert book and pages: " + (System.currentTimeMillis() - start) + "ms");

    // success
    return new ResponseEntity<>(HttpStatus.OK);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



sessions/next24/books-genai-vertex-langchain4j/src/main/java/services/web/DocumentEmbeddingController.java [42:179]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@RestController
@RequestMapping("/document")
public class DocumentEmbeddingController {

  private static final Logger logger = LoggerFactory.getLogger(DocumentEmbeddingController.class);

  BooksService booksService;
  CloudStorageService cloudStorageService;

  VertexAIClient vertexAIClient;

  @Value("${prompts.promptTransformTF}")
  private String promptTransformTF;

  @Value("${spring.ai.vertex.ai.gemini.chat.options.model}")
  private String model;

  @Value("classpath:/bashscripts/provision-cloud-infra.sh")
  private Resource bashscript;

  public DocumentEmbeddingController(BooksService booksService,
      CloudStorageService cloudStorageService, VertexAIClient vertexAIClient) {
    this.booksService = booksService;
    this.cloudStorageService = cloudStorageService;
    this.vertexAIClient = vertexAIClient;
  }

  @PostConstruct
  public void init() {
    logger.info("BookImagesApplication: DocumentEmbeddingController Post Construct Initializer "
        + new SimpleDateFormat("HH:mm:ss.SSS").format(
        new java.util.Date(System.currentTimeMillis())));
    logger.info(
        "BookImagesApplication: DocumentEmbeddingController Post Construct - StartupCheck can be enabled");

    StartupCheck.up();
  }

  @GetMapping("start")
  String start() {
    logger.info(
        "BookImagesApplication: DocumentEmbeddingController - Executed start endpoint request "
            + new SimpleDateFormat("HH:mm:ss.SSS").format(
            new java.util.Date(System.currentTimeMillis())));
    return "DocumentEmbeddingController started";
  }

  // used for testing
  @RequestMapping(value = "/category/books", method = RequestMethod.GET)
  public ResponseEntity<List<Map<String, Object>>> getTable(
      @RequestParam(name = "prompt") String prompt,
      @RequestParam(name = "contentCharactersLimit", defaultValue = "2000") String contentCharactersLimit) {
    return new ResponseEntity<>(
        booksService.prompt(prompt, Integer.parseInt(contentCharactersLimit)), HttpStatus.OK);
  }

  // used for testing
  @RequestMapping(value = "/category/books", method = RequestMethod.POST)
  public ResponseEntity<Integer> insertTable(@RequestBody Map<String, Object> body) {
    String fileName = (String) body.get("fileName");
    booksService.insertBook(fileName);
    return new ResponseEntity<>(HttpStatus.OK);
  }

  @RequestMapping(value = "/terraform", method = RequestMethod.POST)
  public ResponseEntity<String> receiveMessageTransform(
          @RequestBody Map<String, Object> body, @RequestHeader Map<String, String> headers) {
    String errorMsg = RequestValidationUtility.validateRequest(body,headers);
    if (!errorMsg.isBlank()) {
      return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
    }

    // get document name and bucket
    String fileName = (String) body.get("name");
    String bucketName = (String) body.get("bucket");

    logger.info("New script to transform:" + fileName);

    // read file from Cloud Storage
    BufferedReader br = cloudStorageService.readFile(bucketName, fileName);

    String response = tfTransform(br.toString());

    // success
    return new ResponseEntity<>(response, HttpStatus.OK);
  }

  @CrossOrigin
  @RequestMapping(value = "/bash/to-terraform", method = RequestMethod.POST)
  public ResponseEntity<String> tfTransformTransform(
          @RequestBody Map<String, Object> body) {

    String script = (String) body.get("script");
    String response = tfTransform(script);

    // success
    return new ResponseEntity<>(response, HttpStatus.OK);
  }

  public String tfTransform(String script) {
    logger.info("tf transform flow -");
    long start = System.currentTimeMillis();
    List<Map<String, Object>> responseDoc = booksService.prompt("Find paragraphs mentioning Terraform best practices for general style, structure, and dependency management", 6000);
    String transformScriptPrompt =  PromptUtility.formatPromptTF(responseDoc, promptTransformTF, script);
    logger.info("TF transform: prompt LLM: " + (System.currentTimeMillis() - start) + "ms");
    String response = vertexAIClient.promptModel(transformScriptPrompt);
    logger.info("TF transform flow: " + (System.currentTimeMillis() - start) + "ms");
    return response;
  }

  @RequestMapping(value = "/embeddings", method = RequestMethod.POST)
  public ResponseEntity<String> receiveMessage(
      @RequestBody Map<String, Object> body, @RequestHeader Map<String, String> headers) {
    String errorMsg = RequestValidationUtility.validateRequest(body,headers);
    if (!errorMsg.isBlank()) {
      return new ResponseEntity<>(errorMsg, HttpStatus.BAD_REQUEST);
    }

    // get document name and bucket
    String fileName = (String) body.get("name");
    String bucketName = (String) body.get("bucket");

    logger.info("New book uploaded for embedding:" + fileName);

    // read file from Cloud Storage
    long start = System.currentTimeMillis();
    BufferedReader br = cloudStorageService.readFile(bucketName, fileName);
    logger.info("Embedding flow - read book: " + (System.currentTimeMillis() - start) + "ms");

    // add embedding functionality here
    // persist book and pages to AlloyDB
    start = System.currentTimeMillis();
    Integer bookId = booksService.insertBook(fileName);
    booksService.insertPagesBook(br, bookId);
    logger.info("Embedding flow - insert book and pages: " + (System.currentTimeMillis() - start) + "ms");

    // success
    return new ResponseEntity<>(HttpStatus.OK);
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



