# Hints

### Hint 1

The part of this exercise is about creating our prompt and authentication token ([see the introduction presentation for more on this](https://app.gitbook.com/o/RcswY5TelREASsdLiDLX/s/aSJL6Od4KuV8hrdCZbXp/~/changes/40/exercises/ai-driven-product-reviews#introduction-ecommerce)). The prompt is basically what we ask the AI do - exactly the same as using ChatGPT.

We add two new columns with two Text Column tools. We call the first column Prompt and the second Token.&#x20;

<div data-full-width="true"><figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FciDVy08kfdD5ikAR8fqY%2FCleanShot%202024-03-14%20at%2010.48.38.png?alt=media&#x26;token=f6403145-b53b-4709-9466-b1128a9f3fda" alt=""><figcaption></figcaption></figure></div>

In the prompt, we input the below text. You can and should experiment with this to get the results you want. This is known as Prompt Engineering (fancy word for something straightforward...).

> "You are to evaluate the product reviews data on a scale from 0-10 where 10 is an absolute fantastic review and 1 is a horrible review. The product is a bluetooth speaker called Amazon Tap. ONLY OUTPUT AN INTERGER NUMBER BETWEEN 1 AND 10 (inclusive)."

In the Token column, we input our OpenAI API Key. Follow the steps in the Introduction Presentation to generate you own.

***

### Hint 2

The second part is where the magic happens 🪄. We use an API tool to call OpenAI and generate our product review scores.&#x20;

<div data-full-width="true"><figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FtBFn4YTMnP4DBctjfcen%2FCleanShot%202024-03-14%20at%2011.00.22.png?alt=media&#x26;token=5ecb1bd7-a5dd-416f-ae7c-0bb701d7e617" alt=""><figcaption></figcaption></figure></div>

The configuration of the API tools is shown below.

<div data-full-width="true"><figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FyYwZX95EUeSFwe3CEvF7%2FCleanShot%202024-03-14%20at%2010.57.02.png?alt=media&#x26;token=fb847a06-ccd7-4712-9c81-432cb7d1ce96" alt=""><figcaption></figcaption></figure></div>

* In #1, we use a POST call
* In #2, we input the right URL (this is the *address* telling OpenAI what service we're looking for). This is the URL: *<https://api.openai.com/v1/chat/completions>*
* In #3, we add our API headers. We add *Content-Type = application/json* to tell OpenAI that we want to receive a response in JSON. We add *Authorization = bearer <mark style="background-color:blue;">\[Token]</mark>* to authenticate our call. This is basically an ID Card telling OpenAI who we are. The *<mark style="background-color:blue;">\[Token]</mark>* is a reference to our Token column that we created earlier. So the *<mark style="background-color:blue;">\[Token]</mark>* looks up the value in the Token column. You can also statically input "bearer sk\_QhX...".&#x20;
* In #5, we input our body. This is the specification of what we want OpenAI to do. You really only have to pay attention to the *messages* object. In the *content,* we again reference two columns. First we give a reference to the prompt column that we created earlier. Then we give a reference to the *reviews\_text* column which is what contains the actual product review. So in essence we're giving the AI model the instruction first and then telling giving the AI model that text it should evaluate.

```json
{
    "model": "gpt-3.5-turbo",
    "messages": [
        {
            "role": "user",
            "content": "[Prompt]]\n###Text: [reviews_text]###"
        }
    ],
    "temperature": 1,
    "top_p": 0.5,
    "n": 1,
    "stream": false,
    "max_tokens": 1000,
    "presence_penalty": 0,
    "frequency_penalty": 0
}



```

The API action can take a while to run. So be patient! In our case, you can see that it take around 50 seconds below.

{% file src="<https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FDrmBvD8RFqiXSBUrSSrP%2FCleanShot%202024-03-14%20at%2011.08.01-converted.mp4?alt=media&token=84a9cecd-d7bb-4f93-9e40-623371517c89>" %}

***

### Hint 3

After calling the API we get a couple of new columns - some of which are structured as JSON. JSON looks a bit unfamiliar but we can make it familiar with the Parse JSON tool.

<figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2F5jEzXzgrQvHl13EC8ATq%2FCleanShot%202024-03-14%20at%2011.16.09.png?alt=media&#x26;token=6a629210-942b-4859-bb8f-d939a21d5849" alt=""><figcaption></figcaption></figure>

Before doing that we remove all rows that are where Response Status is not equal to 200. Response Status == 200 means that it was successful and we only want those.

Then we go into the Parse JSON, where we essentially parse through the nested layers/levels that are in the response result column. We give the Parse JSON an ID Column in #1 (use the review\_id), the in #2 we give it what column what want to parse and in #3 we give it the number of level we want to parse. JSON data is often comprised of levels of data that are nested. You can read more about JSON format [here](https://www.digitalocean.com/community/tutorials/an-introduction-to-json). For now, go with 4 and try to change it to see how you data changes.

<figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2F1gLMvB1vkAfICQDdXhtQ%2FCleanShot%202024-03-14%20at%2011.23.27.png?alt=media&#x26;token=62d045f1-6165-4dfd-a9fb-40e1d20fa96e" alt=""><figcaption></figcaption></figure>

***

### Hint 4

In the final part, we remove all the columns except the review\_id and the content column which we in Tool ID 6. In the Combine, we merge our original dataset using the *review\_id* column, so we have all our original data an our newly create AI generated review score. In Tool ID 10, we rename our *content* column, AI\_review\_score and we are done!

<figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2Fyv4xQ7mRMFuOmJgEsI65%2FCleanShot%202024-03-14%20at%2011.30.01.png?alt=media&#x26;token=6e539d38-3c5d-490f-a6ae-29bcd0ee2087" alt=""><figcaption></figcaption></figure>

<figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FhiJHQ7JXo3RamW8xnmfU%2FCleanShot%202024-03-14%20at%2011.26.15.png?alt=media&#x26;token=1f14988d-ce43-4588-9332-407ed15b62d2" alt=""><figcaption></figcaption></figure>

<div data-full-width="true"><figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2FozMSeClb1h5uIoUDJPN3%2FCleanShot%202024-03-14%20at%2011.28.32.png?alt=media&#x26;token=f80a8d32-b2c2-4653-867e-2b3ab8487d1e" alt=""><figcaption></figcaption></figure></div>

***

### Hint 5

You can very easily calculate the cost of your API call by doing the below if you want.

<figure><img src="https://1596277999-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FaSJL6Od4KuV8hrdCZbXp%2Fuploads%2Fnm5ZbPAlqfhp0jjk6KZI%2FCleanShot%202024-03-14%20at%2011.27.17.png?alt=media&#x26;token=e80fac5c-1780-47d1-b5f0-daf0a49b5626" alt=""><figcaption></figcaption></figure>

This is the formula in the Calculate tool:

> ((\[sum\_prompt\_tokens]/1000)\*0.0005)+((\[sum\_completion\_tokens]/1000)\*0.0015)

***
