Skip to content
Closed
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
11 changes: 11 additions & 0 deletions infra/bicep/deploy_azure_function.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ param applicationInsightsId string
param functionAppName string
param containerAppEnvame string
param logAnalyticsWorkspaceName string
@secure()
param aiProjectConnectionString string
param useAIProjectClientFlag string = 'false'

var azureOpenAIDeploymentModel = 'gpt-4o-mini'
var azureOpenAIEmbeddingDeployment = 'text-embedding-ada-002'
Expand Down Expand Up @@ -182,6 +185,14 @@ resource functionApp 'Microsoft.Web/sites@2024-04-01' = {
name: 'SQLDB_USER_MID'
value: userassignedIdentityClientId
}
{
name: 'AZURE_AI_PROJECT_CONN_STRING'
value: aiProjectConnectionString
}
{
name: 'USE_AI_PROJECT_CLIENT'
value: useAIProjectClientFlag
}
]
}
}
Expand Down
3 changes: 2 additions & 1 deletion infra/bicep/main.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,8 @@ module azureFunctions 'deploy_azure_function.bicep' = {
userassignedIdentityId:managedIdentityModule.outputs.managedIdentityFnAppOutput.id
applicationInsightsId: aifoundry.outputs.applicationInsightsId
storageAccountName:aifoundry.outputs.storageAccountName
logAnalyticsWorkspaceName: aifoundry.outputs.logAnalyticsWorkspaceResourceName
logAnalyticsWorkspaceName:aifoundry.outputs.logAnalyticsWorkspaceResourceName
aiProjectConnectionString:keyVault.getSecret('AZURE-AI-PROJECT-CONN-STRING')
}
dependsOn:[keyVault]
}
Expand Down
27 changes: 25 additions & 2 deletions infra/bicep/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"_generator": {
"name": "bicep",
"version": "0.34.44.8038",
"templateHash": "5995671308040328650"
"templateHash": "5749110950649335177"
}
},
"parameters": {
Expand Down Expand Up @@ -2214,6 +2214,14 @@
},
"logAnalyticsWorkspaceName": {
"value": "[reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, resourceGroup().name), 'Microsoft.Resources/deployments', 'deploy_ai_foundry'), '2022-09-01').outputs.logAnalyticsWorkspaceResourceName.value]"
},
"aiProjectConnectionString": {
"reference": {
"keyVault": {
"id": "[extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, resourceGroup().name), 'Microsoft.KeyVault/vaults', reference(extensionResourceId(format('/subscriptions/{0}/resourceGroups/{1}', subscription().subscriptionId, resourceGroup().name), 'Microsoft.Resources/deployments', 'deploy_ai_foundry'), '2022-09-01').outputs.keyvaultName.value)]"
},
"secretName": "AZURE-AI-PROJECT-CONN-STRING"
}
}
},
"template": {
Expand All @@ -2223,7 +2231,7 @@
"_generator": {
"name": "bicep",
"version": "0.34.44.8038",
"templateHash": "2778443846888822542"
"templateHash": "2915460189477392481"
}
},
"parameters": {
Expand Down Expand Up @@ -2301,6 +2309,13 @@
},
"logAnalyticsWorkspaceName": {
"type": "string"
},
"aiProjectConnectionString": {
"type": "securestring"
},
"useAIProjectClientFlag": {
"type": "string",
"defaultValue": "false"
}
},
"variables": {
Expand Down Expand Up @@ -2428,6 +2443,14 @@
{
"name": "SQLDB_USER_MID",
"value": "[parameters('userassignedIdentityClientId')]"
},
{
"name": "AZURE_AI_PROJECT_CONN_STRING",
"value": "[parameters('aiProjectConnectionString')]"
},
{
"name": "USE_AI_PROJECT_CLIENT",
"value": "[parameters('useAIProjectClientFlag')]"
}
]
}
Expand Down
8 changes: 8 additions & 0 deletions infra/scripts/run_create_index_scripts.sh
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ if [ $? -ne 0 ]; then
error_flag=true
fi

# revert the key vault name and managed identity client id in the python files
sed -i "s/${keyvaultName}/kv_to-be-replaced/g" ${pythonScriptPath}"create_search_index.py"
sed -i "s/${keyvaultName}/kv_to-be-replaced/g" ${pythonScriptPath}"create_sql_tables.py"
if [ -n "$managedIdentityClientId" ]; then
sed -i "s/${managedIdentityClientId}/mici_to-be-replaced/g" ${pythonScriptPath}"create_search_index.py"
sed -i "s/${managedIdentityClientId}/mici_to-be-replaced/g" ${pythonScriptPath}"create_sql_tables.py"
fi

if [ "$error_flag" = true ]; then
echo "One or more errors occurred during the script execution."
exit 1
Expand Down
119 changes: 81 additions & 38 deletions src/AzureFunction/function_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import os
from typing import Annotated

from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient

from semantic_kernel.agents.open_ai import AzureAssistantAgent
from semantic_kernel.contents.chat_message_content import ChatMessageContent
from semantic_kernel.contents.utils.author_role import AuthorRole
Expand All @@ -24,6 +27,8 @@
api_key = os.environ.get("AZURE_OPEN_AI_API_KEY")
api_version = os.environ.get("OPENAI_API_VERSION")
deployment = os.environ.get("AZURE_OPEN_AI_DEPLOYMENT_MODEL")
project_connection_string = os.environ.get("AZURE_AI_PROJECT_CONN_STRING")
use_ai_project_client = os.environ.get("USE_AI_PROJECT_CLIENT", "false").lower() == "true"
temperature = 0

search_endpoint = os.environ.get("AZURE_AI_SEARCH_ENDPOINT")
Expand Down Expand Up @@ -60,27 +65,50 @@ def greeting(self, input: Annotated[str, "the question"]) -> Annotated[str, "The
Simple greeting handler using Azure OpenAI.
"""
try:
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version=api_version
)
completion = client.chat.completions.create(
model=deployment,
messages=[
{
"role": "system",
"content": "You are a helpful assistant to respond to greetings or general questions."
},
{
"role": "user",
"content": input
},
],
temperature=0,
top_p=1,
n=1
)
if self.use_ai_project_client:
project = AIProjectClient.from_connection_string(
conn_str=self.azure_ai_project_conn_string,
credential=DefaultAzureCredential()
)
client = project.inference.get_chat_completions_client()

completion = client.complete(
model=self.azure_openai_deployment_model,
messages=[
{
"role": "system",
"content": "You are a helpful assistant to respond to greetings or general questions."
},
{
"role": "user",
"content": input
},
],
temperature=0,
)
else:
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version=api_version
)
completion = client.chat.completions.create(
model=deployment,
messages=[
{
"role": "system",
"content": "You are a helpful assistant to respond to greetings or general questions."
},
{
"role": "user",
"content": input
},
],
temperature=0,
top_p=1,
n=1
)

answer = completion.choices[0].message.content
except Exception as e:
answer = f"Error retrieving greeting response: {str(e)}"
Expand All @@ -99,13 +127,6 @@ def get_SQL_Response(
clientid = ClientId
query = input

# Initialize the Azure OpenAI client
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version=api_version
)

# Retrieve the SQL prompt from environment variables (if available)
sql_prompt = os.environ.get("AZURE_SQL_SYSTEM_PROMPT")
if sql_prompt:
Expand Down Expand Up @@ -137,16 +158,38 @@ def get_SQL_Response(
Only return the generated SQL query. Do not return anything else.'''

try:
completion = client.chat.completions.create(
model=deployment,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": sql_prompt},
],
temperature=0,
top_p=1,
n=1
)
if use_ai_project_client:
project = AIProjectClient.from_connection_string(
conn_str=project_connection_string,
credential=DefaultAzureCredential()
)
client = project.inference.get_chat_completions_client()
completion = client.complete(
model=deployment,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": sql_prompt},
],
temperature=0,
)

else:
# Initialize the Azure OpenAI client
client = openai.AzureOpenAI(
azure_endpoint=endpoint,
api_key=api_key,
api_version=api_version
)
completion = client.chat.completions.create(
model=deployment,
messages=[
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": sql_prompt},
],
temperature=0,
top_p=1,
n=1
)

sql_query = completion.choices[0].message.content

Expand Down
2 changes: 2 additions & 0 deletions src/AzureFunction/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ openai==1.64.0
semantic_kernel==1.21.3
pymssql==2.3.2
azure-search-documents==11.6.0b9
azure-ai-projects==1.0.0b9
azure-ai-inference==1.0.0b9

pyodbc==5.2.0
azure-identity==1.20.0