|
1 | 1 | import { Model } from "../.."; |
2 | 2 |
|
3 | | -// Reference: https://platform.openai.com/docs/api-reference/embeddings |
4 | | - |
| 3 | +/** |
| 4 | + * Provides input and output types that conform to the OpenAI Embeddings API. |
| 5 | + * |
| 6 | + * Reference: https://platform.openai.com/docs/api-reference/embeddings |
| 7 | + */ |
5 | 8 | export class EmbeddingsModel extends Model<EmbeddingsInput, EmbeddingsOutput> { |
| 9 | + /** |
| 10 | + * Creates an input object for the OpenAI Embeddings API. |
| 11 | + * |
| 12 | + * @param content The input content to vectorize. Can be any of: |
| 13 | + * - A string representing the text to vectorize. |
| 14 | + * - An array of strings representing multiple texts to vectorize. |
| 15 | + * - An array of integers representing pre-tokenized text to vectorize. |
| 16 | + * - An array of arrays of integers representing multiple pre-tokenized texts to vectorize. |
| 17 | + * |
| 18 | + * @returns An input object that can be passed to the `invoke` method. |
| 19 | + * |
| 20 | + * @remarks |
| 21 | + * The input content must not exceed the maximum token limit of the model. |
| 22 | + */ |
6 | 23 | createInput<T>(content: T): EmbeddingsInput { |
7 | 24 | const model = this.info.fullName; |
8 | 25 |
|
@@ -32,64 +49,143 @@ export class EmbeddingsModel extends Model<EmbeddingsInput, EmbeddingsOutput> { |
32 | 49 | } |
33 | 50 | } |
34 | 51 |
|
35 | | - |
| 52 | +/** |
| 53 | + * The input object for the OpenAI Embeddings API. |
| 54 | + */ |
36 | 55 | @json |
37 | 56 | class EmbeddingsInput { |
| 57 | + /** |
| 58 | + * The name of the model to use for the embeddings. |
| 59 | + * Must be the exact string expected by the model provider. |
| 60 | + * For example, "text-embedding-3-small". |
| 61 | + * |
| 62 | + * @remarks |
| 63 | + * This field is automatically set by the `createInput` method when creating this object. |
| 64 | + * It does not need to be set manually. |
| 65 | + */ |
38 | 66 | model!: string; |
39 | 67 |
|
40 | | - |
41 | | - @omitif("this.encodingFormat.type == 'float'") |
42 | | - encodingFormat: EncodingFormat = EncodingFormat.Float; |
43 | | - |
44 | | - |
| 68 | + /** |
| 69 | + * The encoding format for the output embeddings. |
| 70 | + * |
| 71 | + * @default EncodingFormat.Float |
| 72 | + * |
| 73 | + * @remarks |
| 74 | + * Currently only `EncodingFormat.Float` is supported. |
| 75 | + */ |
| 76 | + @alias("encoding_format") |
| 77 | + @omitif("this.encodingFormat == 'float'") |
| 78 | + encodingFormat: string = EncodingFormat.Float; |
| 79 | + |
| 80 | + /** |
| 81 | + * The maximum number of dimensions for the output embeddings. |
| 82 | + * If not specified, the model's default number of dimensions will be used. |
| 83 | + */ |
45 | 84 | @omitif("this.dimensions == -1") |
46 | 85 | dimensions: i32 = -1; // TODO: make this an `i32 | null` when supported |
47 | 86 |
|
48 | | - |
| 87 | + /** |
| 88 | + * The user ID to associate with the request. |
| 89 | + * If not specified, the request will be anonymous. |
| 90 | + * See https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids |
| 91 | + */ |
49 | 92 | @omitnull() |
50 | 93 | user: string | null = null; |
51 | 94 | } |
52 | 95 |
|
53 | | - |
| 96 | +/** |
| 97 | + * The input object for the OpenAI Embeddings API. |
| 98 | + */ |
54 | 99 | @json |
55 | 100 | class TypedEmbeddingsInput<T> extends EmbeddingsInput { |
| 101 | + /** |
| 102 | + * The input content to vectorize. |
| 103 | + */ |
56 | 104 | input!: T; |
57 | 105 | } |
58 | 106 |
|
59 | | - |
| 107 | +/** |
| 108 | + * The output object for the OpenAI Embeddings API. |
| 109 | + */ |
60 | 110 | @json |
61 | 111 | class EmbeddingsOutput { |
| 112 | + /** |
| 113 | + * The name of the output object type returned by the API. |
| 114 | + * Always `"list"`. |
| 115 | + */ |
62 | 116 | object!: string; |
| 117 | + |
| 118 | + /** |
| 119 | + * The name of the model used to generate the embeddings. |
| 120 | + * In most cases, this will match the requested `model` field in the input. |
| 121 | + */ |
63 | 122 | model!: string; |
| 123 | + |
| 124 | + /** |
| 125 | + * The usage statistics for the request. |
| 126 | + */ |
64 | 127 | usage!: Usage; |
| 128 | + |
| 129 | + /** |
| 130 | + * The output vector embeddings data. |
| 131 | + */ |
65 | 132 | data!: Embedding[]; |
66 | 133 | } |
67 | 134 |
|
68 | | - |
69 | | -@json |
70 | | -export class EncodingFormat { |
71 | | - type: string = "float"; |
72 | | - |
73 | | - static Float: EncodingFormat = { type: "float" }; |
74 | | - static Base64: EncodingFormat = { type: "base64" }; |
| 135 | +/** |
| 136 | + * The encoding format for the output embeddings. |
| 137 | + */ |
| 138 | +// eslint-disable-next-line @typescript-eslint/no-namespace |
| 139 | +export namespace EncodingFormat { |
| 140 | + /** |
| 141 | + * The output embeddings are encoded as an array of floating-point numbers. |
| 142 | + */ |
| 143 | + export const Float = "float"; |
| 144 | + |
| 145 | + /** |
| 146 | + * The output embeddings are encoded as a base64-encoded string, |
| 147 | + * containing an binary representation of an array of floating-point numbers. |
| 148 | + * |
| 149 | + * @remarks |
| 150 | + * This format is currently not supported through this interface. |
| 151 | + */ |
| 152 | + export const Base64 = "base64"; |
75 | 153 | } |
| 154 | +export type EncodingFormat = string; |
76 | 155 |
|
77 | | - |
| 156 | +/** |
| 157 | + * The output vector embeddings data. |
| 158 | + */ |
78 | 159 | @json |
79 | 160 | class Embedding { |
| 161 | + /** |
| 162 | + * The name of the output object type returned by the API. |
| 163 | + * Always `"embedding"`. |
| 164 | + */ |
80 | 165 | object!: string; |
| 166 | + |
| 167 | + /** |
| 168 | + * The index of the input text that corresponds to this embedding. |
| 169 | + * Used when requesting embeddings for multiple texts. |
| 170 | + */ |
81 | 171 | index!: i32; |
82 | | - embedding!: f64[]; |
| 172 | + embedding!: f32[]; // TODO: support `f32[] | string` based on input encoding format |
83 | 173 | } |
84 | 174 |
|
85 | | - |
| 175 | +/** |
| 176 | + * The usage statistics for the request. |
| 177 | + */ |
86 | 178 | @json |
87 | 179 | class Usage { |
88 | | - |
| 180 | + /** |
| 181 | + * The number of prompt tokens used in the request. |
| 182 | + */ |
89 | 183 | @alias("prompt_tokens") |
90 | 184 | promptTokens!: i32; |
91 | 185 |
|
92 | | - |
| 186 | + /** |
| 187 | + * The total number of tokens used in the request. |
| 188 | + */ |
93 | 189 | @alias("total_tokens") |
94 | 190 | totalTokens!: i32; |
95 | 191 | } |
0 commit comments