-
Notifications
You must be signed in to change notification settings - Fork 28
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
Changes from 15 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
e1ca88f
refactor SimpleOpenAI into a base class + a class with OpenAI specifi…
the-gigi 8a7e9a6
added dedicated classes for Anyscale and Azure OpenAI + demos
the-gigi b731d6e
remove unused constructor
the-gigi 4df6fd4
optimize imports
the-gigi 1d215f8
revert optimize imports
the-gigi c524404
reverted the refactoring of core functionality into a base class
the-gigi 8359999
throw 'not implemented' instead of returning null
the-gigi d897f0a
refactor to base class instead of interface
the-gigi ee44c38
parameterize apiVersion + chat completion with image works
the-gigi 848d1b5
make base url NotNull
the-gigi ae1afa9
get rid of redundant Getter annotationations
the-gigi 5bc64a6
fix constructor docmentation
the-gigi f4c2e8b
moved tool choice fix to OpenAI
the-gigi 497d89b
addressed review comments
the-gigi 2ce5a9b
added tests for new classes, BaseSimpleOPenAIArgs construction and re…
the-gigi 0d91616
address more review comments
the-gigi 39203bf
refactored updateRequest() function
the-gigi 9ecae33
updated tool choice verification
the-gigi fb1a45c
fix constructor parameters
the-gigi b553ab5
fixed class comment for SimpleOpenAI
the-gigi bac3158
fixed more constructor arguments
the-gigi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
108 changes: 108 additions & 0 deletions
108
src/demo/java/io/github/sashirestela/openai/demo/AnyscaleChatServiceDemo.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,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 { | ||
|
||
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(); | ||
} | ||
} |
174 changes: 174 additions & 0 deletions
174
src/demo/java/io/github/sashirestela/openai/demo/AzureChatServiceDemo.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,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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be consistent with the other demos, rename it to ChatAzureServiceDemo There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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