Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added audio endpoint #130

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions OpenAI_API/Audio/AudioEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using System.Net.Http;
using System.Threading.Tasks;

namespace OpenAI_API.Audio
{
/// <summary>
/// You can use this endpoint for audio transcription or translation.
/// </summary>
public class AudioEndpoint : EndpointBase, IAudioEndpoint
{
/// <summary>
/// Creates audio endpoint object.
/// </summary>
/// <param name="api"></param>
public AudioEndpoint(OpenAIAPI api) : base(api)
{
}

/// <summary>
/// Audio endpoint.
/// </summary>
protected override string Endpoint { get { return "audio"; } }

/// <summary>
/// Sends transcript request to openai and returns verbose_json result.
/// </summary>
public Task<TranscriptionVerboseJsonResult> CreateTranscriptionAsync(TranscriptionRequest request)
{
return PostAudioAsync($"{Url}/transcriptions", request);
}

/// <summary>
/// Translates audio into English.
/// </summary>
public Task<TranscriptionVerboseJsonResult> CreateTranslationAsync(TranslationRequest request)
{
return PostAudioAsync($"{Url}/translations",new TranscriptionRequest {
File = request.File,
Model = request.Model,
Prompt = request.Prompt,
ResponseFormat = request.ResponseFormat,
Temperature = request.Temperature}
);
}

private Task<TranscriptionVerboseJsonResult> PostAudioAsync(string url, TranscriptionRequest request)
{
var content = new MultipartFormDataContent();

var fileContent = new StreamContent(request.File.File);
fileContent.Headers.ContentLength = request.File.ContentLength;
fileContent.Headers.ContentType =
new System.Net.Http.Headers.MediaTypeHeaderValue(request.File.ContentType);

content.Add(fileContent, "file", request.File.Name);
content.Add(new StringContent(request.Model), "model");

if (!IsNullOrWhiteSpace(request.Prompt))
content.Add(new StringContent(request.Prompt), "prompt");

if (!IsNullOrWhiteSpace(request.ResponseFormat))
content.Add(new StringContent(request.ResponseFormat), "response_format");

if (!request.Temperature.HasValue)
content.Add(new StringContent(request.Temperature.ToString()), "temperature");

if (!IsNullOrWhiteSpace(request.Language))
content.Add(new StringContent(request.Language), "language");

return HttpPost<TranscriptionVerboseJsonResult>(url, postData: content);
}

private bool IsNullOrWhiteSpace(string str) => string.IsNullOrWhiteSpace(str);
}
}
30 changes: 30 additions & 0 deletions OpenAI_API/Audio/AudioFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.IO;

namespace OpenAI_API.Audio
{
/// <summary>
/// Audio file object for transcript and translate requests.
/// </summary>
public class AudioFile
{
/// <summary>
/// Stream of the file.
/// </summary>
public Stream File { get; set; }

/// <summary>
/// Content length of the file
/// </summary>
public long ContentLength { get { return File.Length; } }

/// <summary>
/// Type of audio file.Must be mp3, mp4, mpeg, mpga, m4a, wav, or webm.
/// </summary>
public string ContentType { get; set; }

/// <summary>
/// Full name of the file. such as test.mp3
/// </summary>
public string Name { get; set; }
}
}
21 changes: 21 additions & 0 deletions OpenAI_API/Audio/IAudioEndpoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System.Threading.Tasks;

namespace OpenAI_API.Audio
{
/// <summary>
/// You can use this endpoint for audio transcription or translation.
/// </summary>
public interface IAudioEndpoint
{
/// <summary>
/// Sends transcript request to openai and returns verbose_json result.
/// </summary>
Task<TranscriptionVerboseJsonResult> CreateTranscriptionAsync(TranscriptionRequest request);


/// <summary>
/// Translates audio into into English.
/// </summary>
public Task<TranscriptionVerboseJsonResult> CreateTranslationAsync(TranslationRequest request);
}
}
14 changes: 14 additions & 0 deletions OpenAI_API/Audio/TranscriptionRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace OpenAI_API.Audio
{
/// <summary>
/// Transcribes audio into the input language.
/// </summary>
public class TranscriptionRequest:TranslationRequest
{
/// <summary>
/// The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.Visit to list ISO-639-1 formats <see href="https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes"/>
/// </summary>
public string Language { get; set; }

}
}
123 changes: 123 additions & 0 deletions OpenAI_API/Audio/TranscriptionResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace OpenAI_API.Audio
{
/// <summary>
/// Default format for transcript result.
/// </summary>
public class TranscriptionResult:ApiResultBase
{
/// <summary>
/// Text of the transcript result.
/// </summary>
public string Text { get; set; }
}

/// <summary>
/// Transcription format for vtt results.
/// </summary>
public class TranscriptionVerboseJsonResult:TranscriptionResult
{
/// <summary>
/// Task type. Translate or transcript.
/// </summary>
[JsonProperty("task")]
public string Task { get; set; }

/// <summary>
/// Language of the audio.
/// </summary>
[JsonProperty("language")]
public string Language { get; set; }

/// <summary>
/// Audio duration.
/// </summary>
[JsonProperty("duration")]
public float Duration { get; set; }

/// <summary>
/// Audio segments.
/// </summary>
[JsonProperty("segments")]
public List<TranscriptionSegment> Segments { get; set; }

/// <summary>
/// Creates a verbose json result object.
/// </summary>
public TranscriptionVerboseJsonResult()
{
Segments = new List<TranscriptionSegment>();
}

}

/// <summary>
/// Segment of the transcript.
/// </summary>
public class TranscriptionSegment
{
/// <summary>
/// Segment id
/// </summary>
[JsonProperty("id")]
public int Id { get; set; }

/// <summary>
/// Start time.
/// </summary>
[JsonProperty("start")]
public float Start { get; set; }

/// <summary>
/// End time.
/// </summary>
[JsonProperty("end")]
public float End { get; set; }

/// <summary>
/// Segment text.
/// </summary>
[JsonProperty("text")]
public string Text { get; set; }

/// <summary>
/// Text tokens.
/// </summary>
[JsonProperty("tokens")]
public int[] Tokens { get; set; }

/// <summary>
/// Temperature.
/// </summary>
[JsonProperty("temperature")]
public double Temperature { get; set; }

/// <summary>
/// Average log probabilities of the text.
/// </summary>
[JsonProperty("avg_logprob")]
public double AvgLogProb { get; set; }

/// <summary>
/// Compression ratio.
/// </summary>
[JsonProperty("compression_ratio")]
public double CompressionRation { get; set; }

/// <summary>
/// No speech probability.
/// </summary>
[JsonProperty("no_speech_prob")]
public double NoSpeechProb { get; set; }

/// <summary>
/// Transient.
/// </summary>
[JsonProperty("transient")]
public bool Transient { get; set; }
}


}
37 changes: 37 additions & 0 deletions OpenAI_API/Audio/TranslationRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System.IO;

namespace OpenAI_API.Audio
{
/// <summary>
/// Creates translation request object for translate audios to english language.
/// </summary>
public class TranslationRequest
{
/// <summary>
/// The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm
/// </summary>
public AudioFile File { get; set; }

/// <summary>
/// ID of the model to use. Only whisper-1 is currently available.
/// </summary>
public string Model { get; set; } = Models.Model.Whisper_1;

/// <summary>
/// An optional text to guide the model's style or continue a previous audio segment. The Prompt should match the audio language. Please review href="https://platform.openai.com/docs/guides/speech-to-text/prompting"/>
/// </summary>
public string Prompt { get; set; }

/// <summary>
/// The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
/// </summary>
public string ResponseFormat { get; set; } = "verbose_json";

/// <summary>
/// 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.
/// </summary>
public float? Temperature { get; set; }
}


}
10 changes: 4 additions & 6 deletions OpenAI_API/Chat/ChatResult.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;

namespace OpenAI_API.Chat
{
/// <summary>
/// Represents a result from calling the Chat API
/// </summary>
public class ChatResult : ApiResultBase
/// <summary>
/// Represents a result from calling the Chat API
/// </summary>
public class ChatResult : ApiResultBase
{
/// <summary>
/// The identifier of the result, which may be used during troubleshooting
Expand Down
18 changes: 9 additions & 9 deletions OpenAI_API/Chat/Conversation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public OpenAI_API.Models.Model Model
/// <summary>
/// After calling <see cref="GetResponseFromChatbotAsync"/>, this contains the full response object which can contain useful metadata like token usages, <see cref="ChatChoice.FinishReason"/>, etc. This is overwritten with every call to <see cref="GetResponseFromChatbotAsync"/> and only contains the most recent result.
/// </summary>
public ChatResult MostResentAPIResult { get; private set; }
public ChatResult MostRecentApiResult { get; private set; }

/// <summary>
/// Creates a new conversation with ChatGPT chat
Expand Down Expand Up @@ -71,7 +71,7 @@ public Conversation(ChatEndpoint endpoint, OpenAI_API.Models.Model model = null,
private List<ChatMessage> _Messages;

/// <summary>
/// Appends a <see cref="ChatMessage"/> to the chat hstory
/// Appends a <see cref="ChatMessage"/> to the chat history
/// </summary>
/// <param name="message">The <see cref="ChatMessage"/> to append to the chat history</param>
public void AppendMessage(ChatMessage message)
Expand All @@ -80,33 +80,33 @@ public void AppendMessage(ChatMessage message)
}

/// <summary>
/// Creates and appends a <see cref="ChatMessage"/> to the chat hstory
/// Creates and appends a <see cref="ChatMessage"/> to the chat history
/// </summary>
/// <param name="role">The <see cref="ChatMessageRole"/> for the message. Typically, a conversation is formatted with a system message first, followed by alternating user and assistant messages. See <see href="https://platform.openai.com/docs/guides/chat/introduction">the OpenAI docs</see> for more details about usage.</param>
/// <param name="content">The content of the message)</param>
public void AppendMessage(ChatMessageRole role, string content) => this.AppendMessage(new ChatMessage(role, content));

/// <summary>
/// Creates and appends a <see cref="ChatMessage"/> to the chat hstory with the Role of <see cref="ChatMessageRole.User"/>. The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
/// Creates and appends a <see cref="ChatMessage"/> to the chat history with the Role of <see cref="ChatMessageRole.User"/>. The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
/// </summary>
/// <param name="content">Text content generated by the end users of an application, or set by a developer as an instruction</param>
public void AppendUserInput(string content) => this.AppendMessage(new ChatMessage(ChatMessageRole.User, content));

/// <summary>
/// Creates and appends a <see cref="ChatMessage"/> to the chat hstory with the Role of <see cref="ChatMessageRole.User"/>. The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
/// Creates and appends a <see cref="ChatMessage"/> to the chat history with the Role of <see cref="ChatMessageRole.User"/>. The user messages help instruct the assistant. They can be generated by the end users of an application, or set by a developer as an instruction.
/// </summary>
/// <param name="userName">The name of the user in a multi-user chat</param>
/// <param name="content">Text content generated by the end users of an application, or set by a developer as an instruction</param>
public void AppendUserInputWithName(string userName, string content) => this.AppendMessage(new ChatMessage(ChatMessageRole.User, content) { Name = userName });


/// <summary>
/// Creates and appends a <see cref="ChatMessage"/> to the chat hstory with the Role of <see cref="ChatMessageRole.System"/>. The system message helps set the behavior of the assistant.
/// Creates and appends a <see cref="ChatMessage"/> to the chat history with the Role of <see cref="ChatMessageRole.System"/>. The system message helps set the behavior of the assistant.
/// </summary>
/// <param name="content">text content that helps set the behavior of the assistant</param>
public void AppendSystemMessage(string content) => this.AppendMessage(new ChatMessage(ChatMessageRole.System, content));
/// <summary>
/// Creates and appends a <see cref="ChatMessage"/> to the chat hstory with the Role of <see cref="ChatMessageRole.Assistant"/>. Assistant messages can be written by a developer to help give examples of desired behavior.
/// Creates and appends a <see cref="ChatMessage"/> to the chat history with the Role of <see cref="ChatMessageRole.Assistant"/>. Assistant messages can be written by a developer to help give examples of desired behavior.
/// </summary>
/// <param name="content">Text content written by a developer to help give examples of desired behavior</param>
public void AppendExampleChatbotOutput(string content) => this.AppendMessage(new ChatMessage(ChatMessageRole.Assistant, content));
Expand All @@ -123,7 +123,7 @@ public async Task<string> GetResponseFromChatbotAsync()
req.Messages = _Messages.ToList();

var res = await _endpoint.CreateChatCompletionAsync(req);
MostResentAPIResult = res;
MostRecentApiResult = res;

if (res.Choices.Count > 0)
{
Expand Down Expand Up @@ -201,7 +201,7 @@ public async IAsyncEnumerable<string> StreamResponseEnumerableFromChatbotAsync()
yield return deltaContent;
}
}
MostResentAPIResult = res;
MostRecentApiResult = res;
}

if (responseRole != null)
Expand Down
Loading