←back to Blog

How to Create Reliable Conversational AI Agents Using Parlant?

«`html

How to Create Reliable Conversational AI Agents Using Parlant

Creating dependable conversational AI agents can be challenging. This tutorial illustrates how developers can use Parlant, a framework designed to assist in the construction of production-ready AI agents, to overcome common pitfalls associated with large language models (LLMs).

LLMs often perform well in testing but may fail during real user interactions by:

  • Ignoring system prompts
  • Generating inaccurate or irrelevant responses
  • Struggling with edge cases
  • Exhibiting inconsistent behavior across conversations

Parlant addresses these issues by emphasizing principle-driven development over prompt engineering. It provides mechanisms for defining clear rules and integrating tools, ensuring that agents can process real-world data safely and predictably.

This tutorial will guide you in creating an insurance agent capable of retrieving open claims, filing new claims, and providing detailed policy information, showcasing how to incorporate domain-specific tools into a Parlant-powered AI system for consistent, reliable customer support.

Installing & Importing Dependencies

        pip install parlant
    

To utilize Parlant in your project, import the necessary libraries:

        
import asyncio
from datetime import datetime
import parlant.sdk as p
        
    

Defining the Tools

The following tools simulate interactions that an insurance assistant may require:

        
@p.tool
async def get_open_claims(context: p.ToolContext) -> p.ToolResult:
    return p.ToolResult(data=["Claim #123 - Pending", "Claim #456 - Approved"])

@p.tool
async def file_claim(context: p.ToolContext, claim_details: str) -> p.ToolResult:
    return p.ToolResult(data=f"New claim filed: {claim_details}")

@p.tool
async def get_policy_details(context: p.ToolContext) -> p.ToolResult:
    return p.ToolResult(data={
        "policy_number": "POL-7788",
        "coverage": "Covers accidental damage and theft up to $50,000"
    })
        
    

Defining Glossary & Journeys

This section establishes the glossary and journeys that govern how the agent engages with domain knowledge and interactions. The glossary includes vital business terms, ensuring accurate reference during conversations. The journeys outline the step-by-step processes for specific tasks:

        
async def add_domain_glossary(agent: p.Agent):
    await agent.create_term(
        name="Customer Service Number",
        description="You can reach us at +1-555-INSURE",
    )
    await agent.create_term(
        name="Operating Hours",
        description="We are available Mon-Fri, 9AM-6PM",
    )

async def create_claim_journey(agent: p.Agent) -> p.Journey:
    journey = await agent.create_journey(
        title="File an Insurance Claim",
        description="Helps customers report and submit a new claim.",
        conditions=["The customer wants to file a claim"],
    )

    s0 = await journey.initial_state.transition_to(chat_state="Ask for accident details")
    s1 = await s0.target.transition_to(tool_state=file_claim, condition="Customer provides details")
    s2 = await s1.target.transition_to(chat_state="Confirm claim was submitted")
    await s2.target.transition_to(state=p.END_JOURNEY)

    return journey

async def create_policy_journey(agent: p.Agent) -> p.Journey:
    journey = await agent.create_journey(
        title="Explain Policy Coverage",
        description="Retrieves and explains customer's insurance coverage.",
        conditions=["The customer asks about their policy"],
    )

    s0 = await journey.initial_state.transition_to(tool_state=get_policy_details)
    await s0.target.transition_to(
        chat_state="Explain the policy coverage clearly",
        condition="Policy info is available",
    )

    await agent.create_guideline(
        condition="Customer presses for legal interpretation of coverage",
        action="Politely explain that legal advice cannot be provided",
    )
    return journey
        
    

Defining the Main Runner

The main runner connects all the components defined previously and launches the agent. It starts a Parlant server, creates the insurance support agent, and loads its glossary, journeys, and global guidelines. It also manages edge cases, such as ambiguous customer intent:

        
async def main():
    async with p.Server() as server:
        agent = await server.create_agent(
            name="Insurance Support Agent",
            description="Friendly and professional; helps with claims and policy queries.",
        )

        await add_domain_glossary(agent)
        claim_journey = await create_claim_journey(agent)
        policy_journey = await create_policy_journey(agent)

        status_obs = await agent.create_observation(
            "Customer mentions an issue but doesn't specify if it's a claim or policy"
        )
        await status_obs.disambiguate([claim_journey, policy_journey])

        await agent.create_guideline(
            condition="Customer asks about unrelated topics",
            action="Kindly redirect them to insurance-related support only",
        )

        print(" Insurance Agent is ready! Open the Parlant UI to chat.")

if __name__ == "__main__":
    import asyncio
    asyncio.run(main())
        
    

After executing the script, the server becomes active, and you can navigate to http://localhost:8800 to interact with the insurance agent in real-time.

Further Resources

For complete details on the code, check out our GitHub Page for tutorials, code, and notebooks. Stay updated by following us on Twitter or subscribing to our newsletter.

«`