-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat : when apikey is demo modify headers
- Loading branch information
1 parent
54787f6
commit 14358b4
Showing
7 changed files
with
146 additions
and
5 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
49 changes: 49 additions & 0 deletions
49
chatmodel-springai/src/main/java/com/example/ai/config/CustomClientHttpResponse.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,49 @@ | ||
package com.example.ai.config; | ||
|
||
import org.springframework.http.HttpHeaders; | ||
import org.springframework.http.HttpStatusCode; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Collections; | ||
|
||
public class CustomClientHttpResponse implements ClientHttpResponse { | ||
|
||
private final ClientHttpResponse originalResponse; | ||
private final HttpHeaders headers; | ||
public CustomClientHttpResponse(ClientHttpResponse originalResponse) { | ||
this.originalResponse = originalResponse; | ||
MultiValueMap<String, String> modifiedHeaders = new LinkedMultiValueMap<>(originalResponse.getHeaders()); | ||
modifiedHeaders.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(MediaType.APPLICATION_JSON_VALUE)); | ||
this.headers = new HttpHeaders(modifiedHeaders); | ||
} | ||
|
||
@Override | ||
public HttpStatusCode getStatusCode() throws IOException { | ||
return originalResponse.getStatusCode(); | ||
} | ||
|
||
@Override | ||
public String getStatusText() throws IOException { | ||
return originalResponse.getStatusText(); | ||
} | ||
|
||
@Override | ||
public void close() { | ||
|
||
} | ||
|
||
@Override | ||
public InputStream getBody() throws IOException { | ||
return originalResponse.getBody(); | ||
} | ||
|
||
@Override | ||
public HttpHeaders getHeaders() { | ||
return headers; | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
chatmodel-springai/src/main/java/com/example/ai/config/LoggingConfig.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,60 @@ | ||
package com.example.ai.config; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.BufferingClientHttpRequestFactory; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | ||
import org.springframework.util.StreamUtils; | ||
import org.springframework.web.client.RestClient; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
@Configuration(proxyBeanMethods = false) | ||
@ConditionalOnProperty(value = "spring.ai.openai.api-key", havingValue = "demo") | ||
public class LoggingConfig { | ||
|
||
private final Logger log = LoggerFactory.getLogger(LoggingConfig.class); | ||
|
||
@Bean | ||
RestClient.Builder restClientBuilder() { | ||
return RestClient.builder().requestFactory(new BufferingClientHttpRequestFactory(new HttpComponentsClientHttpRequestFactory())) | ||
.requestInterceptor((request, body, execution) -> { | ||
logRequest(request, body); | ||
ClientHttpResponse response = execution.execute(request, body); | ||
logResponse(response); | ||
return new CustomClientHttpResponse(response); | ||
}).defaultHeaders(httpHeaders -> { | ||
httpHeaders.setContentType(MediaType.APPLICATION_JSON); | ||
httpHeaders.setAccept(List.of(MediaType.ALL)); | ||
}); | ||
} | ||
|
||
private void logResponse(ClientHttpResponse response) throws IOException { | ||
log.info("============================response begin=========================================="); | ||
log.info("Status code : {}", response.getStatusCode()); | ||
log.info("Status text : {}", response.getStatusText()); | ||
log.info("Headers : {}", response.getHeaders()); | ||
log.info("Response body: {}", StreamUtils.copyToString(response.getBody(), Charset.defaultCharset())); | ||
log.info("=======================response end================================================="); | ||
} | ||
|
||
private void logRequest(HttpRequest request, byte[] body) { | ||
|
||
log.info("===========================request begin================================================"); | ||
log.info("URI : {}", request.getURI()); | ||
log.info("Method : {}", request.getMethod()); | ||
log.info("Headers : {}", request.getHeaders()); | ||
log.info("Request body: {}", new String(body, StandardCharsets.UTF_8)); | ||
log.info("==========================request end================================================"); | ||
|
||
} | ||
} |
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
4 changes: 4 additions & 0 deletions
4
chatmodel-springai/src/main/java/com/example/ai/model/response/AIChatResponse.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,4 @@ | ||
package com.example.ai.model.response; | ||
|
||
public record AIChatResponse(String answer) { | ||
} |
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
21 changes: 21 additions & 0 deletions
21
chatmodel-springai/src/test/java/com/example/ai/controller/ChatControllerTest.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,21 @@ | ||
package com.example.ai.controller; | ||
|
||
import org.hamcrest.Matchers; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.restassured.RestAssured; | ||
|
||
public class ChatControllerTest { | ||
|
||
@Test | ||
void testChat() { | ||
RestAssured.given() | ||
.param("question", "Hello?") | ||
.when() | ||
.get("http://localhost:8080/api/ai/chat") | ||
.then() | ||
.statusCode(200) | ||
.body("question", Matchers.equalTo("Hello?")) | ||
.body("answer", Matchers.equalTo("Hi!")); | ||
} | ||
} |