Add Completion api
Might add a builder or something to make setting specific parameters easier
This commit is contained in:
parent
13336e6cb6
commit
5d74cfde5a
@ -1,6 +1,7 @@
|
||||
package example;
|
||||
|
||||
import openai.OpenAiService;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.engine.Engine;
|
||||
|
||||
class OpenAiApiExample {
|
||||
@ -8,11 +9,18 @@ class OpenAiApiExample {
|
||||
String token = System.getenv("OPENAI_TOKEN");
|
||||
OpenAiService service = new OpenAiService(token);
|
||||
|
||||
System.out.println("Getting available engines");
|
||||
System.out.println("Getting available engines...");
|
||||
service.getEngines().forEach(System.out::println);
|
||||
|
||||
System.out.println("Getting ada engine");
|
||||
System.out.println("Getting ada engine...");
|
||||
Engine ada = service.getEngine("ada");
|
||||
System.out.println(ada);
|
||||
|
||||
System.out.println("Creating completion...");
|
||||
CompletionRequest completionRequest = new CompletionRequest();
|
||||
completionRequest.setPrompt("Somebody once told me the world is gonna roll me");
|
||||
completionRequest.setEcho(true);
|
||||
service.createCompletion("ada", completionRequest).getChoices().forEach(System.out::println);
|
||||
|
||||
}
|
||||
}
|
@ -1,8 +1,12 @@
|
||||
package openai;
|
||||
|
||||
import io.reactivex.Single;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.completion.CompletionResult;
|
||||
import openai.engine.Engine;
|
||||
import retrofit2.http.Body;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.Path;
|
||||
|
||||
public interface OpenAiApi {
|
||||
@ -12,4 +16,7 @@ public interface OpenAiApi {
|
||||
|
||||
@GET("/v1/engines/{engine_id}")
|
||||
Single<Engine> getEngine(@Path("engine_id") String engineId);
|
||||
|
||||
@POST("/v1/engines/{engine_id}/completions")
|
||||
Single<CompletionResult> createCompletion(@Path("engine_id") String engineId, @Body CompletionRequest request);
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
|
||||
import okhttp3.ConnectionPool;
|
||||
import okhttp3.OkHttpClient;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.completion.CompletionResult;
|
||||
import openai.engine.Engine;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
@ -46,4 +48,8 @@ public class OpenAiService {
|
||||
public Engine getEngine(String engineId) {
|
||||
return api.getEngine(engineId).blockingGet();
|
||||
}
|
||||
|
||||
public CompletionResult createCompletion(String engineId, CompletionRequest request) {
|
||||
return api.createCompletion(engineId, request).blockingGet();
|
||||
}
|
||||
}
|
||||
|
11
openai/src/main/java/openai/completion/CompletionChoice.java
Normal file
11
openai/src/main/java/openai/completion/CompletionChoice.java
Normal file
@ -0,0 +1,11 @@
|
||||
package openai.completion;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class CompletionChoice {
|
||||
String text;
|
||||
Integer index;
|
||||
// todo add logprobs
|
||||
String finish_reason;
|
||||
}
|
@ -0,0 +1,21 @@
|
||||
package openai.completion;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class CompletionRequest {
|
||||
String prompt;
|
||||
Integer maxTokens;
|
||||
Double temperature;
|
||||
Double topP;
|
||||
Integer n;
|
||||
Boolean stream;
|
||||
Integer logprobs;
|
||||
Boolean echo;
|
||||
List<String> stop; //todo test this
|
||||
Double presencePenalty;
|
||||
Double frequencyPenalty;
|
||||
Integer bestOf;
|
||||
}
|
14
openai/src/main/java/openai/completion/CompletionResult.java
Normal file
14
openai/src/main/java/openai/completion/CompletionResult.java
Normal file
@ -0,0 +1,14 @@
|
||||
package openai.completion;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class CompletionResult {
|
||||
String id;
|
||||
String object;
|
||||
long created;
|
||||
String model;
|
||||
List<CompletionChoice> choices;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user