Skip to content

Commit

Permalink
feat: Allow to specify OpenAI custom instance (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmigloz committed Feb 14, 2024
1 parent 2cbd538 commit 4744648
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@ class ChatOpenAI extends BaseChatModel<ChatOpenAIOptions> {
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -129,6 +132,7 @@ class CreateChatCompletionRequest with _$CreateChatCompletionRequest {
'model',
'messages',
'frequency_penalty',
'instance_id',
'logit_bias',
'logprobs',
'top_logprobs',
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
}
Expand Down Expand Up @@ -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.
///
Expand Down
40 changes: 36 additions & 4 deletions packages/openai_dart/lib/src/generated/schema/schema.freezed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -3479,6 +3483,7 @@ abstract class $CreateChatCompletionRequestCopyWith<$Res> {
List<ChatCompletionMessage> messages,
@JsonKey(name: 'frequency_penalty', includeIfNull: false)
double? frequencyPenalty,
@JsonKey(name: 'instance_id', includeIfNull: false) String? instanceId,
@JsonKey(name: 'logit_bias', includeIfNull: false)
Map<String, int>? logitBias,
@JsonKey(includeIfNull: false) bool? logprobs,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3706,6 +3716,7 @@ abstract class _$$CreateChatCompletionRequestImplCopyWith<$Res>
List<ChatCompletionMessage> messages,
@JsonKey(name: 'frequency_penalty', includeIfNull: false)
double? frequencyPenalty,
@JsonKey(name: 'instance_id', includeIfNull: false) String? instanceId,
@JsonKey(name: 'logit_bias', includeIfNull: false)
Map<String, int>? logitBias,
@JsonKey(includeIfNull: false) bool? logprobs,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -3872,6 +3888,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest {
required final List<ChatCompletionMessage> 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<String, int>? logitBias,
@JsonKey(includeIfNull: false) this.logprobs,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down Expand Up @@ -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
Expand All @@ -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) ||
Expand Down Expand Up @@ -4128,6 +4152,7 @@ class _$CreateChatCompletionRequestImpl extends _CreateChatCompletionRequest {
model,
const DeepCollectionEquality().hash(_messages),
frequencyPenalty,
instanceId,
const DeepCollectionEquality().hash(_logitBias),
logprobs,
topLogprobs,
Expand Down Expand Up @@ -4170,6 +4195,8 @@ abstract class _CreateChatCompletionRequest
required final List<ChatCompletionMessage> 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<String, int>? logitBias,
@JsonKey(includeIfNull: false) final bool? logprobs,
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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.
///
Expand Down
3 changes: 3 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.

8 changes: 7 additions & 1 deletion packages/openai_dart/oas/openapi_curated.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down
19 changes: 17 additions & 2 deletions packages/openai_dart/oas/openapi_official.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 4744648

Please sign in to comment.