Improvements and understandings

This commit is contained in:
Mahesh Kommareddi 2024-06-15 17:32:58 -04:00
parent fc2c279836
commit c1e9d7bcd7
4 changed files with 342 additions and 1 deletions

Binary file not shown.

Binary file not shown.

275
tasks.py
View File

@ -1,6 +1,8 @@
from celery import Celery
from crewai import Agent, Task, Crew, tasks
from crewai import Process, Agent, Task, Crew, tasks
from crewai_tools import SerperDevTool, ScrapeWebsiteTool, SeleniumScrapingTool
from crewai_tools import BaseTool
from tools import MockTool
from langchain_openai import ChatOpenAI
from pymongo import MongoClient
import langchain_core
@ -30,11 +32,282 @@ llm = ChatOpenAI(
temperature=0.1
)
# Example usage
order_details = {
'customer_id': 1234,
'items': [
{'product_id': 'ABC123', 'quantity': 2},
{'product_id': 'XYZ456', 'quantity': 1}
]
}
customer_inquiry = {
'customer_id': 5678,
'issue': 'Order status inquiry'
}
shipping_details = {
'order_id': 9876,
'shipping_address': '123 Main St, Anytown USA'
}
order = {
'order_details': order_details,
'customer_inquiry': customer_inquiry,
'shipping_details': shipping_details
}
# Load tools
search_tool = SerperDevTool()
scrape_tool = SeleniumScrapingTool()
scrape_tool_bare = ScrapeWebsiteTool()
order_management_tool = MockTool()
order_management_tool.set_name("Order Management")
inventory_tool = MockTool()
inventory_tool.set_name("Inventory")
customer_support_tool = MockTool()
customer_support_tool.set_name("Customer Support")
order_tracking_tool = MockTool()
order_tracking_tool.set_name("Order Tracking")
supplier_management_tool = MockTool()
supplier_management_tool.set_name("Supplier Management")
shipping_tool = MockTool()
shipping_tool.set_name("Shipping")
tracking_tool = MockTool()
tracking_tool.set_name("Tracking")
# Callback functions
def order_callback(output):
if isinstance(output, langchain_core.agents.AgentFinish):
print("Order Agent finished")
elif output and output[0]:
print(output[0])
if output[0][0]:
print(output[0][0].log)
def customer_callback(output):
if isinstance(output, langchain_core.agents.AgentFinish):
print("Customer Agent finished")
elif output and output[0]:
print(output[0])
if output[0][0]:
print(output[0][0].log)
def inventory_callback(output):
if isinstance(output, langchain_core.agents.AgentFinish):
print("Inventory Agent finished")
elif output and output[0]:
print(output[0])
if output[0][0]:
print(output[0][0].log)
def logistics_callback(output):
if isinstance(output, langchain_core.agents.AgentFinish):
print("Logistics Agent finished")
elif output and output[0]:
print(output[0])
if output[0][0]:
print(output[0][0].log)
# Define the Order Agent
order_agent = Agent(
role='Order Manager',
goal='Manage and process customer orders efficiently',
backstory=(
"You are the Order Manager at an e-commerce company."
"Your responsibilities include receiving and processing customer orders,"
"updating inventory levels, and coordinating with the logistics team for shipping."
"You have access to the company's order management system and inventory database."
),
tools=[order_management_tool, inventory_tool],
max_rpm=100,
step_callback=order_callback
)
order_task = Task(
description=(
"Process the incoming customer order."
"Update the inventory levels accordingly."
"Coordinate with the logistics team for shipping."
),
expected_output="Order processed successfully, inventory updated, and shipping coordinated.",
agent=order_agent
)
# Define the Customer Agent
customer_agent = Agent(
role='Customer Service Representative',
goal='Provide excellent customer support and resolve inquiries',
backstory=(
"You are a Customer Service Representative at an e-commerce company."
"Your role is to assist customers with their questions, concerns, and complaints."
"You have access to customer order history, product information, and company policies."
),
tools=[customer_support_tool, order_tracking_tool],
max_rpm=100,
step_callback=customer_callback
)
customer_task = Task(
description=(
"Respond to the customer inquiry or complaint."
"Provide relevant information, track order status, or escalate the issue if necessary."
),
expected_output="Customer inquiry resolved or escalated appropriately.",
agent=customer_agent
)
# Define the Inventory Agent
inventory_agent = Agent(
role='Inventory Manager',
goal='Maintain accurate inventory levels and optimize stock',
backstory=(
"You are the Inventory Manager at an e-commerce company."
"Your responsibilities include monitoring inventory levels, reordering products,"
"and ensuring efficient stock management."
"You have access to the company's inventory database and supplier information."
),
tools=[inventory_tool, supplier_management_tool],
max_rpm=100,
step_callback=inventory_callback
)
inventory_task = Task(
description=(
"Monitor inventory levels and reorder products as needed."
"Coordinate with suppliers for timely restocking."
"Optimize stock levels based on demand and sales data."
),
expected_output="Inventory levels optimized, and restocking coordinated with suppliers.",
agent=inventory_agent
)
# Define the Logistics Agent
logistics_agent = Agent(
role='Logistics Coordinator',
goal='Ensure efficient and timely shipping of orders',
backstory=(
"You are the Logistics Coordinator at an e-commerce company."
"Your role is to manage the shipping process, coordinate with carriers,"
"and track shipments to ensure timely delivery."
"You have access to the company's order management system and carrier integrations."
),
tools=[shipping_tool, tracking_tool],
max_rpm=100,
step_callback=logistics_callback
)
logistics_task = Task(
description=(
"Coordinate the shipping of the processed order."
"Select the appropriate carrier and shipping method."
"Track the shipment and provide updates to the customer."
),
expected_output="Order shipped successfully, and tracking information provided to the customer.",
agent=logistics_agent
)
# Update the Company class
class Company:
def __init__(self):
self.order_agent = order_agent
self.customer_agent = customer_agent
self.inventory_agent = inventory_agent
self.logistics_agent = logistics_agent
self.order_crew = Crew(agents=[order_agent], tasks=[order_task])
self.customer_crew = Crew(agents=[customer_agent], tasks=[customer_task])
self.inventory_crew = Crew(agents=[inventory_agent], tasks=[inventory_task])
self.logistics_crew = Crew(agents=[logistics_agent], tasks=[logistics_task])
def process_order(self, order):
# Execute the order crew with the provided order details
order_result = self.order_crew.kickoff(order_task, order)
return order_result
def handle_customer_inquiry(self, inquiry):
# Execute the customer crew with the provided inquiry details
inquiry_result = self.customer_crew.kickoff(customer_task, inquiry)
return inquiry_result
def manage_inventory(self):
# Execute the inventory crew
inventory_result = self.inventory_crew.kickoff(inventory_task)
return inventory_result
def ship_order(self, order):
# Execute the logistics crew with the provided order details
shipping_result = self.logistics_crew.kickoff(logistics_task, order)
return shipping_result
company = Company()
class ManagerTool(BaseTool):
name: str | None = "Manager Tool"
description: str | None = "A tool for the Manager Agent to coordinate other agents and handle business processes."
# name = "Manager Tool"
# description = "A tool for the Manager Agent to coordinate other agents and handle business processes."
def _run(self, **kwargs) -> str:
# Parse the argument to determine the requested action
if len(kwargs) > 0:
action = kwargs[0]
if action == "process_order":
order_details = kwargs.get('order_details', order['order_details'])
result = company.process_order(order_details)
elif action == "handle_inquiry":
customer_inquiry = kwargs.get('customer_inquiry', order['customer_inquiry'])
result = company.handle_customer_inquiry(customer_inquiry)
elif action == "manage_inventory":
result = company.manage_inventory()
elif action == "ship_order":
shipping_details = kwargs.get('shipping_details', order['shipping_details'])
result = company.ship_order(shipping_details)
else:
result = f"The orders and status is: {order}"
return str(result)
return f"I understand. I have done what is necessary."
# Define the Manager Agent
manager_agent = Agent(
role="Business Operations Manager",
goal="Coordinate and manage business processes efficiently",
backstory=(
"You are the Business Operations Manager at an e-commerce company."
"Your role is to oversee and coordinate various business processes,"
"including order processing, customer support, inventory management, and logistics."
"You have access to a tool that allows you to delegate tasks to specialized agents."
"The agents you can delegate to are order_agent, customer_agent, inventory_agent, and logistics_agent"
),
tools=[ManagerTool()],
max_rpm=100,
)
manager_task = Task(
description="Manage and coordinate the appropriate business processes based on the given input.",
expected_output="Business processes executed successfully.",
agent=manager_agent,
)
project_crew = Crew(
agents=[manager_agent, order_agent, customer_agent, inventory_agent, logistics_agent],
tasks=[manager_task, order_task, customer_task, inventory_task, logistics_task],
manager_llm=ChatOpenAI(
model="amazon-embeddings",
base_url="http://chat.the.mk:1337",
temperature=0.1
), # Mandatory for hierarchical process
process=Process.hierarchical, # Specifies the hierarchical management approach
# memory=True, # Enable memory usage for enhanced task execution
)
# Pass the order_details, customer_inquiry, and shipping_details as arguments to the kickoff method
project_crew.kickoff()
# Function to update task status in MongoDB
def update_task_status(task_id, status, message):
try:

68
tools.py Normal file
View File

@ -0,0 +1,68 @@
# tools.py
from crewai_tools import BaseTool
class MockTool(BaseTool):
name: str | None = "mock_tool"
description: str | None = "useful for when you need to mock a tool"
def set_name(self, set_name):
self.name = set_name
def _run(self, **kwargs) -> str:
if len(kwargs) > 0:
argument = kwargs[0]
# Implementation goes here
if self.name == "Order Management":
return self._handle_order_management(argument)
elif self.name == "Customer Support":
return self._handle_customer_support(argument)
elif self.name == "Inventory":
return self._handle_inventory(argument)
elif self.name == "Supplier Management":
return self._handle_supplier_management(argument)
elif self.name == "Shipping":
return self._handle_shipping(argument)
elif self.name == "Tracking":
return self._handle_tracking(argument)
else:
return f"Mock output from {self.name} tool for query: {argument}"
else:
return f"Ran the action successfully"
def _handle_order_management(self, argument):
if "get order details" in argument.lower():
return "Mock order details: [{'product_id': 'ABC123', 'quantity': 2}, {'product_id': 'XYZ456', 'quantity': 1}], Delivery Address: 123 Main St, Anytown USA"
else:
return f"Mock output from Order Management tool for query: {argument}"
def _handle_customer_support(self, argument):
if "customer inquiry" in argument.lower():
return "Mock customer inquiry: Order status inquiry"
else:
return f"Mock output from Customer Support tool for query: {argument}"
def _handle_inventory(self, argument):
if "check stock levels" in argument.lower():
return "Mock stock levels: Product ABC123 - 100 units, Product XYZ456 - 50 units"
else:
return f"Mock output from Inventory tool for query: {argument}"
def _handle_supplier_management(self, argument):
if "check recent orders" in argument.lower():
return "Mock recent orders: Product ABC123 - 500 units ordered from Supplier A, Product XYZ456 - 200 units ordered from Supplier B"
else:
return f"Mock output from Supplier Management tool for query: {argument}"
def _handle_shipping(self, argument):
if "check shipping options" in argument.lower():
return "Mock shipping options: Standard (5-7 business days), Express (2-3 business days)"
else:
return f"Mock output from Shipping tool for query: {argument}"
def _handle_tracking(self, argument):
if "track shipment" in argument.lower():
return "Mock tracking information: Order ABC123XYZ, Shipped via Express, Estimated Delivery: 2023-06-10"
else:
return f"Mock output from Tracking tool for query: {argument}"