From f4d245ed6242d2c1362c1cef6713517ea025cac4 Mon Sep 17 00:00:00 2001 From: F_L_A Date: Wed, 16 Oct 2024 23:59:44 +0330 Subject: [PATCH 1/9] fix settings --- OpenAI_API/EndpointBase.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenAI_API/EndpointBase.cs b/OpenAI_API/EndpointBase.cs index d981c7e..389a1ad 100644 --- a/OpenAI_API/EndpointBase.cs +++ b/OpenAI_API/EndpointBase.cs @@ -125,7 +125,7 @@ private async Task HttpRequestRaw(string url = null, HttpMe } else { - string jsonContent = JsonConvert.SerializeObject(postData, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore }); + string jsonContent = JsonConvert.SerializeObject(postData, new JsonSerializerSettings() { NullValueHandling = NullValueHandling.Ignore ,TypeNameHandling=TypeNameHandling.None}); var stringContent = new StringContent(jsonContent, UnicodeEncoding.UTF8, "application/json"); req.Content = stringContent; } From 722b42a364071eba7e491702836fcfd3bd0e567f Mon Sep 17 00:00:00 2001 From: lotfiaghel Date: Sun, 1 Dec 2024 22:43:18 +0330 Subject: [PATCH 2/9] -- --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 2081699..a2dd3a6 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ + + + Microsoft reached out to me about transitioning this library into a new official C# OpenAI library and now it's ready to go! Starting with [v2.0.0-beta.3](https://www.nuget.org/packages/OpenAI/2.0.0-beta.3), the official library now has full coverage and will stay up-to-date. More details in the blog post here: https://devblogs.microsoft.com/dotnet/openai-dotnet-library This github repo will remain here to document my original version of the library through [version 1.11, which is still available on Nuget as well](https://www.nuget.org/packages/OpenAI/1.11.0). From 9f1e4a5fe659bb0949d7900ce72440bf3d215a61 Mon Sep 17 00:00:00 2001 From: mjdavari Date: Sun, 1 Dec 2024 22:57:40 +0330 Subject: [PATCH 3/9] Update README.md --- README.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/README.md b/README.md index a2dd3a6..242e4ef 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,4 @@ - - Microsoft reached out to me about transitioning this library into a new official C# OpenAI library and now it's ready to go! Starting with [v2.0.0-beta.3](https://www.nuget.org/packages/OpenAI/2.0.0-beta.3), the official library now has full coverage and will stay up-to-date. More details in the blog post here: https://devblogs.microsoft.com/dotnet/openai-dotnet-library This github repo will remain here to document my original version of the library through [version 1.11, which is still available on Nuget as well](https://www.nuget.org/packages/OpenAI/1.11.0). From 72f53a07525764118c9a9da4c212e9f83723aac6 Mon Sep 17 00:00:00 2001 From: lotfiaghel Date: Sun, 1 Dec 2024 23:13:56 +0330 Subject: [PATCH 4/9] -- --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 242e4ef..9f0b18d 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ Microsoft reached out to me about transitioning this library into a new official This github repo will remain here to document my original version of the library through [version 1.11, which is still available on Nuget as well](https://www.nuget.org/packages/OpenAI/1.11.0). 🎉 + + # C#/.NET SDK for accessing the OpenAI APIs, including GPT-3.5/4, GPT-3.5/4-Turbo, and DALL-E 2/3 A simple C# .NET wrapper library to use with OpenAI's API. More context [on my blog](https://rogerpincombe.com/openai-dotnet-api). **This is my original unofficial wrapper library around the OpenAI API.** From 52be796cba8c438ced85cde2c06c6a51e51c7ca0 Mon Sep 17 00:00:00 2001 From: mjdavari Date: Sun, 1 Dec 2024 23:14:53 +0330 Subject: [PATCH 5/9] added word --- OpenAI_API/Audio/AudioResult.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/OpenAI_API/Audio/AudioResult.cs b/OpenAI_API/Audio/AudioResult.cs index 5c77983..55ded4b 100644 --- a/OpenAI_API/Audio/AudioResult.cs +++ b/OpenAI_API/Audio/AudioResult.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Tracing; using System.Text; namespace OpenAI_API.Audio @@ -12,9 +13,12 @@ public class AudioResultVerbose : ApiResultBase public double duration { get; set; } public string language { get; set; } public List segments { get; set; } + public List words { get; set; } + public string task { get; set; } public string text { get; set; } + public class word { } public class Segment { public double avg_logprob { get; set; } From 6dfff4cca19e60507c915dc549222a5d8d02baff Mon Sep 17 00:00:00 2001 From: mjdavari Date: Sun, 1 Dec 2024 23:56:44 +0330 Subject: [PATCH 6/9] updated AudioResult and AudioRequest models to include word and timestamps --- OpenAI_API/Audio/AudioRequest.cs | 99 +++--- OpenAI_API/Audio/AudioResult.cs | 8 +- OpenAI_API/Audio/ITranscriptionEndpoint.cs | 6 +- OpenAI_API/Audio/TranscriptionEndpoint.cs | 382 +++++++++++---------- 4 files changed, 271 insertions(+), 224 deletions(-) diff --git a/OpenAI_API/Audio/AudioRequest.cs b/OpenAI_API/Audio/AudioRequest.cs index 6912a13..b13c554 100644 --- a/OpenAI_API/Audio/AudioRequest.cs +++ b/OpenAI_API/Audio/AudioRequest.cs @@ -6,54 +6,57 @@ namespace OpenAI_API.Audio { - /// - /// Parameters for requests made by the . - /// - public class AudioRequest - { - /// - /// The model to use for this request. Currently only is supported. - /// - [JsonProperty("model")] - public string Model { get; set; } = OpenAI_API.Models.Model.DefaultTranscriptionModel; - - /// - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language for transcriptions, or English for translations. - /// - [JsonProperty("prompt", DefaultValueHandling = DefaultValueHandling.Ignore)] - public string Prompt { get; set; } = null; - - /// - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// - [JsonProperty("language", DefaultValueHandling = DefaultValueHandling.Ignore)] - public string Language { get; set; } = null; - - /// - /// The format of the transcript output, should be one of the options in . See - /// - [JsonProperty("response_format", DefaultValueHandling = DefaultValueHandling.Ignore)] - public string ResponseFormat { get; set; } = null; - - /// - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// - [JsonProperty("temperature", DefaultValueHandling = DefaultValueHandling.Ignore)] - public double Temperature { get; set; } = 0; - - - /// - /// The format of the transcript output. See - /// - public static class ResponseFormats - { + /// + /// Parameters for requests made by the . + /// + public class AudioRequest + { + /// + /// The model to use for this request. Currently only is supported. + /// + [JsonProperty("model")] + public string Model { get; set; } = OpenAI_API.Models.Model.DefaultTranscriptionModel; + + /// + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language for transcriptions, or English for translations. + /// + [JsonProperty("prompt", DefaultValueHandling = DefaultValueHandling.Ignore)] + public string Prompt { get; set; } = null; + + /// + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// + [JsonProperty("language", DefaultValueHandling = DefaultValueHandling.Ignore)] + public string Language { get; set; } = null; + + /// + /// The format of the transcript output, should be one of the options in . See + /// + [JsonProperty("response_format", DefaultValueHandling = DefaultValueHandling.Ignore)] + public string ResponseFormat { get; set; } = null; + + /// + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// + [JsonProperty("temperature", DefaultValueHandling = DefaultValueHandling.Ignore)] + public double Temperature { get; set; } = 0; + + [JsonProperty("timestamp_granularities", DefaultValueHandling = DefaultValueHandling.Ignore)] + public List TimeStamp_granularities { get; set; } = new List { "word", "segment" }; + + + /// + /// The format of the transcript output. See + /// + public static class ResponseFormats + { #pragma warning disable CS1591 // Missing XML comment for publicly visible type or member - public const string JSON = "json"; - public const string Text = "text"; - public const string SRT = "srt"; - public const string VerboseJson = "verbose_json"; - public const string VTT = "vtt"; + public const string JSON = "json"; + public const string Text = "text"; + public const string SRT = "srt"; + public const string VerboseJson = "verbose_json"; + public const string VTT = "vtt"; #pragma warning restore CS1591 // Missing XML comment for publicly visible type or member - } - } + } + } } diff --git a/OpenAI_API/Audio/AudioResult.cs b/OpenAI_API/Audio/AudioResult.cs index 55ded4b..92263c8 100644 --- a/OpenAI_API/Audio/AudioResult.cs +++ b/OpenAI_API/Audio/AudioResult.cs @@ -13,12 +13,16 @@ public class AudioResultVerbose : ApiResultBase public double duration { get; set; } public string language { get; set; } public List segments { get; set; } - public List words { get; set; } + public List words { get; set; } public string task { get; set; } public string text { get; set; } - public class word { } + public class Word { + public double end { get; set; } + public double start { get; set; } + public string word { get; set; } + } public class Segment { public double avg_logprob { get; set; } diff --git a/OpenAI_API/Audio/ITranscriptionEndpoint.cs b/OpenAI_API/Audio/ITranscriptionEndpoint.cs index f08c93e..cfdc667 100644 --- a/OpenAI_API/Audio/ITranscriptionEndpoint.cs +++ b/OpenAI_API/Audio/ITranscriptionEndpoint.cs @@ -1,4 +1,5 @@ -using System.IO; +using System.Collections.Generic; +using System.IO; using System.Threading.Tasks; namespace OpenAI_API.Audio @@ -77,5 +78,6 @@ public interface ITranscriptionEndpoint /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. /// A string of the transcribed text Task GetTextAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null); - } + Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps, double? temperature = null); + } } \ No newline at end of file diff --git a/OpenAI_API/Audio/TranscriptionEndpoint.cs b/OpenAI_API/Audio/TranscriptionEndpoint.cs index fe1a268..c9fc676 100644 --- a/OpenAI_API/Audio/TranscriptionEndpoint.cs +++ b/OpenAI_API/Audio/TranscriptionEndpoint.cs @@ -2,190 +2,228 @@ using System.Collections.Generic; using System.IO; using System.Net.Http; +using System.Runtime.InteropServices.ComTypes; using System.Text; using System.Threading.Tasks; namespace OpenAI_API.Audio { - /// - /// Transcribe audio into text, with optional translation into English. - /// - public class TranscriptionEndpoint : EndpointBase, ITranscriptionEndpoint - { - /// - protected override string Endpoint - { - get - { - if (TranslateToEnglish) - { - return "audio/translations"; - } - else - { - return "audio/transcriptions"; - } - } - } + /// + /// Transcribe audio into text, with optional translation into English. + /// + public class TranscriptionEndpoint : EndpointBase, ITranscriptionEndpoint + { + /// + protected override string Endpoint + { + get + { + if (TranslateToEnglish) + { + return "audio/translations"; + } + else + { + return "audio/transcriptions"; + } + } + } - /// - /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . - /// - /// Pass in the instance of the api - /// If , the response will translate non-English audio into English. Otherwise the returned text will be in the spoken language. - internal TranscriptionEndpoint(OpenAIAPI api, bool translate) : base(api) - { - TranslateToEnglish = translate; - } + /// + /// Constructor of the api endpoint. Rather than instantiating this yourself, access it through an instance of as . + /// + /// Pass in the instance of the api + /// If , the response will translate non-English audio into English. Otherwise the returned text will be in the spoken language. + internal TranscriptionEndpoint(OpenAIAPI api, bool translate) : base(api) + { + TranslateToEnglish = translate; + } - /// - /// This allows you to set default parameters for every request, for example to set a default language. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. - /// - public AudioRequest DefaultRequestArgs { get; set; } = new AudioRequest(); + /// + /// This allows you to set default parameters for every request, for example to set a default language. For every request, if you do not have a parameter set on the request but do have it set here as a default, the request will automatically pick up the default value. + /// + public AudioRequest DefaultRequestArgs { get; set; } = new AudioRequest(); - /// - /// If , the response will translate non-English audio into English. Otherwise the returned text will be in the spoken language. - /// - private bool TranslateToEnglish { get; } + /// + /// If , the response will translate non-English audio into English. Otherwise the returned text will be in the spoken language. + /// + private bool TranslateToEnglish { get; } - /// - /// Gets the transcription of the audio stream as a text string - /// - /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetTextAsync(Stream audioStream, string filename, string language = null, string prompt = null, double? temperature = null) - => await GetAsFormatAsync(audioStream, filename, AudioRequest.ResponseFormats.Text, language, prompt, temperature); + /// + /// Gets the transcription of the audio stream as a text string + /// + /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetTextAsync(Stream audioStream, string filename, string language = null, string prompt = null, double? temperature = null) + => await GetAsFormatAsync(audioStream, filename, AudioRequest.ResponseFormats.Text, language, prompt, temperature); - /// - /// Gets the transcription of the audio file as a text string - /// - /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetTextAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null) - { - using (var fileStream = File.OpenRead(audioFilePath)) - { - return await GetTextAsync(fileStream, Path.GetFileName(audioFilePath), language, prompt, temperature); - } - } + /// + /// Gets the transcription of the audio file as a text string + /// + /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetTextAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null) + { + using (var fileStream = File.OpenRead(audioFilePath)) + { + return await GetTextAsync(fileStream, Path.GetFileName(audioFilePath), language, prompt, temperature); + } + } - /// - /// Gets the transcription of the audio stream, with full metadata - /// - /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetWithDetailsAsync(Stream audioStream, string filename, string language = null, string prompt = null, double? temperature = null) - { - var request = new AudioRequest() - { - Language = language ?? DefaultRequestArgs.Language, - Model = DefaultRequestArgs.Model, - Prompt = prompt ?? DefaultRequestArgs.Prompt, - Temperature = temperature ?? DefaultRequestArgs.Temperature - }; - request.ResponseFormat = AudioRequest.ResponseFormats.VerboseJson; - MultipartFormDataContent content; - using (var memoryStream = new MemoryStream()) - { - audioStream.CopyTo(memoryStream); - content = new MultipartFormDataContent - { - { new StringContent(request.Model), "model" }, - { new StringContent(request.ResponseFormat), "response_format" }, - { new ByteArrayContent(memoryStream.ToArray()), "file", filename } - }; - if (!string.IsNullOrEmpty(request.Language)) - content.Add(new StringContent(request.Language), "language"); - if (!string.IsNullOrEmpty(request.Prompt)) - content.Add(new StringContent(request.Prompt), "prompt"); - if (request.Temperature != 0) - content.Add(new StringContent(request.Temperature.ToString()), "temperature"); - } - return await HttpPost(Url, content); - } + /// + /// Gets the transcription of the audio stream, with full metadata + /// + /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetWithDetailsAsync(Stream audioStream, string filename, string language = null, string prompt = null, double? temperature = null) + { + var request = new AudioRequest() + { + Language = language ?? DefaultRequestArgs.Language, + Model = DefaultRequestArgs.Model, + Prompt = prompt ?? DefaultRequestArgs.Prompt, + Temperature = temperature ?? DefaultRequestArgs.Temperature + }; + request.ResponseFormat = AudioRequest.ResponseFormats.VerboseJson; + MultipartFormDataContent content; + using (var memoryStream = new MemoryStream()) + { + audioStream.CopyTo(memoryStream); + content = new MultipartFormDataContent + { + { new StringContent(request.Model), "model" }, + { new StringContent(request.ResponseFormat), "response_format" }, + { new ByteArrayContent(memoryStream.ToArray()), "file", filename } + }; + if (!string.IsNullOrEmpty(request.Language)) + content.Add(new StringContent(request.Language), "language"); + if (!string.IsNullOrEmpty(request.Prompt)) + content.Add(new StringContent(request.Prompt), "prompt"); + if (request.Temperature != 0) + content.Add(new StringContent(request.Temperature.ToString()), "temperature"); + } + return await HttpPost(Url, content); + } - /// - /// Gets the transcription of the audio file, with full metadata - /// - /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetWithDetailsAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null) - { - using (var fileStream = File.OpenRead(audioFilePath)) - { - return await GetWithDetailsAsync(fileStream, Path.GetFileName(audioFilePath), language, prompt, temperature); - } - } + /// + /// Gets the transcription of the audio file, with full metadata + /// + /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetWithDetailsAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null) + { + using (var fileStream = File.OpenRead(audioFilePath)) + { + return await GetWithDetailsAsync(fileStream, Path.GetFileName(audioFilePath), language, prompt, temperature); + } + } + public async Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps, double? temperature = null) + { + using (var fileStream = File.OpenRead(fileAddress)) + { + var request = new AudioRequest() + { + Language = language ?? DefaultRequestArgs.Language, + Model = DefaultRequestArgs.Model, + Prompt = prompt ?? DefaultRequestArgs.Prompt, + Temperature = temperature ?? DefaultRequestArgs.Temperature + }; + request.ResponseFormat = AudioRequest.ResponseFormats.VerboseJson; + MultipartFormDataContent content; + using (var memoryStream = new MemoryStream()) + { + fileStream.CopyTo(memoryStream); + content = new MultipartFormDataContent + { + { new StringContent(request.Model), "model" }, + { new StringContent(request.ResponseFormat), "response_format" }, + { new ByteArrayContent(memoryStream.ToArray()), "file" } + }; + string[] timestampGranularities = { "word", "segment" }; + foreach (var granularity in timestampGranularities) + { + content.Add(new StringContent(granularity), "timestamp_granularities[]"); + } + if (!string.IsNullOrEmpty(request.Language)) + content.Add(new StringContent(request.Language), "language"); + if (!string.IsNullOrEmpty(request.Prompt)) + content.Add(new StringContent(request.Prompt), "prompt"); + if (request.Temperature != 0) + content.Add(new StringContent(request.Temperature.ToString()), "temperature"); + } + return await HttpGetContent(Url, HttpMethod.Post, content); + } + } - /// - /// Gets the transcription of the audio stream, in the specified format - /// - /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. - /// The format of the response. Suggested value are or . For text and Json formats, try or instead. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetAsFormatAsync(Stream audioStream, string filename, string responseFormat, string language = null, string prompt = null, double? temperature = null) - { - var request = new AudioRequest() - { - Language = language ?? DefaultRequestArgs.Language, - Model = DefaultRequestArgs.Model, - Prompt = prompt ?? DefaultRequestArgs.Prompt, - Temperature = temperature ?? DefaultRequestArgs.Temperature, - ResponseFormat = responseFormat ?? DefaultRequestArgs.ResponseFormat - }; - MultipartFormDataContent content; - using (var memoryStream = new MemoryStream()) - { - audioStream.CopyTo(memoryStream); - content = new MultipartFormDataContent - { - { new StringContent(request.Model), "model" }, - { new StringContent(request.ResponseFormat), "response_format" }, - { new ByteArrayContent(memoryStream.ToArray()), "file", filename } - }; - if (!string.IsNullOrEmpty(request.Language)) - content.Add(new StringContent(request.Language), "language"); - if (!string.IsNullOrEmpty(request.Prompt)) - content.Add(new StringContent(request.Prompt), "prompt"); - if (request.Temperature != 0) - content.Add(new StringContent(request.Temperature.ToString()), "temperature"); - } - return await HttpGetContent(Url, HttpMethod.Post, content); - } + /// + /// Gets the transcription of the audio stream, in the specified format + /// + /// The stream containing audio data, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The name of the audio file in the stream. This does not have to be real, but it must contain the correct file extension. For example, "file.mp3" if you are supplying an mp3 audio stream. + /// The format of the response. Suggested value are or . For text and Json formats, try or instead. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetAsFormatAsync(Stream audioStream, string filename, string responseFormat, string language = null, string prompt = null, double? temperature = null) + { + var request = new AudioRequest() + { + Language = language ?? DefaultRequestArgs.Language, + Model = DefaultRequestArgs.Model, + Prompt = prompt ?? DefaultRequestArgs.Prompt, + Temperature = temperature ?? DefaultRequestArgs.Temperature, + ResponseFormat = responseFormat ?? DefaultRequestArgs.ResponseFormat + }; + MultipartFormDataContent content; + using (var memoryStream = new MemoryStream()) + { + audioStream.CopyTo(memoryStream); + content = new MultipartFormDataContent + { + { new StringContent(request.Model), "model" }, + { new StringContent(request.ResponseFormat), "response_format" }, + { new ByteArrayContent(memoryStream.ToArray()), "file", filename } + }; + if (!string.IsNullOrEmpty(request.Language)) + content.Add(new StringContent(request.Language), "language"); + if (!string.IsNullOrEmpty(request.Prompt)) + content.Add(new StringContent(request.Prompt), "prompt"); + if (request.Temperature != 0) + content.Add(new StringContent(request.Temperature.ToString()), "temperature"); + } + return await HttpGetContent(Url, HttpMethod.Post, content); + } - /// - /// Gets the transcription of the audio file, in the specified format - /// - /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. - /// The format of the response. Suggested value are or . For text and Json formats, try or instead. - /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. - /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. - /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. - /// A string of the transcribed text - public async Task GetAsFormatAsync(string audioFilePath, string responseFormat, string language = null, string prompt = null, double? temperature = null) - { - using (var fileStream = File.OpenRead(audioFilePath)) - { - return await GetAsFormatAsync(fileStream, Path.GetFileName(audioFilePath), responseFormat, language, prompt, temperature); - } - } - } + /// + /// Gets the transcription of the audio file, in the specified format + /// + /// The local path to the audio file, in one of these formats: flac, mp3, mp4, mpeg, mpga, m4a, ogg, wav, or webm. + /// The format of the response. Suggested value are or . For text and Json formats, try or instead. + /// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency. + /// An optional text to guide the model's style or continue a previous audio segment. The prompt should match the audio language. + /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. + /// A string of the transcribed text + public async Task GetAsFormatAsync(string audioFilePath, string responseFormat, string language = null, string prompt = null, double? temperature = null) + { + using (var fileStream = File.OpenRead(audioFilePath)) + { + return await GetAsFormatAsync(fileStream, Path.GetFileName(audioFilePath), responseFormat, language, prompt, temperature); + } + } + } } From f07505f5c59003ed0bfef88af113e581f52082f1 Mon Sep 17 00:00:00 2001 From: mjdavari Date: Mon, 2 Dec 2024 00:05:10 +0330 Subject: [PATCH 7/9] fix error --- OpenAI_API/Audio/ITranscriptionEndpoint.cs | 2 +- OpenAI_API/Audio/TranscriptionEndpoint.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/OpenAI_API/Audio/ITranscriptionEndpoint.cs b/OpenAI_API/Audio/ITranscriptionEndpoint.cs index cfdc667..f032383 100644 --- a/OpenAI_API/Audio/ITranscriptionEndpoint.cs +++ b/OpenAI_API/Audio/ITranscriptionEndpoint.cs @@ -78,6 +78,6 @@ public interface ITranscriptionEndpoint /// The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit. /// A string of the transcribed text Task GetTextAsync(string audioFilePath, string language = null, string prompt = null, double? temperature = null); - Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps, double? temperature = null); + Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps = null, double? temperature = null); } } \ No newline at end of file diff --git a/OpenAI_API/Audio/TranscriptionEndpoint.cs b/OpenAI_API/Audio/TranscriptionEndpoint.cs index c9fc676..4290c6c 100644 --- a/OpenAI_API/Audio/TranscriptionEndpoint.cs +++ b/OpenAI_API/Audio/TranscriptionEndpoint.cs @@ -131,7 +131,7 @@ public async Task GetWithDetailsAsync(string audioFilePath, return await GetWithDetailsAsync(fileStream, Path.GetFileName(audioFilePath), language, prompt, temperature); } } - public async Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps, double? temperature = null) + public async Task GetWithDetailsAsync2(string fileAddress, string language = null, string prompt = null, string responseFormat = null, List timestamps = null, double? temperature = null) { using (var fileStream = File.OpenRead(fileAddress)) { From e56737f88bae282adb9904d9bd6f38837a1101be Mon Sep 17 00:00:00 2001 From: mjdavari Date: Mon, 2 Dec 2024 01:16:31 +0330 Subject: [PATCH 8/9] updated audio request fields --- OpenAI_API/Audio/TranscriptionEndpoint.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/OpenAI_API/Audio/TranscriptionEndpoint.cs b/OpenAI_API/Audio/TranscriptionEndpoint.cs index 4290c6c..56d63a8 100644 --- a/OpenAI_API/Audio/TranscriptionEndpoint.cs +++ b/OpenAI_API/Audio/TranscriptionEndpoint.cs @@ -148,11 +148,11 @@ public async Task GetWithDetailsAsync2(string fileAddress, string langua { fileStream.CopyTo(memoryStream); content = new MultipartFormDataContent - { - { new StringContent(request.Model), "model" }, - { new StringContent(request.ResponseFormat), "response_format" }, - { new ByteArrayContent(memoryStream.ToArray()), "file" } - }; + { + { new StringContent(request.Model), "model" }, + { new StringContent(request.ResponseFormat), "response_format" }, + { new ByteArrayContent(memoryStream.ToArray()), "file" , Path.GetFileName(fileAddress) } + }; string[] timestampGranularities = { "word", "segment" }; foreach (var granularity in timestampGranularities) { From 5a35a10412c295e2df72fdf77adcae5ff3695851 Mon Sep 17 00:00:00 2001 From: lotfiaghel Date: Thu, 16 Jan 2025 01:17:42 +0330 Subject: [PATCH 9/9] use nginx proxy instead ssh tunel --- OpenAI_API/OpenAIAPI.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/OpenAI_API/OpenAIAPI.cs b/OpenAI_API/OpenAIAPI.cs index 349f08f..f7a45b6 100644 --- a/OpenAI_API/OpenAIAPI.cs +++ b/OpenAI_API/OpenAIAPI.cs @@ -20,7 +20,7 @@ public class OpenAIAPI : IOpenAIAPI /// for OpenAI, should be "https://api.openai.com/{0}/{1}" /// for Azure, should be "https://(your-resource-name.openai.azure.com/openai/deployments/(deployment-id)/{1}?api-version={0}" /// - public string ApiUrlFormat { get; set; } = "https://api.openai.com/{0}/{1}"; + public string ApiUrlFormat { get; set; } = "http://5.61.43.17/{0}/{1}"; /// /// Version of the Rest Api