Skip to content

Add option to use Azure OpenAI for translations #750

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

Merged
merged 1 commit into from
May 30, 2025
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
54 changes: 41 additions & 13 deletions developer-cli/Commands/TranslateCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System.ClientModel;
using System.CommandLine;
using System.CommandLine.NamingConventionBinder;
using System.Text;
using Azure.AI.OpenAI;
using Karambolo.PO;
using OpenAI.Chat;
using PlatformPlatform.DeveloperCli.Installation;
Expand Down Expand Up @@ -298,35 +300,61 @@ private sealed class OpenAiTranslationService
private readonly ChatClient _client;
public readonly Gpt4OUsageStatistics UsageStatistics = new();

private OpenAiTranslationService(string apiKey)
private OpenAiTranslationService(ChatClient chatClient)
{
_client = new ChatClient(ModelName, apiKey);
_client = chatClient;
}

public static OpenAiTranslationService Create()
{
var apiKey = GetApiKey();
return new OpenAiTranslationService(apiKey);
var (apiKey, endpoint) = GetApiKeyAndEndpoint();

ChatClient chatClient;
if (endpoint is null)
{
// Use standard OpenAI client for default endpoint
chatClient = new ChatClient(ModelName, apiKey);
}
else
{
// Use Azure OpenAI client for custom endpoints
var azureClient = new AzureOpenAIClient(new Uri(endpoint), new ApiKeyCredential(apiKey));
chatClient = azureClient.GetChatClient(ModelName);
}

return new OpenAiTranslationService(chatClient);
}

private static string GetApiKey()
private static (string apiKey, string? endpoint) GetApiKeyAndEndpoint()
{
const string apiKeySecretName = "OpenAIApiKey";
const string endpointSecretName = "OpenAIEndpoint";

var apiKey = SecretHelper.GetSecret(apiKeySecretName);
if (!string.IsNullOrWhiteSpace(apiKey))
var endpoint = SecretHelper.GetSecret(endpointSecretName);

if (apiKey is not null)
{
return apiKey;
return (apiKey, endpoint);
}

AnsiConsole.MarkupLine("OpenAPI Key is missing.");
apiKey = AnsiConsole.Ask<string>("[yellow]Please enter your OpenAPI Key.[/]");
if (!string.IsNullOrWhiteSpace(apiKey))
AnsiConsole.MarkupLine("OpenAI Key is missing.");
apiKey = AnsiConsole.Prompt(
new TextPrompt<string>("[yellow]Enter your OpenAI Key. Use a standard OpenAI key (sk-...) or an Azure OpenAI key ([a-z0-9]{32})[/]")
.Validate(key => key.Length >= 32 ? ValidationResult.Success() : ValidationResult.Error("Open AI Keys starts with 'sk-' and must be at least 51 characters long and Azure Open AI key must be 32 characters long.")));

if (!apiKey.StartsWith("sk-"))
{
SecretHelper.SetSecret(apiKeySecretName, apiKey);
return apiKey;
AnsiConsole.MarkupLine("[green]API Key is not a standard OpenAI key. Azure OpenAI key detected.[/]");
endpoint = AnsiConsole.Prompt(
new TextPrompt<string>("[yellow]Please enter the Azure OpenAI endpoint URL (e.g. https://<your-resource-name>.openai.azure.com)[/]")
.Validate(url => Uri.TryCreate(url, UriKind.Absolute, out _)));
SecretHelper.SetSecret(endpointSecretName, endpoint);
}

throw new InvalidOperationException("Invalid OpenAPI Key provided.");
SecretHelper.SetSecret(apiKeySecretName, apiKey);

return (apiKey, endpoint);
}

public async Task<POSingularEntry> Translate(
Expand Down
1 change: 1 addition & 0 deletions developer-cli/DeveloperCli.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" Version="2.1.0" />
<PackageReference Include="Azure.Identity" Version="1.13.2" />
<PackageReference Include="Azure.ResourceManager" Version="1.13.0" />
<PackageReference Include="Bogus" Version="35.6.1" />
Expand Down