> For the complete documentation index, see [llms.txt](https://resources.less.tech/less-tech/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://resources.less.tech/less-tech/exercises/ai-driven-product-reviews/hints.md).

# 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="/files/Dtkfwk4LDpcpCneS7G7x" 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="/files/ziaqblZ8b9w5cf5clZOu" alt=""><figcaption></figcaption></figure></div>

The configuration of the API tools is shown below.

<div data-full-width="true"><figure><img src="/files/ivaXjWYEGvPPsadhX2QA" 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="/files/tsNuEq3Ck8tY5uOiSx4A" %}

***

### 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="/files/aAZqDt09HB2e50Jhy6mJ" 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="/files/riMTKuYUWpXHs0o7C6Ej" 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="/files/D17UnwJejewBpLfPm9C5" alt=""><figcaption></figcaption></figure>

<figure><img src="/files/wKHRrLvnO0XocTkq3pbE" alt=""><figcaption></figcaption></figure>

<div data-full-width="true"><figure><img src="/files/MEaq7mepG91KVNrWocZq" 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="/files/V3IVNyHxZuPw8P6O2sS3" alt=""><figcaption></figcaption></figure>

This is the formula in the Calculate tool:

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

***


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://resources.less.tech/less-tech/exercises/ai-driven-product-reviews/hints.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
