2021-03-09 22:31:03 -05:00
![Maven Central ](https://img.shields.io/maven-central/v/com.theokanning.openai-gpt3-java/client?color=blue )
2020-10-04 20:50:49 -04:00
# OpenAI-Java
Java libraries for using OpenAI's GPT-3 api.
Includes the following artifacts:
2020-10-05 18:41:16 -04:00
- `api` : request/response POJOs for the GPT-3 engine, completion, and search APIs.
2020-10-07 10:38:16 -04:00
- `client` : a basic retrofit client for the GPT-3 endpoints, includes the `api` module
2020-10-04 20:50:49 -04:00
as well as an example project using the client.
2020-10-04 22:30:20 -04:00
## Usage
2020-10-07 10:38:16 -04:00
### Importing into a gradle project
2020-10-07 10:47:48 -04:00
`implementation 'com.theokanning.openai-gpt3-java:api:<version>'`
2020-10-07 10:38:16 -04:00
or
`implementation 'com.theokanning.openai-gpt3-java:client:<version>'`
2020-10-05 18:27:59 -04:00
### Using OpenAiService
2021-03-01 10:31:39 -05:00
If you're looking for the fastest solution, import the `client` and use [OpenAiService ](client/src/main/java/com/theokanning/openai/OpenAiService.java ).
2020-10-04 20:50:49 -04:00
```
OpenAiService service = new OpenAiService(your_token)
2020-10-05 18:41:16 -04:00
CompletionRequest completionRequest = CompletionRequest.builder()
.prompt("Somebody once told me the world is gonna roll me")
.echo(true)
.build();
2020-10-04 20:50:49 -04:00
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
```
2020-10-05 18:27:59 -04:00
### Using OpenAiApi Retrofit client
2021-03-01 10:31:39 -05:00
If you're using retrofit, you can import the `client` module and use the [OpenAiApi ](client/src/main/java/com/theokanning/openai/OpenAiApi.java ).
You'll have to add your auth token as a header (see [AuthenticationInterceptor ](client/src/main/java/com/theokanning/openai/AuthenticationInterceptor.java ))
2020-10-04 20:50:49 -04:00
and set your converter factory to use snake case and only include non-null fields.
2020-10-05 18:27:59 -04:00
### Using data classes only
If you want to make your own client, just import the POJOs from the `api` module.
Your client will need to use snake case to work with the OpenAI API.
2020-10-04 20:50:49 -04:00
## Running the example project
All the [example ](example/src/main/java/example/OpenAiApiExample.java ) project requires is your OpenAI api token
2020-09-14 20:22:46 -04:00
```
export OPENAI_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
./gradlew example:run
2020-10-04 22:30:20 -04:00
```
## License
2020-10-07 10:38:16 -04:00
Published under the MIT License