55import pyodbc
66import pandas as pd
77from datetime import datetime , timedelta
8+ from urllib .parse import urlparse
89from azure .identity import get_bearer_token_provider
910from azure .keyvault .secrets import SecretClient
1011from azure .search .documents import SearchClient
1112from azure .search .documents .indexes import SearchIndexClient
1213from azure .storage .filedatalake import DataLakeServiceClient
1314from azure .ai .inference import ChatCompletionsClient , EmbeddingsClient
15+ from azure .ai .inference .models import SystemMessage , UserMessage
1416from content_understanding_client import AzureContentUnderstandingClient
1517from azure_credential_utils import get_azure_credential
1618from azure .search .documents .indexes .models import (
@@ -43,8 +45,7 @@ def get_secrets_from_kv(kv_name, secret_name):
4345
4446# Retrieve secrets
4547search_endpoint = get_secrets_from_kv (KEY_VAULT_NAME , "AZURE-SEARCH-ENDPOINT" )
46- openai_api_base = get_secrets_from_kv (KEY_VAULT_NAME , "AZURE-OPENAI-ENDPOINT" )
47- openai_api_version = get_secrets_from_kv (KEY_VAULT_NAME , "AZURE-OPENAI-PREVIEW-API-VERSION" )
48+ ai_project_endpoint = get_secrets_from_kv (KEY_VAULT_NAME , "AZURE-AI-AGENT-ENDPOINT" )
4849deployment = get_secrets_from_kv (KEY_VAULT_NAME , "AZURE-OPENAI-DEPLOYMENT-MODEL" )
4950account_name = get_secrets_from_kv (KEY_VAULT_NAME , "ADLS-ACCOUNT-NAME" )
5051server = get_secrets_from_kv (KEY_VAULT_NAME , "SQLDB-SERVER" )
@@ -70,14 +71,21 @@ def get_secrets_from_kv(kv_name, secret_name):
7071print ("Azure Search setup complete." )
7172
7273# ---------- Azure AI Foundry (Inference) clients (Managed Identity) ----------
73- # For Azure OpenAI endpoints, the Inference SDK expects the deployment path and api_version + scopes.
74- # chat deployment (already coming from Key Vault as `deployment`)
75- chat_endpoint = f"{ openai_api_base } /openai/deployments/{ deployment } "
74+ # Project endpoint has the form: https://your-ai-services-account-name.services.ai.azure.com/api/projects/your-project-name
75+ # Inference endpoint has the form: https://your-ai-services-account-name.services.ai.azure.com/models
76+ # Strip the "/api/projects/your-project-name" part and replace with "/models":
77+ inference_endpoint = f"https://{ urlparse (ai_project_endpoint ).netloc } /models"
78+
7679chat_client = ChatCompletionsClient (
77- endpoint = chat_endpoint ,
80+ endpoint = inference_endpoint ,
81+ credential = credential ,
82+ credential_scopes = ["https://ai.azure.com/.default" ],
83+ )
84+
85+ embeddings_client = EmbeddingsClient (
86+ endpoint = inference_endpoint ,
7887 credential = credential ,
79- credential_scopes = ["https://cognitiveservices.azure.com/.default" ],
80- api_version = openai_api_version ,
88+ credential_scopes = ["https://ai.azure.com/.default" ],
8189)
8290
8391# Delete the search index
@@ -125,7 +133,7 @@ def create_search_index():
125133 vectorizer_name = "myOpenAI" ,
126134 kind = "azureOpenAI" ,
127135 parameters = AzureOpenAIVectorizerParameters (
128- resource_url = openai_api_base ,
136+ resource_url = ai_project_endpoint ,
129137 deployment_name = embedding_model ,
130138 model_name = embedding_model
131139 )
@@ -178,16 +186,14 @@ def create_search_index():
178186print ("Content Understanding client initialized." )
179187
180188# Utility functions
181- def get_embeddings (text : str , openai_api_base , openai_api_version ):
182- embeddings_endpoint = f"{ openai_api_base } /openai/deployments/{ embedding_model } "
183- embeddings_client = EmbeddingsClient (
184- endpoint = embeddings_endpoint ,
185- credential = credential ,
186- credential_scopes = ["https://cognitiveservices.azure.com/.default" ],
187- api_version = openai_api_version
188- )
189- response = embeddings_client .embed (input = [text ])
190- return response .data [0 ].embedding
189+ def get_embeddings (text : str ):
190+ # Uses Azure AI Inference EmbeddingsClient with the AI Foundry project inference endpoint.
191+ try :
192+ resp = embeddings_client .embed (model = embedding_model , input = [text ])
193+ return resp .data [0 ].embedding
194+ except Exception as e :
195+ print (f"Error getting embeddings: { e } " )
196+ raise
191197# --------------------------------------------------------------------------
192198
193199def clean_spaces_with_regex (text ):
@@ -217,12 +223,14 @@ def prepare_search_doc(content, document_id, path_name):
217223 for idx , chunk in enumerate (chunks , 1 ):
218224 chunk_id = f"{ document_id } _{ str (idx ).zfill (2 )} "
219225 try :
220- v_contentVector = get_embeddings (str (chunk ),openai_api_base ,openai_api_version )
221- except :
226+ v_contentVector = get_embeddings (str (chunk ))
227+ except Exception as e :
228+ print (f"Error getting embeddings on first try: { e } " )
222229 time .sleep (30 )
223230 try :
224- v_contentVector = get_embeddings (str (chunk ),openai_api_base ,openai_api_version )
225- except :
231+ v_contentVector = get_embeddings (str (chunk ))
232+ except Exception as e :
233+ print (f"Error getting embeddings: { e } " )
226234 v_contentVector = []
227235 docs .append ({
228236 "id" : chunk_id ,
@@ -402,9 +410,10 @@ def call_gpt4(topics_str1, client):
402410 Do not return anything else.
403411 """
404412 response = client .complete (
413+ model = deployment ,
405414 messages = [
406- { "role" : "system" , " content" : " You are a helpful assistant."} ,
407- { "role" : "user" , " content" : topic_prompt } ,
415+ SystemMessage ( content = " You are a helpful assistant.") ,
416+ UserMessage ( content = topic_prompt ) ,
408417 ],
409418 temperature = 0 ,
410419 )
@@ -431,9 +440,10 @@ def get_mined_topic_mapping(input_text, list_of_topics):
431440 from a list of topics - { list_of_topics } .
432441 ALWAYS only return a topic from list - { list_of_topics } . Do not add any other text.'''
433442 response = chat_client .complete (
443+ model = deployment ,
434444 messages = [
435- { "role" : "system" , " content" : " You are a helpful assistant."} ,
436- { "role" : "user" , " content" : prompt } ,
445+ SystemMessage ( content = " You are a helpful assistant.") ,
446+ UserMessage ( content = prompt ) ,
437447 ],
438448 temperature = 0 ,
439449 )
0 commit comments