Skip to content

Commit 184e63d

Browse files
truncate sql message
1 parent 83c5cdc commit 184e63d

File tree

6 files changed

+28
-58
lines changed

6 files changed

+28
-58
lines changed

infra/deploy_backend_docker.bicep

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@ param solutionName string
44
@secure()
55
param appSettings object = {}
66
param appServicePlanId string
7+
@secure()
8+
param azureOpenAIKey string
9+
@secure()
10+
param azureAiProjectConnString string
11+
@secure()
12+
param azureSearchAdminKey string
713
param userassignedIdentityId string
8-
param keyVaultName string
914

1015
var imageName = 'DOCKER|kmcontainerreg.azurecr.io/km-api:${imageTag}'
1116
var name = '${solutionName}-api'
@@ -84,6 +89,9 @@ module appService 'deploy_app_service.bicep' = {
8489
appSettings: union(
8590
appSettings,
8691
{
92+
AZURE_OPENAI_API_KEY: azureOpenAIKey
93+
AZURE_AI_SEARCH_API_KEY: azureSearchAdminKey
94+
AZURE_AI_PROJECT_CONN_STRING:azureAiProjectConnString
8795
APPINSIGHTS_INSTRUMENTATIONKEY: reference(applicationInsightsId, '2015-05-01').InstrumentationKey
8896
REACT_APP_LAYOUT_CONFIG: reactAppLayoutConfig
8997
}
@@ -110,25 +118,4 @@ resource role 'Microsoft.DocumentDB/databaseAccounts/sqlRoleAssignments@2022-05-
110118
}
111119
}
112120

113-
resource keyVault 'Microsoft.KeyVault/vaults@2023-07-01' existing = {
114-
name: keyVaultName
115-
}
116-
117-
var keyVaultSecretsOfficerId='b86a8fe4-44ce-4948-aee5-eccb2c155cd7'
118-
@description('The built-in role for Key Vault Secrets Officer.')
119-
resource keyVaultSecretsOfficerRoleDefinition 'Microsoft.Authorization/roleDefinitions@2022-04-01' existing = {
120-
scope: subscription()
121-
name: keyVaultSecretsOfficerId
122-
}
123-
124-
resource keyVaultSecretsOfficerRoleAssignment 'Microsoft.Authorization/roleAssignments@2022-04-01' = {
125-
scope: keyVault
126-
name: guid(keyVault.id, keyVaultSecretsOfficerRoleDefinition.id)
127-
properties: {
128-
roleDefinitionId: keyVaultSecretsOfficerRoleDefinition.id
129-
principalId: appService.outputs.identityPrincipalId
130-
principalType: 'ServicePrincipal'
131-
}
132-
}
133-
134121
output appUrl string = appService.outputs.appUrl

infra/main.bicep

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,16 @@ module backend_docker 'deploy_backend_docker.bicep'= {
194194
imageTag: imageTag
195195
appServicePlanId: hostingplan.outputs.name
196196
applicationInsightsId: aifoundry.outputs.applicationInsightsId
197+
azureOpenAIKey:keyVault.getSecret('AZURE-OPENAI-KEY')
198+
azureAiProjectConnString:keyVault.getSecret('AZURE-AI-PROJECT-CONN-STRING')
199+
azureSearchAdminKey:keyVault.getSecret('AZURE-SEARCH-KEY')
197200
solutionName: solutionPrefix
198201
userassignedIdentityId: managedIdentityModule.outputs.managedIdentityBackendAppOutput.id
199-
keyVaultName:aifoundry.outputs.keyvaultName
200202
appSettings:{
201203
AZURE_OPEN_AI_DEPLOYMENT_MODEL:gptModelName
202204
AZURE_OPEN_AI_ENDPOINT:aifoundry.outputs.aiServicesTarget
203205
AZURE_OPENAI_API_VERSION: azureOpenAIApiVersion
204206
AZURE_OPENAI_RESOURCE:aifoundry.outputs.aiServicesName
205-
AZURE_OPENAI_API_KEY:'AZURE-OPENAI-KEY'
206-
AZURE_KEY_VAULT_URL: kvault.outputs.keyvaultUri
207207
USE_CHAT_HISTORY_ENABLED:'True'
208208
AZURE_COSMOSDB_ACCOUNT: cosmosDBModule.outputs.cosmosAccountName
209209
AZURE_COSMOSDB_CONVERSATIONS_CONTAINER: cosmosDBModule.outputs.cosmosContainerName
@@ -217,9 +217,7 @@ module backend_docker 'deploy_backend_docker.bicep'= {
217217
OPENAI_API_VERSION: azureOpenAIApiVersion
218218
AZURE_AI_SEARCH_ENDPOINT: aifoundry.outputs.aiSearchTarget
219219
AZURE_AI_SEARCH_INDEX: 'call_transcripts_index'
220-
AZURE_AI_SEARCH_API_KEY:'AZURE-SEARCH-KEY'
221220
USE_AI_PROJECT_CLIENT:'False'
222-
AZURE_AI_PROJECT_CONN_STRING:'AZURE-AI-PROJECT-CONN-STRING'
223221
DISPLAY_CHART_DEFAULT:'True'
224222
}
225223
}

src/api/ApiApp.Dockerfile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
FROM python:3.11-alpine
22

33
# Install system dependencies required for building and running the application
4-
RUN apk add --no-cache --virtual .build-deps \
5-
build-base \
6-
libffi-dev \
7-
openssl-dev \
4+
RUN apk add --no-cache --virtual .build-deps \
5+
build-base \
6+
libffi-dev \
7+
openssl-dev \
88
curl \
9-
unixodbc-dev \
10-
libpq
9+
unixodbc-dev \
10+
libpq \
11+
opus-dev \
12+
libvpx-dev
1113

1214
# Download and install Microsoft ODBC Driver and MSSQL tools
1315
RUN curl -O https://download.microsoft.com/download/e/4/e/e4e67866-dffd-428c-aac7-8d28ddafb39b/msodbcsql17_17.10.6.1-1_amd64.apk \
@@ -23,7 +25,8 @@ WORKDIR /app
2325
COPY ./requirements.txt .
2426

2527
# Install Python dependencies
26-
RUN pip install --no-cache-dir -r requirements.txt && rm -rf /root/.cache
28+
RUN pip install --upgrade pip setuptools wheel \
29+
&& pip install --no-cache-dir -r requirements.txt && rm -rf /root/.cache
2730

2831
# Copy the backend application code into the container
2932
COPY ./ .
@@ -32,4 +35,4 @@ COPY ./ .
3235
EXPOSE 80
3336

3437
# Start the application using Uvicorn
35-
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]
38+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "80"]

src/api/common/config/config.py

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
import os
2-
from azure.identity import DefaultAzureCredential
3-
from azure.keyvault.secrets import SecretClient
42
from dotenv import load_dotenv
53

64
load_dotenv()
75

86

97
class Config:
108
def __init__(self):
11-
# Initialize Key Vault client
12-
key_vault_url = os.getenv("AZURE_KEY_VAULT_URL")
13-
credential = DefaultAzureCredential()
14-
self.secret_client = SecretClient(vault_url=key_vault_url, credential=credential)
159

1610
# SQL Database configuration
1711
self.sqldb_database = os.getenv("SQLDB_DATABASE")
@@ -23,32 +17,22 @@ def __init__(self):
2317
# Azure OpenAI configuration
2418
self.azure_openai_endpoint = os.getenv("AZURE_OPEN_AI_ENDPOINT")
2519
self.azure_openai_deployment_model = os.getenv("AZURE_OPEN_AI_DEPLOYMENT_MODEL")
26-
self.azure_openai_api_key = self.get_secret("AZURE_OPENAI_API_KEY")
20+
self.azure_openai_api_key = os.getenv("AZURE_OPENAI_API_KEY")
2721
self.azure_openai_api_version = os.getenv("AZURE_OPENAI_API_VERSION")
2822
self.azure_openai_resource = os.getenv("AZURE_OPENAI_RESOURCE")
2923

3024
# Azure AI Search configuration
3125
self.azure_ai_search_endpoint = os.getenv("AZURE_AI_SEARCH_ENDPOINT")
32-
self.azure_ai_search_api_key = self.get_secret("AZURE_AI_SEARCH_API_KEY")
26+
self.azure_ai_search_api_key = os.getenv("AZURE_AI_SEARCH_API_KEY")
3327
self.azure_ai_search_index = os.getenv("AZURE_AI_SEARCH_INDEX")
3428

3529
# AI Project Client configuration
3630
self.use_ai_project_client = os.getenv("USE_AI_PROJECT_CLIENT", "False").lower() == "true"
37-
self.azure_ai_project_conn_string = self.get_secret("AZURE_AI_PROJECT_CONN_STRING")
31+
self.azure_ai_project_conn_string = os.getenv("AZURE_AI_PROJECT_CONN_STRING")
3832

3933
# Chat history configuration
4034
self.use_chat_history_enabled = os.getenv("USE_CHAT_HISTORY_ENABLED", "false").strip().lower() == "true"
4135
self.azure_cosmosdb_database = os.getenv("AZURE_COSMOSDB_DATABASE")
4236
self.azure_cosmosdb_account = os.getenv("AZURE_COSMOSDB_ACCOUNT")
4337
self.azure_cosmosdb_conversations_container = os.getenv("AZURE_COSMOSDB_CONVERSATIONS_CONTAINER")
4438
self.azure_cosmosdb_enable_feedback = os.getenv("AZURE_COSMOSDB_ENABLE_FEEDBACK", "false").lower() == "true"
45-
46-
def get_secret(self, secret_name):
47-
"""Retrieve a secret value from Azure Key Vault."""
48-
secret_name_value = os.getenv(secret_name, "")
49-
try:
50-
secret = self.secret_client.get_secret(secret_name_value)
51-
return secret.value
52-
except Exception as e:
53-
print(f"Error retrieving secret '{secret_name_value}': {e}")
54-
return None

src/api/plugins/chat_with_data_plugin.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ def get_SQL_Response(
122122
sql_query = sql_query.replace("```sql", '').replace("```", '')
123123

124124
answer = execute_sql_query(sql_query)
125+
answer = answer[:20000]
125126

126127
except Exception as e:
127128
# 'Information from database could not be retrieved. Please try again later.'

src/api/services/chat_service.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,7 @@ async def stream_openai_text(self, conversation_id: str, query: str) -> Streamin
122122
api_version=config.azure_openai_api_version,
123123
)
124124

125-
thread_id = thread_cache.get(conversation_id)
126-
if not thread_id:
127-
thread_id = await agent.create_thread()
128-
thread_cache[conversation_id] = thread_id
125+
thread_id = await agent.create_thread()
129126

130127
# Add user message to the thread
131128
message = ChatMessageContent(role=AuthorRole.USER, content=query)

0 commit comments

Comments
 (0)