Skip to content

Commit 28e12f2

Browse files
committed
Support edits
1 parent 0136545 commit 28e12f2

12 files changed

Lines changed: 357 additions & 2 deletions

File tree

docs/docs/reference/edits.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Edits
3+
---
4+
5+
!!! Note
6+
7+
Please build the client before calling, the build code is as follows:
8+
9+
```java
10+
OpenAiClient client = OpenAiClient.builder()
11+
.apiHost("https://api.openai.com")
12+
.apiKey(System.getProperty("openai.token"))
13+
.build();
14+
```
15+
16+
`System.getProperty("openai.token")` is the key to access the API authorization.
17+
18+
### Create edit
19+
20+
---
21+
22+
Creates a new edit for the provided input, instruction, and parameters.
23+
24+
```java
25+
EditEntity configure = EditEntity.builder()
26+
.model(EditModel.TEXT_DAVINCI_EDIT_001)
27+
.input("Hello OpenAi Java SDK")
28+
.instruction("Fix the spelling mistakes")
29+
.build();
30+
client.edit(configure);
31+
```
32+
33+
Returns
34+
35+
```json
36+
{
37+
"object": "edit",
38+
"created": 1589478378,
39+
"choices": [
40+
{
41+
"text": "What day of the week is it?",
42+
"index": 0,
43+
}
44+
],
45+
"usage": {
46+
"prompt_tokens": 25,
47+
"completion_tokens": 32,
48+
"total_tokens": 57
49+
}
50+
}
51+
```

docs/docs/reference/edits.zh.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
---
2+
title: Edits
3+
---
4+
5+
!!! Note
6+
7+
调用前请先构建客户端,构建代码如下:
8+
9+
```java
10+
OpenAiClient client = OpenAiClient.builder()
11+
.apiHost("https://api.openai.com")
12+
.apiKey(System.getProperty("openai.token"))
13+
.build();
14+
```
15+
16+
`System.getProperty("openai.token")` 是访问 API 授权的关键。
17+
18+
### Create edit
19+
20+
---
21+
22+
为提供的输入、指令和参数创建新的编辑。
23+
24+
```java
25+
EditEntity configure = EditEntity.builder()
26+
.model(EditModel.TEXT_DAVINCI_EDIT_001)
27+
.input("Hello OpenAi Java SDK")
28+
.instruction("Fix the spelling mistakes")
29+
.build();
30+
client.edit(configure);
31+
```
32+
33+
Returns
34+
35+
```json
36+
{
37+
"object": "edit",
38+
"created": 1589478378,
39+
"choices": [
40+
{
41+
"text": "What day of the week is it?",
42+
"index": 0,
43+
}
44+
],
45+
"usage": {
46+
"prompt_tokens": 25,
47+
"completion_tokens": 32,
48+
"total_tokens": 57
49+
}
50+
}
51+
```

docs/mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ nav:
6666
- reference/embeddings.md
6767
- reference/audio.md
6868
- reference/moderations.md
69+
- reference/edits.md
6970
- Provider:
7071
- reference/provider/azure.md
7172
- released.md

src/main/java/org/devlive/sdk/openai/DefaultApi.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import okhttp3.RequestBody;
66
import org.devlive.sdk.openai.entity.ChatEntity;
77
import org.devlive.sdk.openai.entity.CompletionEntity;
8+
import org.devlive.sdk.openai.entity.EditEntity;
89
import org.devlive.sdk.openai.entity.EmbeddingEntity;
910
import org.devlive.sdk.openai.entity.ImageEntity;
1011
import org.devlive.sdk.openai.entity.ModelEntity;
@@ -13,6 +14,7 @@
1314
import org.devlive.sdk.openai.response.AudioResponse;
1415
import org.devlive.sdk.openai.response.ChatResponse;
1516
import org.devlive.sdk.openai.response.CompleteResponse;
17+
import org.devlive.sdk.openai.response.EditResponse;
1618
import org.devlive.sdk.openai.response.EmbeddingResponse;
1719
import org.devlive.sdk.openai.response.ImageResponse;
1820
import org.devlive.sdk.openai.response.ModelResponse;
@@ -121,4 +123,12 @@ Single<AudioResponse> fetchAudioTranscriptions(@Url String url,
121123
@POST
122124
Single<ModerationResponse> fetchModerations(@Url String url,
123125
@Body ModerationEntity configure);
126+
127+
/**
128+
* Creates a new edit for the provided input, instruction, and parameters.
129+
* 为提供的输入、指令和参数创建新的编辑。
130+
*/
131+
@POST
132+
Single<EditResponse> fetchEdits(@Url String url,
133+
@Body EditEntity configure);
124134
}

src/main/java/org/devlive/sdk/openai/DefaultClient.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.devlive.sdk.openai.entity.AudioEntity;
88
import org.devlive.sdk.openai.entity.ChatEntity;
99
import org.devlive.sdk.openai.entity.CompletionEntity;
10+
import org.devlive.sdk.openai.entity.EditEntity;
1011
import org.devlive.sdk.openai.entity.EmbeddingEntity;
1112
import org.devlive.sdk.openai.entity.ImageEntity;
1213
import org.devlive.sdk.openai.entity.ModelEntity;
@@ -17,6 +18,7 @@
1718
import org.devlive.sdk.openai.response.AudioResponse;
1819
import org.devlive.sdk.openai.response.ChatResponse;
1920
import org.devlive.sdk.openai.response.CompleteResponse;
21+
import org.devlive.sdk.openai.response.EditResponse;
2022
import org.devlive.sdk.openai.response.EmbeddingResponse;
2123
import org.devlive.sdk.openai.response.ImageResponse;
2224
import org.devlive.sdk.openai.response.ModelResponse;
@@ -121,6 +123,12 @@ public ModerationResponse moderations(ModerationEntity configure)
121123
.blockingGet();
122124
}
123125

126+
public EditResponse edit(EditEntity configure)
127+
{
128+
return this.api.fetchEdits(ProviderUtils.getUrl(provider, UrlModel.FETCH_EDITS), configure)
129+
.blockingGet();
130+
}
131+
124132
public void close()
125133
{
126134
if (ObjectUtils.isNotEmpty(this.client)) {
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
package org.devlive.sdk.openai.entity;
2+
3+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4+
import com.fasterxml.jackson.annotation.JsonProperty;
5+
import lombok.AllArgsConstructor;
6+
import lombok.Builder;
7+
import lombok.Data;
8+
import lombok.NoArgsConstructor;
9+
import lombok.ToString;
10+
import org.apache.commons.lang3.ObjectUtils;
11+
import org.apache.commons.lang3.StringUtils;
12+
import org.devlive.sdk.openai.exception.ParamException;
13+
import org.devlive.sdk.openai.model.EditModel;
14+
15+
import java.util.Arrays;
16+
17+
@Data
18+
@Builder
19+
@ToString
20+
@NoArgsConstructor
21+
@AllArgsConstructor
22+
@JsonIgnoreProperties(ignoreUnknown = true)
23+
@Deprecated
24+
public class EditEntity
25+
{
26+
/**
27+
* ID of the model to use. You can use the text-davinci-edit-001 or code-davinci-edit-001 model with this endpoint.
28+
* 要使用的模型的 ID。您可以对此端点使用 text-davinci-edit-001 或 code-davinci-edit-001 模型。
29+
*
30+
* @see org.devlive.sdk.openai.model.EditModel
31+
*/
32+
@JsonProperty(value = "model")
33+
private String model;
34+
35+
/**
36+
* The input text to use as a starting point for the edit.
37+
* 用作编辑起点的输入文本。
38+
*/
39+
@JsonProperty(value = "input")
40+
private String input;
41+
42+
/**
43+
* The instruction that tells the model how to edit the prompt.
44+
* 告诉模型如何编辑提示的指令。
45+
*/
46+
@JsonProperty(value = "instruction")
47+
private String instruction;
48+
49+
/**
50+
* How many edits to generate for the input and instruction.
51+
* 为输入和指令生成多少编辑。
52+
*/
53+
@JsonProperty(value = "n")
54+
private Integer count;
55+
56+
/**
57+
* What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
58+
* 使用什么采样温度,介于 0 和 2 之间。较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更加集中和确定性。
59+
* <p>
60+
* We generally recommend altering this or top_p but not both.
61+
* 我们通常建议更改此值或 top_p,但不要同时更改两者。
62+
*/
63+
@JsonProperty(value = "temperature")
64+
private Double temperature;
65+
66+
/**
67+
* An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
68+
* 温度采样的替代方法称为核采样,其中模型考虑具有 top_p 概率质量的标记的结果。因此 0.1 意味着仅考虑包含前 10% 概率质量的标记。
69+
* <p>
70+
* We generally recommend altering this or temperature but not both.
71+
* 我们通常建议更改此值或温度,但不能同时更改两者。
72+
*/
73+
@JsonProperty(value = "top_p")
74+
private Double topP;
75+
76+
private EditEntity(EditEntityBuilder builder)
77+
{
78+
if (ObjectUtils.isEmpty(builder.model)) {
79+
throw new ParamException(String.format("Invalid model, Must be one of %s", Arrays.toString(EditModel.values())));
80+
}
81+
this.model = builder.model;
82+
83+
if (StringUtils.isEmpty(builder.input)) {
84+
builder.input(null);
85+
}
86+
this.input = builder.input;
87+
88+
if (StringUtils.isEmpty(builder.instruction)) {
89+
builder.instruction(null);
90+
}
91+
this.instruction = builder.instruction;
92+
93+
if (ObjectUtils.isEmpty(builder.count)) {
94+
builder.count(1);
95+
}
96+
this.count = builder.count;
97+
98+
if (ObjectUtils.isEmpty(builder.temperature)) {
99+
builder.temperature(1D);
100+
}
101+
this.temperature = builder.temperature;
102+
103+
if (ObjectUtils.isEmpty(builder.topP)) {
104+
builder.topP(1D);
105+
}
106+
this.topP = builder.topP;
107+
}
108+
109+
public static class EditEntityBuilder
110+
{
111+
public EditEntityBuilder model(EditModel model)
112+
{
113+
this.model = model.getName();
114+
return this;
115+
}
116+
117+
public EditEntityBuilder input(String input)
118+
{
119+
if (StringUtils.isEmpty(input)) {
120+
throw new ParamException("Invalid input must be not empty");
121+
}
122+
this.input = input;
123+
return this;
124+
}
125+
126+
public EditEntityBuilder instruction(String instruction)
127+
{
128+
if (StringUtils.isEmpty(instruction)) {
129+
throw new ParamException("Invalid instruction must be not empty");
130+
}
131+
this.instruction = instruction;
132+
return this;
133+
}
134+
135+
public EditEntityBuilder temperature(Double temperature)
136+
{
137+
if (temperature < 0 || temperature > 2) {
138+
throw new ParamException(String.format("Invalid temperature: %s , between 0 and 2", temperature));
139+
}
140+
this.temperature = temperature;
141+
return this;
142+
}
143+
144+
public EditEntity build()
145+
{
146+
return new EditEntity(this);
147+
}
148+
}
149+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.devlive.sdk.openai.model;
2+
3+
public enum EditModel
4+
{
5+
TEXT_DAVINCI_EDIT_001("text-davinci-edit-001"),
6+
CODE_DAVINCI_EDIT_001("code-davinci-edit-001");
7+
8+
private final String name;
9+
10+
public String getName()
11+
{
12+
return name;
13+
}
14+
15+
EditModel(String name)
16+
{
17+
this.name = name;
18+
}
19+
}

src/main/java/org/devlive/sdk/openai/model/UrlModel.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ public enum UrlModel
1313
FETCH_IMAGES_VARIATIONS,
1414
FETCH_EMBEDDINGS,
1515
FETCH_AUDIO_TRANSCRIPTIONS,
16-
FETCH_MODERATIONS
16+
FETCH_MODERATIONS,
17+
FETCH_EDITS
1718
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.devlive.sdk.openai.response;
2+
3+
import com.fasterxml.jackson.annotation.JsonFormat;
4+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5+
import com.fasterxml.jackson.annotation.JsonProperty;
6+
import lombok.AllArgsConstructor;
7+
import lombok.Builder;
8+
import lombok.Data;
9+
import lombok.NoArgsConstructor;
10+
import lombok.ToString;
11+
import org.devlive.sdk.openai.choice.BasicChoice;
12+
import org.devlive.sdk.openai.entity.UsageEntity;
13+
14+
import java.util.List;
15+
16+
@Data
17+
@Builder
18+
@ToString
19+
@NoArgsConstructor
20+
@AllArgsConstructor
21+
@JsonIgnoreProperties(ignoreUnknown = true)
22+
public class EditResponse
23+
{
24+
@JsonProperty(value = "object")
25+
private String object;
26+
27+
@JsonProperty(value = "created")
28+
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
29+
private String createTime;
30+
31+
@JsonProperty(value = "choices")
32+
private List<BasicChoice> choices;
33+
34+
@JsonProperty(value = "usage")
35+
private UsageEntity usage;
36+
}

src/main/java/org/devlive/sdk/openai/utils/ProviderUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public class ProviderUtils
2424
DEFAULT_PROVIDER.put(UrlModel.FETCH_EMBEDDINGS, "v1/embeddings");
2525
DEFAULT_PROVIDER.put(UrlModel.FETCH_AUDIO_TRANSCRIPTIONS, "v1/audio/transcriptions");
2626
DEFAULT_PROVIDER.put(UrlModel.FETCH_MODERATIONS, "v1/moderations");
27+
DEFAULT_PROVIDER.put(UrlModel.FETCH_EDITS, "v1/edits");
2728

2829
AZURE_PROVIDER.put(UrlModel.FETCH_COMPLETIONS, "completions");
2930
AZURE_PROVIDER.put(UrlModel.FETCH_CHAT_COMPLETIONS, "chat/completions");

0 commit comments

Comments
 (0)