Enhancing Customer Service Efficiency: Leveraging Mandatory Tool Calls in ChatCompletion

Anis MarrouchiAI Bot
By Anis Marrouchi & AI Bot ·

Loading the Text to Speech Audio Player...

In the realm of web development services, enhancing customer service efficiency is crucial for maintaining high customer satisfaction and operational efficiency. Leveraging the mandatory tool calls in ChatCompletion can streamline customer service operations by ensuring that each conversation includes essential steps for resolving customer inquiries. This tutorial provides an advanced step-by-step guide on implementing this feature using OpenAI's API.

Introduction

ChatCompletion endpoints now support mandatory tool calls by specifying tool_choice='required' as a parameter, adding an element of determinism to your customer service applications. This guide will help you implement a deterministic customer service agent using OpenAI's ChatCompletion.

Step-by-Step Tutorial

Step 1: Set Up the OpenAI Client

Start by importing the necessary libraries and setting up the OpenAI client. Make sure you have your OpenAI API key handy.

import json
from openai import OpenAI
import os
 
# Initialize the OpenAI client
client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
GPT_MODEL = "gpt-4-turbo"

Step 2: Define Tools and Instructions

Define the tools your customer service agent will use, such as functions for communicating with users and retrieving instructions based on the customer’s problem.

tools = [
    {
        "type": "function",
        "function": {
            "name": "speak_to_user",
            "description": "Use this to speak to the user to give them information and to ask for anything required for their case.",
            "parameters": {
                "type": "object",
                "properties": {
                    "message": {
                        "type": "string",
                        "description": "Text of message to send to user. Can cover multiple topics."
                    }
                },
                "required": ["message"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "get_instructions",
            "description": "Used to get instructions to deal with the user's problem.",
            "parameters": {
                "type": "object",
                "properties": {
                    "problem": {
                        "type": "string",
                        "enum": ["fraud", "refund", "information"],
                        "description": "The type of problem the customer has."
                    }
                },
                "required": ["problem"]
            }
        }
    }
]

Define example instructions that the customer service assistant can use to address different customer problems.

INSTRUCTIONS = [
    {
        "type": "fraud",
        "instructions": """• Ask the customer to describe the fraudulent activity, including the date and items involved in the suspected fraud.
                           • Offer the customer a refund.
                           • Report the fraud to the security team for further investigation.
                           • Thank the customer for contacting support and invite them to reach out with any future queries."""
    },
    {
        "type": "refund",
        "instructions": """• Confirm the customer's purchase details and verify the transaction in the system.
                           • Check the company's refund policy to ensure the request meets the criteria.
                           • Ask the customer to provide a reason for the refund.
                           • Submit the refund request to the accounting department.
                           • Inform the customer of the expected time frame for the refund processing.
                           • Thank the customer for contacting support and invite them to reach out with any future queries."""
    },
    // More instructions for information queries
]

Step 3: Define System Prompts and Handlers

Set up the system prompts and message handlers.

assistant_system_prompt = """You are a customer service assistant. Your role is to answer user questions politely and competently. Follow these instructions to solve the case: 
- Understand their problem and get the relevant instructions.
- Follow the instructions to solve the customer's problem.
- Get their confirmation before performing a permanent operation like a refund.
- Help them with any other problems or close the case."""
 
def submit_user_message(user_query, conversation_messages=[]):
    """Message handling function which loops through tool calls until a response is required"""
    respond = False
    user_message = {"role": "user", "content": user_query}
    conversation_messages.append(user_message)
    print(f"User: {user_query}")
 
    while not respond:
        messages = [{"role": "system", "content": assistant_system_prompt}]
        messages.extend(conversation_messages)
 
        # Make the ChatCompletion call with tool_choice='required'
        response = client.chat.completions.create(
            model=GPT_MODEL,
            messages=messages,
            temperature=0,
            tools=tools,
            tool_choice='required'
        )
        conversation_messages.append(response.choices[0].message)
        respond, conversation_messages = execute_function(response.choices[0].message, conversation_messages)
 
    return conversation_messages
 
def execute_function(function_calls, messages):
    """Wrapper function to execute the tool calls"""
    respond = False
 
    for function_call in function_calls.tool_calls:
        function_id = function_call.id
        function_name = function_call.function.name
        print(f"Calling function {function_name}")
        function_arguments = json.loads(function_call.function.arguments)
 
        if function_name == 'get_instructions':
            instruction_name = function_arguments['problem']
            instructions = INSTRUCTIONS[instruction_name]
            messages.append({
                "tool_call_id": function_id,
                "role": "tool",
                "name": function_name,
                "content": instructions['instructions'],
            })
 
        elif function_name != 'get_instructions':
            respond = True
            messages.append({
                "tool_call_id": function_id,
                "role": "tool",
                "name": function_name,
                "content": function_arguments['message'],
            })
 
        print(f"Assistant: {function_arguments['message']}")
 
    return respond, messages

Step 4: Test the Implementation

Run a test by simulating a customer service query.

messages = submit_user_message("Hi, I have had an item stolen that was supposed to be delivered yesterday.")
# Continue the conversation as needed

Example Interaction

messages = submit_user_message("Hi, I have had an item stolen that was supposed to be delivered yesterday.")

Output:

User: Hi, I have had an item stolen that was supposed to be delivered to me yesterday.
Calling function get_instructions
Calling function speak_to_user
Assistant: I'm sorry to hear about the stolen item...
Customer Service Interaction

Step 5: Evaluation

Evaluate the performance of the customer service agent by simulating different scenarios.

def execute_conversation(objective):
    conversation_messages = []
    user_query = objective
    done = False
 
    while not done:
        conversation_messages = submit_user_message(user_query, conversation_messages)
        messages_string = '\n'.join([f"{x['role']}: {x['content']}" for x in conversation_messages if isinstance(x, dict)])
 
        messages = [
            {"role": "system", "content": customer_system_prompt.format(query=objective, chat_history=messages_string)},
            {"role": "user", "content": "Continue the chat to solve your query. Remember, you are the user. Do not provide User: or Assistant: in your response"}
        ]
 
        user_response = client.chat.completions.create(model=GPT_MODEL, messages=messages, temperature=0.5)
        conversation_messages.append({"role": "user", "content": user_response.choices[0].message.content})
 
        if 'DONE' in user_response.choices[0].message.content:
            done = True
            print("Achieved objective, closing conversation\n")
 
        user_query = user_response.choices[0].message.content
 
questions = [
    "I want to get a refund for the suit I ordered last Friday.",
    "Can you tell me what your policy is for returning damaged goods?",
    "Please tell me what your complaint policy is."
]
 
for q in questions:
    execute_conversation(q)

Conclusion

By making tool use mandatory in your ChatCompletion, you can ensure that all critical steps are performed during each customer interaction, leading to more consistent and reliable customer service.

This tutorial showcased an advanced implementation for improving customer service efficiency. Learn more about enhancing tools for your customer service workflows here.

References

Colin Jarvis, "Enhancing Customer Service Efficiency: Leveraging Mandatory Tool Calls in ChatCompletion", May 1, 2024. Source

Implement these steps to enhance your customer service process, ensuring your team can handle customer interactions with greater efficiency and reliability.


Want to read more tutorials? Check out our latest tutorial on Mastering Note-Taking with FlutterFlow and Supabase: A Complete Guide.

Discuss Your Project with Us

We're here to help with your web development needs. Schedule a call to discuss your project and how we can assist you.

Let's find the best solutions for your needs.