**Rephrased Blog Content**

**Seamless Human-AI Collaboration: Implementing a Human Handoff System for an AI-Powered Insurance Agent**

In the realm of customer service automation, human handoff plays a pivotal role. It ensures a smooth transition from AI to human agents when the AI’s capabilities are exhausted. This tutorial guides you through creating a human handoff system for an AI-powered insurance agent using Parlant, a conversational AI platform. By the end, you’ll have a Streamlit-based interface that allows human operators (Tier 2) to view live customer messages and respond directly within the same session, bridging the gap between automation and human expertise.

**Setting Up the Stage**

Before diving in, ensure you have a valid OpenAI API key. Store it securely in a `.env` file in your project’s root directory.

“`bash
OPENAI_API_KEY=your_api_key_here
“`

Install the required dependencies:

“`bash
pip install parlant dotenv streamlit
“`

**Crafting the Insurance Agent (agent.py)**

Our journey begins by building the agent script, which defines the AI’s behavior, conversation journeys, glossary, and the human handoff mechanism. This script will be the core logic powering our insurance assistant in Parlant.

**1. Importing Libraries and Loading Credentials**

“`python
import asyncio
import os
from datetime import datetime
from dotenv import load_dotenv
import parlant.sdk as p

load_dotenv()
“`

**2. Defining the Agent’s Tools**

We create four tools to simulate interactions an insurance assistant might need:

– `get_open_claims`: Retrieves a list of open insurance claims.
– `file_claim`: Accepts claim details and simulates filing a new claim.
– `get_policy_details`: Provides essential policy information.
– `initiate_human_handoff`: Enables the AI agent to transfer a conversation to a human operator when necessary.

“`python
@p.tool
async def get_open_claims(context: p.ToolContext) -> p.ToolResult:
# …

@p.tool
async def file_claim(context: p.ToolContext, claim_details: str) -> p.ToolResult:
# …

@p.tool
async def get_policy_details(context: p.ToolContext) -> p.ToolResult:
# …

@p.tool
async def initiate_human_handoff(context: p.ToolContext, reason: str) -> p.ToolResult:
# …
“`

**3. Defining the Glossary**

A glossary defines key terms and phrases the AI agent should recognize and respond to consistently.

“`python
async def add_domain_glossary(agent: p.Agent):
# …
“`

**4. Defining the Journeys**

We create two journeys: `Claim Journey` and `Policy Journey`. The Claim Journey guides customers through filing a new insurance claim, while the Policy Journey helps customers understand their insurance coverage.

“`python
async def create_claim_journey(agent: p.Agent) -> p.Journey:
# …

async def create_policy_journey(agent: p.Agent) -> p.Journey:
# …
“`

**5. Defining the Main Runner**

The `main` function sets up the agent, adds shared terms and definitions, creates journeys, and defines disambiguation rules and global guidelines.

“`python
async def main():
# …
“`

**Running the Agent**

Start the Parlant agent locally by running:

“`bash
python agent.py
“`

**Building the Human Handoff Interface (handoff.py)**

With the AI agent script running, we’ll connect it to a Streamlit-based human handoff interface. This UI allows human operators to view ongoing sessions, read customer messages, and respond in real-time, creating a seamless collaboration between AI automation and human expertise.

**1. Importing Libraries and Setting Up the Parlant Client**

“`python
import asyncio
import streamlit as st
from datetime import datetime
from parlant.client import AsyncParlantClient

client = AsyncParlantClient(base_url=”http://localhost:8800″)
“`

**2. Session State Management**

Streamlit’s `session_state` is used to persist data across user interactions.

“`python
if “events” not in st.session_state:
st.session_state.events = []
if “last_offset” not in st.session_state:
st.session_state.last_offset = 0
“`

**3. Message Rendering Function**

This function controls how messages appear in the Streamlit interface, differentiating between customers, AI, and human agents for clarity.

“`python
def render_message(message, source, participant_name, timestamp):
# …
“`

**4. Fetching Events from Parlant**

This asynchronous function retrieves new messages (events) from Parlant for the given session.

“`python
async def fetch_events(session_id):
# …
“`

**5. Sending Messages as Human or AI**

Two helper functions are defined to send messages: one as a human operator and another as if sent by the AI, but manually triggered by a human.

“`python
async def send_human_message(session_id: str, message: str, operator_name: str = “Tier-2 Operator”):
# …

async def send_message_as_ai(session_id: str, message: str):
# …
“`

**6. Streamlit Interface**

Finally, we build a simple, interactive Streamlit UI to enter a session ID, view chat history, send messages as either Human or AI, and refresh to pull new messages.

“`python
st.title(” Human Handoff Assistant”)

session_id = st.text_input(“Enter Parlant Session ID:”)

if session_id:
# …
“`

With these steps, you’ve successfully implemented a human handoff system for an AI-powered insurance agent, enabling seamless collaboration between AI automation and human expertise.

Share.
Leave A Reply

Exit mobile version