-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from the-gigi/main
Add support for Azure OpenAI
- Loading branch information
Showing
8 changed files
with
125 additions
and
32 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
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
81 changes: 81 additions & 0 deletions
81
src/demo/java/io/github/sashirestela/openai/demo/AzureOpenAIChatServiceDemo.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,81 @@ | ||
package io.github.sashirestela.openai.demo; | ||
|
||
|
||
import io.github.sashirestela.cleverclient.support.ContentType; | ||
import io.github.sashirestela.openai.domain.chat.ChatRequest; | ||
import io.github.sashirestela.openai.domain.chat.message.ChatMsgSystem; | ||
import io.github.sashirestela.openai.domain.chat.message.ChatMsgUser; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class AzureOpenAIChatServiceDemo extends AbstractDemo { | ||
private static final String AZURE_OPENAI_API_KEY_HEADER = "api-key"; | ||
private final ChatRequest chatRequest; | ||
|
||
@SuppressWarnings("unchecked") | ||
public AzureOpenAIChatServiceDemo(String baseUrl, String apiKey, String model) { | ||
super(baseUrl, apiKey, request -> { | ||
var url = request.getUrl(); | ||
var contentType = request.getContentType(); | ||
var body = request.getBody(); | ||
|
||
// add a header to the request | ||
var headers = request.getHeaders(); | ||
headers.put(AZURE_OPENAI_API_KEY_HEADER, apiKey); | ||
request.setHeaders(headers); | ||
|
||
// add a query parameter to url | ||
url += (url.contains("?") ? "&" : "?") + "api-version=2023-05-15"; | ||
// remove '/vN' or '/vN.M' from url | ||
url = url.replaceFirst("(\\/v\\d+\\.*\\d*)", ""); | ||
request.setUrl(url); | ||
|
||
if (contentType != null) { | ||
if (contentType.equals(ContentType.APPLICATION_JSON)) { | ||
var bodyJson = (String) request.getBody(); | ||
// remove a field from body (as Json) | ||
bodyJson = bodyJson.replaceFirst(",?\"model\":\"[^\"]*\",?", ""); | ||
bodyJson = bodyJson.replaceFirst("\"\"", "\",\""); | ||
body = bodyJson; | ||
} | ||
if (contentType.equals(ContentType.MULTIPART_FORMDATA)) { | ||
Map<String, Object> bodyMap = (Map<String, Object>) request.getBody(); | ||
// remove a field from body (as Map) | ||
bodyMap.remove("model"); | ||
body = bodyMap; | ||
} | ||
request.setBody(body); | ||
} | ||
|
||
return request; | ||
}); | ||
|
||
chatRequest = ChatRequest.builder() | ||
.model(model) | ||
.message(new ChatMsgSystem("You are an expert in AI.")) | ||
.message( | ||
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words.")) | ||
.temperature(0.0) | ||
.maxTokens(300) | ||
.build(); | ||
} | ||
|
||
public void demoCallChatBlocking() { | ||
var futureChat = openAI.chatCompletions().create(chatRequest); | ||
var chatResponse = futureChat.join(); | ||
System.out.println(chatResponse.firstContent()); | ||
} | ||
|
||
public static void main(String[] args) { | ||
var baseUrl = System.getenv("CUSTOM_OPENAI_BASE_URL"); | ||
var apiKey = System.getenv("CUSTOM_OPENAI_API_KEY"); | ||
// Services like Azure OpenAI don't require a model (endpoints have built-in model) | ||
var model = Optional.ofNullable(System.getenv("CUSTOM_OPENAI_MODEL")) | ||
.orElse("N/A"); | ||
var demo = new AzureOpenAIChatServiceDemo(baseUrl, apiKey, model); | ||
|
||
demo.addTitleAction("Call Completion (Blocking Approach)", demo::demoCallChatBlocking); | ||
|
||
demo.run(); | ||
} | ||
} |
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
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
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
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
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