@@ -323,39 +323,57 @@ private ChatResponse executeCompletionRequest(String queryJson, @Nullable Intege
323323 request .header (HttpHeader .AUTHORIZATION , "Bearer " + apiKey );
324324 }
325325
326- if (logger .isDebugEnabled ()) {
326+ logger .debug ("Request to {} (POST): payload size = {} bytes" , baseUrl + PATH_CHAT_COMPLETIONS ,
327+ queryJson .getBytes (StandardCharsets .UTF_8 ).length );
328+ if (logger .isTraceEnabled ()) {
327329 try {
328330 String prettyRequest = objectMapper .writerWithDefaultPrettyPrinter ()
329331 .writeValueAsString (objectMapper .readTree (queryJson ));
330- logger .debug ("Request to {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , prettyRequest );
332+ logger .trace ("Request payload to {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , prettyRequest );
331333 } catch (IOException e ) {
332- logger .debug ("Request to {} (POST): {}" , baseUrl + PATH_CHAT_COMPLETIONS , queryJson );
334+ logger .trace ("Request payload to {} (POST): {}" , baseUrl + PATH_CHAT_COMPLETIONS , queryJson );
333335 }
334336 }
335337 try {
336338 ContentResponse response = request .send ();
337339 if (response .getStatus () == HttpStatus .OK_200 ) {
338340 String body = response .getContentAsString ();
339- if (logger .isDebugEnabled ()) {
341+ ChatResponse chatResponse = objectMapper .readValue (body , ChatResponse .class );
342+ ChatResponse .Usage usage = chatResponse .getUsage ();
343+ if (usage != null ) {
344+ logger .debug (
345+ "Response from {} (POST): payload size = {} bytes, prompt tokens = {}, completion tokens = {}, total tokens = {}" ,
346+ baseUrl + PATH_CHAT_COMPLETIONS , body .getBytes (StandardCharsets .UTF_8 ).length ,
347+ usage .getPromptTokens (), usage .getCompletionTokens (), usage .getTotalTokens ());
348+ } else {
349+ logger .debug ("Response from {} (POST): payload size = {} bytes" , baseUrl + PATH_CHAT_COMPLETIONS ,
350+ body .getBytes (StandardCharsets .UTF_8 ).length );
351+ }
352+ if (logger .isTraceEnabled ()) {
340353 try {
341354 String prettyResponse = objectMapper .writerWithDefaultPrettyPrinter ()
342355 .writeValueAsString (objectMapper .readTree (body ));
343- logger .debug ("Response from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , prettyResponse );
356+ logger .trace ("Response payload from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS ,
357+ prettyResponse );
344358 } catch (IOException e ) {
345- logger .debug ("Response from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , body );
359+ logger .trace ("Response payload from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , body );
346360 }
347361 }
348- return objectMapper . readValue ( body , ChatResponse . class ) ;
362+ return chatResponse ;
349363 } else {
350364 String errorBody = response .getContentAsString ();
351- if (logger .isDebugEnabled ()) {
365+ logger .debug ("Error response from {} (POST): HTTP {} {}, payload size = {} bytes" ,
366+ baseUrl + PATH_CHAT_COMPLETIONS , response .getStatus (), response .getReason (),
367+ errorBody .getBytes (StandardCharsets .UTF_8 ).length );
368+ if (logger .isTraceEnabled ()) {
352369 try {
353370 String prettyError = objectMapper .writerWithDefaultPrettyPrinter ()
354371 .writeValueAsString (objectMapper .readTree (errorBody ));
355- logger .debug ("Error response from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS ,
372+ logger .trace ("Error response payload from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS ,
356373 prettyError );
357374 } catch (IOException e ) {
358- logger .debug ("Error response from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS , errorBody );
375+ logger .trace ("Error response payload from {} (POST):\n {}" , baseUrl + PATH_CHAT_COMPLETIONS ,
376+ errorBody );
359377 }
360378 }
361379 throw new ChatGPTApiException (
@@ -387,8 +405,10 @@ public List<String> fetchModels(@Nullable Integer timeoutSeconds) throws ChatGPT
387405 ContentResponse response = request .send ();
388406 if (response .getStatus () == HttpStatus .OK_200 ) {
389407 String body = response .getContentAsString ();
390- if (logger .isDebugEnabled ()) {
391- logger .debug ("Response from {} (GET):\n {}" , baseUrl + PATH_MODELS , body );
408+ logger .debug ("Response from {} (GET): payload size = {} bytes" , baseUrl + PATH_MODELS ,
409+ body .getBytes (StandardCharsets .UTF_8 ).length );
410+ if (logger .isTraceEnabled ()) {
411+ logger .trace ("Response payload from {} (GET):\n {}" , baseUrl + PATH_MODELS , body );
392412 }
393413 JsonNode modelsNode = objectMapper .readTree (body );
394414 JsonNode data = modelsNode .get ("data" );
0 commit comments