1818import com .r7b7 .entity .CompletionResponse ;
1919import com .r7b7 .entity .ErrorResponse ;
2020
21- public class DefaultOpenAIClient implements IOpenAIClient {
22- private final String OPENAI_API_URL ;
21+ public class DefaultOpenAIClient implements LlmHttpClient {
22+ private final String apiUrl ;
23+ private final String providerName ;
2324 private final HttpClient httpClient ;
2425 private final ObjectMapper objectMapper ;
2526 private final Duration requestTimeout ;
2627
2728 public DefaultOpenAIClient () {
28- this (null , null , null , null );
29+ this (null , null , null , null , null );
2930 }
3031
31- public DefaultOpenAIClient (URI baseUri , HttpClient httpClient , ObjectMapper objectMapper , Duration requestTimeout ) {
32+ public DefaultOpenAIClient (URI baseUri , String providerName , HttpClient httpClient ,
33+ ObjectMapper objectMapper , Duration requestTimeout ) {
3234 try {
3335 Properties properties = PropertyConfig .loadConfig ();
34- URI resolved = baseUri != null ? baseUri : URI .create (properties .getProperty (HospAiKeys .Properties .OPENAI_URL ));
35- this .OPENAI_API_URL = resolved .toString ();
36+ URI resolved = baseUri != null ? baseUri
37+ : URI .create (properties .getProperty (HospAiKeys .Properties .OPENAI_URL ));
38+ this .apiUrl = resolved .toString ();
3639 } catch (Exception ex ) {
3740 throw new IllegalStateException ("Critical configuration missing: " + HospAiKeys .Properties .OPENAI_URL , ex );
3841 }
42+ this .providerName = providerName != null ? providerName : "OpenAI" ;
3943 this .httpClient = httpClient != null ? httpClient : HttpClient .newHttpClient ();
4044 this .objectMapper = objectMapper != null ? objectMapper : new ObjectMapper ();
4145 this .requestTimeout = requestTimeout ;
@@ -47,7 +51,7 @@ public CompletionResponse generateCompletion(CompletionRequest request) {
4751 String jsonRequest = this .objectMapper .writeValueAsString (request .requestBody ());
4852
4953 HttpRequest .Builder httpRequestBuilder = HttpRequest .newBuilder ()
50- .uri (URI .create (this .OPENAI_API_URL ))
54+ .uri (URI .create (this .apiUrl ))
5155 .header (HospAiKeys .Headers .CONTENT_TYPE , HospAiKeys .ContentTypes .APPLICATION_JSON )
5256 .header (HospAiKeys .Headers .AUTHORIZATION , "Bearer " + request .apiKey ())
5357 .POST (HttpRequest .BodyPublishers .ofString (jsonRequest ));
@@ -56,10 +60,10 @@ public CompletionResponse generateCompletion(CompletionRequest request) {
5660 httpRequestBuilder .timeout (requestTimeout );
5761 }
5862
59- HttpRequest httpRequest = httpRequestBuilder .build ();
60- HttpResponse < String > response = httpClient . send ( httpRequest , HttpResponse .BodyHandlers .ofString ());
63+ HttpResponse < String > response = httpClient . send ( httpRequestBuilder .build (),
64+ HttpResponse .BodyHandlers .ofString ());
6165 if (response .statusCode () == 200 ) {
62- return extractResponseText (response .body ());
66+ return extractResponse (response .body ());
6367 } else {
6468 return new CompletionResponse (null , null , new ErrorResponse (
6569 "Request sent to LLM failed: " + response .statusCode () + response .body (), null ));
@@ -69,28 +73,24 @@ public CompletionResponse generateCompletion(CompletionRequest request) {
6973 }
7074 }
7175
72- private CompletionResponse extractResponseText (String responseBody ) {
73- List <Message > msgs = null ;
74- OpenAIResponse response = null ;
75- ErrorResponse error = null ;
76- Map <String , Object > metadata = null ;
77-
76+ private CompletionResponse extractResponse (String responseBody ) {
7877 try {
79- response = objectMapper .readValue (responseBody , OpenAIResponse .class );
80- msgs = response .choices ().stream ()
78+ OpenAIResponse response = objectMapper .readValue (responseBody , OpenAIResponse .class );
79+ List < Message > msgs = response .choices ().stream ()
8180 .map (choice -> new Message (choice .message ().role (), choice .message ().content (),
8281 choice .message ().toolCalls ()))
8382 .toList ();
84- metadata = Map .of (
83+ Map < String , Object > metadata = Map .of (
8584 "id" , response .id (),
8685 "model" , response .model (),
87- "provider" , "OpenAi" ,
86+ "provider" , providerName ,
8887 "prompt_tokens" , response .usage ().promptTokens (),
8988 "completion_tokens" , response .usage ().completionTokens (),
9089 "total_tokens" , response .usage ().totalTokens ());
90+ return new CompletionResponse (msgs , metadata , null );
9191 } catch (Exception ex ) {
92- error = new ErrorResponse ("Exception occurred in extracting response" , ex );
92+ return new CompletionResponse (null , null ,
93+ new ErrorResponse ("Exception occurred in extracting response" , ex ));
9394 }
94- return new CompletionResponse (msgs , metadata , error );
9595 }
9696}
0 commit comments