Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Functions Python (v2) – Agent + Databricks Sample

This project is an Azure Functions app (Python Programming Model v2) exposing a single HTTP endpoint that:

  • Accepts a user_input prompt.
  • Runs an agent (OpenAI Agents SDK) which can call a Databricks SQL tool.
  • Returns a JSON response with the final output.

Key files

  • function_app.py: Function definitions, agent setup, Databricks tool.
  • requirements.txt: Python dependencies.
  • local.settings.json: Local-only app settings (not deployed).
  • data/team_stats_data_model.json: Bundled JSON used in agent instructions.
  • openapi.yaml: OpenAPI 3.1.0 spec for /api/agent_invoke (GET documented).

Prerequisites

  • Python 3.10 or 3.11
  • Azure Functions Core Tools v4
  • Azure CLI

Setup & Run Locally

cd /Users/vince/dev_projects/azure/agent_functionapp
python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
func start

Configure local settings

Edit local.settings.json (values are examples; do not commit secrets):

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "DATABRICKS_HOST": "https://<your-databricks-workspace-host>",
    "DATABRICKS_TOKEN": "<your-personal-access-token>",
    "DATABRICKS_WAREHOUSE_ID": "<optional-warehouse-id>"
  }
}

Test locally

  • GET
curl "http://localhost:7071/api/agent_invoke?user_input=Hello"
  • POST
curl -X POST http://localhost:7071/api/agent_invoke \
  -H "Content-Type: application/json" \
  -d '{"user_input":"Use query_databricks to run: SELECT 1 AS x"}'

Responses

{
  "last_agent": "Assistant",
  "final_output": "..."
}

On error (centralized error handling):

{
  "error": "Function execution failed",
  "message": "<exception message>"
}

Databricks connectivity

The tool uses the SQL Statements REST API with INLINE + JSON_ARRAY results. Ensure these app settings are set locally (above) and in Azure (see Deploy below):

  • DATABRICKS_HOST (e.g., https://dbc-xxxx.cloud.databricks.com)
  • DATABRICKS_TOKEN (Databricks PAT)
  • DATABRICKS_WAREHOUSE_ID (optional; recommended)

Bundled JSON data

data/team_stats_data_model.json is packaged with the app and read at import time to inform the agent. Place your own JSON under data/ and load it via a module-scope read for best cold-start performance.

OpenAPI

  • File: openapi.yaml (OpenAPI 3.1.0)
  • Documents the GET /api/agent_invoke endpoint. Import it into Swagger UI/Postman.
  • You can extend it to include the POST body schema if desired.

Deploy to Azure

az login
az account set --subscription "<SUBSCRIPTION>"

RESOURCE_GROUP="rg-agent-func"
LOCATION="eastus"
APP_NAME="agent-func-$RANDOM"   # must be globally unique
STORAGE_ACCOUNT="stagent$RANDOM" # must be globally unique

az group create --name $RESOURCE_GROUP --location $LOCATION
az storage account create --name $STORAGE_ACCOUNT --resource-group $RESOURCE_GROUP --location $LOCATION --sku Standard_LRS
az functionapp create \
  --resource-group $RESOURCE_GROUP \
  --consumption-plan-location $LOCATION \
  --name $APP_NAME \
  --storage-account $STORAGE_ACCOUNT \
  --functions-version 4 \
  --runtime python \
  --runtime-version 3.11 \
  --os-type Linux

# App settings (set secrets here, not in code)
az functionapp config appsettings set \
  --name $APP_NAME \
  --resource-group $RESOURCE_GROUP \
  --settings \
  DATABRICKS_HOST="https://<your-databricks-host>" \
  DATABRICKS_TOKEN="<your-pat>" \
  DATABRICKS_WAREHOUSE_ID="<your-warehouse>"

# Publish from project folder
cd /Users/vince/dev_projects/azure/agent_functionapp
func azure functionapp publish $APP_NAME --python

# Invoke
curl "https://$APP_NAME.azurewebsites.net/api/agent_invoke?user_input=Hello"

CORS (if calling from a browser)

az functionapp cors add --resource-group $RESOURCE_GROUP --name $APP_NAME --allowed-origins https://portal.azure.com https://ms.portal.azure.com
# Optionally your app origins
az functionapp cors add --resource-group $RESOURCE_GROUP --name $APP_NAME --allowed-origins http://localhost:3000

Troubleshooting

  • Module not found agents: ensure you start the host from the venv and dependencies are installed.
    source .venv/bin/activate
    pip install -r requirements.txt
    python -c "import agents; print(agents.__file__)"
    func start
  • Malformed input / 400: send user_input either as ?user_input=... or { "user_input": "..." } JSON body.
  • 500 with JSON error: check az webapp log tail --resource-group $RESOURCE_GROUP --name $APP_NAME and your app settings.
  • Do not commit secrets in code or in local.settings.json. Use Azure App Settings; rotate any exposed tokens.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages