Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 48cd7272f7 | |||
| 1600841a58 | |||
| 8d7f240010 | |||
| c76923926e | |||
| 0caab13666 | |||
| 5e14d4f62b |
@@ -41,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
|
||||
|
||||
+2
-2
@@ -3,8 +3,8 @@ apply plugin: "com.vanniktech.maven.publish"
|
||||
|
||||
dependencies {
|
||||
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.9.0'
|
||||
compileOnly 'org.projectlombok:lombok:1.18.12'
|
||||
annotationProcessor 'org.projectlombok:lombok:1.18.12'
|
||||
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;
|
||||
}
|
||||
@@ -6,6 +6,7 @@ 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.
|
||||
@@ -110,6 +111,15 @@ public class CompletionRequest {
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.theokanning.openai.completion;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -35,4 +36,9 @@ public class CompletionResult {
|
||||
* A list of generated completions.
|
||||
*/
|
||||
List<CompletionChoice> choices;
|
||||
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
Usage usage;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.theokanning.openai.edit;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -30,5 +31,5 @@ public class EditResult {
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
public EditUsage usage;
|
||||
public Usage usage;
|
||||
}
|
||||
|
||||
@@ -5,9 +5,12 @@ 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 {
|
||||
|
||||
/**
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.theokanning.openai.embedding;
|
||||
|
||||
import com.theokanning.openai.Usage;
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
@@ -26,4 +27,9 @@ public class EmbeddingResult {
|
||||
* A list of the calculated embeddings
|
||||
*/
|
||||
List<Embedding> data;
|
||||
|
||||
/**
|
||||
* The API usage for this request
|
||||
*/
|
||||
Usage usage;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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;
|
||||
@@ -21,6 +22,7 @@ public class CompletionTest {
|
||||
.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();
|
||||
|
||||
@@ -2,26 +2,19 @@ package example;
|
||||
|
||||
import com.theokanning.openai.OpenAiService;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
|
||||
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);
|
||||
service.createCompletion(completionRequest).getChoices().forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,5 +1,5 @@
|
||||
GROUP=com.theokanning.openai-gpt3-java
|
||||
VERSION_NAME=0.8.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