Delete deprecated Answer, Classification, and Search APIs (#59)

OpenAI deprecated these months ago, and they have now stopped working.
This commit is contained in:
Theo Kanning 2023-01-02 15:36:33 -06:00 committed by GitHub
parent 48cd7272f7
commit 5b3955d81a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 13 additions and 608 deletions

View File

@ -1,141 +0,0 @@
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;
}

View File

@ -1,44 +0,0 @@
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;
}

View File

@ -1,22 +0,0 @@
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;
}

View File

@ -1,121 +0,0 @@
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;
}

View File

@ -1,45 +0,0 @@
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;
}

View File

@ -1,27 +0,0 @@
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;
}

View File

@ -1,33 +0,0 @@
package com.theokanning.openai.search;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
* A request to the document search api.
* 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/searches
*/
@Deprecated
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Data
public class SearchRequest {
/**
* Documents to search over
*/
List<String> documents;
/**
* Search query
*/
String query;
}

View File

@ -1,28 +0,0 @@
package com.theokanning.openai.search;
import lombok.Data;
/**
* A search result for a single document.
*
* https://beta.openai.com/docs/api-reference/searches
*/
@Deprecated
@Data
public class SearchResult {
/**
* The position of this document in the request list
*/
Integer document;
/**
* The type of object returned, should be "search_result"
*/
String object;
/**
* A number measuring the document's correlation with the query.
* A higher score means a stronger relationship.
*/
Double score;
}

View File

@ -1,9 +1,5 @@
package com.theokanning.openai;
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;
@ -18,8 +14,6 @@ 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;
@ -98,16 +92,4 @@ public interface OpenAiApi {
@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);
}

View File

@ -4,10 +4,6 @@ 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.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;
@ -22,8 +18,6 @@ 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;
@ -38,6 +32,7 @@ public class OpenAiService {
/**
* Creates a new OpenAiService that wraps OpenAiApi
*
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
*/
public OpenAiService(String token) {
@ -46,7 +41,8 @@ public class OpenAiService {
/**
* Creates a new OpenAiService that wraps OpenAiApi
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
*
* @param token OpenAi token string "sk-XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
* @param timeout http read timeout in seconds, 0 means no timeout
*/
public OpenAiService(String token, int timeout) {
@ -73,6 +69,7 @@ public class OpenAiService {
/**
* Creates a new OpenAiService that wraps OpenAiApi
*
* @param api OpenAiApi instance to use for all methods
*/
public OpenAiService(OpenAiApi api) {
@ -91,7 +88,9 @@ public class OpenAiService {
return api.createCompletion(request).blockingGet();
}
/** Use {@link OpenAiService#createCompletion(CompletionRequest)} and {@link CompletionRequest#model}instead */
/**
* Use {@link OpenAiService#createCompletion(CompletionRequest)} and {@link CompletionRequest#model}instead
*/
@Deprecated
public CompletionResult createCompletion(String engineId, CompletionRequest request) {
return api.createCompletion(engineId, request).blockingGet();
@ -101,7 +100,9 @@ public class OpenAiService {
return api.createEdit(request).blockingGet();
}
/** Use {@link OpenAiService#createEdit(EditRequest)} and {@link EditRequest#model}instead */
/**
* Use {@link OpenAiService#createEdit(EditRequest)} and {@link EditRequest#model}instead
*/
@Deprecated
public EditResult createEdit(String engineId, EditRequest request) {
return api.createEdit(engineId, request).blockingGet();
@ -111,7 +112,9 @@ public class OpenAiService {
return api.createEmbeddings(request).blockingGet();
}
/** Use {@link OpenAiService#createEmbeddings(EmbeddingRequest)} and {@link EmbeddingRequest#model}instead */
/**
* Use {@link OpenAiService#createEmbeddings(EmbeddingRequest)} and {@link EmbeddingRequest#model}instead
*/
@Deprecated
public EmbeddingResult createEmbeddings(String engineId, EmbeddingRequest request) {
return api.createEmbeddings(engineId, request).blockingGet();
@ -179,19 +182,4 @@ public class OpenAiService {
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;
}
}

View File

@ -1,37 +0,0 @@
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));
}
}

View File

@ -1,39 +0,0 @@
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());
}
}

View File

@ -1,28 +0,0 @@
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());
}
}