parent
1c0330a931
commit
100a54f148
@ -0,0 +1,17 @@
|
||||
package com.olexyn.burnsmail.model;
|
||||
|
||||
import com.sun.mail.imap.IMAPMessage;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class AMail {
|
||||
|
||||
|
||||
private String from;
|
||||
private String to;
|
||||
private String subject;
|
||||
private String content;
|
||||
private IMAPMessage imapMessage;
|
||||
private Double spamScore;
|
||||
|
||||
}
|
@ -0,0 +1,13 @@
|
||||
package com.olexyn.burnsmail.openai;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
@AllArgsConstructor
|
||||
public class ChatMessage {
|
||||
|
||||
private String role;
|
||||
private String content;
|
||||
|
||||
}
|
@ -0,0 +1,9 @@
|
||||
package com.olexyn.burnsmail.openai;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class ChatModels {
|
||||
|
||||
public final static String GPT_35_TURBO = "gpt-3.5-turbo";
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
package com.olexyn.burnsmail.openai;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class ChatRequest {
|
||||
|
||||
private String model;
|
||||
private List<ChatMessage> messages = new ArrayList<>();
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,13 @@
|
||||
package com.olexyn.burnsmail.openai;
|
||||
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
@UtilityClass
|
||||
public class ChatRoles {
|
||||
|
||||
public static final String SPAM_CLASSIFIER = "You are a spam classifier. " +
|
||||
"On any input String you can give any score between 0.00 and 0.99, " +
|
||||
"where 0.99 means the input is definitely spam and 0.00 means it is definitely not spam. " +
|
||||
"Your response shall only ever be a String representation of a double. ";
|
||||
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
package com.olexyn.burnsmail.openai;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import lombok.experimental.UtilityClass;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpMethod;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.client.RestTemplate;
|
||||
|
||||
@UtilityClass
|
||||
public class RequestDispatcher {
|
||||
|
||||
|
||||
public static String dispatch(String model, String systemContent, String userContent) throws JsonProcessingException {
|
||||
// Create RestTemplate instance
|
||||
var restTemplate = new RestTemplate();
|
||||
|
||||
// Set headers
|
||||
var headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON);
|
||||
headers.set("Authorization", "Bearer " + System.getenv("openai.api.key"));
|
||||
|
||||
// Create request entity
|
||||
String url = "https://api.openai.com/v1/chat/completions";
|
||||
|
||||
var chatRequest = new ChatRequest();
|
||||
chatRequest.setModel(model);
|
||||
chatRequest.getMessages().add(new ChatMessage("system", systemContent));
|
||||
chatRequest.getMessages().add(new ChatMessage("user", userContent));
|
||||
var om = new ObjectMapper();
|
||||
|
||||
String jsonBody = om.writeValueAsString(chatRequest);
|
||||
|
||||
HttpEntity<String> requestEntity = new HttpEntity<>(jsonBody, headers);
|
||||
|
||||
// Make the HTTP POST request
|
||||
ResponseEntity<String> responseEntity = restTemplate
|
||||
.exchange(url, HttpMethod.POST, requestEntity, String.class);
|
||||
|
||||
// Handle response
|
||||
HttpStatus statusCode = responseEntity.getStatusCode();
|
||||
if (statusCode == HttpStatus.OK) {
|
||||
return responseEntity.getBody();
|
||||
// Process response body
|
||||
} else {
|
||||
return "error";
|
||||
// Handle error
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue