1
1
2
+ using System . Net . Http . Headers ;
2
3
using System . Net . Http . Json ;
3
4
using System . Text . Json ;
5
+ using Azure . Core ;
6
+ using Azure . Identity ;
4
7
using Redis . OM . Contracts ;
5
8
using Redis . OM . Modeling ;
6
9
@@ -11,7 +14,8 @@ namespace Redis.OM.Vectorizers;
11
14
/// </summary>
12
15
public class AzureOpenAIVectorizer : IVectorizer < string >
13
16
{
14
- private readonly string _apiKey ;
17
+ private readonly TokenCredential ? _tokenCredential ;
18
+ private readonly string ? _apiKey ;
15
19
private readonly string _resourceName ;
16
20
private readonly string _deploymentName ;
17
21
@@ -28,6 +32,22 @@ public AzureOpenAIVectorizer(string apiKey, string resourceName, string deployme
28
32
_resourceName = resourceName ;
29
33
_deploymentName = deploymentName ;
30
34
Dim = dim ;
35
+ _tokenCredential = new DefaultAzureCredential ( ) ;
36
+ }
37
+
38
+ /// <summary>
39
+ /// Initialize vectorizer
40
+ /// </summary>
41
+ /// <param name="resourceName">The Azure Resource's name</param>
42
+ /// <param name="deploymentName">The Azure deployment name</param>
43
+ /// <param name="dim">The dimensions of the model addressed by this resource/deployment.</param>
44
+ public AzureOpenAIVectorizer ( string resourceName , string deploymentName , int dim )
45
+ {
46
+ _apiKey = null ;
47
+ _resourceName = resourceName ;
48
+ _deploymentName = deploymentName ;
49
+ Dim = dim ;
50
+ _tokenCredential = new DefaultAzureCredential ( ) ;
31
51
}
32
52
33
53
/// <inheritdoc />
@@ -37,9 +57,9 @@ public AzureOpenAIVectorizer(string apiKey, string resourceName, string deployme
37
57
public int Dim { get ; }
38
58
39
59
/// <inheritdoc />
40
- public byte [ ] Vectorize ( string str ) => GetFloats ( str , _resourceName , _deploymentName , _apiKey ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ;
60
+ public byte [ ] Vectorize ( string str ) => GetFloats ( str , _resourceName , _deploymentName , _apiKey , _tokenCredential ) . SelectMany ( BitConverter . GetBytes ) . ToArray ( ) ;
41
61
42
- internal static float [ ] GetFloats ( string s , string resourceName , string deploymentName , string apiKey )
62
+ internal static float [ ] GetFloats ( string s , string resourceName , string deploymentName , string ? apiKey , TokenCredential ? azureCredentials )
43
63
{
44
64
var client = Configuration . Instance . Client ;
45
65
var requestContent = JsonContent . Create ( new { input = s } ) ;
@@ -49,9 +69,24 @@ internal static float[] GetFloats(string s, string resourceName, string deployme
49
69
Method = HttpMethod . Post ,
50
70
RequestUri = new Uri (
51
71
$ "https://{ resourceName } .openai.azure.com/openai/deployments/{ deploymentName } /embeddings?api-version=2023-05-15") ,
52
- Content = requestContent ,
53
- Headers = { { "api-key" , apiKey } }
72
+ Content = requestContent
54
73
} ;
74
+
75
+ if ( string . IsNullOrEmpty ( apiKey ) && azureCredentials != null )
76
+ {
77
+ var scope = "https://cognitiveservices.azure.com/.default" ;
78
+ AccessToken token = azureCredentials . GetToken ( new TokenRequestContext ( new [ ] { scope } ) , CancellationToken . None ) ;
79
+ request . Headers . Authorization = new AuthenticationHeaderValue ( "Bearer" , token . Token ) ;
80
+ }
81
+ else if ( ! string . IsNullOrEmpty ( apiKey ) )
82
+ {
83
+ request . Headers . Add ( "api-key" , apiKey ) ;
84
+ }
85
+ else
86
+ {
87
+ throw new InvalidOperationException ( "Either apiKey or azureCredentials must be provided." ) ;
88
+ }
89
+
55
90
56
91
var res = client . Send ( request ) ;
57
92
if ( ! res . IsSuccessStatusCode )
0 commit comments