### Install Dependencies and Start Service (Python/Bash) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Installs project dependencies using pip and activates a virtual environment. It then starts the Flask development server for local testing. Requires Python and pip. ```bash python -m venv venv && source venv/bin/activate pip install -r lib/requirements.txt flask run # → Running on http://0.0.0.0:8002 ``` -------------------------------- ### Minimal Video Object-Detection Pipeline Example Source: https://asvskartheek.github.io/aidf-docs/first-steps_q= An example illustrating a simple pipeline flow for video object detection. It shows the sequence of services from ingestion to output, including the start and end points. ```text Start → Video Ingestion (Azure Blob → AIDF) → YOLO Detection → Output to Data Hub ``` -------------------------------- ### Environment Configuration File Example (.config) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= This file provides an example of environment configuration settings, including connection strings for Azure App Configuration and OpenAI, database credentials, and Flask application settings. It is intended to be sourced into the shell environment before running the application. ```env AZURE_APP_CONFIG_CONNECTION_STRING=Endpoint=https://your-appconfig.azconfig.io;Id=XXXX;Secret=XXXX AZURE_OPENAI_ENDPOINT=https://your-oai-resource.openai.azure.com/ AZURE_OPENAI_KEY=your-api-key-here AZURE_OPENAI_DEPLOYMENT=gpt-4o AZURE_OPENAI_API_VERSION=2024-08-01-preview DB_CONNECTION_STRING=postgresql://user:password@host:5432/aiservices_db AI_SERVICE_ID=ticket-classifier ENVIRONMENT=Development FLASK_APP=src/aiservice.py FLASK_RUN_HOST=0.0.0.0 FLASK_RUN_PORT=8002 ``` -------------------------------- ### Docker Deployment Commands Source: https://asvskartheek.github.io/aidf-docs/reference/suspicious-activity Provides the Docker commands for building and running the Suspicious Activity Detection service. This includes building the Docker image and starting a container, exposing the necessary port and setting the environment. ```bash docker build -t suspicious-activity:1.0 . docker run -p 8002:8002 -e ENVIRONMENT=Development suspicious-activity:1.0 ``` -------------------------------- ### Standard Dockerfile Structure for Python Source: https://asvskartheek.github.io/aidf-docs/developer/docker_q= Illustrates a typical Dockerfile for a Python application, including setting the base image, working directory, copying requirements and source code, installing dependencies, exposing ports, setting environment variables, and defining the command to run the application. ```dockerfile FROM python:3.11 # Base Python 3.11 image WORKDIR /code # Set working directory COPY lib/requirements.txt /code/requirements.txt RUN pip install --no-cache-dir -r requirements.txt \ && pip install internal-utils==x.y.z # Internal utility library COPY src/loop_log.ini /code/ # Copy logging config COPY . /code # Copy all service files EXPOSE 8002 # Document listening port ENV FLASK_APP=src.aiservice ENV FLASK_RUN_HOST=0.0.0.0 ENV FLASK_RUN_PORT=8002 ENV FLASK_ENV=production CMD ["flask", "run"] # Start Flask server ``` -------------------------------- ### Deploy Service via Infra Hub (Conceptual) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Outlines the steps for deploying the ticket classifier service using Infra Hub. This involves registering the service, uploading code, triggering the Manifest Engine for build and deployment to AKS, and provisioning a PostgreSQL table. This is a conceptual guide and not direct code. ```text 1. Navigate to Infra Hub → Your Project → AI Services 2. Click "Register AI Service" — Name: `ticket-classifier`, Module: LLM, Version: 1 3. Upload your code — push to the connected Git repository or use file upload 4. Trigger the Manifest Engine — click "Build & Deploy": * Reads `manifest.xml` to get build parameters * Runs `docker build` with `ARTIFACTS_PAT` from Azure Key Vault * Pushes image to ACR (`yourorg.azurecr.io/ticket-classifier:v1-dev`) * Applies AKS deployment with `small` pod size (Dev) * Provisions `ticket_classifications` table in PostgreSQL 5. Verify deployment — wait for all pods to show Running; platform calls `/health` 6. Run QA Tests from Infra Hub — Go to QA view → select your service → "Run Auto QA" 7. Promote: Dev → QA → Staging → Production (each promotion re-deploys with appropriate pod size) ``` -------------------------------- ### Initialize Request Context in ModelStrategy (Python) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Defines the initialization method for the `ModelStrategy` class, which is part of the core AI service logic. It records the start time, stores input data, and logs the received ticket ID. This method sets up the context for processing incoming requests. ```python class ModelStrategy(GENAI_Abstract_Class): def __init__(self): self.start_time: float = 0.0 self.input_data: Dict[str, Any] = {} self.processed_input: Dict[str, Any] = {} self.module_name: str = TICKET_CLASSIFIER.MODULE.value def on_start(self, input_data: Dict[str, Any]) -> Dict[str, Any]: self.start_time = time.time() self.input_data = input_data ticket_id = input_data.get("input_payload", {}).get("ticket_id", {}).get("value", "unknown") logger.info(f"[on_start] Request received | ticket_id={ticket_id}") return {"status": "initialized", "ticket_id": ticket_id} ``` -------------------------------- ### Standard Dockerfile Structure for Python Application Source: https://asvskartheek.github.io/aidf-docs/developer/docker Illustrates a typical Dockerfile for a Python application using Flask. It specifies the base image, sets the working directory, copies and installs dependencies, copies application files, exposes the application port, sets environment variables for Flask, and defines the command to run the server. ```dockerfile FROM python:3.11 # Base Python 3.11 image WORKDIR /code # Set working directory COPY lib/requirements.txt /code/requirements.txt RUN pip install --no-cache-dir -r requirements.txt \ && pip install internal-utils==x.y.z # Internal utility library COPY src/loop_log.ini /code/ # Copy logging config COPY . /code # Copy all service files EXPOSE 8002 # Document listening port ENV FLASK_APP=src.aiservice ENV FLASK_RUN_HOST=0.0.0.0 ENV FLASK_RUN_PORT=8002 ENV FLASK_ENV=production CMD ["flask", "run"] # Start Flask server ``` -------------------------------- ### Dockerfile: Python AI Service Deployment Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Configures a Docker image for deploying the Python AI service. It specifies the base Python image, sets up the working directory, installs dependencies from a requirements file and a private Azure Artifacts feed, copies application code, and defines runtime environment variables and the entry point. ```dockerfile FROM python:3.11 WORKDIR /code COPY lib/requirements.txt /code/requirements.txt ARG ARTIFACTS_PAT RUN pip install --no-cache-dir --upgrade \ -r requirements.txt \ --extra-index-url "https://build:${ARTIFACTS_PAT}@pkgs.dev.azure.com/YourOrg/YourProject/_packaging/YourFeed/pypi/simple/" COPY ./ /code/ EXPOSE 8002 ENV FLASK_APP=src/aiservice.py \ FLASK_RUN_HOST=0.0.0.0 \ FLASK_RUN_PORT=8002 \ PYTHONPATH=/code/src \ ENVIRONMENT=Production CMD ["flask", "run"] ``` -------------------------------- ### GET /metadata Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Retrieves metadata about the AI model. ```APIDOC ## GET /metadata ### Description Retrieves metadata about the AI model. ### Method GET ### Endpoint `/metadata` ### Parameters None ### Request Example None ### Response #### Success Response (200) - **metadata** (object) - An object containing various metadata about the AI model. #### Response Example ```json { "metadata": { "model_name": "ExampleModel", "version": "1.0.0", "description": "This is an example AI model." } } ``` ``` -------------------------------- ### Build and Run Docker Container (Bash) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Builds a Docker image for the ticket classifier service, passing an ARTIFACTS_PAT build argument. It then runs the container, mapping a port and using an environment file for configuration. Requires Docker. ```bash # Build docker build --build-arg ARTIFACTS_PAT="${ARTIFACTS_PAT}" -t ticket-classifier:local . # Run docker run --rm --env-file config/env.config -p 8002:8002 ticket-classifier:local ``` -------------------------------- ### GET /health Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Checks the health status of the AI service. ```APIDOC ## GET /health ### Description Checks the health status of the AI service. ### Method GET ### Endpoint `/health` ### Parameters None ### Request Example None ### Response #### Success Response (200) - **status** (string) - Indicates the health status of the service (e.g., "healthy"). #### Response Example ```json { "status": "healthy" } ``` ``` -------------------------------- ### Initialize Flask App and OpenAI Client (Python) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Sets up a Flask web application with CORS enabled and initializes an Azure OpenAI client. It imports necessary libraries and defines a routing map based on ticket category and urgency. This code is essential for the core service logic. ```python import time, json, hashlib, logging from typing import Dict, Any, Optional from flask import Flask, request, jsonify from flask_cors import CORS from openai import AzureOpenAI from Abstract_Class import GENAI_Abstract_Class from environment import Config from constants import ( TICKET_CLASSIFIER, TICKET_CATEGORY, TICKET_URGENCY, TICKET_SENTIMENT, ROUTING_QUEUE, COMMAND, ELK_INDEX ) from api_data_access_layer import log_classification_result logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s %(message)s') logger = logging.getLogger(__name__) app = Flask(__name__) CORS(app) # Required: pipeline canvas calls services cross-origin openai_client = AzureOpenAI( azure_endpoint=Config.AZURE_OPENAI_ENDPOINT, api_key=Config.AZURE_OPENAI_KEY, api_version=Config.AZURE_OPENAI_API_VERSION, ) ROUTING_MAP = { ("Billing", "Low"): ROUTING_QUEUE.L1_BILLING.value, ("Billing", "High"): ROUTING_QUEUE.L3_ESCALATION.value, ("Billing", "Critical"): ROUTING_QUEUE.PRIORITY_QUEUE.value, ("Technical", "Low"): ROUTING_QUEUE.L2_TECHNICAL.value, ("Technical", "High"): ROUTING_QUEUE.L3_ESCALATION.value, ("Technical", "Critical"): ROUTING_QUEUE.PRIORITY_QUEUE.value, ("Account", "Low"): ROUTING_QUEUE.L2_ACCOUNT.value, ("Account", "Critical"): ROUTING_QUEUE.PRIORITY_QUEUE.value, # ... (full map covers all category × urgency combinations) } ``` -------------------------------- ### Abstract Base Class for AI Services (Python) Source: https://asvskartheek.github.io/aidf-docs/developer/strategy-pattern_q= Defines the contract and skeleton for all AI services. It specifies the abstract methods that every service must implement, acting as a backbone for consistency and modularity. Example: DF_PRP_Abstract_Class.py. ```python from abc import abstractmethod class AbstractAIService: @abstractmethod def ingest(self, payload): ... @abstractmethod def preprocess(self, data): ... @abstractmethod def execute(self, data): ... @abstractmethod def on_finish(self, result): ... @abstractmethod def on_error(self, error): ... ``` -------------------------------- ### Python: Flask API Routes for AI Service Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Defines Flask routes for the AI service, including a POST endpoint for processing requests and GET endpoints for health checks and metadata retrieval. It handles request parsing, model execution, response formatting, and error handling. ```python @app.route('/api///Basic_functionality', methods=['POST']) def process_request(ai_service_id: str, deploy_env_id: str): Model = ModelStrategy() try: input_data = request.get_json(force=True) if not input_data: return jsonify(Model.on_error("Empty or invalid JSON body", 400)), 400 Model.on_start(input_data) result = Model.execute(input_data) response = Model.on_finish(result) Model.monitor_performance(response["output_kpis"]["execution_time"]) Model.explainability_monitor(input_data, response) return jsonify(response), 200 except ValueError as ve: return jsonify(Model.on_error(str(ve), error_code=422)), 422 except Exception as e: logger.exception(f"[process_request] Unhandled exception: {e}") return jsonify(Model.on_error(str(e), error_code=500)), 500 @app.route('/health', methods=['GET']) def health(): return jsonify(ModelStrategy().health_check()), 200 @app.route('/metadata', methods=['GET']) def metadata(): return jsonify(ModelStrategy().get_model_metadata()), 200 if __name__ == '__main__': app.run(host='0.0.0.0', port=8002, debug=False) ``` -------------------------------- ### Python: on_finish and on_error Response Handling Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Implements methods to format successful responses and error responses for the AI service. `on_finish` structures success data with KPIs, while `on_error` formats error messages and KPIs. Both rely on a `start_time` attribute for execution time calculation. ```python def on_finish(self, data: Dict[str, Any]) -> Dict[str, Any]: execution_time = round(time.time() - self.start_time, 4) return { "execution_status": "success", "status_code": 200, "output_kpis": { "execution_time": execution_time, "processed_records": 1, "tokens_used": data.get("tokens_used", 0), }, "service_output": {k: v for k, v in data.items() if k != "tokens_used"}, } def on_error(self, error_message: str, error_code: int = 500) -> Dict[str, Any]: execution_time = round(time.time() - self.start_time, 4) logger.error(f"[on_error] code={error_code} | {error_message}") return { "execution_status": "failed", "status_code": error_code, "error_message": str(error_message), "output_kpis": {"execution_time": execution_time}, } ``` -------------------------------- ### POST /api/{ai_service_id}/{deploy_env_id}/Basic_functionality Source: https://asvskartheek.github.io/aidf-docs/tutorial Processes a basic functionality request for a given AI service and deployment environment. It accepts input data, executes the AI model, and returns the processed result or an error. ```APIDOC ## POST /api/{ai_service_id}/{deploy_env_id}/Basic_functionality ### Description Processes a basic functionality request for a given AI service and deployment environment. It accepts input data, executes the AI model, and returns the processed result or an error. ### Method POST ### Endpoint `/api///Basic_functionality` ### Parameters #### Path Parameters - **ai_service_id** (string) - Required - The unique identifier for the AI service. - **deploy_env_id** (string) - Required - The identifier for the deployment environment. #### Query Parameters None #### Request Body - **input_payload** (object) - Required - The payload containing the input data for the AI service. - **command** (string) - Required - Processing mode. Example: "INFERENCE". - **ticket_text** (string) - Required - The raw text of the support ticket. Min 10 chars, max 8000 chars. - **ticket_id** (string) - Optional - Unique identifier for the ticket. Auto-generated if not provided. - **customer_tier** (string) - Optional - Customer tier ('standard', 'premium', 'enterprise'). ### Request Example ```json { "input_payload": { "command": "INFERENCE", "ticket_text": "User is unable to log in after the recent password reset. It seems the 2FA is not working correctly.", "ticket_id": "TK-1738291200", "customer_tier": "standard" } } ``` ### Response #### Success Response (200) - **execution_status** (string) - Status of the execution (e.g., "success"). - **status_code** (integer) - HTTP status code of the response. - **output_kpis** (object) - Key performance indicators for the execution. - **execution_time** (float) - Time taken for execution in seconds. - **processed_records** (integer) - Number of records processed. - **tokens_used** (integer) - Number of tokens used by the AI model. - **service_output** (object) - The output generated by the AI service. - **ticket_id** (string) - The processed ticket ID. - **category** (string) - Detected category of the ticket. - **subcategory** (string) - Detected subcategory of the ticket. - **urgency** (string) - Detected urgency level. - **sentiment** (string) - Detected sentiment of the ticket. - **confidence_score** (float) - Confidence score for the predictions. - **reasoning** (string) - Explanation for the predictions. - **recommended_queue** (string) - Recommended queue for the ticket. - **auto_response_template** (string) - Content for an automated response. - **customer_tier** (string) - The customer tier associated with the ticket. #### Response Example ```json { "execution_status": "success", "status_code": 200, "output_kpis": { "execution_time": 1.243, "processed_records": 1, "tokens_used": 312 }, "service_output": { "ticket_id": "TK-1738291200", "category": "Technical", "subcategory": "Login failure 2FA", "urgency": "High", "sentiment": "Frustrated", "confidence_score": 0.94, "reasoning": "User cannot access account due to 2FA failure — High urgency.", "recommended_queue": "L3-Escalation", "auto_response_template": "Thank you for reaching out about this login issue...", "customer_tier": "standard" } } ``` #### Error Response (e.g., 400, 422, 500) - **execution_status** (string) - Status of the execution (e.g., "failed"). - **status_code** (integer) - HTTP status code of the error. - **error_message** (string) - Description of the error. - **output_kpis** (object) - Key performance indicators for the execution. - **execution_time** (float) - Time taken for execution in seconds. #### Error Response Example ```json { "execution_status": "failed", "status_code": 400, "error_message": "Empty or invalid JSON body", "output_kpis": { "execution_time": 0.0123 } } ``` ``` -------------------------------- ### Load Configuration from Azure App Configuration (Python) Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= This Python code defines a singleton `ConfigLoader` to fetch credentials and settings from Azure App Configuration at service startup. It supports fallback to environment variables and trims common prefixes from configuration keys. The `Config` dataclass uses this loader to define various service configurations. ```python import os from dataclasses import dataclass from ai_service_common_utils.app_configuration import get_config_keys ENV = os.getenv("ENVIRONMENT", "Development") class ConfigLoader: """Singleton that connects to Azure App Configuration once at startup.""" _instance = None def __new__(cls): if cls._instance is None: cls._instance = super().__new__(cls) cls._instance._initialize_azure_app_config() return cls._instance def _initialize_azure_app_config(self): trim_prefixes = [ "Common.Be.", "PaasInfra.Be.", "DataMarketPlace.Be.", "Datahub.Be.", "Modalfoundry.Be.", ] self.app_config_client = get_config_keys(trim_prefixes) def get(self, key: str, default=None): try: return self.app_config_client.get(key, default) except Exception: return os.getenv(key, default) config_loader = ConfigLoader() @dataclass class Config: # ── Azure OpenAI ────────────────────────────────────────────────────────── AZURE_OPENAI_ENDPOINT: str = config_loader.get("AZURE_OPENAI_ENDPOINT") AZURE_OPENAI_KEY: str = config_loader.get("AZURE_OPENAI_KEY") AZURE_OPENAI_DEPLOYMENT: str = config_loader.get("AZURE_OPENAI_DEPLOYMENT", "gpt-4o") AZURE_OPENAI_API_VERSION: str = config_loader.get("AZURE_OPENAI_API_VERSION", "2024-08-01-preview") # ── PostgreSQL ──────────────────────────────────────────────────────────── DB_CONNECTION_STRING: str = config_loader.get("DB_CONNECTION_STRING") DB_SCHEMA: str = config_loader.get("DB_SCHEMA", "aiservices") # ── Service Identity ────────────────────────────────────────────────────── AI_SERVICE_ID: str = config_loader.get("AI_SERVICE_ID", "ticket-classifier") WORKSPACE_ID: str = config_loader.get("WORKSPACE_ID") PROJECT_ID: str = config_loader.get("PROJECT_ID") ``` -------------------------------- ### Build and Run Docker Container Source: https://asvskartheek.github.io/aidf-docs/tutorial Builds a Docker image for the ticket classifier and runs it as a container. Requires Docker and an ARTIFACTS_PAT environment variable for building. The container is exposed on port 8002. ```bash docker build --build-arg ARTIFACTS_PAT="${ARTIFACTS_PAT}" -t ticket-classifier:local . docker run --rm --env-file config/env.config -p 8002:8002 ticket-classifier:local ``` -------------------------------- ### POST /api/{ai_service_id}/{deploy_env_id}/Basic_functionality Source: https://asvskartheek.github.io/aidf-docs/tutorial_q= Processes a given request using the specified AI service and deployment environment. It handles input validation, execution, and response formatting, including error handling. ```APIDOC ## POST /api/{ai_service_id}/{deploy_env_id}/Basic_functionality ### Description Processes a given request using the specified AI service and deployment environment. It handles input validation, execution, and response formatting, including error handling. ### Method POST ### Endpoint `/api///Basic_functionality` ### Parameters #### Path Parameters - **ai_service_id** (string) - Required - The ID of the AI service. - **deploy_env_id** (string) - Required - The ID of the deployment environment. #### Query Parameters None #### Request Body - **command** (string) - Required - Processing mode. Possible values: "INFERENCE", "BATCH", "HEALTH". - **ticket_text** (string) - Required - The raw text of the support ticket. Min 10 chars, max 8000 chars. - **ticket_id** (string) - Optional - Unique identifier for the ticket. Auto-generated if not provided. - **customer_tier** (string) - Optional - Customer tier. Possible values: "standard", "premium", "enterprise". ### Request Example ```json { "input_payload": { "command": { "value": "INFERENCE" }, "ticket_text": { "value": "User is unable to log in due to a two-factor authentication issue." }, "ticket_id": { "value": "TK-1234567890" }, "customer_tier": { "value": "premium" } } } ``` ### Response #### Success Response (200) - **execution_status** (string) - Status of the execution (e.g., "success"). - **status_code** (integer) - HTTP status code of the response. - **output_kpis** (object) - Key performance indicators for the execution. - **execution_time** (float) - Time taken for execution in seconds. - **processed_records** (integer) - Number of records processed. - **tokens_used** (integer) - Number of tokens used by the AI model. - **service_output** (object) - The actual output from the AI service. - **ticket_id** (string) - The processed ticket ID. - **category** (string) - The category assigned to the ticket. - **subcategory** (string) - The subcategory assigned to the ticket. - **urgency** (string) - The urgency level of the ticket. - **sentiment** (string) - The sentiment detected in the ticket text. - **confidence_score** (float) - The confidence score of the AI's prediction. - **reasoning** (string) - Explanation for the AI's output. - **recommended_queue** (string) - The recommended queue for the ticket. - **auto_response_template** (string) - A template for an automated response. - **customer_tier** (string) - The customer tier associated with the ticket. #### Response Example ```json { "execution_status": "success", "status_code": 200, "output_kpis": { "execution_time": 1.243, "processed_records": 1, "tokens_used": 312 }, "service_output": { "ticket_id": "TK-1738291200", "category": "Technical", "subcategory": "Login failure 2FA", "urgency": "High", "sentiment": "Frustrated", "confidence_score": 0.94, "reasoning": "User cannot access account due to 2FA failure — High urgency.", "recommended_queue": "L3-Escalation", "auto_response_template": "Thank you for reaching out about this login issue...", "customer_tier": "standard" } } ``` #### Error Response (400, 422, 500) - **execution_status** (string) - Status of the execution (e.g., "failed"). - **status_code** (integer) - HTTP status code of the error. - **error_message** (string) - A message describing the error. - **output_kpis** (object) - Key performance indicators for the execution. - **execution_time** (float) - Time taken for execution in seconds. #### Error Response Example (400 Bad Request) ```json { "execution_status": "failed", "status_code": 400, "error_message": "Empty or invalid JSON body", "output_kpis": { "execution_time": 0.0012 } } ``` #### Error Response Example (422 Unprocessable Entity) ```json { "execution_status": "failed", "status_code": 422, "error_message": "Invalid input data: ...", "output_kpis": { "execution_time": 0.0056 } } ``` #### Error Response Example (500 Internal Server Error) ```json { "execution_status": "failed", "status_code": 500, "error_message": "An unexpected error occurred: ...", "output_kpis": { "execution_time": 0.0123 } } ``` ``` -------------------------------- ### Zip Service Directory for Deployment Source: https://asvskartheek.github.io/aidf-docs/first-steps Compresses a service directory into a ZIP file, which is a prerequisite for uploading and deploying a new AI service. The ZIP file name must match the '' field in the manifest.xml file. ```bash zip -r my_service.zip my_service/ ``` -------------------------------- ### Essential Docker Commands Source: https://asvskartheek.github.io/aidf-docs/developer/docker Provides fundamental Docker commands for building an image from a Dockerfile, running a container locally with port mapping and environment variables, and pushing/pulling images to/from a registry like Azure Container Registry (ACR). ```bash # Build image from Dockerfile in current directory docker build -t myservice:latest . # Run container locally on port 8002 docker run -d -p 8002:8002 myservice:latest # Run with environment variable docker run -d -p 8002:8002 -e ENVIRONMENT=Development myservice:latest # Push to Azure Container Registry docker push myregistry.azurecr.io/myservice:latest # Pull from registry docker pull myregistry.azurecr.io/myservice:latest ```