Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 48cd7272f7 | |||
| 1600841a58 | |||
| 8d7f240010 | |||
| c76923926e | |||
| 0caab13666 | |||
| 5e14d4f62b | |||
| 83df513ddc | |||
| 515a5fc47d | |||
| 553e22fea2 | |||
| 252db27577 | |||
| ff06ffb309 | |||
| d1f274800a | |||
| 77219d497b | |||
| 1391f4074d | |||
| 6f5a4ada76 | |||
| 4d5878db07 | |||
| 9e0a26b576 | |||
| bcfb5da378 | |||
| 7f39df6e0b | |||
| 900e13bbda | |||
| 9f5b64b151 | |||
| 103c34da94 | |||
| ed2f1152e8 |
@@ -6,8 +6,7 @@ on:
|
||||
- '[0-9]+.[0-9]+.[0-9]+'
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
publish:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
@@ -20,6 +19,8 @@ jobs:
|
||||
|
||||
- name: Test
|
||||
run: ./gradlew test
|
||||
env:
|
||||
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
|
||||
|
||||
- name: Publish
|
||||
run: ./gradlew build publish
|
||||
@@ -27,4 +28,4 @@ jobs:
|
||||
ORG_GRADLE_PROJECT_mavenCentralUsername: ${{ secrets.SONATYPE_NEXUS_USERNAME }}
|
||||
ORG_GRADLE_PROJECT_mavenCentralPassword: ${{ secrets.SONATYPE_NEXUS_PASSWORD }}
|
||||
ORG_GRADLE_PROJECT_signingInMemoryKey: ${{ secrets.SIGNING_KEY }}
|
||||
ORG_GRADLE_PROJECT_signingInMemoeryKeyPassword: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||
ORG_GRADLE_PROJECT_signingInMemoryKeyPassword: ${{ secrets.SIGNING_KEY_PASSWORD }}
|
||||
@@ -0,0 +1,24 @@
|
||||
name: Test
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
test:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
|
||||
- name: Set up JDK 1.8
|
||||
uses: actions/setup-java@v1
|
||||
with:
|
||||
java-version: 1.8
|
||||
|
||||
- name: Test
|
||||
run: ./gradlew test --stacktrace
|
||||
env:
|
||||
OPENAI_TOKEN: ${{ secrets.OPENAI_TOKEN }}
|
||||
@@ -1,4 +1,12 @@
|
||||

|
||||
|
||||
> ⚠️The [Answers](https://help.openai.com/en/articles/6233728-answers-transition-guide),
|
||||
>[Classifications](https://help.openai.com/en/articles/6272941-classifications-transition-guide),
|
||||
>and [Searches](https://help.openai.com/en/articles/6272952-search-transition-guide) APIs are deprecated,
|
||||
>and will stop working on December 3rd, 2022.
|
||||
|
||||
> ⚠️OpenAI has deprecated all Engine-based APIs. See [Deprecated Endpoints](https://github.com/TheoKanning/openai-java#deprecated-endpoints) below for more info.
|
||||
|
||||
# OpenAI-Java
|
||||
Java libraries for using OpenAI's GPT-3 api.
|
||||
|
||||
@@ -8,6 +16,21 @@ Includes the following artifacts:
|
||||
|
||||
as well as an example project using the client.
|
||||
|
||||
## Supported APIs
|
||||
- [Models](https://beta.openai.com/docs/api-reference/models)
|
||||
- [Completions](https://beta.openai.com/docs/api-reference/completions)
|
||||
- [Edits](https://beta.openai.com/docs/api-reference/edits)
|
||||
- [Embeddings](https://beta.openai.com/docs/api-reference/embeddings)
|
||||
- [Files](https://beta.openai.com/docs/api-reference/files)
|
||||
- [Fine-tunes](https://beta.openai.com/docs/api-reference/fine-tunes)
|
||||
- [Moderations](https://beta.openai.com/docs/api-reference/moderations)
|
||||
|
||||
#### Deprecated by OpenAI
|
||||
- [Searches](https://beta.openai.com/docs/api-reference/searches)
|
||||
- [Classifications](https://beta.openai.com/docs/api-reference/classifications)
|
||||
- [Answers](https://beta.openai.com/docs/api-reference/answers)
|
||||
- [Engines](https://beta.openai.com/docs/api-reference/engines)
|
||||
|
||||
## Usage
|
||||
|
||||
### Importing into a gradle project
|
||||
@@ -18,12 +41,13 @@ or
|
||||
### Using OpenAiService
|
||||
If you're looking for the fastest solution, import the `client` and use [OpenAiService](client/src/main/java/com/theokanning/openai/OpenAiService.java).
|
||||
```
|
||||
OpenAiService service = new OpenAiService(your_token)
|
||||
OpenAiService service = new OpenAiService("your_token");
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.model("ada")
|
||||
.echo(true)
|
||||
.build();
|
||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
|
||||
```
|
||||
|
||||
### Using OpenAiApi Retrofit client
|
||||
@@ -42,5 +66,12 @@ export OPENAI_TOKEN="sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
||||
./gradlew example:run
|
||||
```
|
||||
|
||||
## Deprecated Endpoints
|
||||
OpenAI has deprecated engine-based endpoints in favor of model-based endpoints.
|
||||
For example, instead of using `v1/engines/{engine_id}/completions`, switch to `v1/completions` and specify the model in the `CompletionRequest`.
|
||||
The code includes upgrade instructions for all deprecated endpoints.
|
||||
|
||||
I won't remove the old endpoints from this library until OpenAI shuts them down.
|
||||
|
||||
## License
|
||||
Published under the MIT License
|
||||
|
||||
+3
-2
@@ -2,8 +2,9 @@ apply plugin: 'java-library'
|
||||
apply plugin: "com.vanniktech.maven.publish"
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.projectlombok:lombok:1.18.12'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.12'
|
||||
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
|
||||
compileOnly 'org.projectlombok:lombok:1.18.24'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.24'
|
||||
}
|
||||
|
||||
compileJava {
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* The OpenAI resources used by a request
|
||||
*/
|
||||
@Data
|
||||
public class Usage {
|
||||
/**
|
||||
* The number of prompt tokens used.
|
||||
*/
|
||||
long promptTokens;
|
||||
|
||||
/**
|
||||
* The number of completion tokens used.
|
||||
*/
|
||||
long completionTokens;
|
||||
|
||||
/**
|
||||
* The number of total tokens used
|
||||
*/
|
||||
long totalTokens;
|
||||
}
|
||||
@@ -0,0 +1,141 @@
|
||||
package com.theokanning.openai.answer;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Given a question, a set of documents, and some examples, the API generates an answer to the question based
|
||||
* on the information in the set of documents. This is useful for question-answering applications on sources of truth,
|
||||
* like company documentation or a knowledge base.
|
||||
*
|
||||
* Documentation taken from
|
||||
* https://beta.openai.com/docs/api-reference/answers/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class AnswerRequest {
|
||||
|
||||
/**
|
||||
* ID of the engine to use for completion.
|
||||
*/
|
||||
@NonNull
|
||||
String model;
|
||||
|
||||
/**
|
||||
* Question to get answered.
|
||||
*/
|
||||
@NonNull
|
||||
String question;
|
||||
|
||||
/**
|
||||
* List of (question, answer) pairs that will help steer the model towards the tone and answer format you'd like.
|
||||
* We recommend adding 2 to 3 examples.
|
||||
*/
|
||||
@NonNull
|
||||
List<List<String>> examples;
|
||||
|
||||
/**
|
||||
* A text snippet containing the contextual information used to generate the answers for the examples you provide.
|
||||
*/
|
||||
@NonNull
|
||||
String examplesContext;
|
||||
|
||||
/**
|
||||
* List of documents from which the answer for the input question should be derived.
|
||||
* If this is an empty list, the question will be answered based on the question-answer examples.
|
||||
*
|
||||
* You should specify either documents or a file, but not both.
|
||||
*/
|
||||
List<String> documents;
|
||||
|
||||
/**
|
||||
* The ID of an uploaded file that contains documents to search over.
|
||||
* See upload file for how to upload a file of the desired format and purpose.
|
||||
*
|
||||
* You should specify either documents or file, but not both.
|
||||
*/
|
||||
String file;
|
||||
|
||||
/**
|
||||
* ID of the engine to use for Search. You can select one of ada, babbage, curie, or davinci.
|
||||
*/
|
||||
String searchModel;
|
||||
|
||||
/**
|
||||
* The maximum number of documents to be ranked by Search when using file.
|
||||
* Setting it to a higher value leads to improved accuracy but with increased latency and cost.
|
||||
*/
|
||||
Integer maxRerank;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* We generally recommend using this or {@link top_p} but not both.
|
||||
*/
|
||||
Double temperature;
|
||||
|
||||
/**
|
||||
* Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens.
|
||||
* For example, if logprobs is 10, the API will return a list of the 10 most likely tokens.
|
||||
* The API will always return the logprob of the sampled token,
|
||||
* so there may be up to logprobs+1 elements in the response.
|
||||
*/
|
||||
Integer logprobs;
|
||||
|
||||
/**
|
||||
* The maximum number of tokens allowed for the generated answer.
|
||||
*/
|
||||
Integer maxTokens;
|
||||
|
||||
/**
|
||||
* Up to 4 sequences where the API will stop generating further tokens.
|
||||
* The returned text will not contain the stop sequence.
|
||||
*/
|
||||
List<String> stop;
|
||||
|
||||
/**
|
||||
* How many answers to generate for each question.
|
||||
*/
|
||||
Integer n;
|
||||
|
||||
/**
|
||||
* Modify the likelihood of specified tokens appearing in the completion.
|
||||
*
|
||||
* Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an
|
||||
* associated bias value from -100 to 100.
|
||||
*/
|
||||
Map<String, Double> logitBias;
|
||||
|
||||
/**
|
||||
* A special boolean flag for showing metadata.
|
||||
* If set to true, each document entry in the returned JSON will contain a "metadata" field.
|
||||
*
|
||||
* This flag only takes effect when file is set.
|
||||
*/
|
||||
Boolean returnMetadata;
|
||||
|
||||
/**
|
||||
* If set to true, the returned JSON will include a "prompt" field containing the final prompt that was
|
||||
* used to request a completion. This is mainly useful for debugging purposes.
|
||||
*/
|
||||
Boolean returnPrompt;
|
||||
|
||||
/**
|
||||
* If an object name is in the list, we provide the full information of the object;
|
||||
* otherwise, we only provide the object ID.
|
||||
*
|
||||
* Currently we support completion and file objects for expansion.
|
||||
*/
|
||||
List<String> expand;
|
||||
|
||||
/**
|
||||
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
|
||||
*/
|
||||
String user;
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.theokanning.openai.answer;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An object containing a response from the answer api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/answers/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class AnswerResult {
|
||||
/**
|
||||
* A list of generated answers to the provided question/
|
||||
*/
|
||||
List<String> answers;
|
||||
|
||||
/**
|
||||
* A unique id assigned to this completion
|
||||
*/
|
||||
String completion;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used for completion
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "answer"
|
||||
*/
|
||||
String object;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used for search
|
||||
*/
|
||||
String searchModel;
|
||||
|
||||
/**
|
||||
* A list of the most relevant documents for the question.
|
||||
*/
|
||||
List<Document> selectedDocuments;
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.theokanning.openai.answer;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Represents an example returned by the classification api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/classifications/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class Document {
|
||||
/**
|
||||
* The position of this example in the example list
|
||||
*/
|
||||
Integer document;
|
||||
|
||||
/**
|
||||
* The text of the example
|
||||
*/
|
||||
String text;
|
||||
}
|
||||
@@ -0,0 +1,121 @@
|
||||
package com.theokanning.openai.classification;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A request for OpenAi to classify text based on provided examples
|
||||
* All fields are nullable.
|
||||
*
|
||||
* Documentation taken from
|
||||
* https://beta.openai.com/docs/api-reference/classifications/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class ClassificationRequest {
|
||||
|
||||
/**
|
||||
* ID of the engine to use for completion
|
||||
*/
|
||||
@NonNull
|
||||
String model;
|
||||
|
||||
/**
|
||||
* Query to be classified
|
||||
*/
|
||||
@NonNull
|
||||
String query;
|
||||
|
||||
/**
|
||||
* A list of examples with labels, in the following format:
|
||||
*
|
||||
* [["The movie is so interesting.", "Positive"], ["It is quite boring.", "Negative"], ...]
|
||||
*
|
||||
* All the label strings will be normalized to be capitalized.
|
||||
*
|
||||
* You should specify either examples or file, but not both.
|
||||
*/
|
||||
List<List<String>> examples;
|
||||
|
||||
/**
|
||||
* The ID of the uploaded file that contains training examples.
|
||||
* See upload file for how to upload a file of the desired format and purpose.
|
||||
*
|
||||
* You should specify either examples or file, but not both.
|
||||
*/
|
||||
String file;
|
||||
|
||||
/**
|
||||
* The set of categories being classified.
|
||||
* If not specified, candidate labels will be automatically collected from the examples you provide.
|
||||
* All the label strings will be normalized to be capitalized.
|
||||
*/
|
||||
List<String> labels;
|
||||
|
||||
/**
|
||||
* ID of the engine to use for Search. You can select one of ada, babbage, curie, or davinci.
|
||||
*/
|
||||
String searchModel;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* We generally recommend using this or {@link top_p} but not both.
|
||||
*/
|
||||
Double temperature;
|
||||
|
||||
/**
|
||||
* Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens.
|
||||
* For example, if logprobs is 10, the API will return a list of the 10 most likely tokens.
|
||||
* The API will always return the logprob of the sampled token,
|
||||
* so there may be up to logprobs+1 elements in the response.
|
||||
*/
|
||||
Integer logprobs;
|
||||
|
||||
/**
|
||||
* The maximum number of examples to be ranked by Search when using file.
|
||||
* Setting it to a higher value leads to improved accuracy but with increased latency and cost.
|
||||
*/
|
||||
Integer maxExamples;
|
||||
|
||||
/**
|
||||
* Modify the likelihood of specified tokens appearing in the completion.
|
||||
*
|
||||
* Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an
|
||||
* associated bias value from -100 to 100.
|
||||
*/
|
||||
Map<String, Double> logitBias;
|
||||
|
||||
/**
|
||||
* If set to true, the returned JSON will include a "prompt" field containing the final prompt that was
|
||||
* used to request a completion. This is mainly useful for debugging purposes.
|
||||
*/
|
||||
Boolean returnPrompt;
|
||||
|
||||
/**
|
||||
* A special boolean flag for showing metadata.
|
||||
* If set to true, each document entry in the returned JSON will contain a "metadata" field.
|
||||
*
|
||||
* This flag only takes effect when file is set.
|
||||
*/
|
||||
Boolean returnMetadata;
|
||||
|
||||
/**
|
||||
* If an object name is in the list, we provide the full information of the object;
|
||||
* otherwise, we only provide the object ID.
|
||||
*
|
||||
* Currently we support completion and file objects for expansion.
|
||||
*/
|
||||
List<String> expand;
|
||||
|
||||
/**
|
||||
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
|
||||
*/
|
||||
String user;
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package com.theokanning.openai.classification;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An object containing a response from the classification api
|
||||
* <
|
||||
* https://beta.openai.com/docs/api-reference/classifications/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class ClassificationResult {
|
||||
|
||||
/**
|
||||
* A unique id assigned to this completion
|
||||
*/
|
||||
String completion;
|
||||
|
||||
/**
|
||||
* The predicted label for the query text.
|
||||
*/
|
||||
String label;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used for completion
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "classification"
|
||||
*/
|
||||
String object;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used for search
|
||||
*/
|
||||
String searchModel;
|
||||
|
||||
/**
|
||||
* A list of the most relevant examples for the query text.
|
||||
*/
|
||||
List<Example> selectedExamples;
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package com.theokanning.openai.classification;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* Represents an example returned by the classification api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/classifications/create
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class Example {
|
||||
/**
|
||||
* The position of this example in the example list
|
||||
*/
|
||||
Integer document;
|
||||
|
||||
/**
|
||||
* The label of the example
|
||||
*/
|
||||
String label;
|
||||
|
||||
/**
|
||||
* The text of the example
|
||||
*/
|
||||
String text;
|
||||
}
|
||||
@@ -5,7 +5,7 @@ import lombok.Data;
|
||||
/**
|
||||
* A completion generated by GPT-3
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/create-completion
|
||||
* https://beta.openai.com/docs/api-reference/completions/create
|
||||
*/
|
||||
@Data
|
||||
public class CompletionChoice {
|
||||
|
||||
@@ -6,13 +6,13 @@ import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A request for OpenAi to generate a predicted completion for a prompt.
|
||||
* All fields are nullable.
|
||||
*
|
||||
* Documentation taken from
|
||||
* https://beta.openai.com/docs/api-reference/create-completion
|
||||
* https://beta.openai.com/docs/api-reference/completions/create
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@@ -21,7 +21,8 @@ import java.util.List;
|
||||
public class CompletionRequest {
|
||||
|
||||
/**
|
||||
* The name of the model to use, only used if specifying a fine tuned model.
|
||||
* The name of the model to use.
|
||||
* Required if specifying a fine tuned model or if using the new v1/completions endpoint.
|
||||
*/
|
||||
String model;
|
||||
|
||||
@@ -41,7 +42,7 @@ public class CompletionRequest {
|
||||
* 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.
|
||||
*
|
||||
* We generally recommend using this or {@link top_p} but not both.
|
||||
* We generally recommend using this or {@link CompletionRequest#topP} but not both.
|
||||
*/
|
||||
Double temperature;
|
||||
|
||||
@@ -50,7 +51,7 @@ public class CompletionRequest {
|
||||
* the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are
|
||||
* considered.
|
||||
*
|
||||
* We generally recommend using this or {@link temperature} but not both.
|
||||
* We generally recommend using this or {@link CompletionRequest#temperature} but not both.
|
||||
*/
|
||||
Double topP;
|
||||
|
||||
@@ -58,7 +59,7 @@ public class CompletionRequest {
|
||||
* How many completions to generate for each prompt.
|
||||
*
|
||||
* Because this parameter generates many completions, it can quickly consume your token quota.
|
||||
* Use carefully and ensure that you have reasonable settings for {@link max_tokens} and {@link stop}.
|
||||
* Use carefully and ensure that you have reasonable settings for {@link CompletionRequest#maxTokens} and {@link CompletionRequest#stop}.
|
||||
*/
|
||||
Integer n;
|
||||
|
||||
@@ -105,8 +106,22 @@ public class CompletionRequest {
|
||||
* (the one with the lowest log probability per token).
|
||||
* Results cannot be streamed.
|
||||
*
|
||||
* When used with {@link n}, best_of controls the number of candidate completions and n specifies how many to return,
|
||||
* When used with {@link CompletionRequest#n}, best_of controls the number of candidate completions and n specifies how many to return,
|
||||
* best_of must be greater than n.
|
||||
*/
|
||||
Integer bestOf;
|
||||
|
||||
/**
|
||||
* Modify the likelihood of specified tokens appearing in the completion.
|
||||
*
|
||||
* Maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/completions/create#completions/create-logit_bias
|
||||
*/
|
||||
Map<String, Integer> logitBias;
|
||||
|
||||
/**
|
||||
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
|
||||
*/
|
||||
String user;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.theokanning.openai.completion;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -7,12 +8,12 @@ import java.util.List;
|
||||
/**
|
||||
* An object containing a response from the completion api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/create-completion
|
||||
* https://beta.openai.com/docs/api-reference/completions/create
|
||||
*/
|
||||
@Data
|
||||
public class CompletionResult {
|
||||
/**
|
||||
* A unique id assigned to this completion
|
||||
* A unique id assigned to this completion.
|
||||
*/
|
||||
String id;
|
||||
|
||||
@@ -27,12 +28,17 @@ public class CompletionResult {
|
||||
long created;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used
|
||||
* The GPT-3 model used.
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* A list of generated completions
|
||||
* A list of generated completions.
|
||||
*/
|
||||
List<CompletionChoice> choices;
|
||||
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
Usage usage;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.theokanning.openai.edit;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* An edit generated by GPT-3
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/edits/create
|
||||
*/
|
||||
@Data
|
||||
public class EditChoice {
|
||||
|
||||
/**
|
||||
* The edited text.
|
||||
*/
|
||||
String text;
|
||||
|
||||
/**
|
||||
* This index of this completion in the returned list.
|
||||
*/
|
||||
Integer index;
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package com.theokanning.openai.edit;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.*;
|
||||
|
||||
/**
|
||||
* Given a prompt and an instruction, OpenAi will return an edited version of the prompt
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/edits/create
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class EditRequest {
|
||||
|
||||
/**
|
||||
* The name of the model to use.
|
||||
* Required if using the new v1/edits endpoint.
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* The input text to use as a starting point for the edit.
|
||||
*/
|
||||
String input;
|
||||
|
||||
/**
|
||||
* The instruction that tells the model how to edit the prompt.
|
||||
* For example, "Fix the spelling mistakes"
|
||||
*/
|
||||
@NonNull
|
||||
String instruction;
|
||||
|
||||
/**
|
||||
* How many edits to generate for the input and instruction.
|
||||
*/
|
||||
Integer n;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* We generally recommend altering this or {@link EditRequest#topP} but not both.
|
||||
*/
|
||||
Double temperature;
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*
|
||||
* We generally recommend altering this or {@link EditRequest#temperature} but not both.
|
||||
*/
|
||||
@JsonProperty("top_p")
|
||||
Double topP;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.theokanning.openai.edit;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A list of edits generated by GPT-3
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/edits/create
|
||||
*/
|
||||
@Data
|
||||
public class EditResult {
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "edit"
|
||||
*/
|
||||
public String object;
|
||||
|
||||
/**
|
||||
* The creation time in epoch milliseconds.
|
||||
*/
|
||||
public long created;
|
||||
|
||||
/**
|
||||
* A list of generated edits.
|
||||
*/
|
||||
public List<EditChoice> choices;
|
||||
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
public Usage usage;
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.theokanning.openai.edit;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* An object containing the API usage for an edit request
|
||||
*
|
||||
* Deprecated, use {@link com.theokanning.openai.Usage} instead
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/edits/create
|
||||
*/
|
||||
@Data
|
||||
@Deprecated
|
||||
public class EditUsage {
|
||||
|
||||
/**
|
||||
* The number of prompt tokens consumed.
|
||||
*/
|
||||
String promptTokens;
|
||||
|
||||
/**
|
||||
* The number of completion tokens consumed.
|
||||
*/
|
||||
String completionTokens;
|
||||
|
||||
/**
|
||||
* The number of total tokens consumed.
|
||||
*/
|
||||
String totalTokens;
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.theokanning.openai.embedding;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents an embedding returned by the embedding api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/classifications/create
|
||||
*/
|
||||
@Data
|
||||
public class Embedding {
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "embedding"
|
||||
*/
|
||||
String object;
|
||||
|
||||
/**
|
||||
* The embedding vector
|
||||
*/
|
||||
List<Double> embedding;
|
||||
|
||||
/**
|
||||
* The position of this embedding in the list
|
||||
*/
|
||||
Integer index;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.theokanning.openai.embedding;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Creates an embedding vector representing the input text.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/embeddings/create
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class EmbeddingRequest {
|
||||
|
||||
/**
|
||||
* The name of the model to use.
|
||||
* Required if using the new v1/embeddings endpoint.
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* Input text to get embeddings for, encoded as a string or array of tokens.
|
||||
* To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
|
||||
* Each input must not exceed 2048 tokens in length.
|
||||
* <p>
|
||||
* Unless your are embedding code, we suggest replacing newlines (\n) in your input with a single space,
|
||||
* as we have observed inferior results when newlines are present.
|
||||
*/
|
||||
@NonNull
|
||||
List<String> input;
|
||||
|
||||
/**
|
||||
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
|
||||
*/
|
||||
String user;
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.theokanning.openai.embedding;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An object containing a response from the answer api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/embeddings/create
|
||||
*/
|
||||
@Data
|
||||
public class EmbeddingResult {
|
||||
|
||||
/**
|
||||
* The GPT-3 model used for generating embeddings
|
||||
*/
|
||||
String model;
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "list"
|
||||
*/
|
||||
String object;
|
||||
|
||||
/**
|
||||
* A list of the calculated embeddings
|
||||
*/
|
||||
List<Embedding> data;
|
||||
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
Usage usage;
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import lombok.Data;
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/retrieve-engine
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class Engine {
|
||||
/**
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
package com.theokanning.openai.finetune;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -22,6 +20,7 @@ public class FineTuneRequest {
|
||||
/**
|
||||
* The ID of an uploaded file that contains training data.
|
||||
*/
|
||||
@NonNull
|
||||
String trainingFile;
|
||||
|
||||
/**
|
||||
@@ -85,7 +84,8 @@ public class FineTuneRequest {
|
||||
*
|
||||
* This parameter is required for multiclass classification.
|
||||
*/
|
||||
Integer classificationNClasses; // todo verify snake case
|
||||
@JsonProperty("classification_n_classes")
|
||||
Integer classificationNClasses;
|
||||
|
||||
/**
|
||||
* The positive class in binary classification.
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.theokanning.openai.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* GPT-3 model details
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/models
|
||||
*/
|
||||
@Data
|
||||
public class Model {
|
||||
/**
|
||||
* An identifier for this model, used to specify the model when making completions, etc
|
||||
*/
|
||||
public String id;
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "model"
|
||||
*/
|
||||
public String object;
|
||||
|
||||
/**
|
||||
* The owner of the GPT-3 model, typically "openai"
|
||||
*/
|
||||
public String ownedBy;
|
||||
|
||||
/**
|
||||
* List of permissions for this model
|
||||
*/
|
||||
public List<Permission> permission;
|
||||
|
||||
/**
|
||||
* The root model that this and its parent (if applicable) are based on
|
||||
*/
|
||||
public String root;
|
||||
|
||||
/**
|
||||
* The parent model that this is based on
|
||||
*/
|
||||
public String parent;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.theokanning.openai.model;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* GPT-3 model permissions
|
||||
* I couldn't find documentation for the specific permissions, and I've elected to leave them undocumented rather than
|
||||
* write something incorrect.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/models
|
||||
*/
|
||||
@Data
|
||||
public class Permission {
|
||||
/**
|
||||
* An identifier for this model permission
|
||||
*/
|
||||
public String id;
|
||||
|
||||
/**
|
||||
* The type of object returned, should be "model_permission"
|
||||
*/
|
||||
public String object;
|
||||
|
||||
/**
|
||||
* The creation time in epoch seconds.
|
||||
*/
|
||||
public long created;
|
||||
|
||||
public boolean allowCreateEngine;
|
||||
|
||||
public boolean allowSampling;
|
||||
|
||||
public boolean allowLogProbs;
|
||||
|
||||
public boolean allowSearchIndices;
|
||||
|
||||
public boolean allowView;
|
||||
|
||||
public boolean allowFineTuning;
|
||||
|
||||
public String organization;
|
||||
|
||||
public String group;
|
||||
|
||||
public boolean isBlocking;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.theokanning.openai.moderation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* An object containing the moderation data for a single input string
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/moderations/create
|
||||
*/
|
||||
@Data
|
||||
public class Moderation {
|
||||
/**
|
||||
* Set to true if the model classifies the content as violating OpenAI's content policy, false otherwise
|
||||
*/
|
||||
public boolean flagged;
|
||||
|
||||
/**
|
||||
* Object containing per-category binary content policy violation flags.
|
||||
* For each category, the value is true if the model flags the corresponding category as violated, false otherwise.
|
||||
*/
|
||||
public ModerationCategories categories;
|
||||
|
||||
/**
|
||||
* Object containing per-category raw scores output by the model, denoting the model's confidence that the
|
||||
* input violates the OpenAI's policy for the category.
|
||||
* The value is between 0 and 1, where higher values denote higher confidence.
|
||||
* The scores should not be interpreted as probabilities.
|
||||
*/
|
||||
public ModerationCategoryScores categoryScores;
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package com.theokanning.openai.moderation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import com.theokanning.openai.completion.CompletionChoice;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An object containing the flags for each moderation category
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/moderations/create
|
||||
*/
|
||||
@Data
|
||||
public class ModerationCategories {
|
||||
|
||||
public boolean hate;
|
||||
|
||||
@JsonProperty("hate/threatening")
|
||||
public boolean hateThreatening;
|
||||
|
||||
@JsonProperty("self-harm")
|
||||
public boolean selfHarm;
|
||||
|
||||
public boolean sexual;
|
||||
|
||||
@JsonProperty("sexual/minors")
|
||||
public boolean sexualMinors;
|
||||
|
||||
public boolean violence;
|
||||
|
||||
@JsonProperty("violence/graphic")
|
||||
public boolean violenceGraphic;
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.theokanning.openai.moderation;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* An object containing the scores for each moderation category
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/moderations/create
|
||||
*/
|
||||
@Data
|
||||
public class ModerationCategoryScores {
|
||||
|
||||
public double hate;
|
||||
|
||||
@JsonProperty("hate/threatening")
|
||||
public double hateThreatening;
|
||||
|
||||
@JsonProperty("self-harm")
|
||||
public double selfHarm;
|
||||
|
||||
public double sexual;
|
||||
|
||||
@JsonProperty("sexual/minors")
|
||||
public double sexualMinors;
|
||||
|
||||
public double violence;
|
||||
|
||||
@JsonProperty("violence/graphic")
|
||||
public double violenceGraphic;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.theokanning.openai.moderation;
|
||||
|
||||
import lombok.*;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A request for OpenAi to detect if text violates OpenAi's content policy.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/moderations/create
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class ModerationRequest {
|
||||
|
||||
/**
|
||||
* The input text to classify.
|
||||
*/
|
||||
@NonNull
|
||||
String input;
|
||||
|
||||
/**
|
||||
* The name of the model to use, defaults to text-moderation-stable.
|
||||
*/
|
||||
String model;
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.theokanning.openai.moderation;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An object containing a response from the moderation api
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/moderations/create
|
||||
*/
|
||||
@Data
|
||||
public class ModerationResult {
|
||||
/**
|
||||
* A unique id assigned to this moderation.
|
||||
*/
|
||||
public String id;
|
||||
|
||||
/**
|
||||
* The GPT-3 model used.
|
||||
*/
|
||||
public String model;
|
||||
|
||||
/**
|
||||
* A list of moderation scores.
|
||||
*/
|
||||
public List<Moderation> results;
|
||||
}
|
||||
@@ -12,8 +12,9 @@ import java.util.List;
|
||||
* GPT-3 will perform a semantic search over the documents and score them based on how related they are to the query.
|
||||
* Higher scores indicate a stronger relation.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/search
|
||||
* https://beta.openai.com/docs/api-reference/searches
|
||||
*/
|
||||
@Deprecated
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
|
||||
@@ -5,8 +5,9 @@ import lombok.Data;
|
||||
/**
|
||||
* A search result for a single document.
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/search
|
||||
* https://beta.openai.com/docs/api-reference/searches
|
||||
*/
|
||||
@Deprecated
|
||||
@Data
|
||||
public class SearchResult {
|
||||
/**
|
||||
|
||||
@@ -15,3 +15,7 @@ compileJava {
|
||||
sourceCompatibility = '1.8'
|
||||
targetCompatibility = '1.8'
|
||||
}
|
||||
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
|
||||
@@ -1,32 +1,58 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.file.File;
|
||||
import com.theokanning.openai.finetune.FineTuneRequest;
|
||||
import com.theokanning.openai.finetune.FineTuneEvent;
|
||||
import com.theokanning.openai.finetune.FineTuneResult;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import io.reactivex.Single;
|
||||
import com.theokanning.openai.answer.AnswerRequest;
|
||||
import com.theokanning.openai.answer.AnswerResult;
|
||||
import com.theokanning.openai.classification.ClassificationRequest;
|
||||
import com.theokanning.openai.classification.ClassificationResult;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.completion.CompletionResult;
|
||||
import com.theokanning.openai.edit.EditRequest;
|
||||
import com.theokanning.openai.edit.EditResult;
|
||||
import com.theokanning.openai.embedding.EmbeddingRequest;
|
||||
import com.theokanning.openai.embedding.EmbeddingResult;
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.file.File;
|
||||
import com.theokanning.openai.finetune.FineTuneEvent;
|
||||
import com.theokanning.openai.finetune.FineTuneRequest;
|
||||
import com.theokanning.openai.finetune.FineTuneResult;
|
||||
import com.theokanning.openai.model.Model;
|
||||
import com.theokanning.openai.moderation.ModerationRequest;
|
||||
import com.theokanning.openai.moderation.ModerationResult;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import com.theokanning.openai.search.SearchResult;
|
||||
import io.reactivex.Single;
|
||||
import okhttp3.MultipartBody;
|
||||
import okhttp3.RequestBody;
|
||||
import retrofit2.http.*;
|
||||
|
||||
public interface OpenAiApi {
|
||||
|
||||
@GET("v1/engines")
|
||||
Single<OpenAiResponse<Engine>> getEngines();
|
||||
@GET("v1/models")
|
||||
Single<OpenAiResponse<Model>> listModels();
|
||||
|
||||
@GET("/v1/engines/{engine_id}")
|
||||
Single<Engine> getEngine(@Path("engine_id") String engineId);
|
||||
@GET("/v1/models/{model_id}")
|
||||
Single<Model> getModel(@Path("model_id") String modelId);
|
||||
|
||||
@POST("/v1/completions")
|
||||
Single<CompletionResult> createCompletion(@Body CompletionRequest request);
|
||||
|
||||
@Deprecated
|
||||
@POST("/v1/engines/{engine_id}/completions")
|
||||
Single<CompletionResult> createCompletion(@Path("engine_id") String engineId, @Body CompletionRequest request);
|
||||
|
||||
@POST("/v1/engines/{engine_id}/search")
|
||||
Single<OpenAiResponse<SearchResult>> search(@Path("engine_id") String engineId, @Body SearchRequest request);
|
||||
@POST("/v1/edits")
|
||||
Single<EditResult> createEdit(@Body EditRequest request);
|
||||
|
||||
@Deprecated
|
||||
@POST("/v1/engines/{engine_id}/edits")
|
||||
Single<EditResult> createEdit(@Path("engine_id") String engineId, @Body EditRequest request);
|
||||
|
||||
@POST("/v1/embeddings")
|
||||
Single<EmbeddingResult> createEmbeddings(@Body EmbeddingRequest request);
|
||||
|
||||
@Deprecated
|
||||
@POST("/v1/engines/{engine_id}/embeddings")
|
||||
Single<EmbeddingResult> createEmbeddings(@Path("engine_id") String engineId, @Body EmbeddingRequest request);
|
||||
|
||||
@GET("/v1/files")
|
||||
Single<OpenAiResponse<File>> listFiles();
|
||||
@@ -62,4 +88,26 @@ public interface OpenAiApi {
|
||||
@DELETE("/v1/models/{fine_tune_id}")
|
||||
Single<DeleteResult> deleteFineTune(@Path("fine_tune_id") String fineTuneId);
|
||||
|
||||
@POST("/v1/moderations")
|
||||
Single<ModerationResult> createModeration(@Body ModerationRequest request);
|
||||
|
||||
@Deprecated
|
||||
@GET("v1/engines")
|
||||
Single<OpenAiResponse<Engine>> getEngines();
|
||||
|
||||
@Deprecated
|
||||
@GET("/v1/engines/{engine_id}")
|
||||
Single<Engine> getEngine(@Path("engine_id") String engineId);
|
||||
|
||||
@Deprecated
|
||||
@POST("v1/answers")
|
||||
Single<AnswerResult> createAnswer(@Body AnswerRequest request);
|
||||
|
||||
@Deprecated
|
||||
@POST("v1/classifications")
|
||||
Single<ClassificationResult> createClassification(@Body ClassificationRequest request);
|
||||
|
||||
@Deprecated
|
||||
@POST("/v1/engines/{engine_id}/search")
|
||||
Single<OpenAiResponse<SearchResult>> search(@Path("engine_id") String engineId, @Body SearchRequest request);
|
||||
}
|
||||
|
||||
@@ -4,16 +4,27 @@ import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import com.theokanning.openai.file.File;
|
||||
import com.theokanning.openai.finetune.FineTuneRequest;
|
||||
import com.theokanning.openai.finetune.FineTuneEvent;
|
||||
import com.theokanning.openai.finetune.FineTuneResult;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import okhttp3.*;
|
||||
import com.theokanning.openai.answer.AnswerRequest;
|
||||
import com.theokanning.openai.answer.AnswerResult;
|
||||
import com.theokanning.openai.classification.ClassificationRequest;
|
||||
import com.theokanning.openai.classification.ClassificationResult;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.completion.CompletionResult;
|
||||
import com.theokanning.openai.edit.EditRequest;
|
||||
import com.theokanning.openai.edit.EditResult;
|
||||
import com.theokanning.openai.embedding.EmbeddingRequest;
|
||||
import com.theokanning.openai.embedding.EmbeddingResult;
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.file.File;
|
||||
import com.theokanning.openai.finetune.FineTuneEvent;
|
||||
import com.theokanning.openai.finetune.FineTuneRequest;
|
||||
import com.theokanning.openai.finetune.FineTuneResult;
|
||||
import com.theokanning.openai.model.Model;
|
||||
import com.theokanning.openai.moderation.ModerationRequest;
|
||||
import com.theokanning.openai.moderation.ModerationResult;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import com.theokanning.openai.search.SearchResult;
|
||||
import okhttp3.*;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
@@ -25,7 +36,20 @@ public class OpenAiService {
|
||||
|
||||
OpenAiApi api;
|
||||
|
||||
/**
|
||||
* Creates a new OpenAiService that wraps OpenAiApi
|
||||
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
||||
*/
|
||||
public OpenAiService(String token) {
|
||||
this(token, 10);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new OpenAiService that wraps OpenAiApi
|
||||
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
|
||||
* @param timeout http read timeout in seconds, 0 means no timeout
|
||||
*/
|
||||
public OpenAiService(String token, int timeout) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
|
||||
@@ -34,6 +58,7 @@ public class OpenAiService {
|
||||
OkHttpClient client = new OkHttpClient.Builder()
|
||||
.addInterceptor(new AuthenticationInterceptor(token))
|
||||
.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS))
|
||||
.readTimeout(timeout, TimeUnit.SECONDS)
|
||||
.build();
|
||||
|
||||
Retrofit retrofit = new Retrofit.Builder()
|
||||
@@ -43,23 +68,53 @@ public class OpenAiService {
|
||||
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
|
||||
.build();
|
||||
|
||||
api = retrofit.create(OpenAiApi.class);
|
||||
this.api = retrofit.create(OpenAiApi.class);
|
||||
}
|
||||
|
||||
public List<Engine> getEngines() {
|
||||
return api.getEngines().blockingGet().data;
|
||||
/**
|
||||
* Creates a new OpenAiService that wraps OpenAiApi
|
||||
* @param api OpenAiApi instance to use for all methods
|
||||
*/
|
||||
public OpenAiService(OpenAiApi api) {
|
||||
this.api = api;
|
||||
}
|
||||
|
||||
public Engine getEngine(String engineId) {
|
||||
return api.getEngine(engineId).blockingGet();
|
||||
public List<Model> listModels() {
|
||||
return api.listModels().blockingGet().data;
|
||||
}
|
||||
|
||||
public Model getModel(String modelId) {
|
||||
return api.getModel(modelId).blockingGet();
|
||||
}
|
||||
|
||||
public CompletionResult createCompletion(CompletionRequest request) {
|
||||
return api.createCompletion(request).blockingGet();
|
||||
}
|
||||
|
||||
/** Use {@link OpenAiService#createCompletion(CompletionRequest)} and {@link CompletionRequest#model}instead */
|
||||
@Deprecated
|
||||
public CompletionResult createCompletion(String engineId, CompletionRequest request) {
|
||||
return api.createCompletion(engineId, request).blockingGet();
|
||||
}
|
||||
|
||||
public List<SearchResult> search(String engineId, SearchRequest request) {
|
||||
return api.search(engineId, request).blockingGet().data;
|
||||
public EditResult createEdit(EditRequest request) {
|
||||
return api.createEdit(request).blockingGet();
|
||||
}
|
||||
|
||||
/** Use {@link OpenAiService#createEdit(EditRequest)} and {@link EditRequest#model}instead */
|
||||
@Deprecated
|
||||
public EditResult createEdit(String engineId, EditRequest request) {
|
||||
return api.createEdit(engineId, request).blockingGet();
|
||||
}
|
||||
|
||||
public EmbeddingResult createEmbeddings(EmbeddingRequest request) {
|
||||
return api.createEmbeddings(request).blockingGet();
|
||||
}
|
||||
|
||||
/** Use {@link OpenAiService#createEmbeddings(EmbeddingRequest)} and {@link EmbeddingRequest#model}instead */
|
||||
@Deprecated
|
||||
public EmbeddingResult createEmbeddings(String engineId, EmbeddingRequest request) {
|
||||
return api.createEmbeddings(engineId, request).blockingGet();
|
||||
}
|
||||
|
||||
public List<File> listFiles() {
|
||||
@@ -110,4 +165,33 @@ public class OpenAiService {
|
||||
public DeleteResult deleteFineTune(String fineTuneId) {
|
||||
return api.deleteFineTune(fineTuneId).blockingGet();
|
||||
}
|
||||
|
||||
public ModerationResult createModeration(ModerationRequest request) {
|
||||
return api.createModeration(request).blockingGet();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public List<Engine> getEngines() {
|
||||
return api.getEngines().blockingGet().data;
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public Engine getEngine(String engineId) {
|
||||
return api.getEngine(engineId).blockingGet();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public AnswerResult createAnswer(AnswerRequest request) {
|
||||
return api.createAnswer(request).blockingGet();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public ClassificationResult createClassification(ClassificationRequest request) {
|
||||
return api.createClassification(request).blockingGet();
|
||||
}
|
||||
|
||||
@Deprecated
|
||||
public List<SearchResult> search(String engineId, SearchRequest request) {
|
||||
return api.search(engineId, request).blockingGet().data;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.answer.AnswerRequest;
|
||||
import com.theokanning.openai.answer.AnswerResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class AnswerTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void createAnswer() {
|
||||
AnswerRequest answerRequest = AnswerRequest.builder()
|
||||
.documents(Arrays.asList("Puppy A is happy.", "Puppy B is sad."))
|
||||
.question("which puppy is happy?")
|
||||
.searchModel("ada")
|
||||
.model("curie")
|
||||
.examplesContext("In 2017, U.S. life expectancy was 78.6 years.")
|
||||
.examples(Collections.singletonList(
|
||||
Arrays.asList("What is human life expectancy in the United States?", "78 years.")
|
||||
))
|
||||
.maxTokens(5)
|
||||
.stop(Arrays.asList("\n", "<|endoftext|>"))
|
||||
.build();
|
||||
|
||||
AnswerResult result = service.createAnswer(answerRequest);
|
||||
|
||||
assertNotNull(result.getAnswers().get(0));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.classification.ClassificationRequest;
|
||||
import com.theokanning.openai.classification.ClassificationResult;
|
||||
import com.theokanning.openai.completion.CompletionChoice;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
|
||||
public class ClassificationTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void createCompletion() {
|
||||
ClassificationRequest classificationRequest = ClassificationRequest.builder()
|
||||
.examples(Arrays.asList(
|
||||
Arrays.asList("A happy moment", "Positive"),
|
||||
Arrays.asList("I am sad.", "Negative"),
|
||||
Arrays.asList("I am feeling awesome", "Positive")
|
||||
))
|
||||
.query("It is a raining day :(")
|
||||
.model("curie")
|
||||
.searchModel("ada")
|
||||
.labels(Arrays.asList("Positive", "Negative", "Neutral"))
|
||||
.build();
|
||||
|
||||
ClassificationResult result = service.createClassification(classificationRequest);
|
||||
|
||||
assertNotNull(result.getCompletion());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.completion.CompletionChoice;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
|
||||
public class CompletionTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void createCompletion() {
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.model("ada")
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.echo(true)
|
||||
.user("testing")
|
||||
.logitBias(new HashMap<>())
|
||||
.build();
|
||||
|
||||
List<CompletionChoice> choices = service.createCompletion(completionRequest).getChoices();
|
||||
assertFalse(choices.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createCompletionDeprecated() {
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.echo(true)
|
||||
.user("testing")
|
||||
.build();
|
||||
|
||||
List<CompletionChoice> choices = service.createCompletion("ada", completionRequest).getChoices();
|
||||
assertFalse(choices.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.edit.EditRequest;
|
||||
import com.theokanning.openai.edit.EditResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class EditTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void edit() {
|
||||
EditRequest request = EditRequest.builder()
|
||||
.model("text-davinci-edit-001")
|
||||
.input("What day of the wek is it?")
|
||||
.instruction("Fix the spelling mistakes")
|
||||
.build();
|
||||
|
||||
EditResult result = service.createEdit( request);
|
||||
|
||||
assertNotNull(result.getChoices().get(0).getText());
|
||||
}
|
||||
|
||||
@Test
|
||||
void editDeprecated() {
|
||||
EditRequest request = EditRequest.builder()
|
||||
.input("What day of the wek is it?")
|
||||
.instruction("Fix the spelling mistakes")
|
||||
.build();
|
||||
|
||||
EditResult result = service.createEdit("text-davinci-edit-001", request);
|
||||
|
||||
assertNotNull(result.getChoices().get(0).getText());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.embedding.Embedding;
|
||||
import com.theokanning.openai.embedding.EmbeddingRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
|
||||
public class EmbeddingTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void createEmbeddings() {
|
||||
EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
|
||||
.model("text-similarity-babbage-001")
|
||||
.input(Collections.singletonList("The food was delicious and the waiter..."))
|
||||
.build();
|
||||
|
||||
List<Embedding> embeddings = service.createEmbeddings(embeddingRequest).getData();
|
||||
|
||||
assertFalse(embeddings.isEmpty());
|
||||
assertFalse(embeddings.get(0).getEmbedding().isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void createEmbeddingsDeprecated() {
|
||||
EmbeddingRequest embeddingRequest = EmbeddingRequest.builder()
|
||||
.input(Collections.singletonList("The food was delicious and the waiter..."))
|
||||
.build();
|
||||
|
||||
List<Embedding> embeddings = service.createEmbeddings("text-similarity-babbage-001", embeddingRequest).getData();
|
||||
|
||||
assertFalse(embeddings.isEmpty());
|
||||
assertFalse(embeddings.get(0).getEmbedding().isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
|
||||
public class EngineTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void getEngines() {
|
||||
List<Engine> engines = service.getEngines();
|
||||
|
||||
assertFalse(engines.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getEngine() {
|
||||
Engine ada = service.getEngine("ada");
|
||||
|
||||
assertEquals("ada", ada.id);
|
||||
}
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import com.theokanning.openai.finetune.FineTuneResult;
|
||||
import org.junit.jupiter.api.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
@@ -17,10 +18,13 @@ public class FineTuneTest {
|
||||
|
||||
|
||||
@BeforeAll
|
||||
static void setup() {
|
||||
static void setup() throws Exception {
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
service = new OpenAiService(token);
|
||||
fileId = service.uploadFile("fine-tune", "src/test/resources/fine-tuning-data.jsonl").getId();
|
||||
|
||||
// wait for file to be processed
|
||||
TimeUnit.SECONDS.sleep(10);
|
||||
}
|
||||
|
||||
@AfterAll
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.model.Model;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
|
||||
public class ModelTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void listModels() {
|
||||
List<Model> models = service.listModels();
|
||||
|
||||
assertFalse(models.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
void getModel() {
|
||||
Model ada = service.getModel("ada");
|
||||
|
||||
assertEquals("ada", ada.id);
|
||||
assertEquals("openai", ada.ownedBy);
|
||||
assertFalse(ada.permission.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.moderation.ModerationRequest;
|
||||
import com.theokanning.openai.moderation.Moderation;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
public class ModerationTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void createModeration() {
|
||||
ModerationRequest moderationRequest = ModerationRequest.builder()
|
||||
.input("I want to kill them")
|
||||
.model("text-moderation-latest")
|
||||
.build();
|
||||
|
||||
Moderation moderationScore = service.createModeration(moderationRequest).getResults().get(0);
|
||||
|
||||
assertTrue(moderationScore.isFlagged());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import com.theokanning.openai.search.SearchResult;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
||||
public class SearchTest {
|
||||
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
@Test
|
||||
void search() {
|
||||
SearchRequest searchRequest = SearchRequest.builder()
|
||||
.documents(Arrays.asList("Water", "Earth", "Electricity", "Fire"))
|
||||
.query("Pikachu")
|
||||
.build();
|
||||
|
||||
List<SearchResult> results = service.search("ada", searchRequest);
|
||||
assertFalse(results.isEmpty());
|
||||
}
|
||||
}
|
||||
@@ -2,36 +2,19 @@ package example;
|
||||
|
||||
import com.theokanning.openai.OpenAiService;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
class OpenAiApiExample {
|
||||
public static void main(String... args) {
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
System.out.println("\nGetting available engines...");
|
||||
service.getEngines().forEach(System.out::println);
|
||||
|
||||
System.out.println("\nGetting ada engine...");
|
||||
Engine ada = service.getEngine("ada");
|
||||
System.out.println(ada);
|
||||
|
||||
System.out.println("\nCreating completion...");
|
||||
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.model("ada")
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.echo(true)
|
||||
.user("testing")
|
||||
.build();
|
||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||
|
||||
System.out.println("\nSearching documents...");
|
||||
SearchRequest searchRequest = SearchRequest.builder()
|
||||
.documents(Arrays.asList("Water", "Earth", "Electricity", "Fire"))
|
||||
.query("Pikachu")
|
||||
.build();
|
||||
service.search("ada", searchRequest).forEach(System.out::println);
|
||||
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
GROUP=com.theokanning.openai-gpt3-java
|
||||
VERSION_NAME=0.5.0
|
||||
VERSION_NAME=0.8.1
|
||||
|
||||
POM_URL=https://github.com/theokanning/openai-java
|
||||
POM_SCM_URL=https://github.com/theokanning/openai-java
|
||||
|
||||
Reference in New Issue
Block a user