mirror of
https://github.com/run-llama/workflows-py.git
synced 2026-07-19 16:53:35 -04:00
10d619a551
* rename llama-index-workflows-client -> llama-agents-client * integration tests too
323 lines
9.4 KiB
Plaintext
323 lines
9.4 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Workflow Server Example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "H_jVQJE-_mCO"
|
|
},
|
|
"source": [
|
|
"Example demonstrating how to use the WorkflowServer with event streaming.\n",
|
|
"\n",
|
|
"This example shows how to:\n",
|
|
"1. Create workflows that emit streaming events\n",
|
|
"2. Set up the server with event streaming support\n",
|
|
"3. Register workflows\n",
|
|
"4. Run the server\n",
|
|
"5. Make HTTP requests to execute workflows\n",
|
|
"6. Stream real-time events from running workflows using the /events endpoint"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {
|
|
"collapsed": true,
|
|
"id": "jnPCUjPk_Kfq"
|
|
},
|
|
"outputs": [],
|
|
"source": "%pip install llama-agents-server"
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {
|
|
"id": "V5j--gYldhud"
|
|
},
|
|
"source": [
|
|
"## Run the server in background"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "L5AsbMnk_xuG",
|
|
"outputId": "0cb5ec84-575e-4484-90ab-94ff73e241d8"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: Started server process [27939]\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: 27939\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": 5,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "Fm1Z0YGmgmRH",
|
|
"outputId": "f4c87973-1034-4fe3-c5f4-82d8c0d78db2"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: 127.0.0.1:65182 - \"GET /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 server is up and running\n",
|
|
"!curl http://localhost:8000/health"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "TD6tz50jeAkf",
|
|
"outputId": "2a622940-7e0b-445a-9292-e89420026ef6"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: 127.0.0.1:65184 - \"GET /workflows HTTP/1.1\" 200 OK\n",
|
|
"{\"workflows\":[\"greeting\",\"math\",\"processing\"]}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# List available workflows\n",
|
|
"!curl http://localhost:8000/workflows"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "wJD4rifveJMt",
|
|
"outputId": "ab382fc4-8e94-4779-826f-0db4c4986f69"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: 127.0.0.1:65190 - \"POST /workflows/greeting/run HTTP/1.1\" 200 OK\n",
|
|
"{\"handler_id\":\"3podAAguhA\",\"workflow_name\":\"greeting\",\"run_id\":\"KD7Y2S0e0h\",\"error\":null,\"result\":{\"value\":{\"result\":\"Hello, Alice!\"},\"qualified_name\":\"workflows.events.StopEvent\",\"type\":\"StopEvent\",\"types\":null},\"status\":\"completed\",\"started_at\":\"2026-01-29T22:36:48.596733+00:00\",\"updated_at\":\"2026-01-29T22:36:49.500898+00:00\",\"completed_at\":\"2026-01-29T22:36:49.500898+00:00\"}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Run greeting workflow\n",
|
|
"!curl -X POST http://localhost:8000/workflows/greeting/run \\\n",
|
|
" -H \"Content-Type: application/json\" \\\n",
|
|
" -d '{\"kwargs\": {\"name\": \"Alice\"}}'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "UkO28ZgxeOyT",
|
|
"outputId": "b0543b29-e102-4ba9-a44c-4115c89c0e44"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: 127.0.0.1:65196 - \"POST /workflows/math/run HTTP/1.1\" 200 OK\n",
|
|
"{\"handler_id\":\"kITlArjbWm\",\"workflow_name\":\"math\",\"run_id\":\"t3jXb1nGtV\",\"error\":null,\"result\":{\"value\":{\"result\":{\"a\":10,\"b\":5,\"operation\":\"multiply\",\"result\":50}},\"qualified_name\":\"workflows.events.StopEvent\",\"type\":\"StopEvent\",\"types\":null},\"status\":\"completed\",\"started_at\":\"2026-01-29T22:36:56.341122+00:00\",\"updated_at\":\"2026-01-29T22:36:56.341812+00:00\",\"completed_at\":\"2026-01-29T22:36:56.341812+00:00\"}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# Run math workflow\n",
|
|
"!curl -X POST http://localhost:8000/workflows/math/run \\\n",
|
|
" -H \"Content-Type: application/json\" \\\n",
|
|
" -d '{\"kwargs\": {\"a\": 10, \"b\": 5, \"operation\": \"multiply\"}}'"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 36,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "-PQ6vt56eTwp",
|
|
"outputId": "7c7f0007-ecd5-4c68-8275-0cbecc15c4d8"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Got handler id: vuyyRzWikr\n",
|
|
"{\"result\":{\"a\":100,\"b\":25,\"operation\":\"divide\",\"result\":4.0}}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%bash\n",
|
|
"# Run workflow with nowait\n",
|
|
"handler_id=$(curl -sX POST http://localhost:8000/workflows/math/run-nowait \\\n",
|
|
" -H \"Content-Type: application/json\" \\\n",
|
|
" -d '{\"kwargs\": {\"a\": 100, \"b\": 25, \"operation\": \"divide\"}}' | jq -r \".handler_id\" )\n",
|
|
"printf \"Got handler id: ${handler_id}\\n\\n\"\n",
|
|
"\n",
|
|
"# Wait for the workflow to run in background\n",
|
|
"sleep 1\n",
|
|
"\n",
|
|
"# Fetch the result asynchronously\n",
|
|
"curl -s http://localhost:8000/results/${handler_id}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {
|
|
"colab": {
|
|
"base_uri": "https://localhost:8080/"
|
|
},
|
|
"id": "iR4-fBOujIXd",
|
|
"outputId": "b35d418f-bbd6-47b9-b0de-deac1ac26bd1"
|
|
},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"INFO: 127.0.0.1:65202 - \"POST /workflows/greeting/run-nowait HTTP/1.1\" 200 OK\n",
|
|
"Got handler id: gbqSi1fzGJ\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Streaming events...\n",
|
|
"INFO: 127.0.0.1:65204 - \"GET /events/gbqSi1fzGJ?sse=true HTTP/1.1\" 200 OK\n",
|
|
"data: {\"value\":{\"sequence\":0},\"qualified_name\":\"__main__.StreamEvent\",\"type\":\"StreamEvent\",\"types\":null}\n",
|
|
"\n",
|
|
"data: {\"value\":{\"sequence\":1},\"qualified_name\":\"__main__.StreamEvent\",\"type\":\"StreamEvent\",\"types\":null}\n",
|
|
"\n",
|
|
"data: {\"value\":{\"sequence\":2},\"qualified_name\":\"__main__.StreamEvent\",\"type\":\"StreamEvent\",\"types\":null}\n",
|
|
"\n",
|
|
"data: {\"value\":{\"result\":\"Hello, Async User!\"},\"qualified_name\":\"workflows.events.StopEvent\",\"type\":\"StopEvent\",\"types\":null}\n",
|
|
"\n",
|
|
"\n",
|
|
"Final result:\n",
|
|
"INFO: 127.0.0.1:65206 - \"GET /results/gbqSi1fzGJ HTTP/1.1\" 200 OK\n",
|
|
"{\"handler_id\":\"gbqSi1fzGJ\",\"workflow_name\":\"greeting\",\"run_id\":\"M5AtcoxBty\",\"error\":null,\"result\":\"Hello, Async User!\",\"status\":\"completed\",\"started_at\":\"2026-01-29T22:37:02.375925+00:00\",\"updated_at\":\"2026-01-29T22:37:03.279696+00:00\",\"completed_at\":\"2026-01-29T22:37:03.279696+00:00\"}"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"%%bash\n",
|
|
"# Stream events from workflow\n",
|
|
"\n",
|
|
"# 1. Run workflow with nowait\n",
|
|
"handler_id=$(curl -sX POST http://localhost:8000/workflows/greeting/run-nowait \\\n",
|
|
" -H \"Content-Type: application/json\" \\\n",
|
|
" -d '{\"kwargs\": {\"name\": \"Async User\"}}' | jq -r \".handler_id\" )\n",
|
|
"printf \"Got handler id: ${handler_id}\\n\\n\"\n",
|
|
"\n",
|
|
"# Wait for the workflow to run in background\n",
|
|
"sleep 1\n",
|
|
"\n",
|
|
"printf \"Streaming events...\\n\"\n",
|
|
"# 2. Stream events using Server-Sent Events using SSE format\n",
|
|
"curl -s http://localhost:8000/events/$handler_id?sse=true\n",
|
|
"\n",
|
|
"\n",
|
|
"printf \"\\nFinal result:\\n\"\n",
|
|
"# 3. Get the final result after events complete\n",
|
|
"curl -s http://localhost:8000/results/$handler_id"
|
|
]
|
|
}
|
|
],
|
|
"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
|
|
}
|