diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ChatCompletionStreamOptionsVariant1OptionSet.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ChatCompletionStreamOptionsVariant1OptionSet.g.cs new file mode 100644 index 000000000..74ef8ebd4 --- /dev/null +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ChatCompletionStreamOptionsVariant1OptionSet.g.cs @@ -0,0 +1,36 @@ +#nullable enable + +using System.CommandLine; + +namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands; + +internal sealed record ChatCompletionStreamOptionsVariant1OptionSet( + Option IncludeUsage, + Option IncludeObfuscation) +{ + public static ChatCompletionStreamOptionsVariant1OptionSet Create(string? prefix = null) + { + var normalizedPrefix = string.IsNullOrWhiteSpace(prefix) + ? string.Empty + : prefix.Trim().Trim('-') + "-"; + return new ChatCompletionStreamOptionsVariant1OptionSet( + IncludeUsage: CliRuntime.CreateNullableBoolOption(name: $"--{normalizedPrefix}include-usage", description: @"If set, an additional chunk will be streamed before the `data: [DONE]` +message. The `usage` field on this chunk shows the token usage statistics +for the entire request, and the `choices` field will always be an empty +array. + +All other chunks will also include a `usage` field, but with a null +value. **NOTE:** If the stream is interrupted, you may not receive the +final usage chunk which contains the total token usage for the request. +"), + IncludeObfuscation: CliRuntime.CreateNullableBoolOption(name: $"--{normalizedPrefix}include-obfuscation", description: @"When true, stream obfuscation will be enabled. Stream obfuscation adds +random characters to an `obfuscation` field on streaming delta events to +normalize payload sizes as a mitigation to certain side-channel attacks. +These obfuscation fields are included by default, but add a small amount +of overhead to the data stream. You can set `include_obfuscation` to +false to optimize for bandwidth if you trust the network links between +your application and the OpenAI API. +") + ); + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsApiGroupCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsApiGroupCommand.g.cs index 84b4c9722..8f8c0a46a 100644 --- a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsApiGroupCommand.g.cs +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsApiGroupCommand.g.cs @@ -12,6 +12,7 @@ public static Command Create() { var command = new Command(@"completions", @"Completions endpoint commands."); command.Subcommands.Add(CompletionsCreateCompletionCommandApiCommand.Create()); + command.Subcommands.Add(CompletionsCreateCompletionAsStreamCommandApiCommand.Create()); CustomizeCommand(ref command); return command; } diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionAsStreamCommandApiCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionAsStreamCommandApiCommand.g.cs new file mode 100644 index 000000000..665a8a6be --- /dev/null +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionAsStreamCommandApiCommand.g.cs @@ -0,0 +1,180 @@ +#nullable enable +#pragma warning disable CS0618 + +using System.CommandLine; + +namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands; + +internal static partial class CompletionsCreateCompletionAsStreamCommandApiCommand +{ + private static Option> Model { get; } = new( + name: @"--model") + { + Description = @"ID of the model to use. You can use the [List models](https://developers.openai.com/api/reference/resources/models/methods/list) API to see all of your available models, or see our [Model overview](https://developers.openai.com/api/docs/models) for descriptions of them. +", + Required = true, + }; + + private static Option, global::System.Collections.Generic.IList, global::System.Collections.Generic.IList>>?> Prompt { get; } = new( + name: @"--prompt") + { + Description = @"The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. + +Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document. +", + }; + + private static Option?> LogitBias { get; } = new( + name: @"--logit-bias") + { + Description = @"Modify the likelihood of specified tokens appearing in the completion. + +Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](https://platform.openai.com/tokenizer?view=bpe) to convert text to token IDs. 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. + +As an example, you can pass `{""50256"": -100}` to prevent the <|endoftext|> token from being generated. +", + }; + + private static Option Stop { get; } = new( + name: @"--stop") + { + Description = @"Not supported with latest reasoning models `o3` and `o4-mini`. + +Up to 4 sequences where the API will stop generating further tokens. The +returned text will not contain the stop sequence. +", + }; + private static readonly CreateCompletionRequestOptionSet CreateCompletionRequestOptionSetOptions = CreateCompletionRequestOptionSet.Create(); + + private static readonly ChatCompletionStreamOptionsVariant1OptionSet StreamOptionsOptions = ChatCompletionStreamOptionsVariant1OptionSet.Create(@"stream"); + private static Option Input { get; } = new(@"--input") + { + Description = "Load request JSON from a file path, '-' for stdin, or an inline JSON object/array string.", + }; + + private static Option RequestJson { get; } = new(@"--request-json") + { + Description = "Request body as JSON.", + Hidden = true, + }; + + private static Option RequestFile { get; } = new(@"--request-file") + { + Description = "Path to a JSON request file, or '-' for stdin.", + Hidden = true, + }; + + static partial void CustomizeCommand(ref Command command); + + public static Command Create(string? commandName = null) + { + var command = new Command(commandName ?? @"create-completion-as-stream", @"Create completion +Creates a completion for the provided prompt and parameters. + +Returns a completion object, or a sequence of completion objects if the request is streamed. +"); + command.Options.Add(Model); + command.Options.Add(Prompt); + command.Options.Add(LogitBias); + command.Options.Add(Stop); command.Options.Add(CreateCompletionRequestOptionSetOptions.BestOf); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Echo); + command.Options.Add(CreateCompletionRequestOptionSetOptions.FrequencyPenalty); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Logprobs); + command.Options.Add(CreateCompletionRequestOptionSetOptions.MaxTokens); + command.Options.Add(CreateCompletionRequestOptionSetOptions.N); + command.Options.Add(CreateCompletionRequestOptionSetOptions.PresencePenalty); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Seed); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Suffix); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Temperature); + command.Options.Add(CreateCompletionRequestOptionSetOptions.TopP); + command.Options.Add(CreateCompletionRequestOptionSetOptions.User); command.Options.Add(StreamOptionsOptions.IncludeUsage); + command.Options.Add(StreamOptionsOptions.IncludeObfuscation); + command.Options.Add(Input); + command.Options.Add(RequestJson); + command.Options.Add(RequestFile); + command.Validators.Add(result => + { + var hasInput = result.GetResult(Input) is not null; + var hasRequestJson = result.GetResult(RequestJson) is not null; + var hasRequestFile = result.GetResult(RequestFile) is not null; + var specifiedCount = (hasInput ? 1 : 0) + (hasRequestJson ? 1 : 0) + (hasRequestFile ? 1 : 0); + if (specifiedCount > 1) + { + result.AddError(@"Specify at most one of --input, --request-json, or --request-file."); + } + }); + + command.SetAction(async (ParseResult parseResult, CancellationToken cancellationToken) => + await CliRuntime.RunAsync(async () => + { + var __requestBase = await CliRuntime.ReadRequestOrDefaultAsync( + parseResult, + Input, + RequestJson, + RequestFile, + global::tryAGI.OpenAI.SourceGenerationContext.Default, + cancellationToken).ConfigureAwait(false); + var model = parseResult.GetRequiredValue(Model); + var prompt = CliRuntime.WasSpecified(parseResult, Prompt) ? parseResult.GetValue(Prompt) : (__requestBase is { } __PromptBaseValue ? __PromptBaseValue.Prompt : default); + var logitBias = CliRuntime.WasSpecified(parseResult, LogitBias) ? parseResult.GetValue(LogitBias) : (__requestBase is { } __LogitBiasBaseValue ? __LogitBiasBaseValue.LogitBias : default); + var stop = CliRuntime.WasSpecified(parseResult, Stop) ? parseResult.GetValue(Stop) : (__requestBase is { } __StopBaseValue ? __StopBaseValue.Stop : default); var bestOf = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.BestOf) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.BestOf) : (__requestBase is { } __BestOfBaseValue ? __BestOfBaseValue.BestOf : default); + var echo = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Echo) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Echo) : (__requestBase is { } __EchoBaseValue ? __EchoBaseValue.Echo : default); + var frequencyPenalty = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.FrequencyPenalty) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.FrequencyPenalty) : (__requestBase is { } __FrequencyPenaltyBaseValue ? __FrequencyPenaltyBaseValue.FrequencyPenalty : default); + var logprobs = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Logprobs) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Logprobs) : (__requestBase is { } __LogprobsBaseValue ? __LogprobsBaseValue.Logprobs : default); + var maxTokens = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.MaxTokens) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.MaxTokens) : (__requestBase is { } __MaxTokensBaseValue ? __MaxTokensBaseValue.MaxTokens : default); + var n = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.N) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.N) : (__requestBase is { } __NBaseValue ? __NBaseValue.N : default); + var presencePenalty = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.PresencePenalty) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.PresencePenalty) : (__requestBase is { } __PresencePenaltyBaseValue ? __PresencePenaltyBaseValue.PresencePenalty : default); + var seed = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Seed) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Seed) : (__requestBase is { } __SeedBaseValue ? __SeedBaseValue.Seed : default); + var suffix = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Suffix) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Suffix) : (__requestBase is { } __SuffixBaseValue ? __SuffixBaseValue.Suffix : default); + var temperature = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Temperature) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Temperature) : (__requestBase is { } __TemperatureBaseValue ? __TemperatureBaseValue.Temperature : default); + var topP = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.TopP) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.TopP) : (__requestBase is { } __TopPBaseValue ? __TopPBaseValue.TopP : default); + var user = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.User) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.User) : (__requestBase is { } __UserBaseValue ? __UserBaseValue.User : default); + + var __StreamOptionsBase = __requestBase is { } __StreamOptionsBaseValue ? __StreamOptionsBaseValue.StreamOptions : default; var streamOptionsIncludeUsage = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeUsage) ? parseResult.GetValue(StreamOptionsOptions.IncludeUsage) : (__StreamOptionsBase is { } __StreamOptionsincludeUsageBaseValue ? __StreamOptionsincludeUsageBaseValue.IncludeUsage : default); + var streamOptionsIncludeObfuscation = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeObfuscation) ? parseResult.GetValue(StreamOptionsOptions.IncludeObfuscation) : (__StreamOptionsBase is { } __StreamOptionsincludeObfuscationBaseValue ? __StreamOptionsincludeObfuscationBaseValue.IncludeObfuscation : default); + var __StreamOptionsSpecified = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeUsage) || CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeObfuscation); + var streamOptions = + __StreamOptionsSpecified || __StreamOptionsBase is not null + ? new global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1 + { + IncludeUsage = streamOptionsIncludeUsage, + IncludeObfuscation = streamOptionsIncludeObfuscation, + + } + : __StreamOptionsBase; + using var client = await CliRuntime.CreateClientAsync(parseResult, cancellationToken).ConfigureAwait(false); + + + var response = client.Completions.CreateCompletionAsStreamAsync( + model: model, + prompt: prompt, + logitBias: logitBias, + stop: stop, + bestOf: bestOf, + echo: echo, + frequencyPenalty: frequencyPenalty, + logprobs: logprobs, + maxTokens: maxTokens, + n: n, + presencePenalty: presencePenalty, + seed: seed, + suffix: suffix, + temperature: temperature, + topP: topP, + user: user, + streamOptions: streamOptions, + cancellationToken: cancellationToken); + + await foreach (var item in response.WithCancellation(cancellationToken).ConfigureAwait(false)) + { + await CliRuntime.WriteResponseLineAsync( + parseResult, + item, + global::tryAGI.OpenAI.SourceGenerationContext.Default, + cancellationToken: cancellationToken).ConfigureAwait(false); + } + }, cancellationToken).ConfigureAwait(false)); + CustomizeCommand(ref command); + return command; + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionCommandApiCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionCommandApiCommand.g.cs index 4bb1b7463..05ea24b1b 100644 --- a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionCommandApiCommand.g.cs +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CompletionsCreateCompletionCommandApiCommand.g.cs @@ -24,31 +24,6 @@ internal static partial class CompletionsCreateCompletionCommandApiCommand ", }; - private static Option BestOf { get; } = new( - name: @"--best-of") - { - Description = @"Generates `best_of` completions server-side and returns the ""best"" (the one with the highest log probability per token). Results cannot be streamed. - -When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. - -**Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. -", - }; - - private static Option Echo { get; } = CliRuntime.CreateNullableBoolOption( - name: @"--echo", - description: @"Echo back the prompt in addition to the completion -"); - - private static Option FrequencyPenalty { get; } = new( - name: @"--frequency-penalty") - { - Description = @"Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. - -[See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text) -", - }; - private static Option?> LogitBias { get; } = new( name: @"--logit-bias") { @@ -60,51 +35,6 @@ [See more information about frequency and presence penalties.](https://developer ", }; - private static Option Logprobs { get; } = new( - name: @"--logprobs") - { - Description = @"Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. - -The maximum value for `logprobs` is 5. -", - }; - - private static Option MaxTokens { get; } = new( - name: @"--max-tokens") - { - Description = @"The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion. - -The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. -", - }; - - private static Option N { get; } = new( - name: @"--n") - { - Description = @"How many completions to generate for each prompt. - -**Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. -", - }; - - private static Option PresencePenalty { get; } = new( - name: @"--presence-penalty") - { - Description = @"Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. - -[See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text) -", - }; - - private static Option Seed { get; } = new( - name: @"--seed") - { - Description = @"If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. - -Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. -", - }; - private static Option Stop { get; } = new( name: @"--stop") { @@ -114,51 +44,9 @@ Up to 4 sequences where the API will stop generating further tokens. The returned text will not contain the stop sequence. ", }; + private static readonly CreateCompletionRequestOptionSet CreateCompletionRequestOptionSetOptions = CreateCompletionRequestOptionSet.Create(); - private static Option Stream { get; } = CliRuntime.CreateNullableBoolOption( - name: @"--stream", - description: @"Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). -"); - - private static Option StreamOptions { get; } = new( - name: @"--stream-options") - { - Description = @"", - }; - - private static Option Suffix { get; } = new( - name: @"--suffix") - { - Description = @"The suffix that comes after a completion of inserted text. - -This parameter is only supported for `gpt-3.5-turbo-instruct`. -", - }; - - private static Option Temperature { get; } = new( - name: @"--temperature") - { - Description = @"What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. - -We generally recommend altering this or `top_p` but not both. -", - }; - - private static Option TopP { get; } = new( - name: @"--top-p") - { - Description = @"An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. - -We generally recommend altering this or `temperature` but not both. -", - }; - - private static Option User { get; } = new( - name: @"--user") - { - Description = @"A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://developers.openai.com/api/docs/guides/safety-best-practices#implement-safety-identifiers). -", - }; + private static readonly ChatCompletionStreamOptionsVariant1OptionSet StreamOptionsOptions = ChatCompletionStreamOptionsVariant1OptionSet.Create(@"stream"); private static Option Input { get; } = new(@"--input") { Description = "Load request JSON from a file path, '-' for stdin, or an inline JSON object/array string.", @@ -207,22 +95,20 @@ public static Command Create(string? commandName = null) "); command.Options.Add(Model); command.Options.Add(Prompt); - command.Options.Add(BestOf); - command.Options.Add(Echo); - command.Options.Add(FrequencyPenalty); command.Options.Add(LogitBias); - command.Options.Add(Logprobs); - command.Options.Add(MaxTokens); - command.Options.Add(N); - command.Options.Add(PresencePenalty); - command.Options.Add(Seed); - command.Options.Add(Stop); - command.Options.Add(Stream); - command.Options.Add(StreamOptions); - command.Options.Add(Suffix); - command.Options.Add(Temperature); - command.Options.Add(TopP); - command.Options.Add(User); + command.Options.Add(Stop); command.Options.Add(CreateCompletionRequestOptionSetOptions.BestOf); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Echo); + command.Options.Add(CreateCompletionRequestOptionSetOptions.FrequencyPenalty); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Logprobs); + command.Options.Add(CreateCompletionRequestOptionSetOptions.MaxTokens); + command.Options.Add(CreateCompletionRequestOptionSetOptions.N); + command.Options.Add(CreateCompletionRequestOptionSetOptions.PresencePenalty); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Seed); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Suffix); + command.Options.Add(CreateCompletionRequestOptionSetOptions.Temperature); + command.Options.Add(CreateCompletionRequestOptionSetOptions.TopP); + command.Options.Add(CreateCompletionRequestOptionSetOptions.User); command.Options.Add(StreamOptionsOptions.IncludeUsage); + command.Options.Add(StreamOptionsOptions.IncludeObfuscation); command.Options.Add(Input); command.Options.Add(RequestJson); command.Options.Add(RequestFile); @@ -250,44 +136,53 @@ await CliRuntime.RunAsync(async () => cancellationToken).ConfigureAwait(false); var model = parseResult.GetRequiredValue(Model); var prompt = CliRuntime.WasSpecified(parseResult, Prompt) ? parseResult.GetValue(Prompt) : (__requestBase is { } __PromptBaseValue ? __PromptBaseValue.Prompt : default); - var bestOf = CliRuntime.WasSpecified(parseResult, BestOf) ? parseResult.GetValue(BestOf) : (__requestBase is { } __BestOfBaseValue ? __BestOfBaseValue.BestOf : default); - var echo = CliRuntime.WasSpecified(parseResult, Echo) ? parseResult.GetValue(Echo) : (__requestBase is { } __EchoBaseValue ? __EchoBaseValue.Echo : default); - var frequencyPenalty = CliRuntime.WasSpecified(parseResult, FrequencyPenalty) ? parseResult.GetValue(FrequencyPenalty) : (__requestBase is { } __FrequencyPenaltyBaseValue ? __FrequencyPenaltyBaseValue.FrequencyPenalty : default); var logitBias = CliRuntime.WasSpecified(parseResult, LogitBias) ? parseResult.GetValue(LogitBias) : (__requestBase is { } __LogitBiasBaseValue ? __LogitBiasBaseValue.LogitBias : default); - var logprobs = CliRuntime.WasSpecified(parseResult, Logprobs) ? parseResult.GetValue(Logprobs) : (__requestBase is { } __LogprobsBaseValue ? __LogprobsBaseValue.Logprobs : default); - var maxTokens = CliRuntime.WasSpecified(parseResult, MaxTokens) ? parseResult.GetValue(MaxTokens) : (__requestBase is { } __MaxTokensBaseValue ? __MaxTokensBaseValue.MaxTokens : default); - var n = CliRuntime.WasSpecified(parseResult, N) ? parseResult.GetValue(N) : (__requestBase is { } __NBaseValue ? __NBaseValue.N : default); - var presencePenalty = CliRuntime.WasSpecified(parseResult, PresencePenalty) ? parseResult.GetValue(PresencePenalty) : (__requestBase is { } __PresencePenaltyBaseValue ? __PresencePenaltyBaseValue.PresencePenalty : default); - var seed = CliRuntime.WasSpecified(parseResult, Seed) ? parseResult.GetValue(Seed) : (__requestBase is { } __SeedBaseValue ? __SeedBaseValue.Seed : default); - var stop = CliRuntime.WasSpecified(parseResult, Stop) ? parseResult.GetValue(Stop) : (__requestBase is { } __StopBaseValue ? __StopBaseValue.Stop : default); - var stream = CliRuntime.WasSpecified(parseResult, Stream) ? parseResult.GetValue(Stream) : (__requestBase is { } __StreamBaseValue ? __StreamBaseValue.Stream : default); - var streamOptions = CliRuntime.WasSpecified(parseResult, StreamOptions) ? parseResult.GetValue(StreamOptions) : (__requestBase is { } __StreamOptionsBaseValue ? __StreamOptionsBaseValue.StreamOptions : default); - var suffix = CliRuntime.WasSpecified(parseResult, Suffix) ? parseResult.GetValue(Suffix) : (__requestBase is { } __SuffixBaseValue ? __SuffixBaseValue.Suffix : default); - var temperature = CliRuntime.WasSpecified(parseResult, Temperature) ? parseResult.GetValue(Temperature) : (__requestBase is { } __TemperatureBaseValue ? __TemperatureBaseValue.Temperature : default); - var topP = CliRuntime.WasSpecified(parseResult, TopP) ? parseResult.GetValue(TopP) : (__requestBase is { } __TopPBaseValue ? __TopPBaseValue.TopP : default); - var user = CliRuntime.WasSpecified(parseResult, User) ? parseResult.GetValue(User) : (__requestBase is { } __UserBaseValue ? __UserBaseValue.User : default); + var stop = CliRuntime.WasSpecified(parseResult, Stop) ? parseResult.GetValue(Stop) : (__requestBase is { } __StopBaseValue ? __StopBaseValue.Stop : default); var bestOf = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.BestOf) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.BestOf) : (__requestBase is { } __BestOfBaseValue ? __BestOfBaseValue.BestOf : default); + var echo = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Echo) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Echo) : (__requestBase is { } __EchoBaseValue ? __EchoBaseValue.Echo : default); + var frequencyPenalty = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.FrequencyPenalty) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.FrequencyPenalty) : (__requestBase is { } __FrequencyPenaltyBaseValue ? __FrequencyPenaltyBaseValue.FrequencyPenalty : default); + var logprobs = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Logprobs) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Logprobs) : (__requestBase is { } __LogprobsBaseValue ? __LogprobsBaseValue.Logprobs : default); + var maxTokens = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.MaxTokens) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.MaxTokens) : (__requestBase is { } __MaxTokensBaseValue ? __MaxTokensBaseValue.MaxTokens : default); + var n = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.N) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.N) : (__requestBase is { } __NBaseValue ? __NBaseValue.N : default); + var presencePenalty = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.PresencePenalty) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.PresencePenalty) : (__requestBase is { } __PresencePenaltyBaseValue ? __PresencePenaltyBaseValue.PresencePenalty : default); + var seed = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Seed) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Seed) : (__requestBase is { } __SeedBaseValue ? __SeedBaseValue.Seed : default); + var suffix = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Suffix) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Suffix) : (__requestBase is { } __SuffixBaseValue ? __SuffixBaseValue.Suffix : default); + var temperature = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.Temperature) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.Temperature) : (__requestBase is { } __TemperatureBaseValue ? __TemperatureBaseValue.Temperature : default); + var topP = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.TopP) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.TopP) : (__requestBase is { } __TopPBaseValue ? __TopPBaseValue.TopP : default); + var user = CliRuntime.WasSpecified(parseResult, CreateCompletionRequestOptionSetOptions.User) ? parseResult.GetValue(CreateCompletionRequestOptionSetOptions.User) : (__requestBase is { } __UserBaseValue ? __UserBaseValue.User : default); + + var __StreamOptionsBase = __requestBase is { } __StreamOptionsBaseValue ? __StreamOptionsBaseValue.StreamOptions : default; var streamOptionsIncludeUsage = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeUsage) ? parseResult.GetValue(StreamOptionsOptions.IncludeUsage) : (__StreamOptionsBase is { } __StreamOptionsincludeUsageBaseValue ? __StreamOptionsincludeUsageBaseValue.IncludeUsage : default); + var streamOptionsIncludeObfuscation = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeObfuscation) ? parseResult.GetValue(StreamOptionsOptions.IncludeObfuscation) : (__StreamOptionsBase is { } __StreamOptionsincludeObfuscationBaseValue ? __StreamOptionsincludeObfuscationBaseValue.IncludeObfuscation : default); + var __StreamOptionsSpecified = CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeUsage) || CliRuntime.WasSpecified(parseResult, StreamOptionsOptions.IncludeObfuscation); + var streamOptions = + __StreamOptionsSpecified || __StreamOptionsBase is not null + ? new global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1 + { + IncludeUsage = streamOptionsIncludeUsage, + IncludeObfuscation = streamOptionsIncludeObfuscation, + + } + : __StreamOptionsBase; using var client = await CliRuntime.CreateClientAsync(parseResult, cancellationToken).ConfigureAwait(false); var response = await client.Completions.CreateCompletionAsync( model: model, prompt: prompt, + logitBias: logitBias, + stop: stop, bestOf: bestOf, echo: echo, frequencyPenalty: frequencyPenalty, - logitBias: logitBias, logprobs: logprobs, maxTokens: maxTokens, n: n, presencePenalty: presencePenalty, seed: seed, - stop: stop, - stream: stream, - streamOptions: streamOptions, suffix: suffix, temperature: temperature, topP: topP, user: user, + streamOptions: streamOptions, cancellationToken: cancellationToken).ConfigureAwait(false); diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CreateCompletionRequestOptionSet.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CreateCompletionRequestOptionSet.g.cs new file mode 100644 index 000000000..d945af9dc --- /dev/null +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/CreateCompletionRequestOptionSet.g.cs @@ -0,0 +1,111 @@ +#nullable enable + +using System.CommandLine; + +namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands; + +internal sealed record CreateCompletionRequestOptionSet( + Option BestOf, + Option Echo, + Option FrequencyPenalty, + Option Logprobs, + Option MaxTokens, + Option N, + Option PresencePenalty, + Option Seed, + Option Stream, + Option Suffix, + Option Temperature, + Option TopP, + Option User) +{ + public static CreateCompletionRequestOptionSet Create(string? prefix = null) + { + var normalizedPrefix = string.IsNullOrWhiteSpace(prefix) + ? string.Empty + : prefix.Trim().Trim('-') + "-"; + return new CreateCompletionRequestOptionSet( + BestOf: new Option($"--{normalizedPrefix}best-of") + { + Description = @"Generates `best_of` completions server-side and returns the ""best"" (the one with the highest log probability per token). Results cannot be streamed. + +When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`. + +**Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. +", + }, + Echo: CliRuntime.CreateNullableBoolOption(name: $"--{normalizedPrefix}echo", description: @"Echo back the prompt in addition to the completion +"), + FrequencyPenalty: new Option($"--{normalizedPrefix}frequency-penalty") + { + Description = @"Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. + +[See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text) +", + }, + Logprobs: new Option($"--{normalizedPrefix}logprobs") + { + Description = @"Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response. + +The maximum value for `logprobs` is 5. +", + }, + MaxTokens: new Option($"--{normalizedPrefix}max-tokens") + { + Description = @"The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion. + +The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens. +", + }, + N: new Option($"--{normalizedPrefix}n") + { + Description = @"How many completions to generate for each prompt. + +**Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`. +", + }, + PresencePenalty: new Option($"--{normalizedPrefix}presence-penalty") + { + Description = @"Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. + +[See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text) +", + }, + Seed: new Option($"--{normalizedPrefix}seed") + { + Description = @"If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result. + +Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. +", + }, + Stream: CliRuntime.CreateNullableBoolOption(name: $"--{normalizedPrefix}stream", description: @"Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions). +"), + Suffix: new Option($"--{normalizedPrefix}suffix") + { + Description = @"The suffix that comes after a completion of inserted text. + +This parameter is only supported for `gpt-3.5-turbo-instruct`. +", + }, + Temperature: new Option($"--{normalizedPrefix}temperature") + { + Description = @"What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. + +We generally recommend altering this or `top_p` but not both. +", + }, + TopP: new Option($"--{normalizedPrefix}top-p") + { + Description = @"An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. + +We generally recommend altering this or `temperature` but not both. +", + }, + User: new Option($"--{normalizedPrefix}user") + { + Description = @"A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://developers.openai.com/api/docs/guides/safety-best-practices#implement-safety-identifiers). +", + } + ); + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesApiGroupCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesApiGroupCommand.g.cs index 5e2e28f28..b901f01d3 100644 --- a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesApiGroupCommand.g.cs +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesApiGroupCommand.g.cs @@ -17,6 +17,7 @@ public static Command Create() command.Subcommands.Add(ResponsesBetaCreateResponseAsStreamCommandApiCommand.Create()); command.Subcommands.Add(ResponsesBetaDeleteResponseCommandApiCommand.Create()); command.Subcommands.Add(ResponsesBetaGetResponseCommandApiCommand.Create()); + command.Subcommands.Add(ResponsesBetaGetResponseAsStreamCommandApiCommand.Create()); command.Subcommands.Add(ResponsesBetaGetinputtokencountsCommandApiCommand.Create()); command.Subcommands.Add(ResponsesBetaListInputItemsCommandApiCommand.Create()); command.Subcommands.Add(ResponsesCancelResponseCommandApiCommand.Create()); @@ -25,6 +26,7 @@ public static Command Create() command.Subcommands.Add(ResponsesCreateResponseAsStreamCommandApiCommand.Create()); command.Subcommands.Add(ResponsesDeleteResponseCommandApiCommand.Create()); command.Subcommands.Add(ResponsesGetResponseCommandApiCommand.Create()); + command.Subcommands.Add(ResponsesGetResponseAsStreamCommandApiCommand.Create()); command.Subcommands.Add(ResponsesGetinputtokencountsCommandApiCommand.Create()); command.Subcommands.Add(ResponsesListInputItemsCommandApiCommand.Create()); CustomizeCommand(ref command); diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesBetaGetResponseAsStreamCommandApiCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesBetaGetResponseAsStreamCommandApiCommand.g.cs new file mode 100644 index 000000000..3c06dcf5c --- /dev/null +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesBetaGetResponseAsStreamCommandApiCommand.g.cs @@ -0,0 +1,104 @@ +#nullable enable +#pragma warning disable CS0618 + +using System.CommandLine; + +namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands; + +internal static partial class ResponsesBetaGetResponseAsStreamCommandApiCommand +{ + private static Argument ResponseId { get; } = new( + name: @"response-id") + { + Description = @"The ID of the response to retrieve.", + }; + + private static Option?> Include { get; } = new( + name: @"--include") + { + Description = @"Additional fields to include in the response. See the `include` +parameter for Response creation above for more information. +", + }; + + private static Option Stream { get; } = CliRuntime.CreateNullableBoolOption( + name: @"--stream", + description: @"If set to true, the model response data will be streamed to the client +as it is generated using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format). +See the [Streaming section below](https://developers.openai.com/api/reference/resources/responses/streaming-events) +for more information. +"); + + private static Option StartingAfter { get; } = new( + name: @"--starting-after") + { + Description = @"The sequence number of the event after which to start streaming. +", + }; + + private static Option IncludeObfuscation { get; } = CliRuntime.CreateNullableBoolOption( + name: @"--include-obfuscation", + description: @"When true, stream obfuscation will be enabled. Stream obfuscation adds +random characters to an `obfuscation` field on streaming delta events +to normalize payload sizes as a mitigation to certain side-channel +attacks. These obfuscation fields are included by default, but add a +small amount of overhead to the data stream. You can set +`include_obfuscation` to false to optimize for bandwidth if you trust +the network links between your application and the OpenAI API. +"); + + private static Option?> OpenaiBeta { get; } = new( + name: @"--openai-beta") + { + Description = @"Optional beta features to enable for this request.", + }; + + static partial void CustomizeCommand(ref Command command); + + public static Command Create(string? commandName = null) + { + var command = new Command(commandName ?? @"beta-get-response-as-stream", @"Get a model response +Retrieves a model response with the given ID. +"); + command.Arguments.Add(ResponseId); + command.Options.Add(Include); + command.Options.Add(Stream); + command.Options.Add(StartingAfter); + command.Options.Add(IncludeObfuscation); + command.Options.Add(OpenaiBeta); + + + command.SetAction(async (ParseResult parseResult, CancellationToken cancellationToken) => + await CliRuntime.RunAsync(async () => + { + var responseId = parseResult.GetRequiredValue(ResponseId); + var include = parseResult.GetValue(Include); + var stream = parseResult.GetValue(Stream); + var startingAfter = parseResult.GetValue(StartingAfter); + var includeObfuscation = parseResult.GetValue(IncludeObfuscation); + var openaiBeta = parseResult.GetValue(OpenaiBeta); + using var client = await CliRuntime.CreateClientAsync(parseResult, cancellationToken).ConfigureAwait(false); + + + var response = client.Responses.BetaGetResponseAsStreamAsync( + responseId: responseId, + include: include, + stream: stream, + startingAfter: startingAfter, + includeObfuscation: includeObfuscation, + openaiBeta: openaiBeta, + cancellationToken: cancellationToken); + + await foreach (var item in response.WithCancellation(cancellationToken).ConfigureAwait(false)) + { + await CliRuntime.WriteResponseLineAsync( + parseResult, + item, + global::tryAGI.OpenAI.SourceGenerationContext.Default, + cancellationToken: cancellationToken).ConfigureAwait(false); + } + }, cancellationToken).ConfigureAwait(false)); + CustomizeCommand(ref command); + return command; + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesGetResponseAsStreamCommandApiCommand.g.cs b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesGetResponseAsStreamCommandApiCommand.g.cs new file mode 100644 index 000000000..0a0324db6 --- /dev/null +++ b/src/libs/tryAGI.OpenAI.CLI/GeneratedApi/Commands/ResponsesGetResponseAsStreamCommandApiCommand.g.cs @@ -0,0 +1,95 @@ +#nullable enable +#pragma warning disable CS0618 + +using System.CommandLine; + +namespace tryAGI.OpenAI.Cli.GeneratedApi.Commands; + +internal static partial class ResponsesGetResponseAsStreamCommandApiCommand +{ + private static Argument ResponseId { get; } = new( + name: @"response-id") + { + Description = @"The ID of the response to retrieve.", + }; + + private static Option?> Include { get; } = new( + name: @"--include") + { + Description = @"Additional fields to include in the response. See the `include` +parameter for Response creation above for more information. +", + }; + + private static Option Stream { get; } = CliRuntime.CreateNullableBoolOption( + name: @"--stream", + description: @"If set to true, the model response data will be streamed to the client +as it is generated using [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format). +See the [Streaming section below](https://developers.openai.com/api/reference/resources/responses/streaming-events) +for more information. +"); + + private static Option StartingAfter { get; } = new( + name: @"--starting-after") + { + Description = @"The sequence number of the event after which to start streaming. +", + }; + + private static Option IncludeObfuscation { get; } = CliRuntime.CreateNullableBoolOption( + name: @"--include-obfuscation", + description: @"When true, stream obfuscation will be enabled. Stream obfuscation adds +random characters to an `obfuscation` field on streaming delta events +to normalize payload sizes as a mitigation to certain side-channel +attacks. These obfuscation fields are included by default, but add a +small amount of overhead to the data stream. You can set +`include_obfuscation` to false to optimize for bandwidth if you trust +the network links between your application and the OpenAI API. +"); + + static partial void CustomizeCommand(ref Command command); + + public static Command Create(string? commandName = null) + { + var command = new Command(commandName ?? @"get-response-as-stream", @"Get a model response +Retrieves a model response with the given ID. +"); + command.Arguments.Add(ResponseId); + command.Options.Add(Include); + command.Options.Add(Stream); + command.Options.Add(StartingAfter); + command.Options.Add(IncludeObfuscation); + + + command.SetAction(async (ParseResult parseResult, CancellationToken cancellationToken) => + await CliRuntime.RunAsync(async () => + { + var responseId = parseResult.GetRequiredValue(ResponseId); + var include = parseResult.GetValue(Include); + var stream = parseResult.GetValue(Stream); + var startingAfter = parseResult.GetValue(StartingAfter); + var includeObfuscation = parseResult.GetValue(IncludeObfuscation); + using var client = await CliRuntime.CreateClientAsync(parseResult, cancellationToken).ConfigureAwait(false); + + + var response = client.Responses.GetResponseAsStreamAsync( + responseId: responseId, + include: include, + stream: stream, + startingAfter: startingAfter, + includeObfuscation: includeObfuscation, + cancellationToken: cancellationToken); + + await foreach (var item in response.WithCancellation(cancellationToken).ConfigureAwait(false)) + { + await CliRuntime.WriteResponseLineAsync( + parseResult, + item, + global::tryAGI.OpenAI.SourceGenerationContext.Default, + cancellationToken: cancellationToken).ConfigureAwait(false); + } + }, cancellationToken).ConfigureAwait(false)); + CustomizeCommand(ref command); + return command; + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/autosdk.generated-examples.json b/src/libs/tryAGI.OpenAI/Generated/autosdk.generated-examples.json index a27c8a7a5..0c2185efc 100644 --- a/src/libs/tryAGI.OpenAI/Generated/autosdk.generated-examples.json +++ b/src/libs/tryAGI.OpenAI/Generated/autosdk.generated-examples.json @@ -83,7 +83,7 @@ "Slug": "createcompletion", "Description": "Creates a completion for the provided prompt and parameters.\n\nReturns a completion object, or a sequence of completion objects if the request is streamed.", "Language": "http", - "Code": "### Create completion\n# @name createCompletion\nPOST {{host}}/completions\nAuthorization: Bearer {{token}}\nContent-Type: application/json\nAccept: application/json, text/plain\n\n{\n \u0022model\u0022: \u0022string\u0022,\n \u0022prompt\u0022: \u0022\\u003C|endoftext|\\u003E\u0022,\n \u0022best_of\u0022: 1,\n \u0022echo\u0022: false,\n \u0022frequency_penalty\u0022: 0,\n \u0022logit_bias\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022logprobs\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022max_tokens\u0022: 16,\n \u0022n\u0022: 1,\n \u0022presence_penalty\u0022: 0,\n \u0022seed\u0022: 0,\n \u0022stop\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022stream\u0022: false,\n \u0022stream_options\u0022: {\n \u0022include_usage\u0022: true,\n \u0022include_obfuscation\u0022: true\n },\n \u0022suffix\u0022: \u0022test.\u0022,\n \u0022temperature\u0022: 1,\n \u0022top_p\u0022: 1,\n \u0022user\u0022: \u0022user-1234\u0022\n}\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json\n# 400\n# Description: Invalid completion request or incompatible request parameters.\n# Content-Type: application/json\n# 401\n# Description: Authentication failed because the API key is missing or revoked, or the client IP is not authorized.\n# Content-Type: application/json, text/plain\n# 403\n# Description: Access to a personal API organization is blocked by the organization policy.\n# Content-Type: text/plain\n# 404\n# Description: The requested model was not found, is unavailable, or does not support this endpoint.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded. A slow_down error means traffic increased too quickly; reduce your request rate, then increase it gradually.\n# Content-Type: application/json\n# 500\n# Description: The completion could not be processed due to an internal error.\n# Content-Type: application/json\n# 503\n# Description: The service is temporarily unavailable. A server_is_overloaded error means the requested model is temporarily overloaded; retry after a brief delay.\n# Content-Type: application/json", + "Code": "### Create completion\n# @name createCompletion\nPOST {{host}}/completions\nAuthorization: Bearer {{token}}\nContent-Type: application/json\nAccept: application/json, text/event-stream, text/plain\n\n{\n \u0022model\u0022: \u0022string\u0022,\n \u0022prompt\u0022: \u0022\\u003C|endoftext|\\u003E\u0022,\n \u0022best_of\u0022: 1,\n \u0022echo\u0022: false,\n \u0022frequency_penalty\u0022: 0,\n \u0022logit_bias\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022logprobs\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022max_tokens\u0022: 16,\n \u0022n\u0022: 1,\n \u0022presence_penalty\u0022: 0,\n \u0022seed\u0022: 0,\n \u0022stop\u0022: \u0022openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464\u0022,\n \u0022stream\u0022: false,\n \u0022stream_options\u0022: {\n \u0022include_usage\u0022: true,\n \u0022include_obfuscation\u0022: true\n },\n \u0022suffix\u0022: \u0022test.\u0022,\n \u0022temperature\u0022: 1,\n \u0022top_p\u0022: 1,\n \u0022user\u0022: \u0022user-1234\u0022\n}\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json, text/event-stream\n# 400\n# Description: Invalid completion request or incompatible request parameters.\n# Content-Type: application/json\n# 401\n# Description: Authentication failed because the API key is missing or revoked, or the client IP is not authorized.\n# Content-Type: application/json, text/plain\n# 403\n# Description: Access to a personal API organization is blocked by the organization policy.\n# Content-Type: text/plain\n# 404\n# Description: The requested model was not found, is unavailable, or does not support this endpoint.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded. A slow_down error means traffic increased too quickly; reduce your request rate, then increase it gradually.\n# Content-Type: application/json\n# 500\n# Description: The completion could not be processed due to an internal error.\n# Content-Type: application/json\n# 503\n# Description: The service is temporarily unavailable. A server_is_overloaded error means the requested model is temporarily overloaded; retry after a brief delay.\n# Content-Type: application/json", "Format": "http", "OperationId": "createCompletion", "Setup": null @@ -215,7 +215,7 @@ "Slug": "beta-getresponse", "Description": "Retrieves a model response with the given ID.", "Language": "http", - "Code": "### Get a model response\n# @name beta_getResponse\nGET {{host}}/responses/{{response_id}}?beta=true?include={{include}}\u0026stream={{stream}}\u0026starting_after={{starting_after}}\u0026include_obfuscation={{include_obfuscation}}\nAuthorization: Bearer {{token}}\nopenai-beta: {{openai-beta}}\nAccept: application/json\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json\n# 404\n# Description: The response was not found.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded.\n# Content-Type: application/json", + "Code": "### Get a model response\n# @name beta_getResponse\nGET {{host}}/responses/{{response_id}}?beta=true?include={{include}}\u0026stream={{stream}}\u0026starting_after={{starting_after}}\u0026include_obfuscation={{include_obfuscation}}\nAuthorization: Bearer {{token}}\nopenai-beta: {{openai-beta}}\nAccept: application/json, text/event-stream\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json, text/event-stream\n# 404\n# Description: The response was not found.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded.\n# Content-Type: application/json", "Format": "http", "OperationId": "beta_getResponse", "Setup": null @@ -270,7 +270,7 @@ "Slug": "getresponse", "Description": "Retrieves a model response with the given ID.", "Language": "http", - "Code": "### Get a model response\n# @name getResponse\nGET {{host}}/responses/{{response_id}}?include={{include}}\u0026stream={{stream}}\u0026starting_after={{starting_after}}\u0026include_obfuscation={{include_obfuscation}}\nAuthorization: Bearer {{token}}\nAccept: application/json\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json\n# 404\n# Description: The response was not found.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded.\n# Content-Type: application/json", + "Code": "### Get a model response\n# @name getResponse\nGET {{host}}/responses/{{response_id}}?include={{include}}\u0026stream={{stream}}\u0026starting_after={{starting_after}}\u0026include_obfuscation={{include_obfuscation}}\nAuthorization: Bearer {{token}}\nAccept: application/json, text/event-stream\n\n## Responses\n# 200\n# Description: OK\n# Content-Type: application/json, text/event-stream\n# 404\n# Description: The response was not found.\n# Content-Type: application/json\n# 429\n# Description: The request was rejected because a rate limit was exceeded.\n# Content-Type: application/json", "Format": "http", "OperationId": "getResponse", "Setup": null diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletion.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletion.g.cs index 5a6083abe..382c4e52a 100644 --- a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletion.g.cs +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletion.g.cs @@ -82,6 +82,27 @@ partial void ProcessCreateCompletionResponseContent( { request = request ?? throw new global::System.ArgumentNullException(nameof(request)); + request = new global::tryAGI.OpenAI.CreateCompletionRequest + { + Model = request.Model, + Prompt = request.Prompt, + BestOf = request.BestOf, + Echo = request.Echo, + FrequencyPenalty = request.FrequencyPenalty, + LogitBias = request.LogitBias, + Logprobs = request.Logprobs, + MaxTokens = request.MaxTokens, + N = request.N, + PresencePenalty = request.PresencePenalty, + Seed = request.Seed, + Stop = request.Stop, + Stream = false, + StreamOptions = request.StreamOptions, + Suffix = request.Suffix, + Temperature = request.Temperature, + TopP = request.TopP, + User = request.User, + }; PrepareArguments( client: HttpClient); PrepareCreateCompletionArguments( @@ -127,6 +148,10 @@ partial void ProcessCreateCompletionResponseContent( __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || @@ -760,10 +785,6 @@ partial void ProcessCreateCompletionResponseContent( /// returned text will not contain the stop sequence.
/// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 /// - /// - /// Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
- /// Default Value: false - /// /// /// /// The suffix that comes after a completion of inserted text.
@@ -803,7 +824,6 @@ partial void ProcessCreateCompletionResponseContent( double? presencePenalty = default, long? seed = default, global::tryAGI.OpenAI.StopConfiguration? stop = default, - bool? stream = default, global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1? streamOptions = default, string? suffix = default, double? temperature = default, @@ -826,7 +846,7 @@ partial void ProcessCreateCompletionResponseContent( PresencePenalty = presencePenalty, Seed = seed, Stop = stop, - Stream = stream, + Stream = false, StreamOptions = streamOptions, Suffix = suffix, Temperature = temperature, diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletionAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletionAsStream.g.cs new file mode 100644 index 000000000..e1883807f --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.CompletionsClient.CreateCompletionAsStream.g.cs @@ -0,0 +1,551 @@ + +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial class CompletionsClient + { + + + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement s_CreateCompletionAsStreamSecurityRequirement0 = + new global::tryAGI.OpenAI.EndPointSecurityRequirement + { + Authorizations = new global::tryAGI.OpenAI.EndPointAuthorizationRequirement[] + { new global::tryAGI.OpenAI.EndPointAuthorizationRequirement + { + Type = "Http", + SchemeId = "ApiKeyAuth", + Location = "Header", + Name = "Bearer", + FriendlyName = "Bearer", + }, + }, + }; + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement[] s_CreateCompletionAsStreamSecurityRequirements = + new global::tryAGI.OpenAI.EndPointSecurityRequirement[] + { s_CreateCompletionAsStreamSecurityRequirement0, + }; + partial void PrepareCreateCompletionAsStreamArguments( + global::System.Net.Http.HttpClient httpClient, + global::tryAGI.OpenAI.CreateCompletionRequest request); + partial void PrepareCreateCompletionAsStreamRequest( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpRequestMessage httpRequestMessage, + global::tryAGI.OpenAI.CreateCompletionRequest request); + partial void ProcessCreateCompletionAsStreamResponse( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpResponseMessage httpResponseMessage); + + /// + /// Create completion
+ /// Creates a completion for the provided prompt and parameters.
+ /// Returns a completion object, or a sequence of completion objects if the request is streamed. + ///
+ /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + public async global::System.Collections.Generic.IAsyncEnumerable CreateCompletionAsStreamAsync( + + global::tryAGI.OpenAI.CreateCompletionRequest request, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default) + { + request = request ?? throw new global::System.ArgumentNullException(nameof(request)); + + request = new global::tryAGI.OpenAI.CreateCompletionRequest + { + Model = request.Model, + Prompt = request.Prompt, + BestOf = request.BestOf, + Echo = request.Echo, + FrequencyPenalty = request.FrequencyPenalty, + LogitBias = request.LogitBias, + Logprobs = request.Logprobs, + MaxTokens = request.MaxTokens, + N = request.N, + PresencePenalty = request.PresencePenalty, + Seed = request.Seed, + Stop = request.Stop, + Stream = true, + StreamOptions = request.StreamOptions, + Suffix = request.Suffix, + Temperature = request.Temperature, + TopP = request.TopP, + User = request.User, + }; + PrepareArguments( + client: HttpClient); + PrepareCreateCompletionAsStreamArguments( + httpClient: HttpClient, + request: request); + + + var __authorizations = global::tryAGI.OpenAI.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_CreateCompletionAsStreamSecurityRequirements, + operationName: "CreateCompletionAsStreamAsync"); + + using var __timeoutCancellationTokenSource = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: false); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::tryAGI.OpenAI.PathBuilder( + path: "/completions", + baseUri: HttpClient.BaseAddress); + var __path = __pathBuilder.ToString(); + __path = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Post, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); +#if NET6_0_OR_GREATER + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "text/event-stream"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + var __httpRequestContentBody = request.ToJson(JsonSerializerContext); + var __httpRequestContent = new global::System.Net.Http.StringContent( + content: __httpRequestContentBody, + encoding: global::System.Text.Encoding.UTF8, + mediaType: "application/json"); + __httpRequest.Content = __httpRequestContent; + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareCreateCompletionAsStreamRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + request: request); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateCompletionAsStream", + methodName: "CreateCompletionAsStreamAsync", + pathTemplate: "\"/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseHeadersRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateCompletionAsStream", + methodName: "CreateCompletionAsStreamAsync", + pathTemplate: "\"/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateCompletionAsStream", + methodName: "CreateCompletionAsStreamAsync", + pathTemplate: "\"/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessCreateCompletionAsStreamResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateCompletionAsStream", + methodName: "CreateCompletionAsStreamAsync", + pathTemplate: "\"/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "CreateCompletionAsStream", + methodName: "CreateCompletionAsStreamAsync", + pathTemplate: "\"/completions\"", + httpMethod: "POST", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + + try + { + __response.EnsureSuccessStatusCode(); + } + catch (global::System.Net.Http.HttpRequestException __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + using var __stream = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + await foreach (var __sseEvent in global::System.Net.ServerSentEvents.SseParser + .Create(__stream).EnumerateAsync(__effectiveCancellationToken)) + { + var __content = __sseEvent.Data; + if (__content == "[DONE]") + { + yield break; + } + + var __streamedResponse = global::tryAGI.OpenAI.CreateCompletionResponse.FromJson(__content, JsonSerializerContext) ?? + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: $"Response deserialization failed for \"{__content}\" ", + innerException: null, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + + yield return __streamedResponse; + } + + } + } + finally + { + __httpRequest?.Dispose(); + } + } + /// + /// Create completion
+ /// Creates a completion for the provided prompt and parameters.
+ /// Returns a completion object, or a sequence of completion objects if the request is streamed. + ///
+ /// + /// ID of the model to use. You can use the [List models](https://developers.openai.com/api/reference/resources/models/methods/list) API to see all of your available models, or see our [Model overview](https://developers.openai.com/api/docs/models) for descriptions of them. + /// + /// + /// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
+ /// Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
+ /// Default Value: <|endoftext|> + /// + /// + /// Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
+ /// When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`.
+ /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
+ /// Default Value: 1 + /// + /// + /// Echo back the prompt in addition to the completion
+ /// Default Value: false + /// + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
+ /// [See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text)
+ /// Default Value: 0 + /// + /// + /// Modify the likelihood of specified tokens appearing in the completion.
+ /// Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](https://platform.openai.com/tokenizer?view=bpe) to convert text to token IDs. 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.
+ /// As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response.
+ /// The maximum value for `logprobs` is 5.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion.
+ /// The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens.
+ /// Default Value: 16
+ /// Example: 16 + /// + /// + /// How many completions to generate for each prompt.
+ /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
+ /// [See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text)
+ /// Default Value: 0 + /// + /// + /// If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result.
+ /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + /// + /// + /// Not supported with latest reasoning models `o3` and `o4-mini`.
+ /// Up to 4 sequences where the API will stop generating further tokens. The
+ /// returned text will not contain the stop sequence.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// + /// The suffix that comes after a completion of inserted text.
+ /// This parameter is only supported for `gpt-3.5-turbo-instruct`.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464
+ /// Example: test. + /// + /// + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
+ /// We generally recommend altering this or `top_p` but not both.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
+ /// We generally recommend altering this or `temperature` but not both.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://developers.openai.com/api/docs/guides/safety-best-practices#implement-safety-identifiers).
+ /// Example: user-1234 + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + public async global::System.Collections.Generic.IAsyncEnumerable CreateCompletionAsStreamAsync( + global::tryAGI.OpenAI.AnyOf model, + global::tryAGI.OpenAI.OneOf, global::System.Collections.Generic.IList, global::System.Collections.Generic.IList>>? prompt = default, + int? bestOf = default, + bool? echo = default, + double? frequencyPenalty = default, + global::System.Collections.Generic.Dictionary? logitBias = default, + int? logprobs = default, + int? maxTokens = default, + int? n = default, + double? presencePenalty = default, + long? seed = default, + global::tryAGI.OpenAI.StopConfiguration? stop = default, + global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1? streamOptions = default, + string? suffix = default, + double? temperature = default, + double? topP = default, + string? user = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default) + { + var __request = new global::tryAGI.OpenAI.CreateCompletionRequest + { + Model = model, + Prompt = prompt, + BestOf = bestOf, + Echo = echo, + FrequencyPenalty = frequencyPenalty, + LogitBias = logitBias, + Logprobs = logprobs, + MaxTokens = maxTokens, + N = n, + PresencePenalty = presencePenalty, + Seed = seed, + Stop = stop, + Stream = true, + StreamOptions = streamOptions, + Suffix = suffix, + Temperature = temperature, + TopP = topP, + User = user, + }; + + var __enumerable = CreateCompletionAsStreamAsync( + request: __request, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + + await foreach (var __response in __enumerable) + { + yield return __response; + } + } + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletion.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletion.g.cs index 85d9b1f28..8bf852ebe 100644 --- a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletion.g.cs +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletion.g.cs @@ -98,10 +98,6 @@ public partial interface ICompletionsClient /// returned text will not contain the stop sequence.
/// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 /// - /// - /// Whether to stream back partial progress. If set, tokens will be sent as data-only [server-sent events](https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events#Event_stream_format) as they become available, with the stream terminated by a `data: [DONE]` message. [Example Python code](https://cookbook.openai.com/examples/how_to_stream_completions).
- /// Default Value: false - /// /// /// /// The suffix that comes after a completion of inserted text.
@@ -141,7 +137,6 @@ public partial interface ICompletionsClient double? presencePenalty = default, long? seed = default, global::tryAGI.OpenAI.StopConfiguration? stop = default, - bool? stream = default, global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1? streamOptions = default, string? suffix = default, double? temperature = default, diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletionAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletionAsStream.g.cs new file mode 100644 index 000000000..e5ba3c36c --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ICompletionsClient.CreateCompletionAsStream.g.cs @@ -0,0 +1,134 @@ +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial interface ICompletionsClient + { + /// + /// Create completion
+ /// Creates a completion for the provided prompt and parameters.
+ /// Returns a completion object, or a sequence of completion objects if the request is streamed. + ///
+ /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + global::System.Collections.Generic.IAsyncEnumerable CreateCompletionAsStreamAsync( + + global::tryAGI.OpenAI.CreateCompletionRequest request, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + /// + /// Create completion
+ /// Creates a completion for the provided prompt and parameters.
+ /// Returns a completion object, or a sequence of completion objects if the request is streamed. + ///
+ /// + /// ID of the model to use. You can use the [List models](https://developers.openai.com/api/reference/resources/models/methods/list) API to see all of your available models, or see our [Model overview](https://developers.openai.com/api/docs/models) for descriptions of them. + /// + /// + /// The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays.
+ /// Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
+ /// Default Value: <|endoftext|> + /// + /// + /// Generates `best_of` completions server-side and returns the "best" (the one with the highest log probability per token). Results cannot be streamed.
+ /// When used with `n`, `best_of` controls the number of candidate completions and `n` specifies how many to return – `best_of` must be greater than `n`.
+ /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
+ /// Default Value: 1 + /// + /// + /// Echo back the prompt in addition to the completion
+ /// Default Value: false + /// + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.
+ /// [See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text)
+ /// Default Value: 0 + /// + /// + /// Modify the likelihood of specified tokens appearing in the completion.
+ /// Accepts a JSON object that maps tokens (specified by their token ID in the GPT tokenizer) to an associated bias value from -100 to 100. You can use this [tokenizer tool](https://platform.openai.com/tokenizer?view=bpe) to convert text to token IDs. 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.
+ /// As an example, you can pass `{"50256": -100}` to prevent the <|endoftext|> token from being generated.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// Include the log probabilities on the `logprobs` most likely output tokens, as well the chosen tokens. For example, if `logprobs` is 5, the API will return a list of the 5 most likely tokens. The API will always return the `logprob` of the sampled token, so there may be up to `logprobs+1` elements in the response.
+ /// The maximum value for `logprobs` is 5.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// The maximum number of [tokens](https://platform.openai.com/tokenizer) that can be generated in the completion.
+ /// The token count of your prompt plus `max_tokens` cannot exceed the model's context length. [Example Python code](https://cookbook.openai.com/examples/how_to_count_tokens_with_tiktoken) for counting tokens.
+ /// Default Value: 16
+ /// Example: 16 + /// + /// + /// How many completions to generate for each prompt.
+ /// **Note:** Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for `max_tokens` and `stop`.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.
+ /// [See more information about frequency and presence penalties.](https://developers.openai.com/api/docs/guides/text)
+ /// Default Value: 0 + /// + /// + /// If specified, our system will make a best effort to sample deterministically, such that repeated requests with the same `seed` and parameters should return the same result.
+ /// Determinism is not guaranteed, and you should refer to the `system_fingerprint` response parameter to monitor changes in the backend. + /// + /// + /// Not supported with latest reasoning models `o3` and `o4-mini`.
+ /// Up to 4 sequences where the API will stop generating further tokens. The
+ /// returned text will not contain the stop sequence.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464 + /// + /// + /// + /// The suffix that comes after a completion of inserted text.
+ /// This parameter is only supported for `gpt-3.5-turbo-instruct`.
+ /// Default Value: openapi-json-null-sentinel-value-2BF93600-0FE4-4250-987A-E5DDB203E464
+ /// Example: test. + /// + /// + /// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
+ /// We generally recommend altering this or `top_p` but not both.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered.
+ /// We generally recommend altering this or `temperature` but not both.
+ /// Default Value: 1
+ /// Example: 1 + /// + /// + /// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. [Learn more](https://developers.openai.com/api/docs/guides/safety-best-practices#implement-safety-identifiers).
+ /// Example: user-1234 + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + global::System.Collections.Generic.IAsyncEnumerable CreateCompletionAsStreamAsync( + global::tryAGI.OpenAI.AnyOf model, + global::tryAGI.OpenAI.OneOf, global::System.Collections.Generic.IList, global::System.Collections.Generic.IList>>? prompt = default, + int? bestOf = default, + bool? echo = default, + double? frequencyPenalty = default, + global::System.Collections.Generic.Dictionary? logitBias = default, + int? logprobs = default, + int? maxTokens = default, + int? n = default, + double? presencePenalty = default, + long? seed = default, + global::tryAGI.OpenAI.StopConfiguration? stop = default, + global::tryAGI.OpenAI.ChatCompletionStreamOptionsVariant1? streamOptions = default, + string? suffix = default, + double? temperature = default, + double? topP = default, + string? user = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.BetaGetResponseAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.BetaGetResponseAsStream.g.cs new file mode 100644 index 000000000..9cf056f65 --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.BetaGetResponseAsStream.g.cs @@ -0,0 +1,32 @@ +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial interface IResponsesClient + { + /// + /// Get a model response
+ /// Retrieves a model response with the given ID. + ///
+ /// + /// Example: resp_677efb5139a88190b512bc3fef8e535d + /// + /// + /// + /// + /// + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + global::System.Collections.Generic.IAsyncEnumerable BetaGetResponseAsStreamAsync( + string responseId, + global::System.Collections.Generic.IList? include = default, + bool? stream = default, + int? startingAfter = default, + bool? includeObfuscation = default, + global::System.Collections.Generic.IList? openaiBeta = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.GetResponseAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.GetResponseAsStream.g.cs new file mode 100644 index 000000000..ce7cad137 --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.IResponsesClient.GetResponseAsStream.g.cs @@ -0,0 +1,30 @@ +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial interface IResponsesClient + { + /// + /// Get a model response
+ /// Retrieves a model response with the given ID. + ///
+ /// + /// Example: resp_677efb5139a88190b512bc3fef8e535d + /// + /// + /// + /// + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + global::System.Collections.Generic.IAsyncEnumerable GetResponseAsStreamAsync( + string responseId, + global::System.Collections.Generic.IList? include = default, + bool? stream = default, + int? startingAfter = default, + bool? includeObfuscation = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + global::System.Threading.CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponse.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponse.g.cs index e62a49e28..c08b78936 100644 --- a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponse.g.cs +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponse.g.cs @@ -170,6 +170,10 @@ partial void ProcessBetaGetResponseResponseContent( __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponseAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponseAsStream.g.cs new file mode 100644 index 000000000..13465d014 --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.BetaGetResponseAsStream.g.cs @@ -0,0 +1,419 @@ + +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial class ResponsesClient + { + + + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement s_BetaGetResponseAsStreamSecurityRequirement0 = + new global::tryAGI.OpenAI.EndPointSecurityRequirement + { + Authorizations = new global::tryAGI.OpenAI.EndPointAuthorizationRequirement[] + { new global::tryAGI.OpenAI.EndPointAuthorizationRequirement + { + Type = "Http", + SchemeId = "ApiKeyAuth", + Location = "Header", + Name = "Bearer", + FriendlyName = "Bearer", + }, + }, + }; + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement[] s_BetaGetResponseAsStreamSecurityRequirements = + new global::tryAGI.OpenAI.EndPointSecurityRequirement[] + { s_BetaGetResponseAsStreamSecurityRequirement0, + }; + partial void PrepareBetaGetResponseAsStreamArguments( + global::System.Net.Http.HttpClient httpClient, + ref string responseId, + global::System.Collections.Generic.IList? include, + ref bool? stream, + ref int? startingAfter, + ref bool? includeObfuscation, + global::System.Collections.Generic.IList? openaiBeta); + partial void PrepareBetaGetResponseAsStreamRequest( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpRequestMessage httpRequestMessage, + string responseId, + global::System.Collections.Generic.IList? include, + bool? stream, + int? startingAfter, + bool? includeObfuscation, + global::System.Collections.Generic.IList? openaiBeta); + partial void ProcessBetaGetResponseAsStreamResponse( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpResponseMessage httpResponseMessage); + + /// + /// Get a model response
+ /// Retrieves a model response with the given ID. + ///
+ /// + /// Example: resp_677efb5139a88190b512bc3fef8e535d + /// + /// + /// + /// + /// + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + public async global::System.Collections.Generic.IAsyncEnumerable BetaGetResponseAsStreamAsync( + string responseId, + global::System.Collections.Generic.IList? include = default, + bool? stream = default, + int? startingAfter = default, + bool? includeObfuscation = default, + global::System.Collections.Generic.IList? openaiBeta = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default) + { + PrepareArguments( + client: HttpClient); + PrepareBetaGetResponseAsStreamArguments( + httpClient: HttpClient, + responseId: ref responseId, + include: include, + stream: ref stream, + startingAfter: ref startingAfter, + includeObfuscation: ref includeObfuscation, + openaiBeta: openaiBeta); + + + var __authorizations = global::tryAGI.OpenAI.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_BetaGetResponseAsStreamSecurityRequirements, + operationName: "BetaGetResponseAsStreamAsync"); + + using var __timeoutCancellationTokenSource = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::tryAGI.OpenAI.PathBuilder( + path: $"/responses/{responseId}?beta=true", + baseUri: HttpClient.BaseAddress); + __pathBuilder + .AddOptionalParameter("include", include, selector: static x => x.ToValueString(), delimiter: ",", explode: true) + .AddOptionalParameter("stream", stream?.ToString().ToLowerInvariant()) + .AddOptionalParameter("starting_after", startingAfter?.ToString()) + .AddOptionalParameter("include_obfuscation", includeObfuscation?.ToString().ToLowerInvariant()) + ; + var __path = __pathBuilder.ToString(); + __path = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); +#if NET6_0_OR_GREATER + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "text/event-stream"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + + if (openaiBeta != default) + { + __httpRequest.Headers.TryAddWithoutValidation("openai-beta", openaiBeta.ToString()); + } + + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareBetaGetResponseAsStreamRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + responseId: responseId, + include: include, + stream: stream, + startingAfter: startingAfter, + includeObfuscation: includeObfuscation, + openaiBeta: openaiBeta); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "BetaGetResponseAsStream", + methodName: "BetaGetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}?beta=true\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseHeadersRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "BetaGetResponseAsStream", + methodName: "BetaGetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}?beta=true\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "BetaGetResponseAsStream", + methodName: "BetaGetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}?beta=true\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessBetaGetResponseAsStreamResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "BetaGetResponseAsStream", + methodName: "BetaGetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}?beta=true\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "BetaGetResponseAsStream", + methodName: "BetaGetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}?beta=true\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + + try + { + __response.EnsureSuccessStatusCode(); + } + catch (global::System.Net.Http.HttpRequestException __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + using var __stream = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + await foreach (var __sseEvent in global::System.Net.ServerSentEvents.SseParser + .Create(__stream).EnumerateAsync(__effectiveCancellationToken)) + { + var __content = __sseEvent.Data; + if (__content == "[DONE]") + { + yield break; + } + + var __streamedResponse = global::tryAGI.OpenAI.BetaResponseStreamEvent.FromJson(__content, JsonSerializerContext) ?? + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: $"Response deserialization failed for \"{__content}\" ", + innerException: null, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + + yield return __streamedResponse; + } + + } + } + finally + { + __httpRequest?.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponse.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponse.g.cs index d8c58785d..cedc7f6a0 100644 --- a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponse.g.cs +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponse.g.cs @@ -162,6 +162,10 @@ partial void ProcessGetResponseResponseContent( __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; #endif + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "application/json"); + foreach (var __authorization in __authorizations) { if (__authorization.Type == "Http" || diff --git a/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponseAsStream.g.cs b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponseAsStream.g.cs new file mode 100644 index 000000000..ad304c462 --- /dev/null +++ b/src/libs/tryAGI.OpenAI/Generated/tryAGI.OpenAI.ResponsesClient.GetResponseAsStream.g.cs @@ -0,0 +1,407 @@ + +#nullable enable + +namespace tryAGI.OpenAI +{ + public partial class ResponsesClient + { + + + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement s_GetResponseAsStreamSecurityRequirement0 = + new global::tryAGI.OpenAI.EndPointSecurityRequirement + { + Authorizations = new global::tryAGI.OpenAI.EndPointAuthorizationRequirement[] + { new global::tryAGI.OpenAI.EndPointAuthorizationRequirement + { + Type = "Http", + SchemeId = "ApiKeyAuth", + Location = "Header", + Name = "Bearer", + FriendlyName = "Bearer", + }, + }, + }; + private static readonly global::tryAGI.OpenAI.EndPointSecurityRequirement[] s_GetResponseAsStreamSecurityRequirements = + new global::tryAGI.OpenAI.EndPointSecurityRequirement[] + { s_GetResponseAsStreamSecurityRequirement0, + }; + partial void PrepareGetResponseAsStreamArguments( + global::System.Net.Http.HttpClient httpClient, + ref string responseId, + global::System.Collections.Generic.IList? include, + ref bool? stream, + ref int? startingAfter, + ref bool? includeObfuscation); + partial void PrepareGetResponseAsStreamRequest( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpRequestMessage httpRequestMessage, + string responseId, + global::System.Collections.Generic.IList? include, + bool? stream, + int? startingAfter, + bool? includeObfuscation); + partial void ProcessGetResponseAsStreamResponse( + global::System.Net.Http.HttpClient httpClient, + global::System.Net.Http.HttpResponseMessage httpResponseMessage); + + /// + /// Get a model response
+ /// Retrieves a model response with the given ID. + ///
+ /// + /// Example: resp_677efb5139a88190b512bc3fef8e535d + /// + /// + /// + /// + /// + /// Per-request overrides such as headers, query parameters, timeout, retries, and response buffering. + /// The token to cancel the operation with + /// + public async global::System.Collections.Generic.IAsyncEnumerable GetResponseAsStreamAsync( + string responseId, + global::System.Collections.Generic.IList? include = default, + bool? stream = default, + int? startingAfter = default, + bool? includeObfuscation = default, + global::tryAGI.OpenAI.AutoSDKRequestOptions? requestOptions = default, + [global::System.Runtime.CompilerServices.EnumeratorCancellation] global::System.Threading.CancellationToken cancellationToken = default) + { + PrepareArguments( + client: HttpClient); + PrepareGetResponseAsStreamArguments( + httpClient: HttpClient, + responseId: ref responseId, + include: include, + stream: ref stream, + startingAfter: ref startingAfter, + includeObfuscation: ref includeObfuscation); + + + var __authorizations = global::tryAGI.OpenAI.EndPointSecurityResolver.ResolveAuthorizations( + availableAuthorizations: Authorizations, + securityRequirements: s_GetResponseAsStreamSecurityRequirements, + operationName: "GetResponseAsStreamAsync"); + + using var __timeoutCancellationTokenSource = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateTimeoutCancellationTokenSource( + clientOptions: Options, + requestOptions: requestOptions, + cancellationToken: cancellationToken); + var __effectiveCancellationToken = __timeoutCancellationTokenSource?.Token ?? cancellationToken; + var __effectiveReadResponseAsString = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetReadResponseAsString( + clientOptions: Options, + requestOptions: requestOptions, + fallbackValue: ReadResponseAsString); + var __maxAttempts = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetMaxAttempts( + clientOptions: Options, + requestOptions: requestOptions, + supportsRetry: true); + + global::System.Net.Http.HttpRequestMessage __CreateHttpRequest() + { + + var __pathBuilder = new global::tryAGI.OpenAI.PathBuilder( + path: $"/responses/{responseId}", + baseUri: HttpClient.BaseAddress); + __pathBuilder + .AddOptionalParameter("include", include, selector: static x => x.ToValueString(), delimiter: ",", explode: true) + .AddOptionalParameter("stream", stream?.ToString().ToLowerInvariant()) + .AddOptionalParameter("starting_after", startingAfter?.ToString()) + .AddOptionalParameter("include_obfuscation", includeObfuscation?.ToString().ToLowerInvariant()) + ; + var __path = __pathBuilder.ToString(); + __path = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.AppendQueryParameters( + path: __path, + clientParameters: Options.QueryParameters, + requestParameters: requestOptions?.QueryParameters); + var __httpRequest = new global::System.Net.Http.HttpRequestMessage( + method: global::System.Net.Http.HttpMethod.Get, + requestUri: new global::System.Uri(__path, global::System.UriKind.RelativeOrAbsolute)); +#if NET6_0_OR_GREATER + __httpRequest.Version = global::System.Net.HttpVersion.Version11; + __httpRequest.VersionPolicy = global::System.Net.Http.HttpVersionPolicy.RequestVersionOrHigher; +#endif + + __httpRequest.Headers.TryAddWithoutValidation( + "Accept", + "text/event-stream"); + + foreach (var __authorization in __authorizations) + { + if (__authorization.Type == "Http" || + __authorization.Type == "OAuth2" || + __authorization.Type == "OpenIdConnect") + { + __httpRequest.Headers.Authorization = new global::System.Net.Http.Headers.AuthenticationHeaderValue( + scheme: __authorization.Name, + parameter: __authorization.Value); + } + else if (__authorization.Type == "ApiKey" && + __authorization.Location == "Header") + { + __httpRequest.Headers.Add(__authorization.Name, __authorization.Value); + } + } + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ApplyHeaders( + request: __httpRequest, + clientHeaders: Options.Headers, + requestHeaders: requestOptions?.Headers); + + PrepareRequest( + client: HttpClient, + request: __httpRequest); + PrepareGetResponseAsStreamRequest( + httpClient: HttpClient, + httpRequestMessage: __httpRequest, + responseId: responseId, + include: include, + stream: stream, + startingAfter: startingAfter, + includeObfuscation: includeObfuscation); + + return __httpRequest; + } + + global::System.Net.Http.HttpRequestMessage? __httpRequest = null; + global::System.Net.Http.HttpResponseMessage? __response = null; + var __attemptNumber = 0; + try + { + for (var __attempt = 1; __attempt <= __maxAttempts; __attempt++) + { + __attemptNumber = __attempt; + __httpRequest = __CreateHttpRequest(); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnBeforeRequestAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetResponseAsStream", + methodName: "GetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + try + { + __response = await HttpClient.SendAsync( + request: __httpRequest, + completionOption: global::System.Net.Http.HttpCompletionOption.ResponseHeadersRead, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + } + catch (global::System.Net.Http.HttpRequestException __exception) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: null, + attempt: __attempt); + var __willRetry = __attempt < __maxAttempts && !__effectiveCancellationToken.IsCancellationRequested; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetResponseAsStream", + methodName: "GetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: null, + exception: __exception, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: __willRetry, + retryDelay: __willRetry ? __retryDelay : (global::System.TimeSpan?)null, + retryReason: "exception", + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + if (!__willRetry) + { + throw; + } + + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + if (__response != null && + __attempt < __maxAttempts && + global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.ShouldRetryStatusCode(__response.StatusCode)) + { + var __retryDelay = global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.GetRetryDelay( + clientOptions: Options, + requestOptions: requestOptions, + response: __response, + attempt: __attempt); + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetResponseAsStream", + methodName: "GetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attempt, + maxAttempts: __maxAttempts, + willRetry: true, + retryDelay: __retryDelay, + retryReason: "status:" + ((int)__response.StatusCode).ToString(global::System.Globalization.CultureInfo.InvariantCulture), + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + __response.Dispose(); + __response = null; + __httpRequest.Dispose(); + __httpRequest = null; + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.DelayBeforeRetryAsync( + retryDelay: __retryDelay, + cancellationToken: __effectiveCancellationToken).ConfigureAwait(false); + continue; + } + + break; + } + + if (__response == null) + { + throw new global::System.InvalidOperationException("No response received."); + } + + using (__response) + { + + ProcessResponse( + client: HttpClient, + response: __response); + ProcessGetResponseAsStreamResponse( + httpClient: HttpClient, + httpResponseMessage: __response); + if (__response.IsSuccessStatusCode) + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterSuccessAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetResponseAsStream", + methodName: "GetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + else + { + await global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.OnAfterErrorAsync( + clientOptions: Options, + context: global::tryAGI.OpenAI.AutoSDKRequestOptionsSupport.CreateHookContext( + operationId: "GetResponseAsStream", + methodName: "GetResponseAsStreamAsync", + pathTemplate: "$\"/responses/{responseId}\"", + httpMethod: "GET", + baseUri: BaseUri, + request: __httpRequest ?? throw new global::System.InvalidOperationException("The HTTP request was not created before invoking a request hook."), + response: __response, + exception: null, + clientOptions: Options, + requestOptions: requestOptions, + attempt: __attemptNumber, + maxAttempts: __maxAttempts, + willRetry: false, + retryDelay: null, + retryReason: global::System.String.Empty, + cancellationToken: __effectiveCancellationToken)).ConfigureAwait(false); + } + + try + { + __response.EnsureSuccessStatusCode(); + } + catch (global::System.Net.Http.HttpRequestException __ex) + { + string? __content = null; + try + { + __content = await __response.Content.ReadAsStringAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + } + catch (global::System.Exception) + { + } + + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: __content ?? __response.ReasonPhrase ?? string.Empty, + innerException: __ex, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + } + + using var __stream = await __response.Content.ReadAsStreamAsync( + #if NET5_0_OR_GREATER + __effectiveCancellationToken + #endif + ).ConfigureAwait(false); + + await foreach (var __sseEvent in global::System.Net.ServerSentEvents.SseParser + .Create(__stream).EnumerateAsync(__effectiveCancellationToken)) + { + var __content = __sseEvent.Data; + if (__content == "[DONE]") + { + yield break; + } + + var __streamedResponse = global::tryAGI.OpenAI.ResponseStreamEvent.FromJson(__content, JsonSerializerContext) ?? + throw global::tryAGI.OpenAI.ApiException.Create( + statusCode: __response.StatusCode, + message: $"Response deserialization failed for \"{__content}\" ", + innerException: null, + responseBody: __content, + responseHeaders: global::System.Linq.Enumerable.ToDictionary( + __response.Headers, + h => h.Key, + h => h.Value)); + + yield return __streamedResponse; + } + + } + } + finally + { + __httpRequest?.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/src/libs/tryAGI.OpenAI/openapi.yaml b/src/libs/tryAGI.OpenAI/openapi.yaml index d592619c2..cea888f0b 100644 --- a/src/libs/tryAGI.OpenAI/openapi.yaml +++ b/src/libs/tryAGI.OpenAI/openapi.yaml @@ -3825,6 +3825,9 @@ paths: application/json: schema: $ref: "#/components/schemas/CreateCompletionResponse" + text/event-stream: + schema: + $ref: "#/components/schemas/CreateCompletionResponse" "400": description: Invalid completion request or incompatible request parameters. content: @@ -23000,6 +23003,9 @@ paths: application/json: schema: $ref: "#/components/schemas/Response" + text/event-stream: + schema: + $ref: "#/components/schemas/ResponseStreamEvent" "404": description: The response was not found. content: @@ -38377,6 +38383,9 @@ paths: application/json: schema: $ref: "#/components/schemas/BetaResponse" + text/event-stream: + schema: + $ref: "#/components/schemas/BetaResponseStreamEvent" "404": description: The response was not found. content: