11from typing import List , Optional
22
3+ from azure .identity import DefaultAzureCredential , get_bearer_token_provider
34from llama_index .embeddings .azure_openai import AzureOpenAIEmbedding
45
56from docs2vecs .subcommands .indexer .config import Config
@@ -15,12 +16,32 @@ def az_ada002_embeddings(self, content: str, chunk_id=None):
1516 self .logger .debug (
1617 f"Requesting embedding for chunk_id={ chunk_id } , content_length={ len (content )} chars"
1718 )
18- embed_model = AzureOpenAIEmbedding (
19- deployment_name = self ._config ["deployment_name" ],
20- api_key = self ._config ["api_key" ],
21- azure_endpoint = self ._config ["endpoint" ],
22- api_version = self ._config ["api_version" ],
23- )
19+
20+ api_key = self ._config .get ("api_key" )
21+ if api_key :
22+ self .logger .debug ("Using API key authentication" )
23+ embed_model = AzureOpenAIEmbedding (
24+ deployment_name = self ._config ["deployment_name" ],
25+ api_key = api_key ,
26+ azure_endpoint = self ._config ["endpoint" ],
27+ api_version = self ._config ["api_version" ],
28+ )
29+ else :
30+ self .logger .debug (
31+ "No api_key provided, using Azure AD token authentication (DefaultAzureCredential)"
32+ )
33+ credential = DefaultAzureCredential ()
34+ token_provider = get_bearer_token_provider (
35+ credential , "https://cognitiveservices.azure.com/.default"
36+ )
37+ embed_model = AzureOpenAIEmbedding (
38+ deployment_name = self ._config ["deployment_name" ],
39+ azure_ad_token_provider = token_provider ,
40+ azure_endpoint = self ._config ["endpoint" ],
41+ api_version = self ._config ["api_version" ],
42+ use_azure_ad = True ,
43+ )
44+
2445 embedding = embed_model .get_query_embedding (content )
2546 self .logger .debug (
2647 f"Successfully received embedding for chunk_id={ chunk_id } , embedding_dim={ len (embedding ) if embedding else 0 } "
@@ -47,4 +68,4 @@ def run(self, input: Optional[List[Document]] = None) -> Optional[List[Document]
4768 chunk .content , chunk_id = chunk .chunk_id
4869 )
4970
50- return input
71+ return input
0 commit comments