Add builders to request objects
This is so much nicer
This commit is contained in:
parent
44ad76258c
commit
d15dc718c2
11
README.md
11
README.md
@ -2,8 +2,8 @@
|
|||||||
Java libraries for using OpenAI's GPT-3 api.
|
Java libraries for using OpenAI's GPT-3 api.
|
||||||
|
|
||||||
Includes the following artifacts:
|
Includes the following artifacts:
|
||||||
- api : request/response POJOs for the GPT-3 engine, completion, and search APIs.
|
- `api` : request/response POJOs for the GPT-3 engine, completion, and search APIs.
|
||||||
- client : a basic retrofit client for the GPT-3 endpoints
|
- `client` : a basic retrofit client for the GPT-3 endpoints
|
||||||
|
|
||||||
as well as an example project using the client.
|
as well as an example project using the client.
|
||||||
|
|
||||||
@ -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).
|
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)
|
OpenAiService service = new OpenAiService(your_token)
|
||||||
CompletionRequest completionRequest = new CompletionRequest();
|
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||||
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
|
.prompt("Somebody once told me the world is gonna roll me")
|
||||||
completionRequest.setEcho(true);
|
.echo(true)
|
||||||
|
.build();
|
||||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package openai.completion;
|
package openai.completion;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,8 +14,12 @@ import java.util.List;
|
|||||||
* Documentation taken from
|
* Documentation taken from
|
||||||
* https://beta.openai.com/docs/api-reference/create-completion
|
* https://beta.openai.com/docs/api-reference/create-completion
|
||||||
*/
|
*/
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Data
|
@Data
|
||||||
public class CompletionRequest {
|
public class CompletionRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An optional prompt to complete from
|
* An optional prompt to complete from
|
||||||
*/
|
*/
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package openai.search;
|
package openai.search;
|
||||||
|
|
||||||
|
import lombok.AllArgsConstructor;
|
||||||
|
import lombok.Builder;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
|
import lombok.NoArgsConstructor;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -11,8 +14,12 @@ import java.util.List;
|
|||||||
*
|
*
|
||||||
* https://beta.openai.com/docs/api-reference/search
|
* https://beta.openai.com/docs/api-reference/search
|
||||||
*/
|
*/
|
||||||
|
@Builder
|
||||||
|
@NoArgsConstructor
|
||||||
|
@AllArgsConstructor
|
||||||
@Data
|
@Data
|
||||||
public class SearchRequest {
|
public class SearchRequest {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Documents to search over
|
* Documents to search over
|
||||||
*/
|
*/
|
||||||
|
@ -20,15 +20,18 @@ class OpenAiApiExample {
|
|||||||
System.out.println(ada);
|
System.out.println(ada);
|
||||||
|
|
||||||
System.out.println("\nCreating completion...");
|
System.out.println("\nCreating completion...");
|
||||||
CompletionRequest completionRequest = new CompletionRequest();
|
|
||||||
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
|
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||||
completionRequest.setEcho(true);
|
.prompt("Somebody once told me the world is gonna roll me")
|
||||||
|
.echo(true)
|
||||||
|
.build();
|
||||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||||
|
|
||||||
System.out.println("\nSearching documents...");
|
System.out.println("\nSearching documents...");
|
||||||
SearchRequest searchRequest = new SearchRequest();
|
SearchRequest searchRequest = SearchRequest.builder()
|
||||||
searchRequest.setDocuments(Arrays.asList("Water", "Earth", "Electricity", "Fire"));
|
.documents(Arrays.asList("Water", "Earth", "Electricity", "Fire"))
|
||||||
searchRequest.setQuery("Pikachu");
|
.query("Pikachu")
|
||||||
|
.build();
|
||||||
service.search("ada", searchRequest).forEach(System.out::println);
|
service.search("ada", searchRequest).forEach(System.out::println);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user