Skip to content

Commit

Permalink
#149 feat - add new functionality for Azure and OpenAI generation
Browse files Browse the repository at this point in the history
  • Loading branch information
guibranco committed Jan 6, 2025
1 parent 2fcfde1 commit 65fde94
Showing 1 changed file with 63 additions and 48 deletions.
111 changes: 63 additions & 48 deletions Src/AiCommitMessage/Services/GenerateCommitMessageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,57 +100,11 @@ bool debug

if (model.Equals("llama-3-1-405B-Instruct", StringComparison.OrdinalIgnoreCase))
{
var endpoint = new Uri(EnvironmentLoader.LoadLlamaApiUrl());
var credential = new AzureKeyCredential(EnvironmentLoader.LoadLlamaApiKey());

var client = new ChatCompletionsClient(
endpoint,
credential,
new AzureAIInferenceClientOptions()
);

var requestOptions = new ChatCompletionsOptions
{
Messages =
{
new ChatRequestSystemMessage(Constants.SystemMessage),
new ChatRequestUserMessage(formattedMessage),
},
Temperature = 1.0f,
NucleusSamplingFactor = 1.0f,
MaxTokens = 1000,
Model = "Meta-Llama-3.1-405B-Instruct",
};

var response = client.Complete(requestOptions);
text = response.Value.Content;
text = GenerateUsingAzureAi(formattedMessage);
}
else if (model.Equals("gpt-4o-mini", StringComparison.OrdinalIgnoreCase))
{
try
{
var apiUrl = EnvironmentLoader.LoadOpenAiApiUrl();
var apiKey = EnvironmentLoader.LoadOpenAiApiKey();

var client = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(apiKey),
new OpenAIClientOptions { Endpoint = new Uri(apiUrl) }
);

var chatCompletion = client.CompleteChat(
new SystemChatMessage(Constants.SystemMessage),
new UserChatMessage(formattedMessage)
);

text = chatCompletion.Value.Content[0].Text;
}
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
{
throw new InvalidOperationException(
"⚠️ OpenAI API is currently unavailable. Please try again later."
);
}
text = GenerateUsingOpenAi(formattedMessage);
}
else
{
Expand All @@ -169,6 +123,67 @@ bool debug
return text;
}

private static string GenerateUsingAzureAi(string formattedMessage)
{
string text;
var endpoint = new Uri(EnvironmentLoader.LoadLlamaApiUrl());
var credential = new AzureKeyCredential(EnvironmentLoader.LoadLlamaApiKey());

var client = new ChatCompletionsClient(
endpoint,
credential,
new AzureAIInferenceClientOptions()
);

var requestOptions = new ChatCompletionsOptions
{
Messages =
{
new ChatRequestSystemMessage(Constants.SystemMessage),
new ChatRequestUserMessage(formattedMessage),
},
Temperature = 1.0f,
NucleusSamplingFactor = 1.0f,
MaxTokens = 1000,
Model = "Meta-Llama-3.1-405B-Instruct",
};

var response = client.Complete(requestOptions);
text = response.Value.Content;
return text;

Check notice on line 153 in Src/AiCommitMessage/Services/GenerateCommitMessageService.cs

View check run for this annotation

codefactor.io / CodeFactor

Src/AiCommitMessage/Services/GenerateCommitMessageService.cs#L47-L153

Complex Method
}

private static string GenerateUsingOpenAi(string formattedMessage)
{
string text;
try
{
var apiUrl = EnvironmentLoader.LoadOpenAiApiUrl();
var apiKey = EnvironmentLoader.LoadOpenAiApiKey();

var client = new ChatClient(
"gpt-4o-mini",
new ApiKeyCredential(apiKey),
new OpenAIClientOptions { Endpoint = new Uri(apiUrl) }
);

var chatCompletion = client.CompleteChat(
new SystemChatMessage(Constants.SystemMessage),
new UserChatMessage(formattedMessage)
);

text = chatCompletion.Value.Content[0].Text;
}
catch (Exception ex) when (ex is HttpRequestException || ex is TaskCanceledException)
{
throw new InvalidOperationException(
"⚠️ OpenAI API is currently unavailable. Please try again later."
);
}

return text;
}

private static string ProcessGeneratedMessage(string text, string branch, string message)
{
if (text.Length >= 7 && text[..7] == "type - ")
Expand Down

0 comments on commit 65fde94

Please sign in to comment.