Add logprobs field

This commit is contained in:
Theo Kanning 2020-10-05 17:27:59 -05:00
parent f33aea18cb
commit 44ad76258c
4 changed files with 48 additions and 3 deletions

View File

@ -8,6 +8,7 @@ Includes the following artifacts:
as well as an example project using the client.
## Usage
### Using OpenAiService
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)
@ -17,11 +18,14 @@ completionRequest.setEcho(true);
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
```
### Using OpenAiApi Retrofit client
If you're using retrofit, you can import the `client` module and use the [OpenAiApi](client/src/main/java/openai/OpenAiApi.java).
You'll have to add your auth token as a header (see [AuthenticationInterceptor](client/src/main/java/openai/AuthenticationInterceptor.java))
and set your converter factory to use snake case and only include non-null fields.
If you want to make your own client, just import the POJOs from the `api` module.
### 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.
## Running the example project
All the [example](example/src/main/java/example/OpenAiApiExample.java) project requires is your OpenAI api token

View File

@ -18,7 +18,11 @@ public class CompletionChoice {
* This index of this completion in the returned list.
*/
Integer index;
// todo add logprobs
/**
* The log probabilities of the chosen tokens and the top {@link CompletionRequest#logprobs} tokens
*/
LogProbResult logprobs;
/**
* The reason why GPT-3 stopped generating, for example "length".

View File

@ -74,7 +74,7 @@ public class CompletionRequest {
* Up to 4 sequences where the API will stop generating further tokens.
* The returned text will not contain the stop sequence.
*/
List<String> stop; //todo test this
List<String> stop;
/**
* Number between 0 and 1 (default 0) that penalizes new tokens based on whether they appear in the text so far.

View File

@ -0,0 +1,37 @@
package openai.completion;
import lombok.Data;
import java.util.List;
import java.util.Map;
/**
* Log probabilities of different token options
* Returned if {@link CompletionRequest#logprobs} is greater than zero
*
* https://beta.openai.com/docs/api-reference/create-completion
*/
@Data
public class LogProbResult {
/**
* The tokens chosen by the completion api
*/
List<String> tokens;
/**
* The log probability of each token in {@link tokens}
*/
List<Double> tokenLogprobs;
/**
* A map for each index in the completion result.
* The map contains the top {@link CompletionRequest#logprobs} tokens and their probabilities
*/
List<Map<String, Double>> topLogprobs;
/**
* The character offset from the start of the returned text for each of the chosen tokens.
*/
List<Integer> textOffset;
}