This repository was archived by the owner on Jun 6, 2024. It is now read-only.
File tree 7 files changed +134
-1
lines changed
api/src/main/java/com/theokanning/openai
main/java/com/theokanning/openai
test/java/com/theokanning/openai
7 files changed +134
-1
lines changed Original file line number Diff line number Diff line change 6
6
7
7
/**
8
8
* An object containing a response from the answer api
9
- * <p>
9
+ *
10
10
* https://beta.openai.com/docs/api-reference/answers/create
11
11
*/
12
12
@ Data
Original file line number Diff line number Diff line change
1
+ package com .theokanning .openai .embedding ;
2
+
3
+ import lombok .Data ;
4
+
5
+ import java .util .List ;
6
+
7
+ /**
8
+ * Represents an embedding returned by the embedding api
9
+ *
10
+ * https://beta.openai.com/docs/api-reference/classifications/create
11
+ */
12
+ @ Data
13
+ public class Embedding {
14
+
15
+ /**
16
+ * The type of object returned, should be "embedding"
17
+ */
18
+ String object ;
19
+
20
+ /**
21
+ * The embedding vector
22
+ */
23
+ List <Double > embedding ;
24
+
25
+ /**
26
+ * The position of this embedding in the list
27
+ */
28
+ Integer index ;
29
+ }
Original file line number Diff line number Diff line change
1
+ package com .theokanning .openai .embedding ;
2
+
3
+ import lombok .*;
4
+
5
+ import java .util .List ;
6
+
7
+ /**
8
+ * Creates an embedding vector representing the input text.
9
+ *
10
+ * Documentation taken from
11
+ * https://beta.openai.com/docs/api-reference/embeddings/create
12
+ */
13
+ @ Builder
14
+ @ NoArgsConstructor
15
+ @ AllArgsConstructor
16
+ @ Data
17
+ public class EmbeddingRequest {
18
+
19
+ /**
20
+ * Input text to get embeddings for, encoded as a string or array of tokens.
21
+ * To get embeddings for multiple inputs in a single request, pass an array of strings or array of token arrays.
22
+ * Each input must not exceed 2048 tokens in length.
23
+ * <p>
24
+ * Unless your are embedding code, we suggest replacing newlines (\n) in your input with a single space,
25
+ * as we have observed inferior results when newlines are present.
26
+ */
27
+ @ NonNull
28
+ List <String > input ;
29
+
30
+ /**
31
+ * A unique identifier representing your end-user, which will help OpenAI to monitor and detect abuse.
32
+ */
33
+ String user ;
34
+ }
Original file line number Diff line number Diff line change
1
+ package com .theokanning .openai .embedding ;
2
+
3
+ import lombok .Data ;
4
+
5
+ import java .util .List ;
6
+
7
+ /**
8
+ * An object containing a response from the answer api
9
+ *
10
+ * https://beta.openai.com/docs/api-reference/embeddings/create
11
+ */
12
+ @ Data
13
+ public class EmbeddingResult {
14
+
15
+ /**
16
+ * The GPT-3 model used for generating embeddings
17
+ */
18
+ String model ;
19
+
20
+ /**
21
+ * The type of object returned, should be "list"
22
+ */
23
+ String object ;
24
+
25
+ /**
26
+ * A list of the calculated embeddings
27
+ */
28
+ List <Embedding > data ;
29
+ }
Original file line number Diff line number Diff line change 6
6
import com .theokanning .openai .classification .ClassificationResult ;
7
7
import com .theokanning .openai .completion .CompletionRequest ;
8
8
import com .theokanning .openai .completion .CompletionResult ;
9
+ import com .theokanning .openai .embedding .EmbeddingRequest ;
10
+ import com .theokanning .openai .embedding .EmbeddingResult ;
9
11
import com .theokanning .openai .engine .Engine ;
10
12
import com .theokanning .openai .file .File ;
11
13
import com .theokanning .openai .finetune .FineTuneEvent ;
@@ -72,4 +74,8 @@ public interface OpenAiApi {
72
74
@ DELETE ("/v1/models/{fine_tune_id}" )
73
75
Single <DeleteResult > deleteFineTune (@ Path ("fine_tune_id" ) String fineTuneId );
74
76
77
+ @ POST ("/v1/engines/{engine_id}/embeddings" )
78
+ Single <EmbeddingResult > createEmbeddings (@ Path ("engine_id" ) String engineId , @ Body EmbeddingRequest request );
79
+
80
+
75
81
}
Original file line number Diff line number Diff line change 10
10
import com .theokanning .openai .classification .ClassificationResult ;
11
11
import com .theokanning .openai .completion .CompletionRequest ;
12
12
import com .theokanning .openai .completion .CompletionResult ;
13
+ import com .theokanning .openai .embedding .EmbeddingRequest ;
14
+ import com .theokanning .openai .embedding .EmbeddingResult ;
13
15
import com .theokanning .openai .engine .Engine ;
14
16
import com .theokanning .openai .file .File ;
15
17
import com .theokanning .openai .finetune .FineTuneEvent ;
@@ -122,4 +124,8 @@ public List<FineTuneEvent> listFineTuneEvents(String fineTuneId) {
122
124
public DeleteResult deleteFineTune (String fineTuneId ) {
123
125
return api .deleteFineTune (fineTuneId ).blockingGet ();
124
126
}
127
+
128
+ public EmbeddingResult createEmbeddings (String engineId , EmbeddingRequest request ) {
129
+ return api .createEmbeddings (engineId , request ).blockingGet ();
130
+ }
125
131
}
Original file line number Diff line number Diff line change
1
+ package com .theokanning .openai ;
2
+
3
+ import com .theokanning .openai .embedding .Embedding ;
4
+ import com .theokanning .openai .embedding .EmbeddingRequest ;
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import java .util .Collections ;
8
+ import java .util .List ;
9
+
10
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
11
+
12
+
13
+ public class EmbeddingTest {
14
+
15
+ String token = System .getenv ("OPENAI_TOKEN" );
16
+ OpenAiService service = new OpenAiService (token );
17
+
18
+ @ Test
19
+ void createEmbeddings () {
20
+ EmbeddingRequest embeddingRequest = EmbeddingRequest .builder ()
21
+ .input (Collections .singletonList ("The food was delicious and the waiter..." ))
22
+ .build ();
23
+
24
+ List <Embedding > embeddings = service .createEmbeddings ("text-similarity-babbage-001" , embeddingRequest ).getData ();
25
+
26
+ assertFalse (embeddings .isEmpty ());
27
+ assertFalse (embeddings .get (0 ).getEmbedding ().isEmpty ());
28
+ }
29
+ }
You can’t perform that action at this time.
0 commit comments