Skip to main content

AI-powered module for evaluating escalation and de-escalation patterns in chat conversations

Project description

DeEscalation Scoring Module

PyPI version Python 3.10+ License: MIT

A Python module for evaluating escalation and de-escalation patterns in chat conversations using AI-powered scoring. Perfect for customer service, community management, and conflict resolution applications.

🚀 Features

  • AI-Powered Scoring: Analyze chat conversations for escalation levels (1-5 scale)
  • Flexible API Support: Works with Poe API, OpenAI, or any OpenAI-compatible endpoint
  • Multiple Input Formats: Handle string conversations or structured message arrays
  • Batch Processing: Score multiple conversations efficiently
  • Robust JSON Parsing: Automatic handling of LLM response parsing
  • Easy Integration: Simple API for developers and non-technical users
  • Coaching Tips: Get actionable suggestions for improving conversations

📦 Installation

pip install de-escalation-scoring-module

🔧 Quick Setup

1. Environment Variables (Recommended)

export POE_API_KEY="your_poe_api_key_here"

2. Or use API key directly in code

from de_escalation_scoring import DeEscalationScorer

scorer = DeEscalationScorer(api_key="your_poe_api_key_here")

📚 Usage Examples

Basic Usage

from de_escalation_scoring import DeEscalationScorer, score_chat

# Using environment variable for API key
scorer = DeEscalationScorer()

# Score a simple message
result = scorer.score_conversation("Hello, how can I help you today?")
print(result)
# Output: {"score": 5, "reason": "Polite and helpful greeting", "coaching_tip": "Great start to build rapport"}

# Quick scoring function
result = score_chat("I'm really frustrated with this service!")
print(result)
# Output: {"score": 2, "reason": "Shows frustration and dissatisfaction", "coaching_tip": "Acknowledge the frustration and ask for specifics"}

Advanced Usage with Custom API

# Using OpenAI API instead of Poe
scorer = DeEscalationScorer(
    api_key="your_openai_api_key",
    base_url="https://api.openai.com/v1",
    model="gpt-4",
    system_prompt="Analyze conversations for de-escalation patterns and provide constructive feedback."
)

result = scorer.score_conversation("I understand your concern. Let me help you solve this.")

Conversation Thread Scoring

# Score a multi-message conversation
messages = [
    "This service is terrible!",
    "I'm sorry to hear about your experience. Can you tell me what specifically went wrong?",
    "Well, my order was delayed and nobody told me.",
    "I apologize for the delay and lack of communication. Let me check your order status right away."
]

result = scorer.score_message_thread(messages)
print(f"Score: {result['score']}/5")
print(f"Reason: {result['reason']}")
print(f"Tip: {result['coaching_tip']}")

Batch Processing

conversations = [
    "Thank you for your help!",
    "This is unacceptable!",
    "I appreciate your patience while we resolve this."
]

results = scorer.batch_score(conversations)
for i, result in enumerate(results):
    print(f"Conversation {i+1}: Score {result['score']}/5")

Structured Message Format

chat_history = [
    {"role": "user", "content": "I need help with my account"},
    {"role": "assistant", "content": "I'd be happy to help you with your account. What specific issue are you experiencing?"},
    {"role": "user", "content": "I can't log in and it's really frustrating"}
]

result = scorer.score_conversation(chat_history)

🎯 Use Cases

Customer Service

  • Real-time monitoring of support conversations
  • Quality assurance for customer service representatives
  • Training feedback for support teams
  • Escalation prediction to prevent conflicts

Community Management

  • Forum moderation assistance
  • Social media monitoring for brand reputation
  • Discord/Slack channel health monitoring
  • Online community wellness tracking

Conflict Resolution

  • Mediation support in dispute resolution
  • Communication coaching for better outcomes
  • De-escalation training programs
  • Workplace communication improvement

📊 Scoring System

Score Level Description Example
1 Highly Escalated Aggressive, hostile, threatening "This is ridiculous! I demand to speak to your manager now!"
2 Escalated Frustrated, angry, demanding "I'm really upset about this delay. This is unacceptable."
3 Neutral Matter-of-fact, neither calm nor agitated "I have a problem with my order. Can you help?"
4 Calm Polite, patient, understanding "I understand these things happen. How can we fix this?"
5 De-escalating Actively calming, empathetic, solution-focused "I appreciate your patience. Let me personally ensure this gets resolved."

🔑 API Configuration

Supported APIs

Poe API (Default)

scorer = DeEscalationScorer(
    api_key="your_poe_api_key",
    base_url="https://api.poe.com/v1",
    model="DeEscalationScoring"
)

OpenAI

scorer = DeEscalationScorer(
    api_key="your_openai_api_key",
    base_url="https://api.openai.com/v1",
    model="gpt-4"
)

Azure OpenAI

scorer = DeEscalationScorer(
    api_key="your_azure_api_key",
    base_url="https://your-resource.openai.azure.com/",
    model="gpt-4"
)

Custom/Local APIs

scorer = DeEscalationScorer(
    api_key="your_api_key",
    base_url="http://localhost:8000/v1",
    model="your-custom-model"
)

🛡️ Error Handling

try:
    result = scorer.score_conversation("Hello world")
    print(f"Score: {result['score']}")
except ValueError as e:
    print(f"JSON parsing error: {e}")
except Exception as e:
    print(f"API error: {e}")

🔧 Integration Examples

Flask Web Application

from flask import Flask, request, jsonify
from de_escalation_scoring import DeEscalationScorer

app = Flask(__name__)
scorer = DeEscalationScorer()

@app.route('/score', methods=['POST'])
def score_conversation():
    data = request.json
    result = scorer.score_conversation(data['conversation'])
    return jsonify(result)

Discord Bot

import discord
from de_escalation_scoring import DeEscalationScorer

scorer = DeEscalationScorer()

@bot.event
async def on_message(message):
    if not message.author.bot:
        result = scorer.score_conversation(message.content)
        if result['score'] <= 2:
            # Alert moderators for potential escalation
            await message.channel.send("🤖 Escalation detected. Moderator notified.")

Slack Integration

from slack_sdk import WebClient
from de_escalation_scoring import DeEscalationScorer

scorer = DeEscalationScorer()

def handle_message(event):
    text = event['text']
    result = scorer.score_conversation(text)
    
    if result['score'] <= 2:
        # Send coaching tip to manager
        client.chat_postMessage(
            channel="manager-alerts",
            text=f"Escalation detected. Coaching tip: {result['coaching_tip']}"
        )

🤖 For Non-Technical Users

If you're not a developer, you can still use this module by:

  1. Hiring a developer to integrate it into your existing systems
  2. Using AI coding assistants like ChatGPT, Claude, or GitHub Copilot
  3. Consulting with a technical specialist for custom integration

Sample Request for Developers:

"Please integrate the de-escalation-scoring-module into our customer service system. We need to monitor chat conversations and get alerts when escalation levels are high (score ≤ 2). The module is available on PyPI as 'de-escalation-scoring-module'."

📋 Requirements

  • Python 3.10 or higher
  • openai library for API communication
  • json-repair library for robust JSON parsing
  • Valid API key for your chosen AI service

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

🔄 Changelog

v0.1.1

  • Initial release
  • Support for Poe API and OpenAI-compatible endpoints
  • Batch processing capabilities
  • Comprehensive error handling
  • Multiple input format support

Made with ❤️ in Hackathon Hack the Divide

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

de_escalation_scoring_module-0.1.1.tar.gz (11.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

de_escalation_scoring_module-0.1.1-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file de_escalation_scoring_module-0.1.1.tar.gz.

File metadata

File hashes

Hashes for de_escalation_scoring_module-0.1.1.tar.gz
Algorithm Hash digest
SHA256 7f4a20fba3681cdf11720f7294ca2a39a0fd7a3e801d604aa607bdefd7ab0bd4
MD5 53198a8394cc2d771e112eeafc28a418
BLAKE2b-256 5ba3a50d66ac18c5e38656b2f4854fefd0322da57c8312e78398153f78cbacc9

See more details on using hashes here.

Provenance

The following attestation bundles were made for de_escalation_scoring_module-0.1.1.tar.gz:

Publisher: python-publish.yml on NewJerseyStyle/DeEscalationScoringModule

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file de_escalation_scoring_module-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for de_escalation_scoring_module-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 343670524fd7f36939927a34098dee441e1f3055189c4a191b6a66ff6d31d1f1
MD5 358a64028298b8dce395b0da6c968ccd
BLAKE2b-256 79992cb1cdb1d92151ba249ff610fd5a4708a8d32f50344d14d1537fe2766532

See more details on using hashes here.

Provenance

The following attestation bundles were made for de_escalation_scoring_module-0.1.1-py3-none-any.whl:

Publisher: python-publish.yml on NewJerseyStyle/DeEscalationScoringModule

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page