Applying Chain-of-Thought with Azure OpenAI Prompt Engineering in Python

Introduction

Up until now, we've explored Zero-Shot and Few-Shot Prompting. In this article, we'll delve into Chain-of-Thought Prompting.

Chain-of-thought prompting is like teaching a module to solve a big problem bit by bit, just like we solve problems one step at a time.

This helps the module to think better and find answers to complicated questions by following a logical path.

Advantages of Chain-of-thought prompting

  • It helps the model work better on tasks that need multiple steps or breaking down into smaller parts. This makes the model more accurate and reliable.
  • It makes the model easier to understand by showing how it figures out the final answer.
  • It also makes the model more conversational and informative because it gives detailed explanations and reasons.

Disadvantages Chain-of-thought prompting

  • It might not be helpful for tasks that don't need multiple steps or can't be divided into smaller steps.
  • It relies on the quality of the questions given to the model, which can be tricky to create and use for different tasks.
  • When it comes to big problems or lots of data, it might not work as well because it needs more computer power and memory compared to simpler methods.

Difference between  Few-Shot prompting and Chain-of-thought prompting

Few-shot prompting and chain-of-thought prompting are two different techniques for using large language models to perform various tasks. The main difference between them.

Few-shot prompting is when the model is given a few examples of inputs and outputs that demonstrate the task or instruction, and then the model tries to generate an output for a new input that follows the same pattern  Ref more information here.

Chain-of-thought prompting is when the model is given a complex task or instruction that requires multiple steps of reasoning, and the model tries to generate an output that shows its thought process and intermediate steps.

For example, if the input is "Solve this math problem: 2x + 3 = 11", the model will try to show how it solves the equation by breaking it down into simpler steps.

Implementing Chain-of-thought prompting in Python

Objective: We take a tough question and break it into smaller, connected questions. Then, we guide the module through these smaller questions, one by one. Each step helps the module to understand more and get closer to the final answer.

Example

I provide all the details about the pizza order, following the steps outlined to break down the user's request and address their pizza delivery information.

Follow the steps to answer the question.

Step 1. First, check if the product is in the product present in step 2 or not.

Step 2. If the product is present in Step 2, then check whether the product is present in Step 3 or not.

All available pizzas

  1. Pizza: Supreme Delight
    Type: Supreme
    Restaurant: Pizzarella's
    Size: Large
    Toppings: Pepperoni, sausage, mushrooms, onions, bell peppers
    Description: A flavorful combination of savory toppings.
    Price: $15.99
  2. Pizza: Veggie Garden
    Type: Vegetarian
    Restaurant: GreenSlice Pizzeria
    Size: Medium
    Toppings: Spinach, tomatoes, black olives, feta cheese
    Description: A garden-fresh delight for vegetarians.
    Price: $12.99
  3. Pizza: Meat Lover's Feast
    Type: Meat Lover's
    Restaurant: Carnivore Crust
    Size: Extra Large
    Toppings: Bacon, ham, ground beef, sausage, pepperoni
    Description: A carnivore's dream with an abundance of meaty goodness.
    Price: $19.99
  4. Pizza: Hawaiian Luau
    Type: Hawaiian
    Restaurant: TropiPizza
    Size: Small
    Toppings: Ham, pineapple, mozzarella, marinara sauce
    Description: A tropical twist with sweet and savory flavors.
    Price: $10.99
  5. Pizza: Margherita Classic
    Type: Margherita
    Restaurant: Bella Napoli
    Size: Personal
    Toppings: Fresh basil, tomatoes, mozzarella, olive oil
    Description: A classic Italian favorite with simplicity and elegance.
    Price: $9.99

Step 3. Pick the product from step 2 and find the no of the products in step 1.

Step 4. Calculate the total price of the product in step 3.

Step 5. returns the json string of the product in step 4 and the total.

Once the steps have been defined, I set the user message.

Prompt

I would like to order a Margherita Pizza Classic Size Small and Meat Lover's Feast Size Extra Large.

Expected Response

Upon submitting the message to the model, we received the following.

Step 1. Verify if both the Margherita Pizza Classic and Meat Lover's Feast are in our list of pizzas (step 2).

Both pizzas are indeed available.

Step 2. If the product is present in Step 2, check the product is present in Step 3.

Both pizzas (Margherita Pizza Classic and Meat Lover's Feast) are present in step 3.

Step 3. Find the numbers of the pizzas. Margherita Pizza Classic is no.5 and Meat Lover's Feast is no.3

Step 4. Calculate the total price for the pizzas. Margherita Pizza Classic is $9.99, and Meat Lover's Feast is $19.99. Total is $9.99 + $19.99 = $29.98.

Step 5. Return the JSON string of the product in step 4 and total.

{
    "Order": [
        {
            "Pizza": "Margherita Classic",
            "Type": "Margherita",
            "Restaurant": "Bella Napoli",
            "Size": "Personal",
            "Toppings": "Fresh basil, tomatoes, mozzarella, olive oil",
            "Description": "A classic Italian favorite with simplicity and elegance.",
            "Price": "$9.99"
        },
        {
            "Pizza": "Meat Lover's Feast",
            "Type": "Meat Lover's",
            "Restaurant": "Carnivore Crust",
            "Size": "Extra Large",
            "Toppings": "Bacon, ham, ground beef, sausage, pepperoni",
            "Description": "A carnivore's dream with an abundance of meaty goodness.",
            "Price": "$19.99"
        }
    ],
    "Total Price": "$29.98"
}

The user prompt is different.

If the user's prompt differs from our system message.

user_message = f"""\
I would like to order a coffee"""

You can get different outputs.

I'm sorry, but according to steps 1 and 2, the product you are searching for is not in the available list. Please choose a product that is present in the list.

Full Source Code.

import os
import requests
import json
import openai

from dotenv import load_dotenv

# Load the .env file
load_dotenv()

openai.api_key = os.getenv("AZURE_OPEN_KEY")
openai.api_base = os.getenv("AZURE_END_POINT") 
openai.api_type = 'azure'
openai.api_version = '2023-07-01-preview' 

deployment_name=os.getenv("DEPLOYMENT_NAME")

system_role = f"""\
Follow the steps to answer the question:

step 1: First check if the product is in product present in the step 2 or not.

step 2: If the product is present in the step 2 then check the product is present in the step 3 or not.
All available pizzas:
1. Pizza: Supreme Delight
   Type: Supreme
   Restaurant: Pizzarella's
   Size: Large
   Toppings: Pepperoni, sausage, mushrooms, onions, bell peppers
   Description: A flavorful combination of savory toppings.
   Price: $15.99

2. Pizza: Veggie Garden
   Type: Vegetarian
   Restaurant: GreenSlice Pizzeria
   Size: Medium
   Toppings: Spinach, tomatoes, black olives, feta cheese
   Description: A garden-fresh delight for vegetarians.
   Price: $12.99

3. Pizza: Meat Lover's Feast
   Type: Meat Lover's
   Restaurant: Carnivore Crust
   Size: Extra Large
   Toppings: Bacon, ham, ground beef, sausage, pepperoni
   Description: A carnivore's dream with an abundance of meaty goodness.
   Price: $19.99

4. Pizza: Hawaiian Luau
   Type: Hawaiian
   Restaurant: TropiPizza
   Size: Small
   Toppings: Ham, pineapple, mozzarella, marinara sauce
   Description: A tropical twist with sweet and savory flavors.
   Price: $10.99

5. Pizza: Margherita Classic
   Type: Margherita
   Restaurant: Bella Napoli
   Size: Personal
   Toppings: Fresh basil, tomatoes, mozzarella, olive oil
   Description: A classic Italian favorite with simplicity and elegance.
   Price: $9.99

step 3: pick the product from the step 2 and find the no of the products in the step 1 

step 4: calculate the total price of the product in the step 3 

step 5: returns the json string of the product in the step 4 and total 

"""

user_message = f"""\
I would like to order a Margherita Pizza Classic Size small and Meat Lover's Feast Size Extra Large"""

try:
    response = openai.ChatCompletion.create(
        engine=os.getenv("DEPLOYMENT_NAME"),
        messages=[
            {"role": "system", "content": system_role},
            {"role": "user", "content": user_message},        
        ]
    )
except Exception as e:
    print(e.message)

print(response['choices'][0]['message']['content'])

In the next article, we will explore additional prompt engineering techniques.


Similar Articles