Skip to content

Commit

Permalink
Merge pull request #16 from sashirestela/major-changes-in-openai-api
Browse files Browse the repository at this point in the history
Major Changes in Openai API
  • Loading branch information
sashirestela authored Nov 21, 2023
2 parents 27bede6 + 8e79d80 commit db4387c
Show file tree
Hide file tree
Showing 103 changed files with 1,717 additions and 567 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,8 @@ target/
dependency-reduced-pom.xml
.flattened-pom.xml
.factorypath
*.log
*.log

src/demo/resources/response.mp3
.replit
replit.nix
62 changes: 0 additions & 62 deletions .replit

This file was deleted.

86 changes: 53 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,18 @@ Simple-OpenAI uses the [CleverClient](https://github.com/sashirestela/cleverclie


## ✅ Supported Services
Full support for all of the OpenAI services:
Full support for all of the OpenAI services, including the latest changes announced at the [DevDay](https://openai.com/blog/new-models-and-developer-products-announced-at-devday) on Nov 6th, 2023:

* Text to speech (as part of Audio)
* Speech to text (as part of Audio)
* Text generation (as part of Chat)
* Function calling (as part of Chat)
* Image to text (as part of Chat)
* Text to image (as part of Image)
* Embeddings
* Fine tuning

NOTE: Beta services are not included yet.

![Services](media/supported_services.png)

Expand Down Expand Up @@ -77,6 +88,27 @@ var openai = SimpleOpenAI.builder()
After you have created a SimpleOpenAI object, you are ready to call its services in order to communicate to OpenAI Api. Let's see some examples.

#### Audio Service
Example to call th Audio service to transform text to audio. We are requesting to receive the audio in binary format (InputStream):
```java
var speechRequest = AudioSpeechRequest.builder()
.model("tts-1")
.input("Hello world, welcome to the AI universe!")
.voice(Voice.ALLOY)
.responseFormat(SpeechRespFmt.MP3)
.speed(1.0)
.build();
var futureSpeech = openAI.audios().speak(speechRequest);
var speechResponse = futureSpeech.join();
try {
var audioFile = new FileOutputStream("response.mp3");
audioFile.write(speechResponse.readAllBytes());
System.out.println(audioFile.getChannel().size() + " bytes");
audioFile.close();
} catch (Exception e) {
e.printStackTrace();
}
```

Example to call the Audio service to transcribe an audio to text. We are requesting to receive the transcription in plain text format (see the name of the method):
```java
var audioRequest = AudioTranscribeRequest.builder()
Expand All @@ -95,6 +127,7 @@ var imageRequest = ImageRequest.builder()
.n(2)
.size(Size.X256)
.responseFormat(ImageRespFmt.URL)
.model("dall-e-2")
.build();
var futureImage = openai.images().create(imageRequest);
var imageResponse = futureImage.join();
Expand All @@ -104,10 +137,10 @@ imageResponse.stream().forEach(img -> System.out.println("\n" + img.getUrl()));
Example to call the Chat Completion service to ask a question and wait for an answer. We are printing out it in the console:
```java
var chatRequest = ChatRequest.builder()
.model("gpt-3.5-turbo-16k-0613")
.model("gpt-3.5-turbo-1106")
.messages(List.of(
new ChatMessage(Role.SYSTEM, "You are an expert in AI."),
new ChatMessage(Role.USER, "Write an article about ChatGPT, no more than 100 words.")))
new ChatMsgSystem("You are an expert in AI."),
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words.")))
.temperature(0.0)
.maxTokens(300)
.build();
Expand Down Expand Up @@ -138,32 +171,26 @@ public void demoCallChatWithFunctions() {
.description("Run an alarm")
.functionalClass(RunAlarm.class)
.build());
var messages = new ArrayList<ChatMessage>();
messages.add(new ChatMessage(Role.USER, "What is the product of 123 and 456?"));
var chatRequest = ChatRequest.builder()
.model("gpt-3.5-turbo-16k-0613")
.messages(messages)
.functions(functionExecutor.getFunctions())
.functionCall("auto")
.build();
var futureChat = openai.chatCompletions().create(chatRequest);
var messages = new ArrayList<ChatMsg>();
messages.add(new ChatMsgUser("What is the product of 123 and 456?"));
chatRequest = ChatRequest.builder()
.model("gpt-3.5-turbo-1106")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
var result = functionExecutor.execute(chatMessage.getFunctionCall());
var chatToolCall = chatMessage.getToolCalls().get(0);
var result = functionExecutor.execute(chatToolCall.getFunction());
messages.add(chatMessage);
messages.add(
ChatMessage.builder()
.role(Role.FUNCTION)
.content(result.toString())
.name(chatMessage.getFunctionCall().getName())
.build());
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model("gpt-3.5-turbo-16k-0613")
.messages(messages)
.functions(functionExecutor.getFunctions())
.functionCall("auto")
.build();
futureChat = openai.chatCompletions().create(chatRequest);
.model("gpt-3.5-turbo-1106")
.messages(messages)
.tools(functionExecutor.getToolFunctions())
.build();
futureChat = openAI.chatCompletions().create(chatRequest);
chatResponse = futureChat.join();
System.out.println(chatResponse.firstContent());
}
Expand Down Expand Up @@ -205,13 +232,6 @@ public static class RunAlarm implements Functional {
}
```


## 🎬 Demo
Demonstration of the Chat functionality in streaming mode. The application prints the information to the console as soon as it is received from the server token by token, in response to our prompt:

![Demo](media/demo_chat_stream.gif)


## ✳ Run Examples
Examples for each OpenAI service have been created in the folder [demo](https://github.com/sashirestela/simple-openai/tree/main/src/demo/java/io/github/sashirestela/openai/demo) and you can follow the next steps to execute them:
* Clone this respository:
Expand Down
Binary file removed media/demo_chat_stream.gif
Binary file not shown.
Binary file modified media/supported_services.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.sashirestela</groupId>
<artifactId>simple-openai</artifactId>
<version>0.5.0</version>
<version>1.0.0</version>
<packaging>jar</packaging>

<name>simple-openai</name>
Expand Down Expand Up @@ -52,7 +52,7 @@
<maven.compiler.release>11</maven.compiler.release>
<!-- Dependencies Versions -->
<slf4j.version>[2.0.9,3.0.0)</slf4j.version>
<cleverclient.version>0.7.0</cleverclient.version>
<cleverclient.version>0.8.1</cleverclient.version>
<lombok.version>[1.18.30,2.0.0)</lombok.version>
<jackson.version>[2.15.2,3.0.0)</jackson.version>
<json.schema.version>[4.31.1,5.0.0)</json.schema.version>
Expand Down
18 changes: 0 additions & 18 deletions replit.nix

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
package io.github.sashirestela.openai.demo;

import java.io.FileOutputStream;
import java.nio.file.Paths;

import io.github.sashirestela.openai.domain.audio.AudioRespFmt;
import io.github.sashirestela.openai.domain.audio.AudioSpeechRequest;
import io.github.sashirestela.openai.domain.audio.AudioTranscribeRequest;
import io.github.sashirestela.openai.domain.audio.AudioTranslateRequest;
import io.github.sashirestela.openai.domain.audio.SpeechRespFmt;
import io.github.sashirestela.openai.domain.audio.Voice;

public class AudioServiceDemo extends AbstractDemo {
private static final String MODEL_TTS = "tts-1";
private static final String MODEL = "whisper-1";

private String speechFileName;
private String fileName;

public AudioServiceDemo() {
this.speechFileName = "src/demo/resources/response.mp3";
this.fileName = "src/demo/resources/hello_audio.mp3";
}

public void demoCallAudioSpeech() {
var speechRequest = AudioSpeechRequest.builder()
.model(MODEL_TTS)
.input("Hello world, welcome to the AI universe!")
.voice(Voice.ALLOY)
.responseFormat(SpeechRespFmt.MP3)
.speed(1.0)
.build();
var futureSpeech = openAI.audios().speak(speechRequest);
var speechResponse = futureSpeech.join();
try {
var audioFile = new FileOutputStream(speechFileName);
audioFile.write(speechResponse.readAllBytes());
System.out.println(audioFile.getChannel().size() + " bytes");
audioFile.close();
} catch (Exception e) {
e.printStackTrace();
}
}

public void demoCallAudioTranscription() {
var audioRequest = AudioTranscribeRequest.builder()
.file(Paths.get(fileName))
Expand Down Expand Up @@ -60,6 +87,7 @@ public void demoCallAudioTranslationPlain() {
public static void main(String[] args) {
var demo = new AudioServiceDemo();

demo.addTitleAction("Call Audio Speech", demo::demoCallAudioSpeech);
demo.addTitleAction("Call Audio Transcription", demo::demoCallAudioTranscription);
demo.addTitleAction("Call Audio Translation", demo::demoCallAudioTranslation);
demo.addTitleAction("Call Audio Transcription Plain", demo::demoCallAudioTranscriptionPlain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;

import io.github.sashirestela.openai.domain.chat.ChatFunction;
import io.github.sashirestela.openai.domain.chat.ChatMessage;
import io.github.sashirestela.openai.domain.chat.ChatRequest;
import io.github.sashirestela.openai.domain.chat.ChatResponse;
import io.github.sashirestela.openai.domain.chat.Role;
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 io.github.sashirestela.openai.function.Functional;

Expand All @@ -20,12 +22,12 @@ public class ChatServiceDemo extends AbstractDemo {
private String modelIdToUse;

public ChatServiceDemo() {
modelIdToUse = "gpt-3.5-turbo-16k-0613";
modelIdToUse = "gpt-3.5-turbo-1106";
chatRequest = ChatRequest.builder()
.model(modelIdToUse)
.messages(List.of(
new ChatMessage(Role.SYSTEM, "You are an expert in AI."),
new ChatMessage(Role.USER, "Write a technical article about ChatGPT, no more than 100 words.")))
new ChatMsgSystem("You are an expert in AI."),
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words.")))
.temperature(0.0)
.maxTokens(300)
.build();
Expand Down Expand Up @@ -66,30 +68,24 @@ public void demoCallChatWithFunctions() {
.description("Run an alarm")
.functionalClass(RunAlarm.class)
.build());
var messages = new ArrayList<ChatMessage>();
messages.add(new ChatMessage(Role.USER, "What is the product of 123 and 456?"));
var messages = new ArrayList<ChatMsg>();
messages.add(new ChatMsgUser("What is the product of 123 and 456?"));
chatRequest = ChatRequest.builder()
.model(modelIdToUse)
.messages(messages)
.functions(functionExecutor.getFunctions())
.functionCall("auto")
.tools(functionExecutor.getToolFunctions())
.build();
var futureChat = openAI.chatCompletions().create(chatRequest);
var chatResponse = futureChat.join();
var chatMessage = chatResponse.firstMessage();
var result = functionExecutor.execute(chatMessage.getFunctionCall());
var chatToolCall = chatMessage.getToolCalls().get(0);
var result = functionExecutor.execute(chatToolCall.getFunction());
messages.add(chatMessage);
messages.add(
ChatMessage.builder()
.role(Role.FUNCTION)
.content(result.toString())
.name(chatMessage.getFunctionCall().getName())
.build());
messages.add(new ChatMsgTool(result.toString(), chatToolCall.getId()));
chatRequest = ChatRequest.builder()
.model(modelIdToUse)
.messages(messages)
.functions(functionExecutor.getFunctions())
.functionCall("auto")
.tools(functionExecutor.getToolFunctions())
.build();
futureChat = openAI.chatCompletions().create(chatRequest);
chatResponse = futureChat.join();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ public class CompletionServiceDemo extends AbstractDemo {
private String modelIdToUse;

public CompletionServiceDemo() {
modelIdToUse = "text-davinci-003";
modelIdToUse = "gpt-3.5-turbo-instruct";
completionRequest = CompletionRequest.builder()
.model(modelIdToUse)
.prompt("Write a technical article about ChatGPT, no more than 100 words.")
.prompt("Tell me the Pythagorean theorem in no more than 50 words.")
.temperature(0.0)
.maxTokens(300)
.seed(1)
.build();
}

Expand Down
Loading

0 comments on commit db4387c

Please sign in to comment.