Skip to content

Instantly share code, notes, and snippets.

@bajajcodes
Created February 3, 2025 11:20
Show Gist options
  • Select an option

  • Save bajajcodes/cd664c81344ef9f27a4d304de43bd313 to your computer and use it in GitHub Desktop.

Select an option

Save bajajcodes/cd664c81344ef9f27a4d304de43bd313 to your computer and use it in GitHub Desktop.

1. What Is Endpointing?

Imagine you’re talking to a virtual assistant. The system needs to know when you’ve finished speaking so it can process your request. That moment is called endpointing or "end of thought detection." Typically, the system waits a fixed amount of time after it senses silence, but sometimes you want it to be smarter.


2. Why Custom Endpointing Rules?

Sometimes the default waiting time isn’t ideal. For instance:

  • Shorter Wait: If the assistant asks a quick yes/no question like “Are you interested in a loan?”, you might want a very short pause.
  • Longer Wait: If you’re saying a long number (like an account number), you might need more time to finish.

Custom endpointing rules let you set up special patterns (using regex) and specify a timeout for each case. If a message matches a rule, the system uses the rule’s timeout instead of the default.


3. The Role of Regex

Regex (Regular Expression) is like a super-powered search tool. It lets you check if a text contains a particular pattern.

How Regex Works:

  • Basic Match:
    If you have a regex pattern "hello" and test it against "hello world", it returns true because the text contains “hello.”

  • Exact Match:
    To match the whole string exactly, you’d use anchors. For example, "^hello$" matches only if the text is exactly “hello.”

  • Options/Flags:
    Sometimes you want to ignore case. You can set a flag (like an ignore-case option) so that “HELLO” is treated the same as “hello.”

Important Note on Escaping:

When writing regex in JavaScript strings, you need to escape the backslash. So:

  • Wrong: "hello\sthere"
  • Right: "hello\\sthere"

4. Types of Custom Endpointing Rules

There are three types of rules you can set up:

A. Assistant Rule

  • What It Does:
    It checks the assistant’s last message.
  • When to Use:
    Use this for prompts like “Are you interested in a loan?” where the assistant’s wording indicates a quick answer.
  • Key Fields:
    • regex: The pattern to match against the assistant’s message.
    • regexOptions: Options such as ignore-case.
    • timeoutSeconds: How long to wait (in seconds) if the pattern is found.

Example:

{
  "type": "assistant",
  "regex": "loan",
  "regexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
  "timeoutSeconds": 1
}

B. Customer Rule

  • What It Does:
    It checks what the customer is saying as they speak.
  • When to Use:
    Use this if you need to adjust wait times based on the customer’s speech—like waiting longer if they mention numbers.
  • Key Fields:
    • regex: The pattern to match in the customer’s message.
    • regexOptions: Regex options like ignore-case.
    • timeoutSeconds: The timeout if a match is found.

Example:

{
  "type": "customer",
  "regex": "bye",
  "regexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
  "timeoutSeconds": 2
}

C. Both Rule

  • What It Does:
    It checks both the assistant’s and the customer’s messages.
  • When to Use:
    Use this when you want an endpoint to trigger only if both sides match their respective patterns.
  • Key Fields:
    • assistantRegex & assistantRegexOptions: The pattern and options for the assistant’s message.
    • customerRegex & customerRegexOptions: The pattern and options for the customer’s message.
    • timeoutSeconds: The wait time if both patterns match.

Example:

{
  "type": "both",
  "assistantRegex": "how are you",
  "assistantRegexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
  "customerRegex": "fine",
  "customerRegexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
  "timeoutSeconds": 3
}

5. Putting It All Together: An Example

Imagine your configuration looks like this:

{
  "customEndpointingRules": [
    {
      "type": "assistant",
      "regex": "hello",
      "regexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
      "timeoutSeconds": 1
    },
    {
      "type": "customer",
      "regex": "bye",
      "regexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
      "timeoutSeconds": 2
    },
    {
      "type": "both",
      "assistantRegex": "how are you",
      "assistantRegexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
      "customerRegex": "fine",
      "customerRegexOptions": [{ "type": "IGNORE_CASE", "enabled": true }],
      "timeoutSeconds": 3
    }
  ]
}

Now, consider these scenarios:

  • Scenario 1 (Assistant Rule):

    • Assistant Message: "Hello there, welcome!"
    • Customer Message: "Hi"
    • The system checks the first rule. It builds a regex for "hello" (with ignore-case) and finds a match in the assistant’s message. It then sets a delay of 1 * 1000 = 1000ms.
  • Scenario 2 (Customer Rule):

    • Assistant Message: "Good morning"
    • Customer Message: "Bye bye, see you later"
    • The customer’s message matches the regex "bye", so a delay of 2 * 1000 = 2000ms is used.
  • Scenario 3 (Both Rule):

    • Assistant Message: "How are you doing today?"
    • Customer Message: "I'm fine, thanks"
    • Both messages match their respective regex patterns from the “both” rule. The system then sets a delay of 3 * 1000 = 3000ms.

6. Real-Life Analogy

Think of it like adjusting the timer on a kitchen appliance:

  • Default Timer: Normally, the appliance runs for a fixed time.
  • Custom Rules: But if a sensor detects something unusual (like extra thick soup), it might adjust the cooking time.
  • Regex as the Sensor: In our case, regex is that sensor. When it finds a specific word or phrase, it tells the system to change the waiting time accordingly.

7. Summary

  • Custom Endpointing Rules let you change the default waiting period by using patterns (regex) to detect what’s being said.
  • Regex Patterns allow you to search for words or phrases within the assistant’s or customer’s messages.
  • Rule Types:
    • Assistant Rule: Matches the assistant’s last message.
    • Customer Rule: Matches the customer’s current message.
    • Both Rule: Matches patterns in both messages.
  • Timeout Seconds: Defines how long the system should wait after a match is found.
  • JSON Schema: Ensures that your rules include all the necessary fields (like regex, regexOptions, timeoutSeconds) and that they’re valid.

With this setup, you can fine-tune how the system detects the end of a customer’s thought, making it more responsive and better suited to your specific needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment