Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9f5b64b

Browse filesBrowse files
authored
Add answer support (TheoKanning#12)
1 parent 103c34d commit 9f5b64b
Copy full SHA for 9f5b64b

File tree

Expand file treeCollapse file tree

7 files changed

+262
-11
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+262
-11
lines changed
+140Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package com.theokanning.openai.answer;
2+
3+
import lombok.*;
4+
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Given a question, a set of documents, and some examples, the API generates an answer to the question based
10+
* on the information in the set of documents. This is useful for question-answering applications on sources of truth,
11+
* like company documentation or a knowledge base.
12+
*
13+
* Documentation taken from
14+
* https://beta.openai.com/docs/api-reference/answers/create
15+
*/
16+
@Builder
17+
@NoArgsConstructor
18+
@AllArgsConstructor
19+
@Data
20+
public class AnswerRequest {
21+
22+
/**
23+
* ID of the engine to use for completion.
24+
*/
25+
@NonNull
26+
String model;
27+
28+
/**
29+
* Question to get answered.
30+
*/
31+
@NonNull
32+
String question;
33+
34+
/**
35+
* List of (question, answer) pairs that will help steer the model towards the tone and answer format you'd like.
36+
* We recommend adding 2 to 3 examples.
37+
*/
38+
@NonNull
39+
List<List<String>> examples;
40+
41+
/**
42+
* A text snippet containing the contextual information used to generate the answers for the examples you provide.
43+
*/
44+
@NonNull
45+
String examplesContext;
46+
47+
/**
48+
* List of documents from which the answer for the input question should be derived.
49+
* If this is an empty list, the question will be answered based on the question-answer examples.
50+
*
51+
* You should specify either documents or a file, but not both.
52+
*/
53+
List<String> documents;
54+
55+
/**
56+
* The ID of an uploaded file that contains documents to search over.
57+
* See upload file for how to upload a file of the desired format and purpose.
58+
*
59+
* You should specify either documents or file, but not both.
60+
*/
61+
String file;
62+
63+
/**
64+
* ID of the engine to use for Search. You can select one of ada, babbage, curie, or davinci.
65+
*/
66+
String searchModel;
67+
68+
/**
69+
* The maximum number of documents to be ranked by Search when using file.
70+
* Setting it to a higher value leads to improved accuracy but with increased latency and cost.
71+
*/
72+
Integer maxRerank;
73+
74+
/**
75+
* What sampling temperature to use. Higher values means the model will take more risks.
76+
* Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.
77+
*
78+
* We generally recommend using this or {@link top_p} but not both.
79+
*/
80+
Double temperature;
81+
82+
/**
83+
* Include the log probabilities on the logprobs most likely tokens, as well the chosen tokens.
84+
* For example, if logprobs is 10, the API will return a list of the 10 most likely tokens.
85+
* The API will always return the logprob of the sampled token,
86+
* so there may be up to logprobs+1 elements in the response.
87+
*/
88+
Integer logprobs;
89+
90+
/**
91+
* The maximum number of tokens allowed for the generated answer.
92+
*/
93+
Integer maxTokens;
94+
95+
/**
96+
* Up to 4 sequences where the API will stop generating further tokens.
97+
* The returned text will not contain the stop sequence.
98+
*/
99+
List<String> stop;
100+
101+
/**
102+
* How many answers to generate for each question.
103+
*/
104+
Integer n;
105+
106+
/**
107+
* Modify the likelihood of specified tokens appearing in the completion.
108+
*
109+
* Accepts a json object that maps tokens (specified by their token ID in the GPT tokenizer) to an
110+
* associated bias value from -100 to 100.
111+
*/
112+
Map<String, Double> logitBias;
113+
114+
/**
115+
* A special boolean flag for showing metadata.
116+
* If set to true, each document entry in the returned JSON will contain a "metadata" field.
117+
*
118+
* This flag only takes effect when file is set.
119+
*/
120+
Boolean returnMetadata;
121+
122+
/**
123+
* If set to true, the returned JSON will include a "prompt" field containing the final prompt that was
124+
* used to request a completion. This is mainly useful for debugging purposes.
125+
*/
126+
Boolean returnPrompt;
127+
128+
/**
129+
* If an object name is in the list, we provide the full information of the object;
130+
* otherwise, we only provide the object ID.
131+
*
132+
* Currently we support completion and file objects for expansion.
133+
*/
134+
List<String> expand;
135+
136+
/**
137+
* A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
138+
*/
139+
String user;
140+
}
+43Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.theokanning.openai.answer;
2+
3+
import lombok.Data;
4+
5+
import java.util.List;
6+
7+
/**
8+
* An object containing a response from the answer api
9+
* <p>
10+
* https://beta.openai.com/docs/api-reference/answers/create
11+
*/
12+
@Data
13+
public class AnswerResult {
14+
/**
15+
* A list of generated answers to the provided question/
16+
*/
17+
List<String> answers;
18+
19+
/**
20+
* A unique id assigned to this completion
21+
*/
22+
String completion;
23+
24+
/**
25+
* The GPT-3 model used for completion
26+
*/
27+
String model;
28+
29+
/**
30+
* The type of object returned, should be "answer"
31+
*/
32+
String object;
33+
34+
/**
35+
* The GPT-3 model used for search
36+
*/
37+
String searchModel;
38+
39+
/**
40+
* A list of the most relevant documents for the question.
41+
*/
42+
List<Document> selectedDocuments;
43+
}
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.theokanning.openai.answer;
2+
3+
import lombok.Data;
4+
5+
/**
6+
* Represents an example returned by the classification api
7+
*
8+
* https://beta.openai.com/docs/api-reference/classifications/create
9+
*/
10+
@Data
11+
public class Document {
12+
/**
13+
* The position of this example in the example list
14+
*/
15+
Integer document;
16+
17+
/**
18+
* The text of the example
19+
*/
20+
String text;
21+
}

‎api/src/main/java/com/theokanning/openai/classification/ClassificationResult.java

Copy file name to clipboardExpand all lines: api/src/main/java/com/theokanning/openai/classification/ClassificationResult.java
+1-2Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
package com.theokanning.openai.classification;
22

3-
import com.theokanning.openai.completion.CompletionChoice;
43
import lombok.Data;
54

65
import java.util.List;
76

87
/**
98
* An object containing a response from the classification api
10-
* <p>
9+
* <
1110
* https://beta.openai.com/docs/api-reference/classifications/create
1211
*/
1312
@Data

‎client/src/main/java/com/theokanning/openai/OpenAiApi.java

Copy file name to clipboardExpand all lines: client/src/main/java/com/theokanning/openai/OpenAiApi.java
+9-4Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
package com.theokanning.openai;
22

3+
import com.theokanning.openai.answer.AnswerRequest;
4+
import com.theokanning.openai.answer.AnswerResult;
35
import com.theokanning.openai.classification.ClassificationRequest;
46
import com.theokanning.openai.classification.ClassificationResult;
7+
import com.theokanning.openai.completion.CompletionRequest;
8+
import com.theokanning.openai.completion.CompletionResult;
59
import com.theokanning.openai.engine.Engine;
610
import com.theokanning.openai.file.File;
7-
import com.theokanning.openai.finetune.FineTuneRequest;
811
import com.theokanning.openai.finetune.FineTuneEvent;
12+
import com.theokanning.openai.finetune.FineTuneRequest;
913
import com.theokanning.openai.finetune.FineTuneResult;
1014
import com.theokanning.openai.search.SearchRequest;
11-
import io.reactivex.Single;
12-
import com.theokanning.openai.completion.CompletionRequest;
13-
import com.theokanning.openai.completion.CompletionResult;
1415
import com.theokanning.openai.search.SearchResult;
16+
import io.reactivex.Single;
1517
import okhttp3.MultipartBody;
1618
import okhttp3.RequestBody;
1719
import retrofit2.http.*;
@@ -33,6 +35,9 @@ public interface OpenAiApi {
3335
@POST("v1/classifications")
3436
Single<ClassificationResult> createClassification(@Body ClassificationRequest request);
3537

38+
@POST("v1/answers")
39+
Single<AnswerResult> createAnswer(@Body AnswerRequest request);
40+
3641
@GET("/v1/files")
3742
Single<OpenAiResponse<File>> listFiles();
3843

‎client/src/main/java/com/theokanning/openai/OpenAiService.java

Copy file name to clipboardExpand all lines: client/src/main/java/com/theokanning/openai/OpenAiService.java
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
import com.fasterxml.jackson.databind.DeserializationFeature;
55
import com.fasterxml.jackson.databind.ObjectMapper;
66
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
7+
import com.theokanning.openai.answer.AnswerRequest;
8+
import com.theokanning.openai.answer.AnswerResult;
79
import com.theokanning.openai.classification.ClassificationRequest;
810
import com.theokanning.openai.classification.ClassificationResult;
11+
import com.theokanning.openai.completion.CompletionRequest;
12+
import com.theokanning.openai.completion.CompletionResult;
13+
import com.theokanning.openai.engine.Engine;
914
import com.theokanning.openai.file.File;
10-
import com.theokanning.openai.finetune.FineTuneRequest;
1115
import com.theokanning.openai.finetune.FineTuneEvent;
16+
import com.theokanning.openai.finetune.FineTuneRequest;
1217
import com.theokanning.openai.finetune.FineTuneResult;
1318
import com.theokanning.openai.search.SearchRequest;
14-
import okhttp3.*;
15-
import com.theokanning.openai.completion.CompletionRequest;
16-
import com.theokanning.openai.completion.CompletionResult;
17-
import com.theokanning.openai.engine.Engine;
1819
import com.theokanning.openai.search.SearchResult;
20+
import okhttp3.*;
1921
import retrofit2.Retrofit;
2022
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
2123
import retrofit2.converter.jackson.JacksonConverterFactory;
@@ -68,6 +70,10 @@ public ClassificationResult createClassification(ClassificationRequest request)
6870
return api.createClassification(request).blockingGet();
6971
}
7072

73+
public AnswerResult createAnswer(AnswerRequest request) {
74+
return api.createAnswer(request).blockingGet();
75+
}
76+
7177
public List<File> listFiles() {
7278
return api.listFiles().blockingGet().data;
7379
}
+37Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.theokanning.openai;
2+
3+
import com.theokanning.openai.answer.AnswerRequest;
4+
import com.theokanning.openai.answer.AnswerResult;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.Arrays;
8+
import java.util.Collections;
9+
10+
import static org.junit.jupiter.api.Assertions.assertNotNull;
11+
12+
13+
public class AnswerTest {
14+
15+
String token = System.getenv("OPENAI_TOKEN");
16+
OpenAiService service = new OpenAiService(token);
17+
18+
@Test
19+
void createAnswer() {
20+
AnswerRequest answerRequest = AnswerRequest.builder()
21+
.documents(Arrays.asList("Puppy A is happy.", "Puppy B is sad."))
22+
.question("which puppy is happy?")
23+
.searchModel("ada")
24+
.model("curie")
25+
.examplesContext("In 2017, U.S. life expectancy was 78.6 years.")
26+
.examples(Collections.singletonList(
27+
Arrays.asList("What is human life expectancy in the United States?", "78 years.")
28+
))
29+
.maxTokens(5)
30+
.stop(Arrays.asList("\n", "<|endoftext|>"))
31+
.build();
32+
33+
AnswerResult result = service.createAnswer(answerRequest);
34+
35+
assertNotNull(result.getAnswers().get(0));
36+
}
37+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.