Skip to content
This repository was archived by the owner on Oct 8, 2024. It is now read-only.

Commit f401317

Browse files
More updates for OpenAI Embeddings model (#7)
* Fix encoding type * Use an f32[] for output embeddings * Add JS docs * Update CHANGELOG.md
1 parent 607fb4b commit f401317

File tree

2 files changed

+121
-24
lines changed

2 files changed

+121
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
## UNRELEASED
44

5-
- Update OpenAI Embeddings model to support all allowed types of input
5+
- Update OpenAI Embeddings model to support all allowed types of input [#6](https://github.com/hypermodeAI/models-as/pull/6)
6+
- More updates for OpenAI Embeddings model [#7](https://github.com/hypermodeAI/models-as/pull/7)
67

78
## 2024-06-28 - Version 0.1.6
89

src/models/openai/embeddings.ts

Lines changed: 119 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,25 @@
11
import { Model } from "../..";
22

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+
*/
58
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+
*/
623
createInput<T>(content: T): EmbeddingsInput {
724
const model = this.info.fullName;
825

@@ -32,64 +49,143 @@ export class EmbeddingsModel extends Model<EmbeddingsInput, EmbeddingsOutput> {
3249
}
3350
}
3451

35-
52+
/**
53+
* The input object for the OpenAI Embeddings API.
54+
*/
3655
@json
3756
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+
*/
3866
model!: string;
3967

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+
*/
4584
@omitif("this.dimensions == -1")
4685
dimensions: i32 = -1; // TODO: make this an `i32 | null` when supported
4786

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+
*/
4992
@omitnull()
5093
user: string | null = null;
5194
}
5295

53-
96+
/**
97+
* The input object for the OpenAI Embeddings API.
98+
*/
5499
@json
55100
class TypedEmbeddingsInput<T> extends EmbeddingsInput {
101+
/**
102+
* The input content to vectorize.
103+
*/
56104
input!: T;
57105
}
58106

59-
107+
/**
108+
* The output object for the OpenAI Embeddings API.
109+
*/
60110
@json
61111
class EmbeddingsOutput {
112+
/**
113+
* The name of the output object type returned by the API.
114+
* Always `"list"`.
115+
*/
62116
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+
*/
63122
model!: string;
123+
124+
/**
125+
* The usage statistics for the request.
126+
*/
64127
usage!: Usage;
128+
129+
/**
130+
* The output vector embeddings data.
131+
*/
65132
data!: Embedding[];
66133
}
67134

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";
75153
}
154+
export type EncodingFormat = string;
76155

77-
156+
/**
157+
* The output vector embeddings data.
158+
*/
78159
@json
79160
class Embedding {
161+
/**
162+
* The name of the output object type returned by the API.
163+
* Always `"embedding"`.
164+
*/
80165
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+
*/
81171
index!: i32;
82-
embedding!: f64[];
172+
embedding!: f32[]; // TODO: support `f32[] | string` based on input encoding format
83173
}
84174

85-
175+
/**
176+
* The usage statistics for the request.
177+
*/
86178
@json
87179
class Usage {
88-
180+
/**
181+
* The number of prompt tokens used in the request.
182+
*/
89183
@alias("prompt_tokens")
90184
promptTokens!: i32;
91185

92-
186+
/**
187+
* The total number of tokens used in the request.
188+
*/
93189
@alias("total_tokens")
94190
totalTokens!: i32;
95191
}

0 commit comments

Comments
 (0)