# Build your KBAI flow

Once you [added content to your KBAI](https://docs.chatlayer.ai/navigation/knowledge-base-ai/add-content-to-your-kbai), it's time to build a flow where your bot will know when to browse this knowledge base to provide an answer to your customer's question.&#x20;

To create your KBAI flow you could either:

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-cover data-type="files"></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td><strong>Use the KBAI template bot</strong></td><td>Make your KBAI flow with a ready-made bot.</td><td></td><td><a href="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FfiiE5haHAEsXAwCnnngD%2FScreenshot%202024-08-20%20at%2011.42.47.png?alt=media&#x26;token=fdaf386d-847d-4642-bb20-639529c86a6a">Screenshot 2024-08-20 at 11.42.47.png</a></td><td><a href="../../../start-quickly/bot-templates#knowledge-base-ai">#knowledge-base-ai</a></td></tr><tr><td><strong>Build your own KBAI flow</strong></td><td>A step-by-step guide on how to make your FAQ flow using KBAI.</td><td></td><td><a href="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FxaKq6gk58YB79Xs1p9x7%2FScreenshot%202024-08-20%20at%2011.50.58.png?alt=media&#x26;token=26ea8f31-952a-46d3-bb1c-9c22fcb331b5">Screenshot 2024-08-20 at 11.50.58.png</a></td><td><a href="#build-a-knowledge-base-flow">#build-a-knowledge-base-flow</a></td></tr><tr><td></td><td></td><td></td><td></td><td></td></tr></tbody></table>

## Retrieve the KBAI answer

To retrieve an answer from the knowledge base:

1. Drop an [**Action**](https://docs.chatlayer.ai/buildabot/flow-logic/dialog-state/action-bot-dialog) block in your canvas. For this example, we will name this block "Generate answer".

{% hint style="warning" %}
Many customers will preferably have their knowledge base flow happening after the bot did not understand what was asked, as a fallback option. To do this, go to your **Not Understood** block, change its block type to **Action**, and continue by following the steps below.
{% endhint %}

2. Click on **Knowledge base AI.**

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FpNy6O1AUTZxbUbXN32im%2FScreenshot%202024-08-20%20at%2014.16.16.png?alt=media&#x26;token=66528308-3e18-4338-b668-2b5aec75a397" alt="" width="375"><figcaption><p>Add a Knowledge base Action block to generate an answer.</p></figcaption></figure>

3. Fill in the fields accordingly. You can configure:
   * **Destination variable**: the answer from the knowledge base will be stored as a [variable](https://docs.chatlayer.ai/navigation/settings/secure-variables-gdpr). In the first field, you can choose the name of that variable.
   * **On no findings:** if the question that the user asks cannot be answered by the knowledge base AI, a block will be triggered. You can select which block in this dropdown.
   * **On failure**: if there is a problem with the knowledge base AI, and it returns an error, you can select a block which the user will be led to. By default, users will be routed to the **Error Occurred** block.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2FuNYtYLeuemHlekxdHArH%2Fimage.png?alt=media&#x26;token=89c81fea-8210-4c47-b2b4-9233ca905a43" alt=""><figcaption><p>Set up how your bot retrieves an answer with KBAI.</p></figcaption></figure>

3. **Save** your changes.

You bot can now scrape the KBAI and retrieve an answer based on it. But it cannot yet display that answer. Let's see how to do that in the section below.

## Display the KBAI answer

To display the answer from the knowledge base:

1. Open the **Action** block that you just created.
2. At the bottom of the block, add a **Go-to** to a block that you can create from within the dropdown. In this example, we will call this block *KB result*.

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2Fky0Q3LHLCNdQ2pa2eSY8%2Fimage.png?alt=media&#x26;token=ecaabb39-f9d3-4374-a452-9a8049ecc36b" alt=""><figcaption><p>Add a block that displays the generated answer.</p></figcaption></figure>

4. **Save** your changes.
5. Open the newly created "KB result" block.
6. Add a **Text message** that contains the variable you chose to save the knowledge base AI answer into. By default, this is `{knowledgebase.answer}.`

<figure><img src="https://2786867680-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LLTwFwbOqJj4dDhg8Ju%2Fuploads%2Fz8V5mksVw1GN6Z9Kv8Tp%2Fimage.png?alt=media&#x26;token=8e2615b0-7300-437a-81ba-30130929c901" alt=""><figcaption></figcaption></figure>

4. **Save** your changes.&#x20;

### Route your KBAI flow

There are multiple ways to route your KBAI flow.

You could route your KBAI flow based on the [session data](#kbai-session-data) inside the `knowledgebase` object.

<details>

<summary>KBAI session data</summary>

You can access your KBAI session data by using the [**Debugger**](https://docs.chatlayer.ai/buildabot/emulator#debugger) tab inside the Emulator.

The `knowledgebase` object contains several fields:

* `answer`: the answer that can be used in a text message in your bot
* `retrieved`: an array of all the content that was used to formulate the answer. For each array item, the following data is stored:
  * `type`: type of the source: "URL" or "DOC"
  * `name`: filename of the document or link of the URL
  * `content`: snippet of the content that was used to generate the answer
  * `tags`: any [tags](https://docs.chatlayer.ai/navigation/knowledge-base-ai/use-tags-to-limit-your-kbai-content) that were retrieved that are associated to the content source.
  * `contentType`: type of the source: "URL" or "DOC"
* `contentUrl`: URL of the source that was used. If a domain was scraped as content, this URL will refer to the specific page from which the answer was retrieved.

</details>

You might also want to use tags in your content to specify which content the bot should look at.

{% content-ref url="use-tags-to-limit-your-kbai-content" %}
[use-tags-to-limit-your-kbai-content](https://docs.chatlayer.ai/navigation/knowledge-base-ai/use-tags-to-limit-your-kbai-content)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.chatlayer.ai/navigation/knowledge-base-ai/build-your-kbai-flow.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
