-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #314 from Broscorp-net/spring-ai-integration
Integrated spring ai
- Loading branch information
Showing
8 changed files
with
51 additions
and
91 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
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
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
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
76 changes: 10 additions & 66 deletions
76
bot/src/main/java/com/community/tools/service/openai/OpenAiService.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 |
---|---|---|
@@ -1,82 +1,26 @@ | ||
package com.community.tools.service.openai; | ||
|
||
import com.community.tools.dto.OpenAiRequestDto; | ||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import java.net.URI; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.RequestEntity; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.ai.chat.ChatClient; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
@Service | ||
public class OpenAiService { | ||
@Value("${openai.url}") | ||
private String url; | ||
@Value("${openai.token}") | ||
private String apiKey; | ||
@Value("${openai.model}") | ||
private String model; | ||
@Value("${openai.temperature}") | ||
private double temperature; | ||
|
||
/** | ||
* This is the main method in service which processes prompt. | ||
* | ||
* @param prompt users request to the openai api | ||
* @return ready-to-use string response from AI | ||
*/ | ||
public String processPrompt(String prompt) { | ||
try { | ||
RestTemplate restTemplate = new RestTemplate(); | ||
HttpHeaders headers = new HttpHeaders(); | ||
headers.setBearerAuth(apiKey); | ||
headers.setContentType(MediaType.APPLICATION_JSON); | ||
|
||
OpenAiRequestDto requestDto = new OpenAiRequestDto(model, prompt, temperature); | ||
|
||
RequestEntity<OpenAiRequestDto> requestEntity = | ||
new RequestEntity<>(requestDto, headers, HttpMethod.POST, URI.create(url)); | ||
private final ChatClient chatClient; | ||
|
||
ResponseEntity<String> responseEntity = restTemplate.exchange(requestEntity, String.class); | ||
|
||
String jsonResponse = responseEntity.getBody(); | ||
String raw = extractMessageFromJsonResponse(jsonResponse); | ||
return getStringWithLineSeparators(raw); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to communicate with OpenAI service", e); | ||
} | ||
public OpenAiService(ChatClient chatClient) { | ||
this.chatClient = chatClient; | ||
} | ||
|
||
/** | ||
* This private method formatting string form json-looking to normal. | ||
* Processes the user's request by sending a prompt to the OpenAI API through the ChatClient. | ||
* | ||
* @param response json string | ||
* @return raw string, example: "This is example list//n1)...//n..." | ||
*/ | ||
private String extractMessageFromJsonResponse(String response) throws IOException { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode jsonNode = objectMapper.readTree(response); | ||
return jsonNode | ||
.get("choices").get(0) | ||
.get("message").get("content").asText(); | ||
} | ||
|
||
/** | ||
* This private method replaces //n to /n in string. | ||
* @param prompt The user's request to be sent to the OpenAI API. | ||
* @return A string response generated by the AI, ready for use. | ||
* | ||
* @param raw raw string with //n | ||
* @return ready-to-use string | ||
* @see ChatClient#call(String) The underlying method responsible for making the API call. | ||
*/ | ||
private String getStringWithLineSeparators(String raw) { | ||
final String regex = "\\\\n"; | ||
final String newSeparator = System.lineSeparator(); | ||
return raw.replaceAll(regex, newSeparator); | ||
public String processPrompt(String prompt) { | ||
return chatClient.call(prompt); | ||
} | ||
} |
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
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
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