Skip to content

Commit

Permalink
feat: Add support for shortening embeddings (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Jan 26, 2024
1 parent fda1602 commit c725db0
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ class CreateEmbeddingRequest with _$CreateEmbeddingRequest {
@Default(EmbeddingEncodingFormat.float)
EmbeddingEncodingFormat encodingFormat,

/// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
@JsonKey(includeIfNull: false) int? dimensions,

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
@JsonKey(includeIfNull: false) String? user,
}) = _CreateEmbeddingRequest;
Expand All @@ -39,11 +42,18 @@ class CreateEmbeddingRequest with _$CreateEmbeddingRequest {
'model',
'input',
'encoding_format',
'dimensions',
'user'
];

/// Validation constants
static const dimensionsMinValue = 1;

/// Perform validations on the schema property values
String? validateSchema() {
if (dimensions != null && dimensions! < dimensionsMinValue) {
return "The value of 'dimensions' cannot be < $dimensionsMinValue";
}
return null;
}

Expand All @@ -53,6 +63,7 @@ class CreateEmbeddingRequest with _$CreateEmbeddingRequest {
'model': model,
'input': input,
'encoding_format': encodingFormat,
'dimensions': dimensions,
'user': user,
};
}
Expand Down
34 changes: 32 additions & 2 deletions packages/openai_dart/lib/src/generated/schema/schema.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10560,6 +10560,10 @@ mixin _$CreateEmbeddingRequest {
EmbeddingEncodingFormat get encodingFormat =>
throw _privateConstructorUsedError;

/// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
@JsonKey(includeIfNull: false)
int? get dimensions => throw _privateConstructorUsedError;

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
@JsonKey(includeIfNull: false)
String? get user => throw _privateConstructorUsedError;
Expand All @@ -10580,6 +10584,7 @@ abstract class $CreateEmbeddingRequestCopyWith<$Res> {
{@_EmbeddingModelConverter() EmbeddingModel model,
@_EmbeddingInputConverter() EmbeddingInput input,
@JsonKey(name: 'encoding_format') EmbeddingEncodingFormat encodingFormat,
@JsonKey(includeIfNull: false) int? dimensions,
@JsonKey(includeIfNull: false) String? user});

$EmbeddingModelCopyWith<$Res> get model;
Expand All @@ -10603,6 +10608,7 @@ class _$CreateEmbeddingRequestCopyWithImpl<$Res,
Object? model = null,
Object? input = null,
Object? encodingFormat = null,
Object? dimensions = freezed,
Object? user = freezed,
}) {
return _then(_value.copyWith(
Expand All @@ -10618,6 +10624,10 @@ class _$CreateEmbeddingRequestCopyWithImpl<$Res,
? _value.encodingFormat
: encodingFormat // ignore: cast_nullable_to_non_nullable
as EmbeddingEncodingFormat,
dimensions: freezed == dimensions
? _value.dimensions
: dimensions // ignore: cast_nullable_to_non_nullable
as int?,
user: freezed == user
? _value.user
: user // ignore: cast_nullable_to_non_nullable
Expand Down Expand Up @@ -10655,6 +10665,7 @@ abstract class _$$CreateEmbeddingRequestImplCopyWith<$Res>
{@_EmbeddingModelConverter() EmbeddingModel model,
@_EmbeddingInputConverter() EmbeddingInput input,
@JsonKey(name: 'encoding_format') EmbeddingEncodingFormat encodingFormat,
@JsonKey(includeIfNull: false) int? dimensions,
@JsonKey(includeIfNull: false) String? user});

@override
Expand All @@ -10679,6 +10690,7 @@ class __$$CreateEmbeddingRequestImplCopyWithImpl<$Res>
Object? model = null,
Object? input = null,
Object? encodingFormat = null,
Object? dimensions = freezed,
Object? user = freezed,
}) {
return _then(_$CreateEmbeddingRequestImpl(
Expand All @@ -10694,6 +10706,10 @@ class __$$CreateEmbeddingRequestImplCopyWithImpl<$Res>
? _value.encodingFormat
: encodingFormat // ignore: cast_nullable_to_non_nullable
as EmbeddingEncodingFormat,
dimensions: freezed == dimensions
? _value.dimensions
: dimensions // ignore: cast_nullable_to_non_nullable
as int?,
user: freezed == user
? _value.user
: user // ignore: cast_nullable_to_non_nullable
Expand All @@ -10710,6 +10726,7 @@ class _$CreateEmbeddingRequestImpl extends _CreateEmbeddingRequest {
@_EmbeddingInputConverter() required this.input,
@JsonKey(name: 'encoding_format')
this.encodingFormat = EmbeddingEncodingFormat.float,
@JsonKey(includeIfNull: false) this.dimensions,
@JsonKey(includeIfNull: false) this.user})
: super._();

Expand All @@ -10731,14 +10748,19 @@ class _$CreateEmbeddingRequestImpl extends _CreateEmbeddingRequest {
@JsonKey(name: 'encoding_format')
final EmbeddingEncodingFormat encodingFormat;

/// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
@override
@JsonKey(includeIfNull: false)
final int? dimensions;

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
@override
@JsonKey(includeIfNull: false)
final String? user;

@override
String toString() {
return 'CreateEmbeddingRequest(model: $model, input: $input, encodingFormat: $encodingFormat, user: $user)';
return 'CreateEmbeddingRequest(model: $model, input: $input, encodingFormat: $encodingFormat, dimensions: $dimensions, user: $user)';
}

@override
Expand All @@ -10750,13 +10772,15 @@ class _$CreateEmbeddingRequestImpl extends _CreateEmbeddingRequest {
(identical(other.input, input) || other.input == input) &&
(identical(other.encodingFormat, encodingFormat) ||
other.encodingFormat == encodingFormat) &&
(identical(other.dimensions, dimensions) ||
other.dimensions == dimensions) &&
(identical(other.user, user) || other.user == user));
}

@JsonKey(ignore: true)
@override
int get hashCode =>
Object.hash(runtimeType, model, input, encodingFormat, user);
Object.hash(runtimeType, model, input, encodingFormat, dimensions, user);

@JsonKey(ignore: true)
@override
Expand All @@ -10779,6 +10803,7 @@ abstract class _CreateEmbeddingRequest extends CreateEmbeddingRequest {
@_EmbeddingInputConverter() required final EmbeddingInput input,
@JsonKey(name: 'encoding_format')
final EmbeddingEncodingFormat encodingFormat,
@JsonKey(includeIfNull: false) final int? dimensions,
@JsonKey(includeIfNull: false) final String? user}) =
_$CreateEmbeddingRequestImpl;
const _CreateEmbeddingRequest._() : super._();
Expand All @@ -10803,6 +10828,11 @@ abstract class _CreateEmbeddingRequest extends CreateEmbeddingRequest {
EmbeddingEncodingFormat get encodingFormat;
@override

/// The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
@JsonKey(includeIfNull: false)
int? get dimensions;
@override

/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://platform.openai.com/docs/guides/safety-best-practices/end-user-ids).
@JsonKey(includeIfNull: false)
String? get user;
Expand Down
2 changes: 2 additions & 0 deletions packages/openai_dart/lib/src/generated/schema/schema.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions packages/openai_dart/oas/openapi_curated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2205,6 +2205,11 @@ components:
default: "float"
type: string
enum: [ "float", "base64" ]
dimensions:
description: |
The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
type: integer
minimum: 1
user: *end_user_param_configuration
required:
- model
Expand Down
5 changes: 5 additions & 0 deletions packages/openai_dart/oas/openapi_official.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6752,6 +6752,11 @@ components:
default: "float"
type: string
enum: ["float", "base64"]
dimensions:
description: |
The number of dimensions the resulting output embeddings should have. Only supported in `text-embedding-3` and later models.
type: integer
minimum: 1
user: *end_user_param_configuration
required:
- model
Expand Down
14 changes: 14 additions & 0 deletions packages/openai_dart/test/openai_client_embeddings_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,19 @@ void main() {
expect(res.data.first.index, 0);
expect(res.data.first.embeddingVectorBase64, isNotEmpty);
});

test('Test shortening embeddings', () async {
const request = CreateEmbeddingRequest(
model: EmbeddingModel.model(EmbeddingModels.textEmbedding3Large),
input: EmbeddingInput.string(
'The food was delicious and the waiter...',
),
dimensions: 256,
);
final res = await client.createEmbedding(request: request);
expect(res.data, hasLength(1));
expect(res.data.first.index, 0);
expect(res.data.first.embeddingVector, hasLength(256));
});
});
}

0 comments on commit c725db0

Please sign in to comment.