Skip to content

Commit

Permalink
Reapply "Merge pull request #3 from Marcel2511/revert-2-revert-1-p2t-…
Browse files Browse the repository at this point in the history
…webservice-BackendTestsLLM"

This reverts commit 0aa380b.
  • Loading branch information
Marcel2511 committed Jun 3, 2024
1 parent 0aa380b commit 0ea286a
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 11 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,3 @@ build/

### VS Code ###
.vscode/

4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -232,12 +232,12 @@
<id>spotless-check</id>
<phase>compile</phase>
<goals>
<goal>check</goal>
<goal>apply</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

</project>
32 changes: 32 additions & 0 deletions src/main/java/de/dhbw/woped/process2text/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package de.dhbw.woped.process2text.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig {

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

@Bean
@Primary
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
logger.info("Applying CORS configuration");
registry
.addMapping("/**")
.allowedOrigins("http://localhost:4200", "http://localhost:3000/")
.allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
.allowedHeaders("*")
.allowCredentials(true);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.dhbw.woped.process2text.controller;

import de.dhbw.woped.process2text.service.P2TLLMService;
import de.dhbw.woped.process2text.service.P2TService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
Expand All @@ -8,24 +9,42 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@CrossOrigin(origins = "http://localhost:4200")
@RestController
@Slf4j
public class P2TController {

Logger logger = LoggerFactory.getLogger(P2TController.class);

@Autowired P2TService p2tService;
@Autowired private P2TService p2tService;

@Autowired private P2TLLMService llmService;

@ApiOperation(value = "Translate a process model into human readable text.")
@PostMapping(value = "/generateText", consumes = "text/plain", produces = "text/plain")
protected String generateText(@RequestBody String body) {
if (logger
.isDebugEnabled()) { // required so that body.replaceAll is only invoked in case the body is
// logged
logger.debug(body.replaceAll("[\n\r\t]", "_"));
if (logger.isDebugEnabled()) {
logger.debug("Received body: " + body.replaceAll("[\n\r\t]", "_"));
}
return p2tService.generateText(body);
String response = p2tService.generateText(body);
logger.debug("Response: " + response);
return response;
}
}

// TEST
@ApiOperation(
value =
"Translate a process model into human readable text using OpenAIs Large Language Model"
+ " GPT-4 Turbo.")
@PostMapping(value = "/generateTextLLM", consumes = "text/plain", produces = "text/plain")
protected String generateTextLLM(
@RequestBody String body,
@RequestParam(required = false) String apiKey,
@RequestParam(required = false) String prompt) {
if (logger.isDebugEnabled()) {
logger.debug("Received body: " + body.replaceAll("[\n\r\t]", "_"));
}
String response = llmService.callLLM(body, apiKey, prompt);
logger.debug("LLM Response: " + response);
return response;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package de.dhbw.woped.process2text.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class P2TLLMService {

public String callLLM(String text, String apiKey, String prompt) {
String apiUrl = "https://api.openai.com/v1/chat/completions";
RestTemplate restTemplate = new RestTemplate();

HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "Bearer " + apiKey);
headers.setContentType(MediaType.APPLICATION_JSON);

Map<String, Object> requestBody = new HashMap<>();
requestBody.put("model", "gpt-4-turbo");
requestBody.put(
"messages",
List.of(
Map.of("role", "system", "content", "You are a helpful assistant."),
Map.of("role", "user", "content", prompt)));
requestBody.put("max_tokens", 4096);
requestBody.put("temperature", 0.7);

HttpEntity<Map<String, Object>> entity = new HttpEntity<>(requestBody, headers);

return restTemplate.postForObject(apiUrl, entity, String.class);
}
}

0 comments on commit 0ea286a

Please sign in to comment.