forked from microsoft/kernel-memory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureOpenAIEmbeddingsConfig.cs
More file actions
90 lines (75 loc) · 2.73 KB
/
AzureOpenAIEmbeddingsConfig.cs
File metadata and controls
90 lines (75 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright (c) Microsoft. All rights reserved.
using System.Text.Json.Serialization;
using KernelMemory.Core.Config.Enums;
using KernelMemory.Core.Config.Validation;
namespace KernelMemory.Core.Config.Embeddings;
/// <summary>
/// Azure OpenAI embeddings provider configuration
/// </summary>
public sealed class AzureOpenAIEmbeddingsConfig : EmbeddingsConfig
{
/// <inheritdoc />
[JsonIgnore]
public override EmbeddingsTypes Type => EmbeddingsTypes.AzureOpenAI;
/// <summary>
/// Model name (e.g., "text-embedding-ada-002")
/// </summary>
[JsonPropertyName("model")]
public string Model { get; set; } = string.Empty;
/// <summary>
/// Azure OpenAI endpoint (e.g., "https://myservice.openai.azure.com/")
/// </summary>
[JsonPropertyName("endpoint")]
public string Endpoint { get; set; } = string.Empty;
/// <summary>
/// Azure OpenAI API key (optional if using managed identity)
/// </summary>
[JsonPropertyName("apiKey")]
public string? ApiKey { get; set; }
/// <summary>
/// Deployment name in Azure OpenAI
/// </summary>
[JsonPropertyName("deployment")]
public string Deployment { get; set; } = string.Empty;
/// <summary>
/// Use Azure Managed Identity for authentication
/// </summary>
[JsonPropertyName("useManagedIdentity")]
public bool UseManagedIdentity { get; set; }
/// <inheritdoc />
public override void Validate(string path)
{
if (string.IsNullOrWhiteSpace(this.Model))
{
throw new ConfigException($"{path}.Model", "Model name is required");
}
if (string.IsNullOrWhiteSpace(this.Endpoint))
{
throw new ConfigException($"{path}.Endpoint", "Azure OpenAI endpoint is required");
}
if (!Uri.TryCreate(this.Endpoint, UriKind.Absolute, out _))
{
throw new ConfigException($"{path}.Endpoint",
$"Invalid Azure OpenAI endpoint: {this.Endpoint}");
}
if (string.IsNullOrWhiteSpace(this.Deployment))
{
throw new ConfigException($"{path}.Deployment", "Deployment name is required");
}
var hasApiKey = !string.IsNullOrWhiteSpace(this.ApiKey);
if (!hasApiKey && !this.UseManagedIdentity)
{
throw new ConfigException(path,
"Azure OpenAI requires either ApiKey or UseManagedIdentity");
}
if (hasApiKey && this.UseManagedIdentity)
{
throw new ConfigException(path,
"Azure OpenAI: specify either ApiKey or UseManagedIdentity, not both");
}
if (this.BatchSize < 1)
{
throw new ConfigException($"{path}.BatchSize", "BatchSize must be >= 1");
}
}
}