I’m using OpenAI API Key to connect to the spring.
My Spring Code
@Slf4j
@Service
@RequiredArgsConstructor
public class GPTServiceImpl implements GPTService {
@Value("${open-ai-key}")
private String token;
public JsonNode callGPT(String userMsg) throws JsonProcessingException {
final String url = "https://api.openai.com/v1/chat/completions";
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
headers.setBearerAuth(token);
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> bodyMap = new HashMap<>();
bodyMap.put("model", "gpt-3.5-turbo");
List<Map<String, String>> messages = new ArrayList<>();
Map<String, String> userMessage = new HashMap<>();
userMessage.put("role", "user");
userMessage.put("content", userMsg);
messages.add(userMessage);
Map<String, String> assistantMessage = new HashMap<>();
assistantMessage.put("role", "system");
assistantMessage.put("content", "you are a friendly ai");
messages.add(assistantMessage);
bodyMap.put("messages", messages);
String body = objectMapper.writeValueAsString(bodyMap);
HttpEntity<String> request = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); // error here
return objectMapper.readTree(response.getBody());
}
@Override
public ResponseEntity<?> getAssisantMsg(String userMsg) throws JsonProcessingException {
JsonNode jsonNode = callGPT(userMsg);
String content = jsonNode.path("choices").get(0).path("message").path("content").asText();
return ResponseEntity.status(HttpStatus.OK).body(content);
}
and I wrote “open-ai-key” in application-properties.
it can’t work…
Error Log
ERROR 1304 — [project] [nio-8080-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet] : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]] with root cause
org.springframework.web.client.HttpClientErrorException$Unauthorized: 401 Unauthorized: [no body]
at org.springframework.web.client.HttpClientErrorException.create(HttpClientErrorException.java:106) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:183) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:137) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.ResponseErrorHandler.handleError(ResponseErrorHandler.java:63) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:942) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:891) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:790) ~[spring-web-6.1.6.jar:6.1.6]
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:672) ~[spring-web-6.1.6.jar:6.1.6]
at com.project.service.GPTServiceImpl.callGPT(GPTServiceImpl.java:60) ~[classes/:na]
I tried few things.
- Initialize directly to literal in ‘private String token’ : it can work well. I can get answer from gpt.
- Insert a literal api key value other than the ‘token’ field as a parameter for headers.setBearerAuth method : it can work well too.. I can get answer.
- I logged the ‘token’ field to see if the value didn’t come from the ‘@Value’ annotation. : but the api key value was well put in..
how to resolve this problem?
I wonder why between @Value field and literal field come out different result? doesn’t it same?
hooooni is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.