forked from woped/p2t
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reapply "Merge pull request #3 from Marcel2511/revert-2-revert-1-p2t-…
…webservice-BackendTestsLLM" This reverts commit 0aa380b.
- Loading branch information
1 parent
0aa380b
commit 0ea286a
Showing
5 changed files
with
98 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,3 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/main/java/de/dhbw/woped/process2text/config/WebConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
src/main/java/de/dhbw/woped/process2text/service/P2TLLMService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |