Skip to content

Commit

Permalink
Add builders to request objects
Browse files Browse the repository at this point in the history
This is so much nicer
  • Loading branch information
TheoKanning committed Oct 5, 2020
1 parent 44ad762 commit d15dc71
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Java libraries for using OpenAI's GPT-3 api.

Includes the following artifacts:
- api : request/response POJOs for the GPT-3 engine, completion, and search APIs.
- client : a basic retrofit client for the GPT-3 endpoints
- `api` : request/response POJOs for the GPT-3 engine, completion, and search APIs.
- `client` : a basic retrofit client for the GPT-3 endpoints

as well as an example project using the client.

Expand All @@ -12,9 +12,10 @@ as well as an example project using the client.
If you're looking for the fastest solution, import the `client` and use [OpenAiService](client/src/main/java/openai/OpenAiService.java).
```
OpenAiService service = new OpenAiService(your_token)
CompletionRequest completionRequest = new CompletionRequest();
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
completionRequest.setEcho(true);
CompletionRequest completionRequest = CompletionRequest.builder()
.prompt("Somebody once told me the world is gonna roll me")
.echo(true)
.build();
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
```

Expand Down
7 changes: 7 additions & 0 deletions api/src/main/java/openai/completion/CompletionRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package openai.completion;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

Expand All @@ -11,8 +14,12 @@
* Documentation taken from
* https://beta.openai.com/docs/api-reference/create-completion
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class CompletionRequest {

/**
* An optional prompt to complete from
*/
Expand Down
7 changes: 7 additions & 0 deletions api/src/main/java/openai/search/SearchRequest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package openai.search;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

Expand All @@ -11,8 +14,12 @@
*
* https://beta.openai.com/docs/api-reference/search
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class SearchRequest {

/**
* Documents to search over
*/
Expand Down
15 changes: 9 additions & 6 deletions example/src/main/java/example/OpenAiApiExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,18 @@ public static void main(String... args) {
System.out.println(ada);

System.out.println("\nCreating completion...");
CompletionRequest completionRequest = new CompletionRequest();
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
completionRequest.setEcho(true);

CompletionRequest completionRequest = CompletionRequest.builder()
.prompt("Somebody once told me the world is gonna roll me")
.echo(true)
.build();
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);

System.out.println("\nSearching documents...");
SearchRequest searchRequest = new SearchRequest();
searchRequest.setDocuments(Arrays.asList("Water", "Earth", "Electricity", "Fire"));
searchRequest.setQuery("Pikachu");
SearchRequest searchRequest = SearchRequest.builder()
.documents(Arrays.asList("Water", "Earth", "Electricity", "Fire"))
.query("Pikachu")
.build();
service.search("ada", searchRequest).forEach(System.out::println);
}
}

0 comments on commit d15dc71

Please sign in to comment.