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

Refactoring code #51

Merged
merged 1 commit into from
Feb 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.sashirestela.openai.demo;

import java.util.ArrayList;

import io.github.sashirestela.openai.SimpleOpenAIAnyscale;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
Expand All @@ -13,34 +14,31 @@
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 ChatAnyscaleServiceDemo extends AbstractDemo {

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


private ChatRequest chatRequest;


public ChatAnyscaleServiceDemo(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();
.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);
.map(ChatResponse::firstContent)
.forEach(System.out::print);
System.out.println();
}

Expand All @@ -53,30 +51,30 @@ public void demoCallChatBlocking() {
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());
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());
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());
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();
.model(MODEL)
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
Expand All @@ -85,18 +83,18 @@ public void demoCallChatWithFunctions() {
messages.add(chatMessage);
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model(MODEL)
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
.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 ChatAnyscaleServiceDemo(apiKey, MODEL);

demo.addTitleAction("Call Chat (Streaming Approach)", demo::demoCallChatStreaming);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package io.github.sashirestela.openai.demo;

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;

import io.github.sashirestela.openai.SimpleOpenAIAzure;
import io.github.sashirestela.openai.demo.ChatServiceDemo.Product;
Expand All @@ -16,38 +22,32 @@
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 ChatAzureServiceDemo extends AbstractDemo {
private ChatRequest chatRequest;

public ChatAzureServiceDemo(String baseUrl, String apiKey, String apiVersion) {
super(SimpleOpenAIAzure.builder()
.apiKey(apiKey)
.baseUrl(baseUrl)
.apiVersion(apiVersion)
.build());
.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();
.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);
.map(ChatResponse::firstContent)
.forEach(System.out::print);
System.out.println();
}

Expand All @@ -60,30 +60,30 @@ public void demoCallChatBlocking() {
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());
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());
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());
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();
.model("N/A")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
Expand All @@ -92,49 +92,49 @@ public void demoCallChatWithFunctions() {
messages.add(chatMessage);
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model("N/A")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
.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();
.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);
.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();
.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);
.map(chatResp -> chatResp.firstContent())
.forEach(System.out::print);
System.out.println();
}

Expand All @@ -156,14 +156,13 @@ 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 ChatAzureServiceDemo(baseUrl, apiKey, apiVersion);

var demo = new ChatAzureServiceDemo(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")){
} 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);
Expand Down
Loading
Loading