22
33import com .alibaba .fastjson .JSON ;
44import com .alibaba .fastjson .JSONObject ;
5- import com .azure .ai .openai .models .ChatChoice ;
6- import com .azure .ai .openai .models .ChatCompletions ;
7- import com .azure .ai .openai .models .ChatCompletionsOptions ;
8- import com .azure .ai .openai .models .ChatMessage ;
9- import com .azure .ai .openai .models .ChatRole ;
10- import com .microsoft .hydralab .center .openai .data .ChatRequest ;
115import okhttp3 .MediaType ;
126import okhttp3 .OkHttpClient ;
137import okhttp3 .Request ;
1610import org .slf4j .Logger ;
1711import org .slf4j .LoggerFactory ;
1812
19- import com .azure .ai .openai .OpenAIClient ;
20- import com .azure .ai .openai .OpenAIClientBuilder ;
21- import com .azure .core .credential .AzureKeyCredential ;
22-
23- import java .util .ArrayList ;
24- import java .util .List ;
2513import java .util .Objects ;
2614
27- // Copyright (c) Microsoft Corporation.
15+ // Copyright (c) Microsoft Corporation.
2816// Licensed under the MIT License.
29- public class AzureOpenAIServiceClient {
17+ public class AzureOpenAIServiceClient {
3018 public static final String API_VERSION_CHAT = "2023-03-15-preview" ;
3119 public static final String API_VERSION_IMAGE = "2023-06-01-preview" ;
3220 private final Logger logger = LoggerFactory .getLogger (AzureOpenAIServiceClient .class );
3321 private final String apiKey ;
3422 private final String endpoint ;
3523 private final String deployment ;
3624 OkHttpClient client = new OkHttpClient ();
37- private OpenAIClient azureClient = null ;
3825
3926 public AzureOpenAIServiceClient (String apiKey , String deployment , String endpoint ) {
40- this .apiKey = apiKey == null ? "" : apiKey ;
41- this .endpoint = endpoint == null ? "" : endpoint .endsWith ("/" ) ? endpoint .substring (0 , endpoint .length () - 1 ) : endpoint ;
42- this .deployment = deployment == null ? "" : deployment ;
43- if (!apiKey .isEmpty ()) {
44- this .azureClient = new OpenAIClientBuilder ()
45- .endpoint (endpoint )
46- .credential (new AzureKeyCredential (apiKey ))
47- .buildClient ();
48- }
49- }
50-
51- public String completion (String question ) {
52- if (azureClient == null ) {
53- return "" ;
54- }
55- List <ChatMessage > chatMessages = new ArrayList <>();
56- chatMessages .add (new ChatMessage (ChatRole .SYSTEM , "You are a helpful assistant." ));
57- chatMessages .add (new ChatMessage (ChatRole .USER , question ));
58-
59- ChatCompletionsOptions options = new ChatCompletionsOptions (chatMessages );
60- options .setN (1 );
61-
62- ChatCompletions chatCompletions = azureClient .getChatCompletions (deployment , options );
63-
64- for (ChatChoice choice : chatCompletions .getChoices ()) {
65- return choice .getMessage ().getContent ();
66- }
67- return "" ;
27+ this .apiKey = apiKey ;
28+ this .endpoint = endpoint .endsWith ("/" ) ? endpoint .substring (0 , endpoint .length () - 1 ) : endpoint ;
29+ this .deployment = deployment ;
6830 }
6931
7032 public String chatCompletion (ChatRequest request ) {
@@ -74,10 +36,13 @@ public String chatCompletion(ChatRequest request) {
7436 private String callAzureOpenAIAPI (String operation , String requestBodyString , String apiVersion ) {
7537 MediaType mediaType = MediaType .parse ("application/json" );
7638 String url = String .format ("%s/openai/deployments/%s/%s?api-version=%s" , endpoint , deployment , operation , apiVersion );
39+
7740 logger .info ("Request body: {}" , requestBodyString );
41+
7842 RequestBody body = RequestBody .create (requestBodyString , mediaType );
7943 Request httpRequest = new Request .Builder ().url (url ).post (body )
8044 .addHeader ("api-key" , apiKey ).build ();
45+
8146 try (Response response = client .newCall (httpRequest ).execute ()) {
8247 if (!response .isSuccessful ()) {
8348 throw new RuntimeException ("Unexpected response code: " + response );
0 commit comments