# Chatbot with Google Search Assistance

## Getting Started

Navigate to the **Callable Builder** tab in the top right of the navigation bar and go ahead and press **Create Callable**. Name your callable **"llm-with-google-assist"** and give the following description:

> Answer questions with high factual accuracy by searching online and compiling responses based on content downloaded from websites (with references).

Select **Private** and choose the **Barebone** template callable before pressing **Create**.

Once inside the Callable Builder, you should see two blocks: INPUT, OUTPUT. Since we are creating a Chatbot let's switch the **input type** in the **INPUT** block to **ChatDataset**.

<details>

<summary>Understanding Chat Inputs</summary>

## Chat Inputs

Chat Callable's input is a message object that contains an array of messages.

#### Message Object

The `Message` object is a high-level entity that holds an array of individual message items.

* `messages`: An array of message items. Each item in the array is an object that consists of two properties: 'role' and 'content'.

#### Message Item

Each message item is an individual message with its associated role.

* `'role'` Role related to the message (e.g., 'user', 'assistant').
* `'content'` Text of the message.

### Example

Here is an example of what a `Message` object might look like:

```javascript
{
  "messages": [
    {
      "role": "user",
      "content": "Hello, how can I help you?"
    },
    {
      "role": "assistant",
      "content": "I'm here to assist you. What do you need?"
    },
    {
      "role": "user",
      "content": "I need information on your new products."
    }
  ]
}
  
```

</details>

<details>

<summary>Understanding Callable Blocks</summary>

Blocks are fundamental units that form logical sequences inside a Callable. They operate similarly to server-less functions, running independently yet capable of working in cohesion to create complex features.

Think of each block as an individual piece in a larger puzzle that is your Callable. Every block has its unique role and introduces a new functionality to the Callable. This could be anything from making curl requests, leveraging large language models, extracting specific data, to retrieving knowledge.

A key attribute of these blocks is that they can interact with one another by calling upon each other's results. This ability to collaborate makes them incredibly versatile and adaptable, allowing you to build intricate and sophisticated features by chaining them together in the Callables.

</details>

## 1. Formatting Question for Search Query

Message objects contain the entire conversation history as an array with the last message in the array from the user. In order to send a google search query, we first need to extract the relevant context of the conversation to enhance the user’s question and receive correct search results.

To do this, click the plus and create two code blocks: one to extract the question and the second to extract the conversation history.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2F7BmWWNtd3qwtrqwd8wFU%2FScreenshot%202023-07-17%20at%204.50.44%20PM.png?alt=media&#x26;token=37d373da-b3b5-41dc-9e18-010686fb2f76" alt=""><figcaption></figcaption></figure>

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2Fe2fwG5hGUThIO3932pKh%2FScreenshot%202023-07-17%20at%204.50.56%20PM.png?alt=media&#x26;token=01c91d1e-1a9b-4b94-a316-eb1516332b22" alt=""><figcaption></figcaption></figure>

Once we extract the question and history with the respected code blocks, we can refine our question with a language model so that it will include the context of the history inside a Model Block.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FA5cOs3obNy1C6P9IovOo%2FScreenshot%202023-07-17%20at%205.09.47%20PM.png?alt=media&#x26;token=df30c106-b1b7-4e2f-a83c-d3829818ec3c" alt=""><figcaption></figcaption></figure>

For the model configuration, we want a temperature of 0.1 for a more conservative change and so  that our original question is not altered dramatically.

## 2. Google Search and Summarizing Results

After refining our question, we want to create a google search query using the result from the REFINED\_QUESTION block that will provide links to relevant websites.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FoCnqdzi50DoryMXDNKjV%2FScreenshot%202023-07-17%20at%205.26.07%20PM.png?alt=media&#x26;token=44e7bb1b-9cf0-4dea-86c3-08263a918d18" alt=""><figcaption></figcaption></figure>

Our google search query provides us with a few of the most relevant links related to our query. In order to extract the information from these links, we must utilize a Map Reduce block and call a web crawler block on each link to retrieve the details from the google search results.

In order to extract the relevant links and web crawl over them, we want to simplify our GOOGLE\_SEARCH object into a result object with a field for the title and link before starting our Map Reduce block. This can be done with another Code block.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2F6nD7tPSjXTuPeeu8jUtJ%2FScreenshot%202023-07-17%20at%205.26.16%20PM.png?alt=media&#x26;token=450bc7bc-60ba-4499-a9c6-d0b8727c9609" alt=""><figcaption></figcaption></figure>

Now with a list of relevant links formatted correctly, we can start a Map Reduce loop to collect a summary of each link.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FtIx1CbSMdiuTm4vTJeQq%2FScreenshot%202023-07-17%20at%205.26.23%20PM.png?alt=media&#x26;token=e4808e05-ddaa-4fa4-ac93-96e1e8b7f532" alt=""><figcaption></figcaption></figure>

This loop begins by calling upon a Web Page Crawler Block with each link to retrieve the HTML body of the page.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FHvxtyiLtZjMzNUcuOpAi%2FScreenshot%202023-07-17%20at%205.26.31%20PM.png?alt=media&#x26;token=fc8f5d20-129a-44f8-9bf5-51b349dd6a36" alt=""><figcaption></figcaption></figure>

Then, another code block is called upon with the web crawler data to format into content blocks with only the first 2000 bytes of the HTML body.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FZwEFTdPsWbQBS6DDHqmD%2FScreenshot%202023-07-17%20at%205.26.38%20PM.png?alt=media&#x26;token=fbb16e55-6678-4c0e-be57-526fed72c434" alt=""><figcaption></figcaption></figure>

Once the data is collected, a language model block can help us summarize the raw data from the HTML body into a single paragraph for which we can use in our final prompt.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FjeLLmXxEtmCvbM42CVZu%2FScreenshot%202023-07-17%20at%205.26.51%20PM.png?alt=media&#x26;token=78029abf-2517-4825-94a6-8317b9088e46" alt=""><figcaption></figcaption></figure>

The last block in the loop is a code block that nicely formats the link summaries into an object for us to reference in our final prompt.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FffqP4iuWLdj1DOUbJYWl%2FScreenshot%202023-07-18%20at%201.10.19%20PM.png?alt=media&#x26;token=274eb2ba-b654-41d1-967a-de4d71edc041" alt=""><figcaption></figcaption></figure>

## 3. Final Prompt and Returning Result

Lastly, we need to combine the data from all relevant links together along with the original question into a single prompt before sending to our language model. This can be done with a code block that loops through each FORMAT\_SUMMARY object to extract the summary of the links and append it into a single string with the original question.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FdB6EQgDuWBsdL2Sp9sRb%2FScreenshot%202023-07-18%20at%201.12.26%20PM.png?alt=media&#x26;token=ee6e5c03-15d3-4de5-b66c-df5a3318574d" alt=""><figcaption></figcaption></figure>

Once this is finished, the last step is to run the final prompt in a language model prompt and extract the results.

<figure><img src="https://3035252822-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FeiBCAl6qTuG0sZpeXLxC%2Fuploads%2FwbMGTZGvTe75GLJfPzFc%2FScreenshot%202023-07-18%20at%201.17.55%20PM.png?alt=media&#x26;token=d8633896-9c3f-4bea-942f-9535b99b3899" alt=""><figcaption></figcaption></figure>

***

Congratulations, you have created a chatbot that uses google search to find relevant information about the question. Create some test cases and deploy your callable to start using it in any application.

## Clone callable
