Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 618a097254 | |||
| 0297fe0393 | |||
| c2044c64f9 | |||
| 00fa073446 | |||
| d15dc718c2 | |||
| 44ad76258c |
@@ -0,0 +1,26 @@
|
||||
# This workflow will build a Java project with Gradle
|
||||
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-gradle
|
||||
|
||||
name: Java CI with Gradle
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [ main ]
|
||||
pull_request:
|
||||
branches: [ main ]
|
||||
|
||||
jobs:
|
||||
build:
|
||||
|
||||
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: Grant execute permission for gradlew
|
||||
run: chmod +x gradlew
|
||||
- name: Build with Gradle
|
||||
run: ./gradlew build
|
||||
@@ -12,6 +12,7 @@
|
||||
|
||||
# Package Files #
|
||||
*.jar
|
||||
!gradle/wrapper/gradle-wrapper.jar
|
||||
*.war
|
||||
*.nar
|
||||
*.ear
|
||||
|
||||
@@ -2,26 +2,31 @@
|
||||
Java libraries for using OpenAI's GPT-3 api.
|
||||
|
||||
Includes the following artifacts:
|
||||
- api : request/response POJOs for the GPT-3 engine, completion, and search APIs.
|
||||
- client : a basic retrofit client for the GPT-3 endpoints
|
||||
- `api` : request/response POJOs for the GPT-3 engine, completion, and search APIs.
|
||||
- `client` : a basic retrofit client for the GPT-3 endpoints
|
||||
|
||||
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)
|
||||
CompletionRequest completionRequest = new CompletionRequest();
|
||||
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
|
||||
completionRequest.setEcho(true);
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.echo(true)
|
||||
.build();
|
||||
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
|
||||
|
||||
+37
-3
@@ -1,5 +1,6 @@
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
dependencies {
|
||||
compileOnly 'org.projectlombok:lombok:1.18.12'
|
||||
@@ -7,23 +8,56 @@ dependencies {
|
||||
}
|
||||
|
||||
ext {
|
||||
libraryVersion = '0.1.0'
|
||||
libraryVersion = '0.2.0'
|
||||
}
|
||||
|
||||
version = libraryVersion
|
||||
group = 'com.theokanning.openai-gpt3-java'
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
ApiPublication(MavenPublication) {
|
||||
from components.java
|
||||
groupId project.group
|
||||
artifactId 'api'
|
||||
version libraryVersion
|
||||
pom {
|
||||
description = 'POJOs for the OpenAI GPT-3 API'
|
||||
name = 'api'
|
||||
url = 'https://github.com/theokanning/openai-java'
|
||||
developers {
|
||||
developer {
|
||||
id = "theokanning"
|
||||
name = "Theo Kanning"
|
||||
email = "theokanning@gmail.com"
|
||||
}
|
||||
}
|
||||
scm {
|
||||
url = "https://github.com/theokanning/openai-java"
|
||||
}
|
||||
licenses {
|
||||
license {
|
||||
name = "The MIT License"
|
||||
url = "https://www.mit.edu/~amini/LICENSE.md"
|
||||
distribution = "repo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bintray {
|
||||
user = System.getenv("BINTRAY_USER")
|
||||
key = System.getenv("BINTRAY_KEY")
|
||||
|
||||
configurations = ['archives']
|
||||
publications = ['ApiPublication']
|
||||
pkg {
|
||||
repo = 'openai-gpt3-java'
|
||||
name = 'api'
|
||||
vcsUrl = 'https://github.com/TheoKanning/openai-java.git'
|
||||
licenses = ["MIT"]
|
||||
publish = true
|
||||
publish = false
|
||||
version {
|
||||
name = libraryVersion
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package openai;
|
||||
package com.theokanning.openai;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
+6
-2
@@ -1,4 +1,4 @@
|
||||
package openai.completion;
|
||||
package com.theokanning.openai.completion;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -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".
|
||||
+9
-2
@@ -1,6 +1,9 @@
|
||||
package openai.completion;
|
||||
package com.theokanning.openai.completion;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,8 +14,12 @@ import java.util.List;
|
||||
* Documentation taken from
|
||||
* https://beta.openai.com/docs/api-reference/create-completion
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class CompletionRequest {
|
||||
|
||||
/**
|
||||
* An optional prompt to complete from
|
||||
*/
|
||||
@@ -74,7 +81,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.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package openai.completion;
|
||||
package com.theokanning.openai.completion;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.theokanning.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;
|
||||
}
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package openai.engine;
|
||||
package com.theokanning.openai.engine;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
+8
-1
@@ -1,6 +1,9 @@
|
||||
package openai.search;
|
||||
package com.theokanning.openai.search;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@@ -11,8 +14,12 @@ import java.util.List;
|
||||
*
|
||||
* https://beta.openai.com/docs/api-reference/search
|
||||
*/
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Data
|
||||
public class SearchRequest {
|
||||
|
||||
/**
|
||||
* Documents to search over
|
||||
*/
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package openai.search;
|
||||
package com.theokanning.openai.search;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
+37
-3
@@ -1,5 +1,6 @@
|
||||
apply plugin: 'java-library'
|
||||
apply plugin: 'com.jfrog.bintray'
|
||||
apply plugin: 'maven-publish'
|
||||
|
||||
dependencies {
|
||||
api project(":api")
|
||||
@@ -9,23 +10,56 @@ dependencies {
|
||||
}
|
||||
|
||||
ext {
|
||||
libraryVersion = '0.1.0'
|
||||
libraryVersion = '0.2.0'
|
||||
}
|
||||
|
||||
version = libraryVersion
|
||||
group = 'com.theokanning.openai-gpt3-java'
|
||||
|
||||
publishing {
|
||||
publications {
|
||||
ClientPublication(MavenPublication) {
|
||||
from components.java
|
||||
groupId project.group
|
||||
artifactId 'client'
|
||||
version libraryVersion
|
||||
pom {
|
||||
description = 'Basic retrofit client for OpenAI\'s GPT-3 API'
|
||||
name = 'client'
|
||||
url = 'https://github.com/theokanning/openai-java'
|
||||
developers {
|
||||
developer {
|
||||
id = "theokanning"
|
||||
name = "Theo Kanning"
|
||||
email = "theokanning@gmail.com"
|
||||
}
|
||||
}
|
||||
scm {
|
||||
url = "https://github.com/theokanning/openai-java"
|
||||
}
|
||||
licenses {
|
||||
license {
|
||||
name = "The MIT License"
|
||||
url = "https://www.mit.edu/~amini/LICENSE.md"
|
||||
distribution = "repo"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bintray {
|
||||
user = System.getenv("BINTRAY_USER")
|
||||
key = System.getenv("BINTRAY_KEY")
|
||||
|
||||
configurations = ['archives']
|
||||
publications = ['ClientPublication']
|
||||
pkg {
|
||||
repo = 'openai-gpt3-java'
|
||||
name = 'client'
|
||||
vcsUrl = 'https://github.com/TheoKanning/openai-java.git'
|
||||
licenses = ["MIT"]
|
||||
publish = true
|
||||
publish = false
|
||||
version {
|
||||
name = libraryVersion
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
package openai;
|
||||
package com.theokanning.openai;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
+6
-6
@@ -1,11 +1,11 @@
|
||||
package openai;
|
||||
package com.theokanning.openai;
|
||||
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.search.SearchRequest;
|
||||
import io.reactivex.Single;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.completion.CompletionResult;
|
||||
import openai.engine.Engine;
|
||||
import openai.search.SearchRequest;
|
||||
import openai.search.SearchResult;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.completion.CompletionResult;
|
||||
import com.theokanning.openai.search.SearchResult;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
+6
-6
@@ -1,16 +1,16 @@
|
||||
package openai;
|
||||
package com.theokanning.openai;
|
||||
|
||||
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.search.SearchRequest;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.completion.CompletionResult;
|
||||
import openai.engine.Engine;
|
||||
import openai.search.SearchRequest;
|
||||
import openai.search.SearchResult;
|
||||
import com.theokanning.openai.completion.CompletionRequest;
|
||||
import com.theokanning.openai.completion.CompletionResult;
|
||||
import com.theokanning.openai.engine.Engine;
|
||||
import com.theokanning.openai.search.SearchResult;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
@@ -1,9 +1,9 @@
|
||||
package example;
|
||||
|
||||
import openai.OpenAiService;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.engine.Engine;
|
||||
import openai.search.SearchRequest;
|
||||
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;
|
||||
|
||||
@@ -20,15 +20,18 @@ class OpenAiApiExample {
|
||||
System.out.println(ada);
|
||||
|
||||
System.out.println("\nCreating completion...");
|
||||
CompletionRequest completionRequest = new CompletionRequest();
|
||||
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
|
||||
completionRequest.setEcho(true);
|
||||
|
||||
CompletionRequest completionRequest = CompletionRequest.builder()
|
||||
.prompt("Somebody once told me the world is gonna roll me")
|
||||
.echo(true)
|
||||
.build();
|
||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||
|
||||
System.out.println("\nSearching documents...");
|
||||
SearchRequest searchRequest = new SearchRequest();
|
||||
searchRequest.setDocuments(Arrays.asList("Water", "Earth", "Electricity", "Fire"));
|
||||
searchRequest.setQuery("Pikachu");
|
||||
SearchRequest searchRequest = SearchRequest.builder()
|
||||
.documents(Arrays.asList("Water", "Earth", "Electricity", "Fire"))
|
||||
.query("Pikachu")
|
||||
.build();
|
||||
service.search("ada", searchRequest).forEach(System.out::println);
|
||||
}
|
||||
}
|
||||
Vendored
BIN
Binary file not shown.
Reference in New Issue
Block a user