Get startedGet started for free

Text generation

1. Text generation

Let's explore some other AI functionality in Snowflake.

2. Managing hotel review responses

Imagine managing digital operations for a global hotel chain with thousands of reviews daily, some detailed, others vague. Our goal is to automate meaningful, human-like replies at scale.

3. summarize()

Recall that previously, we used the Cortex `summarize()` function to extract the core message from text. For example, take this long review. We can produce a succinct overview. We import summarize, call the function, and assign the long_review variable to the text keyword argument. We store the results as a variable,

4. Printing the summary

then print the output. We stored long_review as a text variable, but how do we extract reviews from our database?

5. Extracting from a database

To do this, we query our table in `cell1`, extracting the latest review by date. We then store the results into a pandas DataFrame using the `.to_pandas()` method on `cell1` and extract the string text using `iloc[]` on the single row. This review is short, so we can skip the summarize step and start crafting a reply.

6. Generating a reply

To do this, we import the `complete` function, which is also part of the `snowflake.cortex` module. We create a prompt, giving an instruction to the model. We use an f-string to feed in our latest review. Next, we call complete, passing the prompt. We can also set other arguments!

7. Generating a reply

Here, we specify that we want to use a Llama model.

8. Generating a reply

For further customization, we use the `options` argument and assign a dictionary. First, we set `temperature` to `0.3`. Temperature controls the randomness of a model's output, with lower values being more predictable and higher values being more creative and diverse.

9. Generating a reply

We also set `max_tokens` to 150. A token is a small chunk of text, such as a word, part of a word, or punctuation, that the model processes individually to understand and generate language. By setting this, we limit the number of tokens in the output to 150.

10. Viewing the response

The output here is a succinct reply that references key feedback from the original review. This process is fast, flexible, and produces content that looks like a real person wrote it.

11. Text generation with AI_COMPLETE()

When we want to generate text straight in SQL cells, we can use the `AI_COMPLETE()` function. First, we specify the model. Then we paste our prompt with a template that includes placeholders, and pass in our DESCRIPTION column via a comma. Placeholders are indexed starting from zero, and we can insert multiple columns into a single prompt. We finally specify our parameters as a dictionary, passing the temperature and max_tokens keys with values.

12. Output in Snowflake notebook

The output in Snowflake notebook will look like this. When we use `AI_COMPLETE()` in SQL cells, we don't need to pre-install any packages - it's available out of the box.

13. Text generation in Snowflake

With text generation in Snowflake, we don't just analyze reviews, we engage with them. Because it runs entirely within Snowflake, there's no need to manage APIs or infrastructure. `AI_COMPLETE()` is useful for generating text in SQL cells for a batch of rows in one go. complete() is powerful when we want to chain multi-step workflows with many functions - more on that later in the course.

14. Let's practice!

Now it's time for you to practice both approaches!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.