Skip to content

2.6 Setup Project Structure

This repository contains the following structure to start with. The *.sample folders or files are there for reference only, so you can check your work.

1
2
3
4
data/            # Contains application data (initial)
docs/            # Contains docs and guides (content)
src.sample/      # Sample src/ folder
.env.sample       # Sample .env file

1. Define src/ folder for code

In this workshop, start by creating a new src/ folder and populating it from scratch to get a sense for the development workflow. Start by creating this folder structure:

1
2
3
mkdir src/
mkdir src/api
mkdir src/api/assets

Your directory structure should now look like this:

1
2
3
4
5
6
7
data/
docs/
src.sample/
.env.sample
src/
src/api
src/api/assets

2. Add src/config.py helper script

For convenience, let's copy this from the sample location - then review the code to see what it does. Use this command at the root of the repo:

1
cp src.sample/api/config.py src/api/.

Expand the code below to get a sense of what this helper does.

  1. Sets the ASSET_PATH to the assets/ folder in the same directory.
  2. Configures the app logger and enables telemetry logging (traces) for app.
Click to expand and view the helper script
src/api/config.py
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# ruff: noqa: ANN201, ANN001
import os
import sys
import pathlib
import logging
from azure.identity import DefaultAzureCredential
from azure.ai.projects import AIProjectClient
from azure.ai.inference.tracing import AIInferenceInstrumentor

# load environment variables from the .env file
from dotenv import load_dotenv

load_dotenv()

# Set "./assets" as the path where assets are stored, resolving the absolute path:
ASSET_PATH = pathlib.Path(__file__).parent.resolve() / "assets"

# Configure an root app logger that prints info level logs to stdout
logger = logging.getLogger("app")
logger.setLevel(logging.INFO)
logger.addHandler(logging.StreamHandler(stream=sys.stdout))


# Returns a module-specific logger, inheriting from the root app logger
def get_logger(module_name):
    return logging.getLogger(f"app.{module_name}")


# Enable instrumentation and logging of telemetry to the project
def enable_telemetry(log_to_project: bool = False):
    AIInferenceInstrumentor().instrument()

    # enable logging message contents
    os.environ["AZURE_TRACING_GEN_AI_CONTENT_RECORDING_ENABLED"] = "true"

    if log_to_project:
        from azure.monitor.opentelemetry import configure_azure_monitor

        project = AIProjectClient.from_connection_string(
            conn_str=os.environ["AIPROJECT_CONNECTION_STRING"], credential=DefaultAzureCredential()
        )
        tracing_link = f"https://ai.azure.com/tracing?wsid=/subscriptions/{project.scope['subscription_id']}/resourceGroups/{project.scope['resource_group_name']}/providers/Microsoft.MachineLearningServices/workspaces/{project.scope['project_name']}"
        application_insights_connection_string = project.telemetry.get_connection_string()
        if not application_insights_connection_string:
            logger.warning(
                "No application insights configured, telemetry will not be logged to project. Add application insights at:"
            )
            logger.warning(tracing_link)

            return

        configure_azure_monitor(connection_string=application_insights_connection_string)
        logger.info("Enabled telemetry logging to project, view traces at:")
        logger.info(tracing_link)

CONGRATULATIONS! Your development environment is ready to use.