Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to add a base class and dedicated classes for Azure and Anyscale #47

Merged
merged 21 commits into from
Feb 17, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 5 additions & 12 deletions src/demo/java/io/github/sashirestela/openai/demo/AbstractDemo.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
package io.github.sashirestela.openai.demo;

import io.github.sashirestela.cleverclient.http.HttpRequestData;
import io.github.sashirestela.openai.BaseSimpleOpenAI;
import io.github.sashirestela.openai.SimpleOpenAI;
import java.util.ArrayList;
import java.util.List;
import java.util.function.UnaryOperator;
import lombok.NonNull;

public abstract class AbstractDemo {

private String apiKey;
private String organizationId;
protected SimpleOpenAI openAI;
protected BaseSimpleOpenAI openAI;

private static List<TitleAction> titleActions = new ArrayList<>();
private int times = 80;
private final int times = 80;

protected AbstractDemo() {
apiKey = System.getenv("OPENAI_API_KEY");
Expand All @@ -25,14 +24,8 @@ protected AbstractDemo() {
.build();
}

protected AbstractDemo(@NonNull String baseUrl,
@NonNull String apiKey,
@NonNull UnaryOperator<HttpRequestData> requestInterceptor) {
openAI = SimpleOpenAI.builder()
.apiKey(apiKey)
.baseUrl(baseUrl)
.requestInterceptor(requestInterceptor)
.build();
protected AbstractDemo(@NonNull BaseSimpleOpenAI openAI) {
this.openAI = openAI;
}

public void addTitleAction(String title, Action action) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package io.github.sashirestela.openai.demo;


import io.github.sashirestela.openai.SimpleOpenAIAnyscale;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
import io.github.sashirestela.openai.demo.ChatServiceDemo.RunAlarm;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Weather;
import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatResponse;
import io.github.sashirestela.openai.domain.chat.message.ChatMsg;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgSystem;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgTool;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgUser;
import io.github.sashirestela.openai.domain.chat.tool.ChatFunction;
import io.github.sashirestela.openai.function.FunctionExecutor;
import java.util.ArrayList;

public class AnyscaleChatServiceDemo extends AbstractDemo {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the other demos, rename it to ChatAnyscaleServiceDemo

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the renaming


public static final String MODEL = "mistralai/Mixtral-8x7B-Instruct-v0.1";


private ChatRequest chatRequest;


public AnyscaleChatServiceDemo(String apiKey, String model) {
super(SimpleOpenAIAnyscale.builder().apiKey(apiKey).build());
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 demoCallChatStreaming() {
var futureChat = openAI.chatCompletions().createStream(chatRequest);
var chatResponse = futureChat.join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(ChatResponse::firstContent)
.forEach(System.out::print);
System.out.println();
}

public void demoCallChatBlocking() {
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
System.out.println(chatResponse.firstContent());
}

public void demoCallChatWithFunctions() {
var functionExecutor = new FunctionExecutor();
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("get_weather")
.description("Get the current weather of a location")
.functionalClass(Weather.class)
.build());
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("product")
.description("Get the product of two numbers")
.functionalClass(Product.class)
.build());
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("run_alarm")
.description("Run an alarm")
.functionalClass(RunAlarm.class)
.build());
var messages = new ArrayList<ChatMsg>();
messages.add(new ChatMsgUser("What is the product of 123 and 456?"));
var chatRequest = ChatRequest.builder()
.model(MODEL)
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
var chatToolCall = chatMessage.getToolCalls().get(0);
var result = functionExecutor.execute(chatToolCall.getFunction());
messages.add(chatMessage);
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model(MODEL)
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
futureChat = openAI.chatCompletions().create(chatRequest);
chatResponse = futureChat.join();
System.out.println(chatResponse.firstContent());
}

public static void main(String[] args) {
var apiKey = System.getenv("ANYSCALE_API_KEY");
// Services like Azure OpenAI don't require a model (endpoints have built-in model)
var demo = new AnyscaleChatServiceDemo(apiKey, MODEL);

demo.addTitleAction("Call Chat (Streaming Approach)", demo::demoCallChatStreaming);
demo.addTitleAction("Call Chat (Blocking Approach)", demo::demoCallChatBlocking);
demo.addTitleAction("Call Chat with Functions", demo::demoCallChatWithFunctions);

demo.run();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package io.github.sashirestela.openai.demo;


import io.github.sashirestela.openai.SimpleOpenAIAzure;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
import io.github.sashirestela.openai.demo.ChatServiceDemo.RunAlarm;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Weather;
import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatResponse;
import io.github.sashirestela.openai.domain.chat.content.ContentPartImage;
import io.github.sashirestela.openai.domain.chat.content.ContentPartText;
import io.github.sashirestela.openai.domain.chat.content.ImageUrl;
import io.github.sashirestela.openai.domain.chat.message.ChatMsg;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgSystem;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgTool;
import io.github.sashirestela.openai.domain.chat.message.ChatMsgUser;
import io.github.sashirestela.openai.domain.chat.tool.ChatFunction;
import io.github.sashirestela.openai.function.FunctionExecutor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;

public class AzureChatServiceDemo extends AbstractDemo {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be consistent with the other demos, rename it to ChatAzureServiceDemo

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the renaming.

private ChatRequest chatRequest;

public AzureChatServiceDemo(String baseUrl, String apiKey, String apiVersion) {
super(SimpleOpenAIAzure.builder()
.apiKey(apiKey)
.baseUrl(baseUrl)
.apiVersion(apiVersion)
.build());
chatRequest = ChatRequest.builder()
.model("N/A")
.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 demoCallChatStreaming() {
var futureChat = openAI.chatCompletions().createStream(chatRequest);
var chatResponse = futureChat.join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(ChatResponse::firstContent)
.forEach(System.out::print);
System.out.println();
}

public void demoCallChatBlocking() {
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
System.out.println(chatResponse.firstContent());
}

public void demoCallChatWithFunctions() {
var functionExecutor = new FunctionExecutor();
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("get_weather")
.description("Get the current weather of a location")
.functionalClass(Weather.class)
.build());
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("product")
.description("Get the product of two numbers")
.functionalClass(Product.class)
.build());
functionExecutor.enrollFunction(
ChatFunction.builder()
.name("run_alarm")
.description("Run an alarm")
.functionalClass(RunAlarm.class)
.build());
var messages = new ArrayList<ChatMsg>();
messages.add(new ChatMsgUser("What is the product of 123 and 456?"));
chatRequest = ChatRequest.builder()
.model("N/A")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
var chatToolCall = chatMessage.getToolCalls().get(0);
var result = functionExecutor.execute(chatToolCall.getFunction());
messages.add(chatMessage);
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model("N/A")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
futureChat = openAI.chatCompletions().create(chatRequest);
chatResponse = futureChat.join();
System.out.println(chatResponse.firstContent());
}

public void demoCallChatWithVisionExternalImage() {
var chatRequest = ChatRequest.builder()
.model("N/A")
.messages(List.of(
new ChatMsgUser(List.of(
new ContentPartText(
"What do you see in the image? Give in details in no more than 100 words."),
new ContentPartImage(new ImageUrl(
"https://upload.wikimedia.org/wikipedia/commons/e/eb/Machu_Picchu%2C_Peru.jpg"))))))
.temperature(0.0)
.maxTokens(500)
.build();
var chatResponse = openAI.chatCompletions().createStream(chatRequest).join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();
}

public void demoCallChatWithVisionLocalImage() {
var chatRequest = ChatRequest.builder()
.model("N/A")
.messages(List.of(
new ChatMsgUser(List.of(
new ContentPartText(
"What do you see in the image? Give in details in no more than 100 words."),
new ContentPartImage(loadImageAsBase64("src/demo/resources/machupicchu.jpg"))))))
.temperature(0.0)
.maxTokens(500)
.build();
var chatResponse = openAI.chatCompletions().createStream(chatRequest).join();
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();
}

private static ImageUrl loadImageAsBase64(String imagePath) {
try {
Path path = Paths.get(imagePath);
byte[] imageBytes = Files.readAllBytes(path);
String base64String = Base64.getEncoder().encodeToString(imageBytes);
var extension = imagePath.substring(imagePath.lastIndexOf(".") + 1);
var prefix = "data:image/" + extension + ";base64,";
return new ImageUrl(prefix + base64String);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

public static void main(String[] args) {
var baseUrl = System.getenv("AZURE_OPENAI_BASE_URL");
var apiKey = System.getenv("AZURE_OPENAI_API_KEY");
var apiVersion = System.getenv("AZURE_OPENAI_API_VERSION");
// Services like Azure OpenAI don't require a model (endpoints have built-in model)
var demo = new AzureChatServiceDemo(baseUrl, apiKey, apiVersion);


demo.addTitleAction("Call Chat (Blocking Approach)", demo::demoCallChatBlocking);
if (baseUrl.contains("gpt-35-turbo")) {
demo.addTitleAction("Call Chat with Functions", demo::demoCallChatWithFunctions);
} else if (baseUrl.contains("gpt-4")){
demo.addTitleAction("Call Chat (Streaming Approach)", demo::demoCallChatStreaming);
demo.addTitleAction("Call Chat with Vision (External image)", demo::demoCallChatWithVisionExternalImage);
demo.addTitleAction("Call Chat with Vision (Local image)", demo::demoCallChatWithVisionLocalImage);
}

demo.run();
}
}
Loading
Loading