Creating a Customer Service Agent with Client-Side Tools
In this recipe, we'll demonstrate how to create a customer service chatbot using Claude 3 plus client-side tools. The chatbot will be able to look up customer information, retrieve order details, and cancel orders on behalf of the customer. We'll define the necessary tools and simulate synthetic responses to showcase the chatbot's capabilities.
Step 1: Set up the environment
First, let's install the required libraries and set up the Claude API client.
%pip install anthropicimport anthropic
client = anthropic.Client()
MODEL_NAME = "claude-opus-4-1"Step 2: Define the client-side tools
Next, we'll define the client-side tools that our chatbot will use to assist customers. We'll create three tools: get_customer_info, get_order_details, and cancel_order.
tools = [
{
"name": "get_customer_info",
"description": "Retrieves customer information based on their customer ID. Returns the customer's name, email, and phone number.",
"input_schema": {
"type": "object",
"properties": {
"customer_id": {
"type": "string",
"description": "The unique identifier for the customer.",
}
},
"required": ["customer_id"],
},
},
{
"name": "get_order_details",
"description": "Retrieves the details of a specific order based on the order ID. Returns the order ID, product name, quantity, price, and order status.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The unique identifier for the order.",
}
},
"required": ["order_id"],
},
},
{
"name": "cancel_order",
"description": "Cancels an order based on the provided order ID. Returns a confirmation message if the cancellation is successful.",
"input_schema": {
"type": "object",
"properties": {
"order_id": {
"type": "string",
"description": "The unique identifier for the order to be cancelled.",
}
},
"required": ["order_id"],
},
},
]Step 3: Simulate synthetic tool responses
Since we don't have real customer data or order information, we'll simulate synthetic responses for our tools. In a real-world scenario, these functions would interact with your actual customer database and order management system.
def get_customer_info(customer_id):
# Simulated customer data
customers = {
"C1": {"name": "John Doe", "email": "[email protected]", "phone": "123-456-7890"},
"C2": {"name": "Jane Smith", "email": "[email protected]", "phone": "987-654-3210"},
}
return customers.get(customer_id, "Customer not found")
def get_order_details(order_id):
# Simulated order data
orders = {
"O1": {
"id": "O1",
"product": "Widget A",
"quantity": 2,
"price": 19.99,
"status": "Shipped",
},
"O2": {
"id": "O2",
"product": "Gadget B",
"quantity": 1,
"price": 49.99,
"status": "Processing",
},
}
return orders.get(order_id, "Order not found")
def cancel_order(order_id):
# Simulated order cancellation
if order_id in ["O1", "O2"]:
return True
else:
return FalseStep 4: Process tool calls and return results
We'll create a function to process the tool calls made by Claude and return the appropriate results.
def process_tool_call(tool_name, tool_input):
if tool_name == "get_customer_info":
return get_customer_info(tool_input["customer_id"])
elif tool_name == "get_order_details":
return get_order_details(tool_input["order_id"])
elif tool_name == "cancel_order":
return cancel_order(tool_input["order_id"])Step 5: Interact with the chatbot
Now, let's create a function to interact with the chatbot. We'll send a user message, process any tool calls made by Claude, and return the final response to the user.
import json
def chatbot_interaction(user_message):
print(f"\n{'=' * 50}\nUser Message: {user_message}\n{'=' * 50}")
messages = [{"role": "user", "content": user_message}]
response = client.messages.create(
model=MODEL_NAME, max_tokens=4096, tools=tools, messages=messages
)
print("\nInitial Response:")
print(f"Stop Reason: {response.stop_reason}")
print(f"Content: {response.content}")
while response.stop_reason == "tool_use":
tool_use = next(block for block in response.content if block.type == "tool_use")
tool_name = tool_use.name
tool_input = tool_use.input
print(f"\nTool Used: {tool_name}")
print("Tool Input:")
print(json.dumps(tool_input, indent=2))
tool_result = process_tool_call(tool_name, tool_input)
print("\nTool Result:")
print(json.dumps(tool_result, indent=2))
messages = [
{"role": "user", "content": user_message},
{"role": "assistant", "content": response.content},
{
"role": "user",
"content": [
{
"type": "tool_result",
"tool_use_id": tool_use.id,
"content": str(tool_result),
}
],
},
]
response = client.messages.create(
model=MODEL_NAME, max_tokens=4096, tools=tools, messages=messages
)
print("\nResponse:")
print(f"Stop Reason: {response.stop_reason}")
print(f"Content: {response.content}")
final_response = next(
(block.text for block in response.content if hasattr(block, "text")),
None,
)
print(f"\nFinal Response: {final_response}")
return final_responseStep 6: Test the chatbot
Let's test our customer service chatbot with a few sample queries.
chatbot_interaction("Can you tell me the email address for customer C1?")
chatbot_interaction("What is the status of order O2?")
chatbot_interaction("Please cancel order O1 for me.")================================================== User Message: Can you tell me the email address for customer C1? ================================================== Initial Response: Stop Reason: tool_use Content: [ContentBlock(text='The get_customer_info function retrieves a customer\'s name, email, and phone number given their customer ID. To call this function, I need the customer_id parameter. The user provided the customer ID "C1" in their request, so I have the necessary information to make the API call. ', type='text'), ContentBlockToolUse(id='toolu_019F9JHokMkJ1dHw5BEh28sA', input={'customer_id': 'C1'}, name='get_customer_info', type='tool_use')] Tool Used: get_customer_info Tool Input: { "customer_id": "C1" } Tool Result: { "name": "John Doe", "email": "[email protected]", "phone": "123-456-7890" } Response: Stop Reason: end_turn Content: [ContentBlock(text='The email address for customer C1 (John Doe) is [email protected].', type='text')] Final Response: The email address for customer C1 (John Doe) is [email protected]. ================================================== User Message: What is the status of order O2? ================================================== Initial Response: Stop Reason: tool_use Content: [ContentBlock(text='\nBased on the provided functions, the most relevant one for this request is get_order_details, which takes an order_id parameter and returns details about that specific order, including the order status.\n\nThe user has provided an order ID in their request - "O2". So the required order_id parameter can be filled with this value.\n\nSince the required parameter is available, I can proceed with calling the get_order_details function to retrieve the order status for order O2.\n ', type='text'), ContentBlockToolUse(id='toolu_01K1u68uC94edXx8MVT35eR3', input={'order_id': 'O2'}, name='get_order_details', type='tool_use')] Tool Used: get_order_details Tool Input: { "order_id": "O2" } Tool Result: { "id": "O2", "product": "Gadget B", "quantity": 1, "price": 49.99, "status": "Processing" } Response: Stop Reason: end_turn Content: [ContentBlock(text='Based on the details returned from the get_order_details function, the status of order O2 is "Processing".', type='text')] Final Response: Based on the details returned from the get_order_details function, the status of order O2 is "Processing". ================================================== User Message: Please cancel order O1 for me. ================================================== Initial Response: Stop Reason: tool_use Content: [ContentBlock(text='\nThe relevant tool to cancel an order is the cancel_order function. \nThis function requires an order_id parameter.\nThe user provided the order ID "O1" in their request, so we have the necessary parameter to call the cancel_order function.\n ', type='text'), ContentBlockToolUse(id='toolu_01W3ZkP2QCrjHf5bKM6wvT2s', input={'order_id': 'O1'}, name='cancel_order', type='tool_use')] Tool Used: cancel_order Tool Input: { "order_id": "O1" } Tool Result: true Response: Stop Reason: end_turn Content: [ContentBlock(text='Based on the confirmation received, your order O1 has been successfully cancelled. Please let me know if there is anything else I can assist you with.', type='text')] Final Response: Based on the confirmation received, your order O1 has been successfully cancelled. Please let me know if there is anything else I can assist you with. 'Based on the confirmation received, your order O1 has been successfully cancelled. Please let me know if there is anything else I can assist you with.'
And that's it! We've created a customer service chatbot using Claude 3 models and client-side tools. The chatbot can look up customer information, retrieve order details, and cancel orders based on the user's requests. By defining clear tool descriptions and schemas, we enable Claude to effectively understand and utilize the available tools to assist customers.
Feel free to expand on this example by integrating with your actual customer database and order management system, and by adding more tools to handle a wider range of customer service tasks.