OpenAI Customer Service Agents Demo

repository·main·Indexed 27 days ago

https://github.com/openai/openai-cs-agents-demo

A customer service interface demo built with the OpenAI Agents SDK, featuring a Python backend for agent orchestration and a Next.js/ChatKit frontend. The demo implements a Triage Agent that routes requests to specialized agents for flight information, booking and cancellations, seat and special services, FAQs, and refunds and compensation. It includes a FastAPI backend with ChatKit API endpoints for message processing and state streaming.

Tokens
3.1K
Snippets
16
Records
23
Agent score
92%

What's inside openai-cs-agents-demo

  1. Overview of included Agents

    main

    The demo includes several specialized agents that are orchestrated by a Triage Agent:

    • Triage Agent: The entry point that routes requests to specialists.
    • Flight Information Agent: Provides live status, connection risk, and alternate options.
    • Booking & Cancellation Agent: Handles booking, rebooking, or cancelling trips.
    • Seat & Special Services Agent: Manages seat assignments and medical/front-row requests.
    • FAQ Agent: Answers policy questions (e.g., baggage, compensation, Wi-Fi).
    • Refunds and Compensation Agent: Opens cases and issues hotel/meal support after disruptions.
  2. Run the Customer Service Agents Demo

    main

    You can run the components either separately or together.

    Run the backend independently: Use this if you want to use a separate UI. The backend will be available at http://localhost:8000.

    Run the UI & backend simultaneously: Use this for the full demo experience. Running the command from the ui folder will start both the frontend (at http://localhost:3000) and the backend.

    # Run backend independently
    cd python-backend
    python -m uvicorn main:app --reload --port 8000
    
    # Run both UI and backend
    cd ui
    npm run dev
  3. Set your OpenAI API key

    main

    You can provide your OpenAI API key using one of the following methods:

    1. Environment Variable: Run the following command in your terminal:
      export OPENAI_API_KEY=your_api_key
    2. .env File: Create an .env file in the python-backend folder. To use this, you must install python-dotenv and add the following to your Python application:
      from dotenv import load_dotenv
      load_dotenv()
    export OPENAI_API_KEY=your_api_key
  4. Install dependencies for the backend and UI

    main

    The project consists of a Python backend and a Next.js UI. Follow these steps to install dependencies:

    Backend Setup (Python): Navigate to the python-backend directory, create a virtual environment, activate it, and install the requirements.

    UI Setup (Next.js): Navigate to the ui directory and run npm install.

    cd python-backend
    python -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
    
    cd ../ui
    npm install
  5. Get a conversation snapshot with snapshot()

    main

    The snapshot method returns a dictionary representing the current state of a thread, including the thread_id, current_agent, the context, a list of all available agents, and the full history of events and guardrails.

    async def snapshot(self, thread_id: Optional[str], context: dict[str, Any]) -> Dict[str, Any]:
  6. Access Airline Agent types and agents

    main

    The module exports the following core components for interacting with the airline agent system:

    Agents:

    • triage_agent: Initial entry point for routing queries.
    • faq_agent: Handles frequently asked questions.
    • flight_information_agent: Provides flight details.
    • booking_cancellation_agent: Manages bookings and cancellations.
    • seat_special_services_agent: Handles seating and special requests.
    • refunds_compensation_agent: Manages refunds and compensation.

    Context Types:

    • AirlineAgentChatContext: Type for chat-specific context.
    • AirlineAgentContext: Base type for agent context.
    • create_initial_context: Function to initialize context.
    • public_context: Pre-defined public context object.
  7. Fetch ChatKit thread state with fetchThreadState

    main
    Use fetchThreadState to retrieve the current state of a specific ChatKit thread for the Agent panel. It requires a threadId and performs a GET request to the /chatkit/state endpoint. If the request fails or an error occurs, it returns null.
  8. Ensure a thread exists with ensure_thread()

    main

    Use ensure_thread to retrieve an existing thread by its thread_id or create a new one if it does not exist. This method interacts with the underlying store to persist thread metadata.

    async def ensure_thread(self, thread_id: Optional[str], context: dict[str, Any]) -> ThreadMetadata:
  9. Stream agent responses with respond()

    main

    The respond method is the primary entry point for processing user input. It streams ThreadStreamEvent objects, which include assistant messages and ClientEffectEvent notifications for runner state updates (e.g., runner_bind_thread, runner_state_update, runner_event_delta). It also handles InputGuardrailTripwireTriggered exceptions by returning a refusal message.

    async def respond(
            self,
            thread: ThreadMetadata,
            input_user_message: UserMessageItem | None,
            context: dict[str, Any],
        ) -> AsyncIterator[ThreadStreamEvent]: