Skip to content

Commit 50a6f8d

Browse files
authored
Merge pull request #35 from sashirestela/release_1_3_0
Release 1.3.0
2 parents 0d9741c + 9ca9aa3 commit 50a6f8d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+264
-502
lines changed

.devfile.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
schemaVersion: 2.2.2
1+
schemaVersion: 2.2.0
22
metadata:
33
attributes:
44
metadata-name-field: generateName
@@ -23,4 +23,4 @@ components:
2323
- name: m2
2424
volume:
2525
size: 5Gi
26-
commands: []
26+
commands: []

README.md

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,8 @@ var futureImage = openai.images().create(imageRequest);
134134
var imageResponse = futureImage.join();
135135
imageResponse.stream().forEach(img -> System.out.println("\n" + img.getUrl()));
136136
```
137-
#### Chat Completion Service
138-
Example to call the Chat Completion service to ask a question and wait for an answer. We are printing out it in the console:
137+
#### Chat Completion Service (blocking mode)
138+
Example to call the Chat Completion service to ask a question and wait for a full answer. We are printing out it in the console:
139139
```java
140140
var chatRequest = ChatRequest.builder()
141141
.model("gpt-3.5-turbo-1106")
@@ -149,6 +149,24 @@ var futureChat = openai.chatCompletions().create(chatRequest);
149149
var chatResponse = futureChat.join();
150150
System.out.println(chatResponse.firstContent());
151151
```
152+
#### Chat Completion Service (streaming mode)
153+
Example to call the Chat Completion service to ask a question and wait for an answer in partial message deltas. We are printing out it in the console as soon as each delta is arriving:
154+
```java
155+
var chatRequest = ChatRequest.builder()
156+
.model("gpt-3.5-turbo-1106")
157+
.messages(List.of(
158+
new ChatMsgSystem("You are an expert in AI."),
159+
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words.")))
160+
.temperature(0.0)
161+
.maxTokens(300)
162+
.build();
163+
var futureChat = openai.chatCompletions().createStream(chatRequest);
164+
var chatResponse = futureChat.join();
165+
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
166+
.map(ChatResponse::firstContent)
167+
.forEach(System.out::print);
168+
System.out.println();
169+
```
152170
#### Chat Completion Service with Functions
153171
This functionality empowers the Chat Completion service to solve specific problems to our context. In this example we are setting three functions and we are entering a prompt that will require to call one of them (the function ```product```). For setting functions we are using additional classes which implements the interface ```Functional```. Those classes define a field by each function argument, annotating them to describe them and each class must override the ```execute``` method with the function's logic. Note that we are using the ```FunctionExecutor``` utility class to enroll the functions and to execute the function selected by the ```openai.chatCompletions()``` calling:
154172
```java
@@ -232,6 +250,26 @@ public static class RunAlarm implements Functional {
232250
}
233251
}
234252
```
253+
#### Chat Completion Service with Vision
254+
Example to call the Chat Completion service to allow the model to take in images and answer questions about them:
255+
```java
256+
var chatRequest = ChatRequest.builder()
257+
.model("gpt-4-vision-preview")
258+
.messages(List.of(
259+
new ChatMsgUser(List.of(
260+
new ContentPartText(
261+
"What do you see in the image? Give in details in no more than 100 words."),
262+
new ContentPartImage(new ImageUrl(
263+
"https://upload.wikimedia.org/wikipedia/commons/e/eb/Machu_Picchu%2C_Peru.jpg"))))))
264+
.temperature(0.0)
265+
.maxTokens(500)
266+
.build();
267+
var chatResponse = openai.chatCompletions().createStream(chatRequest).join();
268+
chatResponse.filter(chatResp -> chatResp.firstContent() != null)
269+
.map(chatResp -> chatResp.firstContent())
270+
.forEach(System.out::print);
271+
System.out.println();
272+
```
235273

236274
## ✳ Run Examples
237275
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:

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>io.github.sashirestela</groupId>
88
<artifactId>simple-openai</artifactId>
9-
<version>1.2.5</version>
9+
<version>1.3.0</version>
1010
<packaging>jar</packaging>
1111

1212
<name>simple-openai</name>

src/demo/java/io/github/sashirestela/openai/demo/ChatServiceDemo.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,8 @@ public ChatServiceDemo() {
2828
modelIdToUse = "gpt-3.5-turbo-1106";
2929
chatRequest = ChatRequest.builder()
3030
.model(modelIdToUse)
31-
.messages(List.of(
32-
new ChatMsgSystem("You are an expert in AI."),
33-
new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words.")))
31+
.message(new ChatMsgSystem("You are an expert in AI."))
32+
.message(new ChatMsgUser("Write a technical article about ChatGPT, no more than 100 words."))
3433
.temperature(0.0)
3534
.maxTokens(300)
3635
.build();

src/main/java/io/github/sashirestela/openai/domain/assistant/ThreadRunRequest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,15 @@
1313

1414
@Getter
1515
@Builder
16-
@JsonInclude(Include.NON_NULL)
16+
@JsonInclude(Include.NON_EMPTY)
1717
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1818
public class ThreadRunRequest {
1919

2020
private String assistantId;
2121
private String model;
2222
private String instructions;
23-
@JsonInclude(Include.NON_EMPTY)
24-
@Singular
25-
private List<AssistantTool> tools;
23+
private String additionalInstructions;
24+
@Singular private List<AssistantTool> tools;
2625
private Map<String, String> metadata;
2726

2827
}

src/main/java/io/github/sashirestela/openai/domain/audio/AudioResponse.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
public class AudioResponse {
1313

1414
private String text;
15-
1615
private String task;
17-
1816
private String language;
19-
2017
private Double duration;
21-
2218
private List<Segment> segments;
2319

2420
}

src/main/java/io/github/sashirestela/openai/domain/audio/AudioSpeechRequest.java

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,22 @@
22

33
import com.fasterxml.jackson.annotation.JsonInclude;
44
import com.fasterxml.jackson.annotation.JsonInclude.Include;
5-
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
6+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
67

78
import lombok.Builder;
89
import lombok.Getter;
910
import lombok.NonNull;
1011

1112
@Getter
1213
@Builder
14+
@JsonInclude(Include.NON_EMPTY)
15+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1316
public class AudioSpeechRequest {
1417

15-
@NonNull
16-
private String model;
17-
18-
@NonNull
19-
private String input;
20-
21-
@NonNull
22-
private Voice voice;
23-
24-
@JsonInclude(Include.NON_NULL)
25-
@JsonProperty("response_format")
18+
@NonNull private String model;
19+
@NonNull private String input;
20+
@NonNull private Voice voice;
2621
private SpeechRespFmt responseFormat;
27-
28-
@JsonInclude(Include.NON_NULL)
2922
private Double speed;
3023
}

src/main/java/io/github/sashirestela/openai/domain/audio/AudioTranscribeRequest.java

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import com.fasterxml.jackson.annotation.JsonInclude;
66
import com.fasterxml.jackson.annotation.JsonInclude.Include;
7-
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
8+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
89

910
import lombok.Builder;
1011
import lombok.Getter;
@@ -13,26 +14,15 @@
1314

1415
@Getter
1516
@Builder
17+
@JsonInclude(Include.NON_EMPTY)
18+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1619
public class AudioTranscribeRequest {
1720

18-
@NonNull
19-
protected Path file;
20-
21-
@NonNull
22-
protected String model;
23-
24-
@JsonInclude(Include.NON_NULL)
25-
protected String prompt;
26-
27-
@JsonInclude(Include.NON_NULL)
28-
protected Double temperature;
29-
30-
@JsonInclude(Include.NON_NULL)
21+
@NonNull private Path file;
22+
@NonNull private String model;
23+
private String prompt;
24+
private Double temperature;
3125
private String language;
32-
33-
@With
34-
@JsonInclude(Include.NON_NULL)
35-
@JsonProperty("response_format")
36-
protected AudioRespFmt responseFormat;
26+
@With private AudioRespFmt responseFormat;
3727

3828
}

src/main/java/io/github/sashirestela/openai/domain/audio/AudioTranslateRequest.java

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import com.fasterxml.jackson.annotation.JsonInclude;
66
import com.fasterxml.jackson.annotation.JsonInclude.Include;
7-
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
8+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
89

910
import lombok.Builder;
1011
import lombok.Getter;
@@ -13,23 +14,14 @@
1314

1415
@Getter
1516
@Builder
17+
@JsonInclude(Include.NON_EMPTY)
18+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1619
public class AudioTranslateRequest {
1720

18-
@NonNull
19-
protected Path file;
20-
21-
@NonNull
22-
protected String model;
23-
24-
@JsonInclude(Include.NON_NULL)
25-
protected String prompt;
26-
27-
@JsonInclude(Include.NON_NULL)
28-
protected Double temperature;
29-
30-
@With
31-
@JsonInclude(Include.NON_NULL)
32-
@JsonProperty("response_format")
33-
protected AudioRespFmt responseFormat;
21+
@NonNull private Path file;
22+
@NonNull private String model;
23+
private String prompt;
24+
private Double temperature;
25+
@With private AudioRespFmt responseFormat;
3426

3527
}

src/main/java/io/github/sashirestela/openai/domain/audio/Segment.java

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import java.util.List;
44

5-
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
6+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
67

78
import lombok.Getter;
89
import lombok.NoArgsConstructor;
@@ -11,29 +12,18 @@
1112
@NoArgsConstructor
1213
@Getter
1314
@ToString
15+
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
1416
public class Segment {
1517

1618
private Integer id;
17-
1819
private Integer seek;
19-
2020
private Double start;
21-
2221
private Double end;
23-
2422
private String text;
25-
2623
private List<Integer> tokens;
27-
2824
private Double temperature;
29-
30-
@JsonProperty("avg_logprob")
31-
private Double averageLogProb;
32-
33-
@JsonProperty("compression_ratio")
25+
private Double avgLogprob;
3426
private Double compressionRatio;
35-
36-
@JsonProperty("no_speech_prob")
3727
private Double noSpeechProb;
3828

3929
}

0 commit comments

Comments
 (0)