Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add E-commerce Order Processing Workflow Cookbook #650

Merged
merged 3 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
381 changes: 381 additions & 0 deletions cookbooks/E_commerce_Order_Processing_Workflow.ipynb
Original file line number Diff line number Diff line change
@@ -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": []
}
]
}
Loading
Loading