{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# FastAPI Example" ] }, { "cell_type": "markdown", "metadata": { "id": "H_jVQJE-_mCO" }, "source": [ "Example demonstrating how to integrate WorkflowServer into an existing FastAPI application.\n", "\n", "This example shows how to:\n", "1. Create a FastAPI application with existing routes\n", "2. Set up WorkflowServer with workflows\n", "3. Mount the workflow server as a sub-application\n", "4. Run the combined application" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true, "id": "jnPCUjPk_Kfq" }, "outputs": [], "source": "%pip install llama-agents-server fastapi" }, { "cell_type": "markdown", "metadata": { "id": "V5j--gYldhud" }, "source": [ "## Run the server in background" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "L5AsbMnk_xuG", "outputId": "35ba5e76-3e81-43c2-d2cb-504378ed9d41" }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO: Started server process [26386]\n", "INFO: Waiting for application startup.\n", "INFO: Application startup complete.\n", "INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Server started with PID: 26386\n" ] } ], "source": [ "import subprocess\n", "import time\n", "\n", "# Start server in background using subprocess\n", "server_process = subprocess.Popen([\"python\", \"server.py\"])\n", "time.sleep(2) # Wait for server to start\n", "print(f\"Server started with PID: {server_process.pid}\")" ] }, { "cell_type": "markdown", "metadata": { "id": "bPbCXF_udl4K" }, "source": [ "## Interact with the server" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "H_NVM5ovmyk_", "outputId": "17738424-95fd-4869-8b0c-19419160ad59" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: 127.0.0.1:65035 - \"GET /users/123 HTTP/1.1\" 200 OK\n", "{\"user_id\":123,\"name\":\"User 123\",\"email\":\"user123@example.com\"}" ] } ], "source": [ "# Confirm existing routes are there\n", "!curl -s http://localhost:8000/users/123" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Fm1Z0YGmgmRH", "outputId": "7150949c-769a-4f6d-d5a8-c2cbb74c599e" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: 127.0.0.1:65039 - \"GET /wf-server/health HTTP/1.1\" 200 OK\n", "{\"status\":\"healthy\",\"loaded_workflows\":0,\"active_workflows\":0,\"idle_workflows\":0}" ] } ], "source": [ "# Hit the health endpoint to see the workflow server is available at the path /wf-server\n", "!curl http://localhost:8000/wf-server/health" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "TD6tz50jeAkf", "outputId": "8c2bac46-dc74-4dbd-fe80-2fb4eaac47e5" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: 127.0.0.1:65043 - \"GET /wf-server/workflows HTTP/1.1\" 200 OK\n", "{\"workflows\":[\"user_processing\",\"notification\"]}" ] } ], "source": [ "# List available workflows\n", "!curl http://localhost:8000/wf-server/workflows" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "wJD4rifveJMt", "outputId": "c2b124f9-234d-4e10-b257-164e6199c282" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "INFO: 127.0.0.1:65050 - \"POST /wf-server/workflows/user_processing/run HTTP/1.1\" 200 OK\n", "{\"handler_id\":\"Mu2qZvQzVu\",\"workflow_name\":\"user_processing\",\"run_id\":\"7hMHiP24AL\",\"error\":null,\"result\":{\"value\":{\"result\":{\"processed_name\":\"ALICE\",\"domain\":\"company.com\",\"status\":\"processed\"}},\"qualified_name\":\"workflows.events.StopEvent\",\"type\":\"StopEvent\",\"types\":null},\"status\":\"completed\",\"started_at\":\"2026-01-29T22:34:33.157083+00:00\",\"updated_at\":\"2026-01-29T22:34:33.157792+00:00\",\"completed_at\":\"2026-01-29T22:34:33.157792+00:00\"}" ] } ], "source": [ "# Run user processing workflow\n", "!curl -X POST http://localhost:8000/wf-server/workflows/user_processing/run \\\n", " -H \"Content-Type: application/json\" \\\n", " -d '{\"kwargs\": {\"user_data\": {\"name\": \"alice\", \"email\": \"alice@company.com\"}}}'" ] } ], "metadata": { "colab": { "provenance": [] }, "kernelspec": { "display_name": ".venv", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.18" } }, "nbformat": 4, "nbformat_minor": 0 }