From 4744648cdf02828b9182ebd34ba3d7db5313786e Mon Sep 17 00:00:00 2001 From: David Miguel Lozano Date: Wed, 14 Feb 2024 21:31:50 +0100 Subject: [PATCH] feat: Allow to specify OpenAI custom instance (#327) --- .../lib/src/chat_models/chat_openai.dart | 1 + .../lib/src/chat_models/models/models.dart | 5 +++ .../create_chat_completion_request.dart | 11 ++++- .../src/generated/schema/schema.freezed.dart | 40 +++++++++++++++++-- .../lib/src/generated/schema/schema.g.dart | 3 ++ packages/openai_dart/oas/openapi_curated.yaml | 8 +++- .../openai_dart/oas/openapi_official.yaml | 19 ++++++++- 7 files changed, 78 insertions(+), 9 deletions(-) diff --git a/packages/langchain_openai/lib/src/chat_models/chat_openai.dart b/packages/langchain_openai/lib/src/chat_models/chat_openai.dart index fa1f52d6..a97dba79 100644 --- a/packages/langchain_openai/lib/src/chat_models/chat_openai.dart +++ b/packages/langchain_openai/lib/src/chat_models/chat_openai.dart @@ -301,6 +301,7 @@ class ChatOpenAI extends BaseChatModel { functionCall: functionCall, frequencyPenalty: options?.frequencyPenalty ?? defaultOptions.frequencyPenalty, + instanceId: options?.instanceId ?? defaultOptions.instanceId, logitBias: options?.logitBias ?? defaultOptions.logitBias, maxTokens: options?.maxTokens ?? defaultOptions.maxTokens, n: options?.n ?? defaultOptions.n, diff --git a/packages/langchain_openai/lib/src/chat_models/models/models.dart b/packages/langchain_openai/lib/src/chat_models/models/models.dart index 57407c4c..33a67427 100644 --- a/packages/langchain_openai/lib/src/chat_models/models/models.dart +++ b/packages/langchain_openai/lib/src/chat_models/models/models.dart @@ -8,6 +8,7 @@ class ChatOpenAIOptions extends ChatModelOptions { const ChatOpenAIOptions({ this.model = 'gpt-3.5-turbo', this.frequencyPenalty, + this.instanceId, this.logitBias, this.maxTokens, this.n, @@ -53,6 +54,10 @@ class ChatOpenAIOptions extends ChatModelOptions { /// See https://platform.openai.com/docs/api-reference/chat/create#chat-create-frequency_penalty final double? frequencyPenalty; + /// An unique identifier to a custom instance to execute the request. + /// The requesting organization is required to have access to the instance. + final String? instanceId; + /// Modify the likelihood of specified tokens appearing in the completion. /// /// See https://platform.openai.com/docs/api-reference/chat/create#chat-create-logit_bias diff --git a/packages/openai_dart/lib/src/generated/schema/create_chat_completion_request.dart b/packages/openai_dart/lib/src/generated/schema/create_chat_completion_request.dart index d67d859a..d8fc2ba2 100644 --- a/packages/openai_dart/lib/src/generated/schema/create_chat_completion_request.dart +++ b/packages/openai_dart/lib/src/generated/schema/create_chat_completion_request.dart @@ -28,6 +28,9 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest { @Default(0.0) double? frequencyPenalty, + /// An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. + @JsonKey(name: 'instance_id', includeIfNull: false) String? instanceId, + /// Modify the likelihood of specified tokens appearing in the completion. /// /// Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. @@ -55,7 +58,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest { @Default(0.0) double? presencePenalty, - /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. /// /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. /// @@ -129,6 +132,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest { 'model', 'messages', 'frequency_penalty', + 'instance_id', 'logit_bias', 'logprobs', 'top_logprobs', @@ -216,6 +220,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest { 'model': model, 'messages': messages, 'frequency_penalty': frequencyPenalty, + 'instance_id': instanceId, 'logit_bias': logitBias, 'logprobs': logprobs, 'top_logprobs': topLogprobs, @@ -273,6 +278,8 @@ enum ChatCompletionModels { gpt35Turbo0613, @JsonValue('gpt-3.5-turbo-1106') gpt35Turbo1106, + @JsonValue('gpt-3.5-turbo-0125') + gpt35Turbo0125, @JsonValue('gpt-3.5-turbo-16k-0613') gpt35Turbo16k0613, } @@ -337,7 +344,7 @@ class _ChatCompletionModelConverter // CLASS: ChatCompletionResponseFormat // ========================================== -/// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. +/// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. /// /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. /// diff --git a/packages/openai_dart/lib/src/generated/schema/schema.freezed.dart b/packages/openai_dart/lib/src/generated/schema/schema.freezed.dart index ed7de3ef..29ce7421 100644 --- a/packages/openai_dart/lib/src/generated/schema/schema.freezed.dart +++ b/packages/openai_dart/lib/src/generated/schema/schema.freezed.dart @@ -3356,6 +3356,10 @@ mixin _$CreateChatCompletionRequest { @JsonKey(name: 'frequency_penalty', includeIfNull: false) double? get frequencyPenalty => throw _privateConstructorUsedError; + /// An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. + @JsonKey(name: 'instance_id', includeIfNull: false) + String? get instanceId => throw _privateConstructorUsedError; + /// Modify the likelihood of specified tokens appearing in the completion. /// /// Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. @@ -3386,7 +3390,7 @@ mixin _$CreateChatCompletionRequest { @JsonKey(name: 'presence_penalty', includeIfNull: false) double? get presencePenalty => throw _privateConstructorUsedError; - /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. /// /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. /// @@ -3479,6 +3483,7 @@ abstract class $CreateChatCompletionRequestCopyWith<$Res> { List messages, @JsonKey(name: 'frequency_penalty', includeIfNull: false) double? frequencyPenalty, + @JsonKey(name: 'instance_id', includeIfNull: false) String? instanceId, @JsonKey(name: 'logit_bias', includeIfNull: false) Map? logitBias, @JsonKey(includeIfNull: false) bool? logprobs, @@ -3530,6 +3535,7 @@ class _$CreateChatCompletionRequestCopyWithImpl<$Res, Object? model = null, Object? messages = null, Object? frequencyPenalty = freezed, + Object? instanceId = freezed, Object? logitBias = freezed, Object? logprobs = freezed, Object? topLogprobs = freezed, @@ -3561,6 +3567,10 @@ class _$CreateChatCompletionRequestCopyWithImpl<$Res, ? _value.frequencyPenalty : frequencyPenalty // ignore: cast_nullable_to_non_nullable as double?, + instanceId: freezed == instanceId + ? _value.instanceId + : instanceId // ignore: cast_nullable_to_non_nullable + as String?, logitBias: freezed == logitBias ? _value.logitBias : logitBias // ignore: cast_nullable_to_non_nullable @@ -3706,6 +3716,7 @@ abstract class _$$CreateChatCompletionRequestImplCopyWith<$Res> List messages, @JsonKey(name: 'frequency_penalty', includeIfNull: false) double? frequencyPenalty, + @JsonKey(name: 'instance_id', includeIfNull: false) String? instanceId, @JsonKey(name: 'logit_bias', includeIfNull: false) Map? logitBias, @JsonKey(includeIfNull: false) bool? logprobs, @@ -3761,6 +3772,7 @@ class __$$CreateChatCompletionRequestImplCopyWithImpl<$Res> Object? model = null, Object? messages = null, Object? frequencyPenalty = freezed, + Object? instanceId = freezed, Object? logitBias = freezed, Object? logprobs = freezed, Object? topLogprobs = freezed, @@ -3792,6 +3804,10 @@ class __$$CreateChatCompletionRequestImplCopyWithImpl<$Res> ? _value.frequencyPenalty : frequencyPenalty // ignore: cast_nullable_to_non_nullable as double?, + instanceId: freezed == instanceId + ? _value.instanceId + : instanceId // ignore: cast_nullable_to_non_nullable + as String?, logitBias: freezed == logitBias ? _value._logitBias : logitBias // ignore: cast_nullable_to_non_nullable @@ -3872,6 +3888,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { required final List messages, @JsonKey(name: 'frequency_penalty', includeIfNull: false) this.frequencyPenalty = 0.0, + @JsonKey(name: 'instance_id', includeIfNull: false) this.instanceId, @JsonKey(name: 'logit_bias', includeIfNull: false) final Map? logitBias, @JsonKey(includeIfNull: false) this.logprobs, @@ -3929,6 +3946,11 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { @JsonKey(name: 'frequency_penalty', includeIfNull: false) final double? frequencyPenalty; + /// An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. + @override + @JsonKey(name: 'instance_id', includeIfNull: false) + final String? instanceId; + /// Modify the likelihood of specified tokens appearing in the completion. /// /// Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. @@ -3976,7 +3998,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { @JsonKey(name: 'presence_penalty', includeIfNull: false) final double? presencePenalty; - /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. /// /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. /// @@ -4080,7 +4102,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { @override String toString() { - return 'CreateChatCompletionRequest(model: $model, messages: $messages, frequencyPenalty: $frequencyPenalty, logitBias: $logitBias, logprobs: $logprobs, topLogprobs: $topLogprobs, maxTokens: $maxTokens, n: $n, presencePenalty: $presencePenalty, responseFormat: $responseFormat, seed: $seed, stop: $stop, stream: $stream, temperature: $temperature, topP: $topP, tools: $tools, toolChoice: $toolChoice, user: $user, functionCall: $functionCall, functions: $functions)'; + return 'CreateChatCompletionRequest(model: $model, messages: $messages, frequencyPenalty: $frequencyPenalty, instanceId: $instanceId, logitBias: $logitBias, logprobs: $logprobs, topLogprobs: $topLogprobs, maxTokens: $maxTokens, n: $n, presencePenalty: $presencePenalty, responseFormat: $responseFormat, seed: $seed, stop: $stop, stream: $stream, temperature: $temperature, topP: $topP, tools: $tools, toolChoice: $toolChoice, user: $user, functionCall: $functionCall, functions: $functions)'; } @override @@ -4092,6 +4114,8 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { const DeepCollectionEquality().equals(other._messages, _messages) && (identical(other.frequencyPenalty, frequencyPenalty) || other.frequencyPenalty == frequencyPenalty) && + (identical(other.instanceId, instanceId) || + other.instanceId == instanceId) && const DeepCollectionEquality() .equals(other._logitBias, _logitBias) && (identical(other.logprobs, logprobs) || @@ -4128,6 +4152,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest { model, const DeepCollectionEquality().hash(_messages), frequencyPenalty, + instanceId, const DeepCollectionEquality().hash(_logitBias), logprobs, topLogprobs, @@ -4170,6 +4195,8 @@ abstract class _CreateChatCompletionRequest required final List messages, @JsonKey(name: 'frequency_penalty', includeIfNull: false) final double? frequencyPenalty, + @JsonKey(name: 'instance_id', includeIfNull: false) + final String? instanceId, @JsonKey(name: 'logit_bias', includeIfNull: false) final Map? logitBias, @JsonKey(includeIfNull: false) final bool? logprobs, @@ -4222,6 +4249,11 @@ abstract class _CreateChatCompletionRequest double? get frequencyPenalty; @override + /// An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. + @JsonKey(name: 'instance_id', includeIfNull: false) + String? get instanceId; + @override + /// Modify the likelihood of specified tokens appearing in the completion. /// /// Accepts a JSON object that maps tokens (specified by their token ID in the tokenizer) to an associated bias value from -100 to 100. Mathematically, the bias is added to the logits generated by the model prior to sampling. The exact effect will vary per model, but values between -1 and 1 should decrease or increase likelihood of selection; values like -100 or 100 should result in a ban or exclusive selection of the relevant token. @@ -4258,7 +4290,7 @@ abstract class _CreateChatCompletionRequest double? get presencePenalty; @override - /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + /// An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. /// /// Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. /// diff --git a/packages/openai_dart/lib/src/generated/schema/schema.g.dart b/packages/openai_dart/lib/src/generated/schema/schema.g.dart index efd7272b..90f10a1d 100644 --- a/packages/openai_dart/lib/src/generated/schema/schema.g.dart +++ b/packages/openai_dart/lib/src/generated/schema/schema.g.dart @@ -304,6 +304,7 @@ _$CreateChatCompletionRequestImpl _$$CreateChatCompletionRequestImplFromJson( .map((e) => ChatCompletionMessage.fromJson(e as Map)) .toList(), frequencyPenalty: (json['frequency_penalty'] as num?)?.toDouble() ?? 0.0, + instanceId: json['instance_id'] as String?, logitBias: (json['logit_bias'] as Map?)?.map( (k, e) => MapEntry(k, e as int), ), @@ -348,6 +349,7 @@ Map _$$CreateChatCompletionRequestImplToJson( } writeNotNull('frequency_penalty', instance.frequencyPenalty); + writeNotNull('instance_id', instance.instanceId); writeNotNull('logit_bias', instance.logitBias); writeNotNull('logprobs', instance.logprobs); writeNotNull('top_logprobs', instance.topLogprobs); @@ -406,6 +408,7 @@ const _$ChatCompletionModelsEnumMap = { ChatCompletionModels.gpt35Turbo0301: 'gpt-3.5-turbo-0301', ChatCompletionModels.gpt35Turbo0613: 'gpt-3.5-turbo-0613', ChatCompletionModels.gpt35Turbo1106: 'gpt-3.5-turbo-1106', + ChatCompletionModels.gpt35Turbo0125: 'gpt-3.5-turbo-0125', ChatCompletionModels.gpt35Turbo16k0613: 'gpt-3.5-turbo-16k-0613', }; diff --git a/packages/openai_dart/oas/openapi_curated.yaml b/packages/openai_dart/oas/openapi_curated.yaml index 0baea463..185c46fe 100644 --- a/packages/openai_dart/oas/openapi_curated.yaml +++ b/packages/openai_dart/oas/openapi_curated.yaml @@ -1481,6 +1481,7 @@ components: "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", "gpt-3.5-turbo-16k-0613", ] messages: @@ -1496,6 +1497,11 @@ components: maximum: 2 nullable: true description: *completions_frequency_penalty_description + instance_id: + type: string + default: null + nullable: true + description: An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. logit_bias: type: object default: null @@ -1542,7 +1548,7 @@ components: title: ChatCompletionResponseFormat type: object description: | - An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](https://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. diff --git a/packages/openai_dart/oas/openapi_official.yaml b/packages/openai_dart/oas/openapi_official.yaml index 61dbc555..4e74cf94 100644 --- a/packages/openai_dart/oas/openapi_official.yaml +++ b/packages/openai_dart/oas/openapi_official.yaml @@ -5806,6 +5806,7 @@ components: "gpt-3.5-turbo-0301", "gpt-3.5-turbo-0613", "gpt-3.5-turbo-1106", + "gpt-3.5-turbo-0125", "gpt-3.5-turbo-16k-0613", ] x-oaiTypeLabel: string @@ -5816,6 +5817,11 @@ components: maximum: 2 nullable: true description: *completions_frequency_penalty_description + instance_id: + type: string + default: null + nullable: true + description: An unique identifier to a custom instance to execute the request. The requesting organization is required to have access to the instance. logit_bias: type: object x-oaiTypeLabel: map @@ -5863,7 +5869,7 @@ components: response_format: type: object description: | - An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](/docs/models/gpt-4-and-gpt-4-turbo) and `gpt-3.5-turbo-1106`. + An object specifying the format that the model must output. Compatible with [GPT-4 Turbo](/docs/models/gpt-4-and-gpt-4-turbo) and all GPT-3.5 Turbo models newer than `gpt-3.5-turbo-1106`. Setting to `{ "type": "json_object" }` enables JSON mode, which guarantees the message the model generates is valid JSON. @@ -6839,6 +6845,16 @@ components: The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use [log probability](https://en.wikipedia.org/wiki/Log_probability) to automatically increase the temperature until certain thresholds are hit. type: number default: 0 + timestamp_granularities[]: + description: | + The timestamp granularities to populate for this transcription. Any of these options: `word`, or `segment`. Note: There is no additional latency for segment timestamps, but generating word timestamps incurs additional latency. + type: array + items: + type: string + enum: + - word + - segment + default: [segment] required: - file - model @@ -8388,7 +8404,6 @@ components: description: | An array of tool calls the run step was involved in. These can be associated with one of three types of tools: `code_interpreter`, `retrieval`, or `function`. items: - type: object oneOf: - $ref: "#/components/schemas/RunStepDetailsToolCallsCodeObject" - $ref: "#/components/schemas/RunStepDetailsToolCallsRetrievalObject"