Skip to content

Commit

Permalink
Merge pull request #21 from PerfectThymeTech/marvinbuss/add_persisten…
Browse files Browse the repository at this point in the history
…t_storage

Add persistent storage
  • Loading branch information
marvinbuss authored Sep 12, 2024
2 parents 932de2c + a5131b0 commit 6fbc4a0
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 10 deletions.
29 changes: 20 additions & 9 deletions code/backend/app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from aiohttp import web
from aiohttp.web import Request, Response
from botbuilder.core import MemoryStorage, ShowTypingMiddleware, UserState
from botbuilder.azure import CosmosDbPartitionedConfig, CosmosDbPartitionedStorage
from botbuilder.core import ShowTypingMiddleware, UserState
from botbuilder.core.inspection import InspectionMiddleware, InspectionState
from botbuilder.core.integration import aiohttp_error_middleware
from botbuilder.integration.aiohttp import (
Expand All @@ -10,31 +11,41 @@
from botframework.connector.auth import MicrosoftAppCredentials
from bots.assistant_bot import AssistantBot
from bots.utils_bot import BotUtils
from core.config import settings
from core.config import settings as CONFIG
from utils import enable_logging

# Enable logging
enable_logging()

# Create MemoryStorage and state
MEMORY = MemoryStorage()
USER_STATE = UserState(storage=MEMORY)
# Create storage and state
STORAGE = CosmosDbPartitionedStorage(
config=CosmosDbPartitionedConfig(
cosmos_db_endpoint=CONFIG.AZURE_COSMOS_ENDPOINT,
auth_key=CONFIG.AZURE_COSMOS_KEY,
database_id=CONFIG.AZURE_COSMOS_DATABASE_ID,
container_id=CONFIG.AZURE_COSMOS_CONTAINER_ID,
cosmos_client_options=None,
container_throughput=None,
key_suffix="",
compatibility_mode=False,
)
)
USER_STATE = UserState(storage=STORAGE)

# Create cloud adapter with middleware
ADAPTER = CloudAdapter(ConfigurationBotFrameworkAuthentication(CONFIG))
ADAPTER.on_turn_error = BotUtils.on_error
ADAPTER.use(ShowTypingMiddleware(delay=0.1, period=2))

# Add inspection middleware for debugging
if settings.DEBUG:
if CONFIG.DEBUG:
INSPECTION_MIDDLEWARE = InspectionMiddleware(
inspection_state=InspectionState(MEMORY),
inspection_state=InspectionState(STORAGE),
user_state=USER_STATE,
conversation_state=None,
credentials=MicrosoftAppCredentials(
app_id=settings.APP_ID,
password=settings.APP_PASSWORD,
app_id=CONFIG.APP_ID,
password=CONFIG.APP_PASSWORD,
),
)
ADAPTER.use(INSPECTION_MIDDLEWARE)
Expand Down
6 changes: 6 additions & 0 deletions code/backend/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,11 @@ class Settings(BaseSettings):
AZURE_OPENAI_MODEL_NAME: str
AZURE_OPENAI_ASSISTANT_ID: str

# Cosmos DB settings
AZURE_COSMOS_ENDPOINT: str
AZURE_COSMOS_KEY: str
AZURE_COSMOS_DATABASE_ID: str
AZURE_COSMOS_CONTAINER_ID: str = "user-state"


settings = Settings()
1 change: 1 addition & 0 deletions code/backend/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
azure-identity~=1.17.1
azure-monitor-opentelemetry~=1.6.1
botbuilder-core~=4.16.1
botbuilder-azure~=4.16.1
botbuilder-integration-aiohttp~=4.16.1
openai~=1.42.0
opentelemetry-instrumentation-aiohttp-client~=0.47b0
Expand Down
97 changes: 97 additions & 0 deletions code/infra/cosmosdb.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
module "cosmosdb_account" {
source = "github.com/PerfectThymeTech/terraform-azurerm-modules//modules/cosmosdb?ref=main"
providers = {
azurerm = azurerm
time = time
}

location = var.location
resource_group_name = azurerm_resource_group.resource_group.name
tags = var.tags
cosmosdb_account_name = "${local.prefix}-cosmos001"
cosmosdb_account_access_key_metadata_writes_enabled = true
cosmosdb_account_analytical_storage_enabled = false
cosmosdb_account_automatic_failover_enabled = false
cosmosdb_account_backup = {
type = "Continuous"
tier = "Continuous7Days"
storage_redundancy = null
retention_in_hours = null
interval_in_minutes = null
}
cosmosdb_account_capabilities = [
"EnableServerless"
]
cosmosdb_account_capacity_total_throughput_limit = -1
cosmosdb_account_consistency_policy = {
consistency_level = "Strong"
max_interval_in_seconds = null
max_staleness_prefix = null
}
cosmosdb_account_cors_rules = {}
cosmosdb_account_default_identity_type = null
cosmosdb_account_geo_location = [
{
location = var.location
failover_priority = 0
zone_redundant = false
}
]
cosmosdb_account_kind = "GlobalDocumentDB"
cosmosdb_account_local_authentication_disabled = false
cosmosdb_account_mongo_server_version = null
cosmosdb_account_partition_merge_enabled = false
diagnostics_configurations = []
subnet_id = azapi_resource.subnet_private_endpoints.id
connectivity_delay_in_seconds = 0
private_endpoint_subresource_names = ["Sql"]
private_dns_zone_id_cosmos_sql = var.private_dns_zone_id_cosmos_sql
private_dns_zone_id_cosmos_mongodb = ""
private_dns_zone_id_cosmos_cassandra = ""
private_dns_zone_id_cosmos_gremlin = ""
private_dns_zone_id_cosmos_table = ""
private_dns_zone_id_cosmos_analytical = ""
private_dns_zone_id_cosmos_coordinator = ""
customer_managed_key = null
}

resource "azurerm_cosmosdb_sql_database" "cosmosdb_sql_database" {
name = "BotDb"
account_name = module.cosmosdb_account.cosmosdb_account_name
resource_group_name = azurerm_resource_group.resource_group.name
}

resource "azurerm_cosmosdb_sql_container" "cosmosdb_sql_container" {
name = local.cosmosdb_sql_container_name
database_name = azurerm_cosmosdb_sql_database.cosmosdb_sql_database.name
account_name = module.cosmosdb_account.cosmosdb_account_name
resource_group_name = azurerm_resource_group.resource_group.name

analytical_storage_ttl = null
# autoscale_settings {
# max_throughput = 400
# }
conflict_resolution_policy {
conflict_resolution_path = "/_ts"
mode = "LastWriterWins"
}
# default_ttl = -1
indexing_policy {
indexing_mode = "consistent"
included_path {
path = "/*"
}
excluded_path {
path = "/\"_etag\"/?"
}
}
partition_key_kind = "Hash"
partition_key_paths = [
"/id"
]
# partition_key_version = 1
# throughput =
# unique_key {
# paths = []
# }
}
9 changes: 8 additions & 1 deletion code/infra/locals.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ locals {
AZURE_OPENAI_MODEL_NAME = azurerm_cognitive_deployment.cognitive_deployment_gpt_4o.name
AZURE_OPENAI_SYSTEM_PROMPT = data.local_file.file_system_prompt.content
AZURE_OPENAI_ASSISTANT_ID = ""

# Cosmos DB settings
AZURE_COSMOS_ENDPOINT = module.cosmosdb_account.cosmosdb_account_endpoint
AZURE_COSMOS_KEY = module.cosmosdb_account.cosmosdb_account_primary_key
AZURE_COSMOS_DATABASE_ID = azurerm_cosmosdb_sql_database.cosmosdb_sql_database.name
AZURE_COSMOS_CONTAINER_ID = azurerm_cosmosdb_sql_container.cosmosdb_sql_container.name
}
web_app_app_settings = merge(local.app_settings_default, var.web_app_app_settings)

Expand Down Expand Up @@ -56,5 +62,6 @@ locals {
customer_managed_key = null

# Other locals
system_prompt_code_path = "${path.module}/../../docs/SystemPrompt.txt"
system_prompt_code_path = "${path.module}/../../docs/SystemPrompt.txt"
cosmosdb_sql_container_name = "user-state"
}
11 changes: 11 additions & 0 deletions code/infra/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,14 @@ variable "private_dns_zone_id_open_ai" {
error_message = "Please specify a valid resource ID for the private DNS Zone."
}
}

variable "private_dns_zone_id_cosmos_sql" {
description = "Specifies the resource ID of the private DNS zone for cosmos db sql. Not required if DNS A-records get created via Azure Policy."
type = string
sensitive = false
default = ""
validation {
condition = var.private_dns_zone_id_cosmos_sql == "" || (length(split("/", var.private_dns_zone_id_cosmos_sql)) == 9 && endswith(var.private_dns_zone_id_cosmos_sql, "privatelink.documents.azure.com"))
error_message = "Please specify a valid resource ID for the private DNS Zone."
}
}
1 change: 1 addition & 0 deletions config/PerfectThymeTech/vars.tfvars
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ private_dns_zone_id_sites = "/subscriptions/8f171ff9-2b5b-4f0
private_dns_zone_id_bot_framework_directline = "/subscriptions/8f171ff9-2b5b-4f0f-aed5-7fa360a1d094/resourceGroups/mycrp-prd-global-dns/providers/Microsoft.Network/privateDnsZones/privatelink.directline.botframework.com"
private_dns_zone_id_bot_framework_token = "/subscriptions/8f171ff9-2b5b-4f0f-aed5-7fa360a1d094/resourceGroups/mycrp-prd-global-dns/providers/Microsoft.Network/privateDnsZones/privatelink.token.botframework.com"
private_dns_zone_id_open_ai = "/subscriptions/8f171ff9-2b5b-4f0f-aed5-7fa360a1d094/resourceGroups/mycrp-prd-global-dns/providers/Microsoft.Network/privateDnsZones/privatelink.openai.azure.com"
private_dns_zone_id_cosmos_sql = "/subscriptions/8f171ff9-2b5b-4f0f-aed5-7fa360a1d094/resourceGroups/mycrp-prd-global-dns/providers/Microsoft.Network/privateDnsZones/privatelink.documents.azure.com"

0 comments on commit 6fbc4a0

Please sign in to comment.