Add search objects and endpoint
This commit is contained in:
parent
61300727c8
commit
cdb7ae22c4
|
@ -3,24 +3,32 @@ package example;
|
|||
import openai.OpenAiService;
|
||||
import openai.completion.CompletionRequest;
|
||||
import openai.engine.Engine;
|
||||
import 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("Getting available engines...");
|
||||
System.out.println("\nGetting available engines...");
|
||||
service.getEngines().forEach(System.out::println);
|
||||
|
||||
System.out.println("Getting ada engine...");
|
||||
System.out.println("\nGetting ada engine...");
|
||||
Engine ada = service.getEngine("ada");
|
||||
System.out.println(ada);
|
||||
|
||||
System.out.println("Creating completion...");
|
||||
System.out.println("\nCreating 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);
|
||||
|
||||
System.out.println("\nSearching documents...");
|
||||
SearchRequest searchRequest = new SearchRequest();
|
||||
searchRequest.setDocuments(Arrays.asList("Water", "Earth", "Electricity", "Fire"));
|
||||
searchRequest.setQuery("Pikachu");
|
||||
service.search("ada", searchRequest).forEach(System.out::println);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
package openai.search;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Data
|
||||
public class SearchRequest {
|
||||
List<String> documents;
|
||||
String query;
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package openai.search;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
@Data
|
||||
public class SearchResult {
|
||||
Integer document;
|
||||
String object;
|
||||
Double score;
|
||||
}
|
|
@ -4,6 +4,8 @@ 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 retrofit2.http.Body;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
|
@ -19,4 +21,7 @@ public interface OpenAiApi {
|
|||
|
||||
@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);
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ import okhttp3.OkHttpClient;
|
|||
import openai.completion.CompletionRequest;
|
||||
import openai.completion.CompletionResult;
|
||||
import openai.engine.Engine;
|
||||
import openai.search.SearchRequest;
|
||||
import openai.search.SearchResult;
|
||||
import retrofit2.Retrofit;
|
||||
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
|
||||
import retrofit2.converter.jackson.JacksonConverterFactory;
|
||||
|
@ -52,4 +54,8 @@ public class OpenAiService {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue