Skip to content
This repository was archived by the owner on Jun 6, 2024. It is now read-only.

Commit 7f39df6

Browse files
authored
Add Edit request functionality (#14)
1 parent 900e13b commit 7f39df6

File tree

12 files changed

+144
-14
lines changed

12 files changed

+144
-14
lines changed

api/src/main/java/com/theokanning/openai/completion/CompletionChoice.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* A completion generated by GPT-3
77
*
8-
* https://beta.openai.com/docs/api-reference/create-completion
8+
* https://beta.openai.com/docs/api-reference/completions/create
99
*/
1010
@Data
1111
public class CompletionChoice {

api/src/main/java/com/theokanning/openai/completion/CompletionRequest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@
1111
* A request for OpenAi to generate a predicted completion for a prompt.
1212
* All fields are nullable.
1313
*
14-
* Documentation taken from
15-
* https://beta.openai.com/docs/api-reference/create-completion
14+
* https://beta.openai.com/docs/api-reference/completions/create
1615
*/
1716
@Builder
1817
@NoArgsConstructor
@@ -41,7 +40,7 @@ public class CompletionRequest {
4140
* What sampling temperature to use. Higher values means the model will take more risks.
4241
* Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.
4342
*
44-
* We generally recommend using this or {@link top_p} but not both.
43+
* We generally recommend using this or {@link CompletionRequest#topP} but not both.
4544
*/
4645
Double temperature;
4746

@@ -50,15 +49,15 @@ public class CompletionRequest {
5049
* the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are
5150
* considered.
5251
*
53-
* We generally recommend using this or {@link temperature} but not both.
52+
* We generally recommend using this or {@link CompletionRequest#temperature} but not both.
5453
*/
5554
Double topP;
5655

5756
/**
5857
* How many completions to generate for each prompt.
5958
*
6059
* Because this parameter generates many completions, it can quickly consume your token quota.
61-
* Use carefully and ensure that you have reasonable settings for {@link max_tokens} and {@link stop}.
60+
* Use carefully and ensure that you have reasonable settings for {@link CompletionRequest#maxTokens} and {@link CompletionRequest#stop}.
6261
*/
6362
Integer n;
6463

@@ -105,7 +104,7 @@ public class CompletionRequest {
105104
* (the one with the lowest log probability per token).
106105
* Results cannot be streamed.
107106
*
108-
* When used with {@link n}, best_of controls the number of candidate completions and n specifies how many to return,
107+
* When used with {@link CompletionRequest#n}, best_of controls the number of candidate completions and n specifies how many to return,
109108
* best_of must be greater than n.
110109
*/
111110
Integer bestOf;

api/src/main/java/com/theokanning/openai/completion/CompletionResult.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
/**
88
* An object containing a response from the completion api
99
*
10-
* https://beta.openai.com/docs/api-reference/create-completion
10+
* https://beta.openai.com/docs/api-reference/completions/create
1111
*/
1212
@Data
1313
public class CompletionResult {
1414
/**
15-
* A unique id assigned to this completion
15+
* A unique id assigned to this completion.
1616
*/
1717
String id;
1818

@@ -27,12 +27,12 @@ public class CompletionResult {
2727
long created;
2828

2929
/**
30-
* The GPT-3 model used
30+
* The GPT-3 model used.
3131
*/
3232
String model;
3333

3434
/**
35-
* A list of generated completions
35+
* A list of generated completions.
3636
*/
3737
List<CompletionChoice> choices;
3838
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.theokanning.openai.edit;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* An edit generated by GPT-3
7+
*
8+
* https://beta.openai.com/docs/api-reference/edits/create
9+
*/
10+
@Data
11+
public class EditChoice {
12+
13+
/**
14+
* The edited text.
15+
*/
16+
String text;
17+
18+
/**
19+
* This index of this completion in the returned list.
20+
*/
21+
Integer index;
22+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.theokanning.openai.edit;
2+
3+
import lombok.AllArgsConstructor;
4+
import lombok.Builder;
5+
import lombok.Data;
6+
import lombok.NoArgsConstructor;
7+
8+
/**
9+
* Given a prompt and an instruction, OpenAi will return an edited version of the prompt
10+
*
11+
* https://beta.openai.com/docs/api-reference/edits/create
12+
*/
13+
@Builder
14+
@NoArgsConstructor
15+
@AllArgsConstructor
16+
@Data
17+
public class EditRequest {
18+
19+
/**
20+
* The input text to use as a starting point for the edit.
21+
*/
22+
String input;
23+
24+
/**
25+
* The instruction that tells the model how to edit the prompt.
26+
* For example, "Fix the spelling mistakes"
27+
*/
28+
String instruction;
29+
30+
/**
31+
* What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.
32+
*
33+
* We generally recommend altering this or {@link EditRequest#topP} but not both.
34+
*/
35+
Double temperature;
36+
37+
/**
38+
* 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.
39+
*
40+
* We generally recommend altering this or {@link EditRequest#temperature} but not both.
41+
*/
42+
Double topP;
43+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.theokanning.openai.edit;
2+
3+
import lombok.Data;
4+
5+
import java.util.List;
6+
7+
/**
8+
* A list of edits generated by GPT-3
9+
*
10+
* https://beta.openai.com/docs/api-reference/edits/create
11+
*/
12+
@Data
13+
public class EditResult {
14+
15+
/**
16+
* The type of object returned, should be "edit"
17+
*/
18+
String object;
19+
20+
/**
21+
* The creation time in epoch milliseconds.
22+
*/
23+
long created;
24+
25+
/**
26+
* A list of generated edits.
27+
*/
28+
List<EditChoice> choices;
29+
}

api/src/main/java/com/theokanning/openai/search/SearchRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* GPT-3 will perform a semantic search over the documents and score them based on how related they are to the query.
1313
* Higher scores indicate a stronger relation.
1414
*
15-
* https://beta.openai.com/docs/api-reference/search
15+
* https://beta.openai.com/docs/api-reference/searches
1616
*/
1717
@Builder
1818
@NoArgsConstructor

api/src/main/java/com/theokanning/openai/search/SearchResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
/**
66
* A search result for a single document.
77
*
8-
* https://beta.openai.com/docs/api-reference/search
8+
* https://beta.openai.com/docs/api-reference/searches
99
*/
1010
@Data
1111
public class SearchResult {

client/src/main/java/com/theokanning/openai/OpenAiApi.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import com.theokanning.openai.classification.ClassificationResult;
77
import com.theokanning.openai.completion.CompletionRequest;
88
import com.theokanning.openai.completion.CompletionResult;
9+
import com.theokanning.openai.edit.EditRequest;
10+
import com.theokanning.openai.edit.EditResult;
911
import com.theokanning.openai.embedding.EmbeddingRequest;
1012
import com.theokanning.openai.embedding.EmbeddingResult;
1113
import com.theokanning.openai.engine.Engine;
@@ -31,6 +33,9 @@ public interface OpenAiApi {
3133
@POST("/v1/engines/{engine_id}/completions")
3234
Single<CompletionResult> createCompletion(@Path("engine_id") String engineId, @Body CompletionRequest request);
3335

36+
@POST("/v1/engines/{engine_id}/edits")
37+
Single<EditResult> createEdit(@Path("engine_id") String engineId, @Body EditRequest request);
38+
3439
@POST("/v1/engines/{engine_id}/search")
3540
Single<OpenAiResponse<SearchResult>> search(@Path("engine_id") String engineId, @Body SearchRequest request);
3641

client/src/main/java/com/theokanning/openai/OpenAiService.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import com.theokanning.openai.classification.ClassificationResult;
1111
import com.theokanning.openai.completion.CompletionRequest;
1212
import com.theokanning.openai.completion.CompletionResult;
13+
import com.theokanning.openai.edit.EditRequest;
14+
import com.theokanning.openai.edit.EditResult;
1315
import com.theokanning.openai.embedding.EmbeddingRequest;
1416
import com.theokanning.openai.embedding.EmbeddingResult;
1517
import com.theokanning.openai.engine.Engine;
@@ -64,6 +66,10 @@ public CompletionResult createCompletion(String engineId, CompletionRequest requ
6466
return api.createCompletion(engineId, request).blockingGet();
6567
}
6668

69+
public EditResult createEdit(String engineId, EditRequest request) {
70+
return api.createEdit(engineId, request).blockingGet();
71+
}
72+
6773
public List<SearchResult> search(String engineId, SearchRequest request) {
6874
return api.search(engineId, request).blockingGet().data;
6975
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.theokanning.openai;
2+
3+
import com.theokanning.openai.edit.EditRequest;
4+
import com.theokanning.openai.edit.EditResult;
5+
import org.junit.jupiter.api.Disabled;
6+
import org.junit.jupiter.api.Test;
7+
8+
import static org.junit.jupiter.api.Assertions.assertEquals;
9+
10+
@Disabled // disabled until edit example CURL works
11+
public class EditTest {
12+
13+
String token = System.getenv("OPENAI_TOKEN");
14+
OpenAiService service = new OpenAiService(token);
15+
16+
@Test
17+
void edit() {
18+
EditRequest request = EditRequest.builder()
19+
.input("What day of the wek is it?")
20+
.instruction("Fix the spelling mistakes")
21+
.build();
22+
23+
EditResult result = service.createEdit("text-ada-001", request);
24+
25+
assertEquals("What day of the week is it?", result.getChoices().get(0).getText());
26+
}
27+
}

example/src/main/java/example/OpenAiApiExample.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public static void main(String... args) {
2020
System.out.println(ada);
2121

2222
System.out.println("\nCreating completion...");
23-
2423
CompletionRequest completionRequest = CompletionRequest.builder()
2524
.prompt("Somebody once told me the world is gonna roll me")
2625
.echo(true)

0 commit comments

Comments
 (0)