# Customizing smart functions output

In the realm of developing digital agents, the ability to tailor the output of smart functions is paramount.&#x20;

These [smart functions](https://app.gitbook.com/o/Mw7WDPYopwKEMNHPT3Gf/s/pgANrtLXcbtmDLovujQ0/), akin to miniature scripts, excel in extracting pertinent information from user utterances, be it named entities or other relevant data. However, the challenge lies in the diverse **formats of these outputs**, ranging from **arrays and dictionaries to simple strings or integers.**

The crux of the matter arises when integrating these extracted entities into the Digital agents's dialogue or synthesized speech, where naturalness and seamlessness are imperative. Thus, leveraging Python syntax, we adeptly mold the outputs from smart functions into variables, ensuring a fluid and natural conversation flow.

**Scenarios Requiring Presentation or Vocalization of Extracted Entities:**

1. **Confirmation Queries:**
   * "Did I understand correctly that your order number is {order\_id}?"
   * "Am I correct in assuming your interest lies in the opening hours of the branch in {city}?"
   * "Have I correctly noted that your name is {full\_name}?"
2. **Composing Email/SMS Texts for User Dispatch:**
   * "Confirming that your order with number {order\_id} will be dispatched to the address provided at {address}, under the name {full\_name}."
3. **Initiating Ticket Creation for Back Office Requests.**

## Address

When dealing with a smart function for `address` extraction, the output manifests in the form of a dictionary, with key-value pairs representing various address components such as city, street, etc. \
\
Let's say we extracted the address from the user's utterance *Send me the copy of the contract to Main Street 123 in Prague* and stored it in a variable named **`extracted_address`**.

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FBaQrGVchzhDsCtowgFA5%2Fimage.png?alt=media&#x26;token=184702b2-c2bc-4d8b-b7d2-4e5f363c2c57" alt=""><figcaption></figcaption></figure>

{% code title="extracted\_address" %}

```
{"city": "Prague", "street": "Main Street 123"}
```

{% endcode %}

To effectively utilize this data in textual contexts, we employ simple Python syntax to parse and store these values into separate variables.

To access individual components of the address, we utilize the following syntax:

* New variable name: **`street_to_read`**\
  New variable value:  `extracted_address["street"]`
* New variable name: **`city_to_read`**\
  New variable value: `extracted_address["city"]`

Subsequently, we integrate these variables into our text, ensuring a coherent and natural flow:

*"I've noted that you reside in {city\_to\_read}, specifically on {street\_to\_read}. Is that correct?"*

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FzP6nkeKaDzZyDtL82x3D%2Fimage.png?alt=media&#x26;token=6065cef4-8218-4757-a530-06d6ad18263b" alt=""><figcaption></figcaption></figure>

**Notes:**

* If extraction of address fails, no output will be stored in `extracted_address`. Therefore, individual variables `street_to_read` and `city_to_read` won't be filled with values as well. Make sure MSG node, where entities are meant be be displayed/read back to user, is entered only under the condition that the address was extracted from user's utterance.
* With languages that use declination, be mindful when crafting message texts, since values in smart function output are always nomitative.\
  :flag\_cz: \
  &#x20;      Rozuměl jsem správně, že bydlíte v {city\_to\_read}? :x:\
  &#x20;      Rozuměl jsem správně, že bydlíte v Praha? :x:\
  &#x20;      Rozuměl jsem správně obec {city\_to\_read}? :white\_check\_mark:\
  &#x20;      Tozuměl jsem správně obec Hradec nad Moravicí? :white\_check\_mark:\
  &#x20;      Bydlíte v obci {city\_to\_read}? :white\_check\_mark:\
  &#x20;      Bydlíte v obci Kunčice? :white\_check\_mark:

***

## Advanced number

The smart function `advanced_number`  excels in consolidating and extracting numbers, typically returning the output as a string by default.

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FI9dGeDTGSdJxm0hqN5Mz%2Fimage.png?alt=media&#x26;token=b8933d5e-262d-4e60-895e-d9b5a59a1413" alt=""><figcaption></figcaption></figure>

For a **chatbot**, no customization is necessary. We can seamlessly integrate the extracted number variable into the dialogue text. For example:<br>

```
Text
Your order ID is {extracted_order_id}. Is that correct? 

```

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FrtxbrXC99ftK4RmbqvII%2Fimage.png?alt=media&#x26;token=d7c7f8c4-a89b-4df5-b631-e82269d1fc69" alt=""><figcaption></figcaption></figure>

However, for a **voicebot,** it's prudent to ensure that the number is dictated in a comprehensible manner. Dictating each digit individually and slowly allows the user ample time to verify the information.

To achieve this, we can split the order number into individual digits:

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FTGXXcnlswIsJRr22jNvR%2Fimage.png?alt=media&#x26;token=68aa55d8-f9ad-470b-8014-8ef59b7ff130" alt=""><figcaption></figcaption></figure>

{% code title="order\_id\_digit\_by\_digit" %}

```python
", ".join(extracted_order_id)
```

{% endcode %}

This transforms "123456789" into "1, 2, 3, 4, 5, 6, 7, 8, 9.

<pre data-overflow="wrap"><code><strong>Speech input:
</strong>Your order ID is {order_id_digit_by_digit}. Is that correct?

Speech output:
Your order ID is 1, 2, 3, 4, 5, 6, 7, 8, 9. Is that correct?
</code></pre>

We then utilize this variable in the speech channel to ensure that the synthetic voice reads it slowly and distinctly, enhancing user comprehension.

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2Fyd5XGDaI8cfvlc4bEHl4%2Fimage.png?alt=media&#x26;token=249076ac-7f9d-4338-aa79-5043630a6bca" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
:bulb:**Pro-tip!**\
Additionally, [SSML](https://docs.borndigital.ai/for-advanced-users/customizing-speech-synthesis#ssml-tags) tags can be incorporated to further customize the reading speed and other aspects of the speech output.

{% code title="Speech" overflow="wrap" %}

```ssml
Your order ID is<prosody rate="-5.00%"><say-as interpret-as="spell-out"> {order_id_digit_by_digit}</say-as>.</prosody> Is that correct?
```

{% endcode %}
{% endhint %}

***

## Full name

Similar to address extraction, the output from the smart function for extracting `full_name` manifests as a dictionary, comprising key-value pairs representing the name and surname components.

Consider the scenario where the output from the smart function is stored in a variable named **`extracted_name`**.&#x20;

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FSPuZwGIWbqD2GWd5GlkJ%2Fimage.png?alt=media&#x26;token=1af778f5-449c-4080-a237-13e9a1941f17" alt=""><figcaption></figcaption></figure>

Output would look like this:

{% code title="extracted\_name" %}

```
{"name": "John", "surname": "Doe"}
```

{% endcode %}

To access individual components of the full name, we utilize the following syntax:

* New variable name: **`name_to_read`**\
  New variable value: `extracted_name["name"]`
* New variable name: **`surname_to_read`**\
  New variable value: `extracted_name["surname"]`

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FqOUxcmyXXpEjnAynvdvL%2Fimage.png?alt=media&#x26;token=8d914c27-8453-4b3f-8c29-316a293be9c6" alt=""><figcaption></figcaption></figure>

Subsequently, we seamlessly integrate these variables into our text to ensure a cohesive and natural flow:

*"I've noted your name as {name\_to\_read} {surname\_to\_read}. Is that correct?"*<br>

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2F6TG16MkXAxNUrGyKU8yo%2Fimage.png?alt=media&#x26;token=5cf80c14-b931-49c7-ab38-a19b13d7d45d" alt=""><figcaption></figcaption></figure>

**Notes:**

* If extraction of `full_name` fails, no output will be stored in `extracted_name`. Therefore, individual variables `name_to_read` and `surname_to_read` won't be filled with values as well. Make sure MSG node, where entities are meant be be displayed/read back to user, is entered only under the condition that the full\_name was extracted from user's utterance.
* With languages that use declination, be mindful when crafting message texts, since values in smart function output are always nomitative.\
  :flag\_cz: Hovořím s {name\_to\_read} {surname\_to\_read}? Hovořím s Anna Nováková? :x:\
  &#x20;     Jste prosím {name\_to\_read} {surname\_to\_read}? Jste prosím Petr Novotný? :white\_check\_mark:

***

## Phone

The smart function `phone` for extracting phone numbers operates by adhering to the language settings configured within the project. It returns an array containing the extracted phone number with the appropriate prefix added, based on the language setting. For instance, for utterances in Czech, Polish, German, and other languages, the format may vary accordingly.

Let's assume the output array for the utterance "*Moje telefonní číslo je 123 456 789*" in Czech language configuration is as follows:

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FqsjOFhyLafBkPvSmRirv%2Fimage.png?alt=media&#x26;token=bf79ada9-90ea-4082-8f13-2aac39aca8bb" alt=""><figcaption></figcaption></figure>

{% code title="extracted\_phone" %}

```python
['+421123456789']
```

{% endcode %}

To integrate this phone number into text, we extract it from the array and store it in a separate variable:

* New variable name: **`phone_to_read`**\
  New variable value: `extracted_phone[0]`

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FTsRKUVXFDvqoMqhmtH7n%2Fimage.png?alt=media&#x26;token=7fec0ddb-4369-4eca-9ea9-eb4d459b4e23" alt=""><figcaption></figcaption></figure>

Next, we use the `phone_to_read` variable in message text to be displayed in chat bubble or read aloud with speech synthesis.<br>

<figure><img src="https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FxL1n7pgJqWAEozhdiugp%2Fimage.png?alt=media&#x26;token=e7428d35-bdfd-4094-b13a-f488968e6507" alt=""><figcaption></figcaption></figure>

{% hint style="info" %}
:bulb: **Pro-tip!** \
When developing a **voicebo**t, it's essential to utilize SSML ([Speech Synthesis Markup Language](https://docs.borndigital.ai/for-advanced-users/conversation-design-tips/customizing-speech-synthesis)) tags to ensure accurate pronunciation and appropriate pacing. This is particularly crucial for reading out phone numbers, where each digit should be pronounced individually rather than as a single number (e.g., "one two three four five six seven eight nine").

Additionally, incorporating SSML tags to slow down the speech rate can enhance clarity, especially for dictation purposes.

{% code title="Speech" overflow="wrap" %}

```ssml
I have noted <prosody rate="-10.00%"><say-as interpret-as="spell-out">{phone_to_read}</say-as></prosody> as your phone number. Is that correct? 
```

{% endcode %}

![](https://4261467870-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F6iQTvxgRZRwPS1NgIGEb%2Fuploads%2FsJ8z4k7YFYBC1dL2PNgT%2Fimage.png?alt=media\&token=e41caba1-f724-49ed-b789-15c900d6d1f6)
{% endhint %}

**Notes**:

* If extraction of `phone` fails, no output will be stored in `extracted_phone`. Therefore, variable `phone_to_read` won't be filled with a value as well. Make sure MSG node, where entities are meant to be displayed/read back to the user, is entered only under the condition that the phone was extracted from user's utterance.
* For voice channel, take your time and c[ustomize speech output](https://docs.borndigital.ai/for-advanced-users/customizing-speech-synthesis#phone-numbers) with SSML to achieve the best result.
