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

Develop #22

Merged
merged 3 commits into from
Oct 7, 2024
Merged
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
11 changes: 3 additions & 8 deletions Apps.AzueOpenAI/Actions/AudioActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,10 @@

namespace Apps.AzureOpenAI.Actions
{
public class AudioActions : BaseActions
public class AudioActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: BaseActions(invocationContext, fileManagementClient)
{
private readonly IFileManagementClient _fileManagementClient;

public AudioActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: base(invocationContext)
{
_fileManagementClient = fileManagementClient;
}
private readonly IFileManagementClient _fileManagementClient = fileManagementClient;

[Action("Create English translation", Description = "Generates a translation into English given an audio or " +
"video file (mp3, mp4, mpeg, mpga, m4a, wav, or webm).")]
Expand Down
100 changes: 98 additions & 2 deletions Apps.AzueOpenAI/Actions/Base/BaseActions.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,27 @@
using Azure;
using Apps.AzureOpenAI.Models.Dto;
using Apps.AzureOpenAI.Models.Entities;
using Apps.AzureOpenAI.Models.Requests.Chat;
using Apps.AzureOpenAI.Models.Responses.Chat;
using Azure;
using Azure.AI.OpenAI;
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Files;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using Blackbird.Xliff.Utils;
using Blackbird.Xliff.Utils.Extensions;
using Newtonsoft.Json;
using RestSharp;

namespace Apps.AzureOpenAI.Actions.Base;

public class BaseActions : BaseInvocable
{
protected readonly OpenAIClient Client;
protected readonly string DeploymentName;
protected readonly IFileManagementClient FileManagementClient;

protected BaseActions(InvocationContext invocationContext)
protected BaseActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: base(invocationContext)
{
DeploymentName = InvocationContext.AuthenticationCredentialsProviders.First(x => x.KeyName == "deployment")
Expand All @@ -19,5 +30,90 @@ protected BaseActions(InvocationContext invocationContext)
new Uri(InvocationContext.AuthenticationCredentialsProviders.First(x => x.KeyName == "url").Value),
new AzureKeyCredential(InvocationContext.AuthenticationCredentialsProviders
.First(x => x.KeyName == "apiKey").Value));
FileManagementClient = fileManagementClient;
}

protected async Task<XliffDocument> DownloadXliffDocumentAsync(FileReference file)
{
var fileStream = await FileManagementClient.DownloadAsync(file);
var xliffMemoryStream = new MemoryStream();
await fileStream.CopyToAsync(xliffMemoryStream);
xliffMemoryStream.Position = 0;

var xliffDocument = xliffMemoryStream.ToXliffDocument();
if (xliffDocument.TranslationUnits.Count == 0)
{
throw new InvalidOperationException("The XLIFF file does not contain any translation units.");
}

return xliffDocument;
}

protected async Task<(string result, UsageDto usage)> ExecuteOpenAIRequestAsync(ExecuteOpenAIRequestParameters parameters)
{
var restClient = new RestClient(InvocationContext.AuthenticationCredentialsProviders
.First(x => x.KeyName == "url").Value);
var request = new RestRequest("/openai/deployments/" + DeploymentName + $"/chat/completions?api-version={parameters.ApiVersion}", Method.Post);

request.AddHeader("Content-Type", "application/json");
request.AddHeader("api-key", InvocationContext.AuthenticationCredentialsProviders
.First(x => x.KeyName == "apiKey").Value);

var body = new Dictionary<string, object>
{
{
"messages", new List<object>
{
new
{
role = "system",
content = parameters.SystemPrompt
},
new
{
role = "user",
content = parameters.Prompt
}
}
},
};

if(parameters.ResponseFormat != null)
{
body.Add("response_format", parameters.ResponseFormat);
}

if(parameters.ChatRequest?.Temperature != null)
{
body.Add("temperature", parameters.ChatRequest.Temperature.Value);
}

if(parameters.ChatRequest?.MaximumTokens != null)
{
body.Add("max_completion_tokens", parameters.ChatRequest.MaximumTokens.Value);
}

if(parameters.ChatRequest?.PresencePenalty != null)
{
body.Add("presence_penalty", parameters.ChatRequest.PresencePenalty.Value);
}

if(parameters.ChatRequest?.FrequencyPenalty != null)
{
body.Add("frequency_penalty", parameters.ChatRequest.FrequencyPenalty.Value);
}

request.AddJsonBody(body);

var response = await restClient.ExecuteAsync(request);

if (!response.IsSuccessStatusCode)
{
var error = JsonConvert.DeserializeObject<ErrorDto>(response.Content!)!;
throw new InvalidOperationException(error.ToString());
}

var chatResponse = JsonConvert.DeserializeObject<OpenAIResponseDto>(response.Content!)!;
return (chatResponse.Choices.First().Message.Content, new(chatResponse.Usage));
}
}
8 changes: 3 additions & 5 deletions Apps.AzueOpenAI/Actions/ChatActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,16 @@
using Blackbird.Applications.Sdk.Common.Invocation;
using Apps.AzureOpenAI.Actions.Base;
using Apps.AzureOpenAI.Utils;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using Newtonsoft.Json;
using TiktokenSharp;

namespace Apps.AzureOpenAI.Actions;

[ActionList]
public class ChatActions : BaseActions
public class ChatActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: BaseActions(invocationContext, fileManagementClient)
{
public ChatActions(InvocationContext invocationContext) : base(invocationContext)
{
}

#region Chat actions

[Action("Generate completion", Description = "Completes the given prompt")]
Expand Down
8 changes: 3 additions & 5 deletions Apps.AzueOpenAI/Actions/ImageActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
using Blackbird.Applications.Sdk.Common.Invocation;
using Apps.AzureOpenAI.Models.Requests.Image;
using Apps.AzureOpenAI.Models.Responses.Image;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;

namespace Apps.AzureOpenAI.Actions
{
[ActionList]
public class ImageActions : BaseActions
public class ImageActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: BaseActions(invocationContext, fileManagementClient)
{
public ImageActions(InvocationContext invocationContext) : base(invocationContext)
{
}

[Action("Generate image", Description = "Generates an image based on a prompt")]
public async Task<ImageResponse> GenerateImage([ActionParameter] ImageRequest input)
{
Expand Down
8 changes: 3 additions & 5 deletions Apps.AzueOpenAI/Actions/TextAnalysisActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
using Blackbird.Applications.Sdk.Common;
using Blackbird.Applications.Sdk.Common.Actions;
using Blackbird.Applications.Sdk.Common.Invocation;
using Blackbird.Applications.SDK.Extensions.FileManagement.Interfaces;
using TiktokenSharp;

namespace Apps.AzureOpenAI.Actions;

[ActionList]
public class TextAnalysisActions : BaseActions
public class TextAnalysisActions(InvocationContext invocationContext, IFileManagementClient fileManagementClient)
: BaseActions(invocationContext, fileManagementClient)
{
public TextAnalysisActions(InvocationContext invocationContext) : base(invocationContext)
{
}

[Action("Create embedding", Description = "Generate an embedding for a text provided. An embedding is a list of " +
"floating point numbers that captures semantic information about the " +
"text that it represents.")]
Expand Down
Loading