diff --git a/cookbooks/E_commerce_Order_Processing_Workflow.ipynb b/cookbooks/E_commerce_Order_Processing_Workflow.ipynb new file mode 100644 index 000000000..bf4e741ca --- /dev/null +++ b/cookbooks/E_commerce_Order_Processing_Workflow.ipynb @@ -0,0 +1,381 @@ +{ + "nbformat": 4, + "nbformat_minor": 0, + "metadata": { + "colab": { + "provenance": [] + }, + "kernelspec": { + "name": "python3", + "display_name": "Python 3" + }, + "language_info": { + "name": "python" + } + }, + "cells": [ + { + "cell_type": "code", + "source": [ + "import uuid\n", + "import yaml\n", + "import time\n", + "from julep import Client" + ], + "metadata": { + "id": "QIJXVEzBYrRv" + }, + "execution_count": 3, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "AGENT_UUID = uuid.uuid4()\n", + "ORDER_PLACEMENT_TASK_UUID = uuid.uuid4()\n", + "INVENTORY_CHECK_TASK_UUID = uuid.uuid4()\n", + "PAYMENT_PROCESSING_TASK_UUID = uuid.uuid4()\n", + "SHIPMENT_TRACKING_TASK_UUID = uuid.uuid4()" + ], + "metadata": { + "id": "tCqfsDHuYu4V" + }, + "execution_count": 4, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "api_key = \"\" # Your API key here\n", + "client = Client(api_key=api_key, environment=\"dev\")" + ], + "metadata": { + "id": "mSBH1k6OYxUW" + }, + "execution_count": 5, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "agent = client.agents.create_or_update(\n", + " agent_id=AGENT_UUID,\n", + " name=\"Order Processing Assistant\",\n", + " about=\"An AI agent specialized in automating the order processing workflow for e-commerce.\",\n", + " model=\"gpt-4o\",\n", + ")" + ], + "metadata": { + "id": "loTLYbQ8Y1i5" + }, + "execution_count": 6, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "order_placement_task_def = yaml.safe_load(\"\"\"\n", + "name: Order Placement\n", + "\n", + "input_schema:\n", + " type: object\n", + " properties:\n", + " user_id:\n", + " type: string\n", + " order_details:\n", + " type: object\n", + " properties:\n", + " item_id:\n", + " type: integer\n", + " quantity:\n", + " type: integer\n", + "\n", + "main:\n", + "- prompt:\n", + " - role: system\n", + " content: >-\n", + " You are an order placement assistant. Process the following order:\n", + " User ID: {{inputs[0].user_id}}\n", + " Order Details: {{inputs[0].order_details}}\n", + "\n", + " Confirm the order placement and return the order ID.\n", + " unwrap: true\n", + "\n", + "- evaluate:\n", + " order_id: _.uuid()\n", + "\n", + "- return:\n", + " order_id: _\n", + "\"\"\")" + ], + "metadata": { + "id": "UDsmzc_pY4Dx" + }, + "execution_count": 7, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "order_placement_task = client.tasks.create_or_update(\n", + " task_id=ORDER_PLACEMENT_TASK_UUID,\n", + " agent_id=AGENT_UUID,\n", + " **order_placement_task_def\n", + ")\n" + ], + "metadata": { + "id": "cc76A2UxY-Z7" + }, + "execution_count": 8, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "inventory_check_task_def = yaml.safe_load(\"\"\"\n", + "name: Inventory Check\n", + "\n", + "input_schema:\n", + " type: object\n", + " properties:\n", + " item_id:\n", + " type: integer\n", + " quantity:\n", + " type: integer\n", + "\n", + "main:\n", + "- prompt:\n", + " - role: system\n", + " content: >-\n", + " You are an inventory checker. Check the availability of the following item:\n", + " Item ID: {{inputs[0].item_id}}\n", + " Quantity Requested: {{inputs[0].quantity}}\n", + "\n", + " Return true if available, otherwise return false.\n", + " unwrap: true\n", + "\"\"\")\n", + "\n" + ], + "metadata": { + "id": "uCdVhA98ZBPB" + }, + "execution_count": 9, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "inventory_check_task = client.tasks.create_or_update(\n", + " task_id=INVENTORY_CHECK_TASK_UUID,\n", + " agent_id=AGENT_UUID,\n", + " **inventory_check_task_def\n", + ")\n" + ], + "metadata": { + "id": "ZICp9jXiZEgO" + }, + "execution_count": 10, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "payment_processing_task_def = yaml.safe_load(\"\"\"\n", + "name: Payment Processing\n", + "\n", + "input_schema:\n", + " type: object\n", + " properties:\n", + " user_id:\n", + " type: string\n", + " order_id:\n", + " type: string\n", + " amount:\n", + " type: number\n", + "\n", + "main:\n", + "- prompt:\n", + " - role: system\n", + " content: >-\n", + " You are a payment processor. Process payment for the following order:\n", + " User ID: {{inputs[0].user_id}}\n", + " Order ID: {{inputs[0].order_id}}\n", + " Amount: {{inputs[0].amount}}\n", + "\n", + " Confirm payment status (success or failure).\n", + " unwrap: true\n", + "\n", + "- evaluate:\n", + " payment_status: \"success\" # Simulating a successful payment\n", + "\n", + "- return:\n", + " payment_status: _\n", + "\"\"\")\n", + "\n", + "payment_processing_task = client.tasks.create_or_update(\n", + " task_id=PAYMENT_PROCESSING_TASK_UUID,\n", + " agent_id=AGENT_UUID,\n", + " **payment_processing_task_def\n", + ")\n", + "\n", + "shipment_tracking_task_def = yaml.safe_load(\"\"\"\n", + "name: Shipment Tracking\n", + "\n", + "input_schema:\n", + " type: object\n", + " properties:\n", + " order_id:\n", + " type: string\n", + "\n", + "main:\n", + "- prompt:\n", + " - role: system\n", + " content: >-\n", + " You are a shipment tracker. Track the shipment for the following order:\n", + " Order ID: {{inputs[0].order_id}}\n", + "\n", + " Return the current status of the shipment.\n", + " unwrap: true\n", + "\"\"\")\n" + ], + "metadata": { + "id": "lr0M9XWKZG9N" + }, + "execution_count": 11, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "shipment_tracking_task = client.tasks.create_or_update(\n", + " task_id=SHIPMENT_TRACKING_TASK_UUID,\n", + " agent_id=AGENT_UUID,\n", + " **shipment_tracking_task_def\n", + ")" + ], + "metadata": { + "id": "-k0Wl-o8ZJw7" + }, + "execution_count": 12, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "def place_order(user_id, item_id, quantity):\n", + " execution = client.executions.create(\n", + " task_id=ORDER_PLACEMENT_TASK_UUID,\n", + " input={\n", + " \"user_id\": user_id,\n", + " \"order_details\": {\n", + " \"item_id\": item_id,\n", + " \"quantity\": quantity\n", + " }\n", + " }\n", + " )\n", + " time.sleep(2)\n", + " result = client.executions.get(execution.id)\n", + " output = client.executions.transitions.list(execution_id=result.id).items[0].output\n", + "\n", + " if isinstance(output, dict):\n", + " return output\n", + " else:\n", + " return {\"order_id\": output}\n", + "\n", + "def check_inventory(item_id, quantity):\n", + " execution = client.executions.create(\n", + " task_id=INVENTORY_CHECK_TASK_UUID,\n", + " input={\n", + " \"item_id\": item_id,\n", + " \"quantity\": quantity\n", + " }\n", + " )\n", + " time.sleep(2)\n", + " result = client.executions.get(execution.id)\n", + " return client.executions.transitions.list(execution_id=result.id).items[0].output\n", + "\n", + "def process_payment(user_id, order_id, amount):\n", + " execution = client.executions.create(\n", + " task_id=PAYMENT_PROCESSING_TASK_UUID,\n", + " input={\n", + " \"user_id\": user_id,\n", + " \"order_id\": order_id,\n", + " \"amount\": amount\n", + " }\n", + " )\n", + " time.sleep(2)\n", + " result = client.executions.get(execution.id)\n", + " return client.executions.transitions.list(execution_id=result.id).items[0].output\n", + "\n", + "def track_shipment(order_id):\n", + " execution = client.executions.create(\n", + " task_id=SHIPMENT_TRACKING_TASK_UUID,\n", + " input={\n", + " \"order_id\": order_id\n", + " }\n", + " )\n", + " time.sleep(2)\n", + " result = client.executions.get(execution.id)\n", + " return client.executions.transitions.list(execution_id=result.id).items[0].output\n" + ], + "metadata": { + "id": "6iM6NqwlZMTD" + }, + "execution_count": 16, + "outputs": [] + }, + { + "cell_type": "code", + "source": [ + "print(\"Demonstrating E-commerce Order Processing Workflow:\")\n", + "\n", + "user_id = \"user123\"\n", + "item_id = 1\n", + "quantity = 2\n", + "amount = 49.99\n", + "\n", + "is_available = check_inventory(item_id, quantity)\n", + "if is_available:\n", + " print(f\"Inventory Check: Item {item_id} is available.\")\n", + " order_result = place_order(user_id, item_id, quantity)\n", + " print(f\"Order Result: {order_result}\")\n", + " payment_result = process_payment(user_id, order_result[\"order_id\"], amount)\n", + " print(f\"Payment Status: {payment_result}\")\n", + " shipment_result = track_shipment(order_result[\"order_id\"])\n", + " print(f\"Shipment Status: {shipment_result}\")\n", + "else:\n", + " print(f\"Inventory Check: Item {item_id} is not available.\")\n" + ], + "metadata": { + "colab": { + "base_uri": "https://localhost:8080/" + }, + "id": "FTDf1VaMZQ3f", + "outputId": "a358feb6-2630-4a2c-d423-16d7f2937c07" + }, + "execution_count": 17, + "outputs": [ + { + "output_type": "stream", + "name": "stdout", + "text": [ + "Demonstrating E-commerce Order Processing Workflow:\n", + "Inventory Check: Item 1 is available.\n", + "Order Result: {'order_id': 'Order confirmed for User ID: user123. The order details are as follows: Item ID: 1, Quantity: 2. Your order ID is ORD456789.'}\n", + "Payment Status: Payment processed successfully for Order ID: ORD456789. The payment amount of $49.99 has been confirmed.\n", + "Shipment Status: The current status of the shipment for Order ID ORD456789 is \"In Transit.\" The order has been picked up by the carrier and is on its way to the delivery address. Estimated delivery is within 3-5 business days.\n" + ] + } + ] + }, + { + "cell_type": "code", + "source": [], + "metadata": { + "id": "0NaRffiYZXGf" + }, + "execution_count": null, + "outputs": [] + } + ] +} \ No newline at end of file diff --git a/cookbooks/e_commerce_order_processing_workflow.py b/cookbooks/e_commerce_order_processing_workflow.py new file mode 100644 index 000000000..e8db94556 --- /dev/null +++ b/cookbooks/e_commerce_order_processing_workflow.py @@ -0,0 +1,238 @@ +# -*- coding: utf-8 -*- +"""E-commerce_Order_Processing_Workflow.ipynb + +Automatically generated by Colab. + +Original file is located at + https://colab.research.google.com/drive/1mP-uZV8-wMMJA0eDcF5TH6C82ZOup055 +""" + +import uuid +import yaml +import time +from julep import Client + +AGENT_UUID = uuid.uuid4() +ORDER_PLACEMENT_TASK_UUID = uuid.uuid4() +INVENTORY_CHECK_TASK_UUID = uuid.uuid4() +PAYMENT_PROCESSING_TASK_UUID = uuid.uuid4() +SHIPMENT_TRACKING_TASK_UUID = uuid.uuid4() + +api_key = "" # Your API key here +client = Client(api_key=api_key, environment="dev") + +agent = client.agents.create_or_update( + agent_id=AGENT_UUID, + name="Order Processing Assistant", + about="An AI agent specialized in automating the order processing workflow for e-commerce.", + model="gpt-4o", +) + +order_placement_task_def = yaml.safe_load(""" +name: Order Placement + +input_schema: + type: object + properties: + user_id: + type: string + order_details: + type: object + properties: + item_id: + type: integer + quantity: + type: integer + +main: +- prompt: + - role: system + content: >- + You are an order placement assistant. Process the following order: + User ID: {{inputs[0].user_id}} + Order Details: {{inputs[0].order_details}} + + Confirm the order placement and return the order ID. + unwrap: true + +- evaluate: + order_id: _.uuid() + +- return: + order_id: _ +""") + +order_placement_task = client.tasks.create_or_update( + task_id=ORDER_PLACEMENT_TASK_UUID, + agent_id=AGENT_UUID, + **order_placement_task_def +) + +inventory_check_task_def = yaml.safe_load(""" +name: Inventory Check + +input_schema: + type: object + properties: + item_id: + type: integer + quantity: + type: integer + +main: +- prompt: + - role: system + content: >- + You are an inventory checker. Check the availability of the following item: + Item ID: {{inputs[0].item_id}} + Quantity Requested: {{inputs[0].quantity}} + + Return true if available, otherwise return false. + unwrap: true +""") + +inventory_check_task = client.tasks.create_or_update( + task_id=INVENTORY_CHECK_TASK_UUID, + agent_id=AGENT_UUID, + **inventory_check_task_def +) + +payment_processing_task_def = yaml.safe_load(""" +name: Payment Processing + +input_schema: + type: object + properties: + user_id: + type: string + order_id: + type: string + amount: + type: number + +main: +- prompt: + - role: system + content: >- + You are a payment processor. Process payment for the following order: + User ID: {{inputs[0].user_id}} + Order ID: {{inputs[0].order_id}} + Amount: {{inputs[0].amount}} + + Confirm payment status (success or failure). + unwrap: true + +- evaluate: + payment_status: "success" # Simulating a successful payment + +- return: + payment_status: _ +""") + +payment_processing_task = client.tasks.create_or_update( + task_id=PAYMENT_PROCESSING_TASK_UUID, + agent_id=AGENT_UUID, + **payment_processing_task_def +) + +shipment_tracking_task_def = yaml.safe_load(""" +name: Shipment Tracking + +input_schema: + type: object + properties: + order_id: + type: string + +main: +- prompt: + - role: system + content: >- + You are a shipment tracker. Track the shipment for the following order: + Order ID: {{inputs[0].order_id}} + + Return the current status of the shipment. + unwrap: true +""") + +shipment_tracking_task = client.tasks.create_or_update( + task_id=SHIPMENT_TRACKING_TASK_UUID, + agent_id=AGENT_UUID, + **shipment_tracking_task_def +) + +def place_order(user_id, item_id, quantity): + execution = client.executions.create( + task_id=ORDER_PLACEMENT_TASK_UUID, + input={ + "user_id": user_id, + "order_details": { + "item_id": item_id, + "quantity": quantity + } + } + ) + time.sleep(2) + result = client.executions.get(execution.id) + output = client.executions.transitions.list(execution_id=result.id).items[0].output + + if isinstance(output, dict): + return output + else: + return {"order_id": output} + +def check_inventory(item_id, quantity): + execution = client.executions.create( + task_id=INVENTORY_CHECK_TASK_UUID, + input={ + "item_id": item_id, + "quantity": quantity + } + ) + time.sleep(2) + result = client.executions.get(execution.id) + return client.executions.transitions.list(execution_id=result.id).items[0].output + +def process_payment(user_id, order_id, amount): + execution = client.executions.create( + task_id=PAYMENT_PROCESSING_TASK_UUID, + input={ + "user_id": user_id, + "order_id": order_id, + "amount": amount + } + ) + time.sleep(2) + result = client.executions.get(execution.id) + return client.executions.transitions.list(execution_id=result.id).items[0].output + +def track_shipment(order_id): + execution = client.executions.create( + task_id=SHIPMENT_TRACKING_TASK_UUID, + input={ + "order_id": order_id + } + ) + time.sleep(2) + result = client.executions.get(execution.id) + return client.executions.transitions.list(execution_id=result.id).items[0].output + +print("Demonstrating E-commerce Order Processing Workflow:") + +user_id = "user123" +item_id = 1 +quantity = 2 +amount = 49.99 + +is_available = check_inventory(item_id, quantity) +if is_available: + print(f"Inventory Check: Item {item_id} is available.") + order_result = place_order(user_id, item_id, quantity) + print(f"Order Result: {order_result}") + payment_result = process_payment(user_id, order_result["order_id"], amount) + print(f"Payment Status: {payment_result}") + shipment_result = track_shipment(order_result["order_id"]) + print(f"Shipment Status: {shipment_result}") +else: + print(f"Inventory Check: Item {item_id} is not available.") +