Skip to content

Commit

Permalink
enable content safety by default
Browse files Browse the repository at this point in the history
  • Loading branch information
ruoccofabrizio committed Oct 12, 2023
1 parent c48bd83 commit db254e0
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 5 deletions.
2 changes: 1 addition & 1 deletion backend/utilities/helpers/ConfigHelper.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def get_default_config():
Question: {question}
Answer: {answer}""",
"enable_post_answering_prompt": False,
"enable_content_safety": False
"enable_content_safety": True
},
"messages": {
"post_answering_filter": "I'm sorry, but I can't answer this question correctly. Please try again by altering or rephrasing your question."
Expand Down
7 changes: 5 additions & 2 deletions backend/utilities/orchestrator/LangChainAgent.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,11 @@ def orchestrate(self, user_message: str, chat_history: List[dict], **kwargs: dic
)
# Run Agent Chain
with get_openai_callback() as cb:
answer = agent_chain.run(user_message)
self.log_tokens(prompt_tokens=cb.prompt_tokens, completion_tokens=cb.completion_tokens)
try:
answer = agent_chain.run(user_message)
self.log_tokens(prompt_tokens=cb.prompt_tokens, completion_tokens=cb.completion_tokens)
except Exception as e:
answer = str(e)
try:
answer = Answer.from_json(answer)
except:
Expand Down
28 changes: 28 additions & 0 deletions infrastructure/deployment.bicep
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ param FunctionName string = '${ResourcePrefix}-backend'

@description('Azure Form Recognizer Name')
param FormRecognizerName string = '${ResourcePrefix}-formrecog'

@description('Azure Content Safety Name')
param ContentSafetyName string = '${ResourcePrefix}-contentsafety'
param newGuidString string = newGuid()

var WebAppImageName = 'DOCKER|fruoccopublic.azurecr.io/rag-webapp'
Expand Down Expand Up @@ -173,6 +176,25 @@ resource FormRecognizer 'Microsoft.CognitiveServices/accounts@2022-12-01' = {
}
}

resource ContentSafety 'Microsoft.CognitiveServices/accounts@2022-03-01' = {
name: ContentSafetyName
location: Location
sku: {
name: 'S0'
}
kind: 'ContentSafety'
identity: {
type: 'None'
}
properties: {
networkAcls: {
defaultAction: 'Allow'
virtualNetworkRules: []
ipRules: []
}
}
}

resource HostingPlan 'Microsoft.Web/serverfarms@2020-06-01' = {
name: HostingPlanName
location: Location
Expand Down Expand Up @@ -223,6 +245,8 @@ resource Website 'Microsoft.Web/sites@2020-06-01' = {
{ name: 'AZURE_BLOB_ACCOUNT_KEY', value: listKeys(StorageAccount.id, '2019-06-01').keys[0].value}
{ name: 'AZURE_BLOB_CONTAINER_NAME', value: BlobContainerName}
{ name: 'ORCHESTRATION_STRATEGY', value: OrchestrationStrategy}
{ name: 'AZURE_CONTENT_SAFETY_ENDPOINT', value: ContentSafetyName}
{ name: 'AZURE_CONTENT_SAFETY_KEY', value: listKeys('Microsoft.CognitiveServices/accounts/${ContentSafetyName}', '2023-05-01').key1}
]
linuxFxVersion: WebAppImageName
}
Expand Down Expand Up @@ -273,6 +297,8 @@ resource WebsiteName_admin 'Microsoft.Web/sites@2020-06-01' = {
{ name: 'BACKEND_URL', value: 'https://${FunctionName}.azurewebsites.net'}
{ name: 'FUNCTION_KEY', value: ClientKey}
{ name: 'ORCHESTRATION_STRATEGY', value: OrchestrationStrategy}
{ name: 'AZURE_CONTENT_SAFETY_ENDPOINT', value: ContentSafetyName}
{ name: 'AZURE_CONTENT_SAFETY_KEY', value: listKeys('Microsoft.CognitiveServices/accounts/${ContentSafetyName}', '2023-05-01').key1}
]
linuxFxVersion: AdminWebAppImageName
}
Expand Down Expand Up @@ -378,6 +404,8 @@ resource Function 'Microsoft.Web/sites@2018-11-01' = {
{ name: 'AZURE_OPENAI_API_VERSION', value: AzureOpenAIApiVersion}
{ name: 'AZURE_SEARCH_INDEX', value: AzureSearchIndex}
{ name: 'ORCHESTRATION_STRATEGY', value: OrchestrationStrategy}
{ name: 'AZURE_CONTENT_SAFETY_ENDPOINT', value: ContentSafetyName}
{ name: 'AZURE_CONTENT_SAFETY_KEY', value: listKeys('Microsoft.CognitiveServices/accounts/${ContentSafetyName}', '2023-05-01').key1}
]
cors: {
allowedOrigins: [
Expand Down
55 changes: 53 additions & 2 deletions infrastructure/deployment.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"metadata": {
"_generator": {
"name": "bicep",
"version": "0.20.4.51522",
"templateHash": "4453445457127590997"
"version": "0.21.1.54444",
"templateHash": "4730872359172000762"
}
},
"parameters": {
Expand Down Expand Up @@ -275,6 +275,13 @@
"description": "Azure Form Recognizer Name"
}
},
"ContentSafetyName": {
"type": "string",
"defaultValue": "[format('{0}-contentsafety', parameters('ResourcePrefix'))]",
"metadata": {
"description": "Azure Content Safety Name"
}
},
"newGuidString": {
"type": "string",
"defaultValue": "[newGuid()]"
Expand Down Expand Up @@ -327,6 +334,26 @@
"publicNetworkAccess": "Enabled"
}
},
{
"type": "Microsoft.CognitiveServices/accounts",
"apiVersion": "2022-03-01",
"name": "[parameters('ContentSafetyName')]",
"location": "[parameters('Location')]",
"sku": {
"name": "S0"
},
"kind": "ContentSafety",
"identity": {
"type": "None"
},
"properties": {
"networkAcls": {
"defaultAction": "Allow",
"virtualNetworkRules": [],
"ipRules": []
}
}
},
{
"type": "Microsoft.Web/serverfarms",
"apiVersion": "2020-06-01",
Expand Down Expand Up @@ -472,6 +499,14 @@
{
"name": "ORCHESTRATION_STRATEGY",
"value": "[parameters('OrchestrationStrategy')]"
},
{
"name": "AZURE_CONTENT_SAFETY_ENDPOINT",
"value": "[parameters('ContentSafetyName')]"
},
{
"name": "AZURE_CONTENT_SAFETY_KEY",
"value": "[listKeys(format('Microsoft.CognitiveServices/accounts/{0}', parameters('ContentSafetyName')), '2023-05-01').key1]"
}
],
"linuxFxVersion": "[variables('WebAppImageName')]"
Expand Down Expand Up @@ -627,6 +662,14 @@
{
"name": "ORCHESTRATION_STRATEGY",
"value": "[parameters('OrchestrationStrategy')]"
},
{
"name": "AZURE_CONTENT_SAFETY_ENDPOINT",
"value": "[parameters('ContentSafetyName')]"
},
{
"name": "AZURE_CONTENT_SAFETY_KEY",
"value": "[listKeys(format('Microsoft.CognitiveServices/accounts/{0}', parameters('ContentSafetyName')), '2023-05-01').key1]"
}
],
"linuxFxVersion": "[variables('AdminWebAppImageName')]"
Expand Down Expand Up @@ -803,6 +846,14 @@
{
"name": "ORCHESTRATION_STRATEGY",
"value": "[parameters('OrchestrationStrategy')]"
},
{
"name": "AZURE_CONTENT_SAFETY_ENDPOINT",
"value": "[parameters('ContentSafetyName')]"
},
{
"name": "AZURE_CONTENT_SAFETY_KEY",
"value": "[listKeys(format('Microsoft.CognitiveServices/accounts/{0}', parameters('ContentSafetyName')), '2023-05-01').key1]"
}
],
"cors": {
Expand Down

0 comments on commit db254e0

Please sign in to comment.