Icon

Sentiment_​Analysis_​with_​LLMs_​Exercise

Step 2. Prompt engineering - Instructions & examples

  1. Use an Expression node to create few-shot examples template.

    • Use the join() function and concatenate the example, the review and the sentiment.

  2. Use the Group by node to combine the two rows of the new column and separate them by "\n\n".

  3. With another Expression node, create a prompt to instructs the LLM to classify the sentiment of the reviews in the data table.

    • Be specific. Write something like "Assign a sentiment label [positive, negative, neutral] to the text below. \n\n"

    • Combine the instructions with the few shot examples.

Step 5. Output Validation

  1. Use the String Cleaner node to clean the response column from leading and trailing whitespace.

  2. Check that the set of responses contains only the desired answers [positive, negative, neutral]. You can use the regex_match() function inside an Expression node.


Hint: Use the String Format Manager node to better display the output table by wrapping long strings in multiple lines.


Step 3. Prompt engineering - Adding new data

  1. Read the reviews.xlsx file from the data folder.

  2. With an Expression node, combine the prompt template and the reviews to classify the sentiment of the reviews.

    • Provide the prompt template via flow variable to the Expression node.

Step 1. Read data

1. The few-shot examples are provided in the Table Creator node. You can add more if needed.

Response control

Exercise 02 - Sentiment analysis using few-shot learning

Learning objective: In this exercise, you'll learn how to create a few-shot prompt and how to validate the content of the response.


Workflow description: The review dataset contains 10 reviews for a product on amazon. You will use two additional examples (few-shot) to provide it to the LLM as already classified examples.


You'll find the instructions to the exercises in the yellow annotations.

Help

If you are having troubles creating the prompts with the Expression node, have a look at the Help metanode.

✨ Alternatively, click Ask K-AI in the Expression node and let the copilot build it for you!

Key - Live course only

Ask your instructor how to get a test key for a cloud-based LLM (does not apply to self-paced courses).

Step 4. Authenticate, connect & prompt

  1. Insert the credential keys for your chosen LLM provider in the String Configuration node*

  2. Pass the key as a flow variable to the OpenAI Authenticator node.

  3. Use the OpenAI LLM Connector to connect to the default model.

  4. Send the prompt to the model with the LLM prompter node.


* If you do not have access to a cloud-based LLM and/or want to connect to a local LLM, refer to this guide.

Credentials Configuration
Help - Expression nodes
Few-shot examples
Table Creator
Double-click to configure
Get API Key
Reviews
Excel Reader

Nodes

Extensions

Links