Azure

Making use of Chain-of-Thought with Azure OpenAI Immediate Engineering in Python

Introduction

Up till now, we have explored Zero-Shot and Few-Shot Prompting. On this article, we’ll delve into Chain-of-Thought Prompting.

Chain-of-thought prompting is like instructing a module to unravel a giant drawback little by little, identical to we resolve issues one step at a time.

This helps the module to assume higher and discover solutions to sophisticated questions by following a logical path.

Benefits of Chain-of-thought prompting

  • It helps the mannequin work higher on duties that want a number of steps or breaking down into smaller components. This makes the mannequin extra correct and dependable.
  • It makes the mannequin simpler to grasp by exhibiting the way it figures out the ultimate reply.
  • It additionally makes the mannequin extra conversational and informative as a result of it provides detailed explanations and causes.

Disadvantages Chain-of-thought prompting

  • It may not be useful for duties that do not want a number of steps or cannot be divided into smaller steps.
  • It depends on the standard of the questions given to the mannequin, which could be tough to create and use for various duties.
  • On the subject of massive issues or plenty of knowledge, it may not work as nicely as a result of it wants extra laptop energy and reminiscence in comparison with easier strategies.

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

Few-shot prompting and chain-of-thought prompting are two totally different methods for utilizing giant language fashions to carry out numerous duties. The principle distinction between them.

Few-shot prompting is when the mannequin is given a couple of examples of inputs and outputs that exhibit the duty or instruction, after which the mannequin tries to generate an output for a brand new enter that follows the identical sample  Ref extra info right here.

Chain-of-thought prompting is when the mannequin is given a posh process or instruction that requires a number of steps of reasoning, and the mannequin tries to generate an output that reveals its thought course of and intermediate steps.

For instance, if the enter is “Resolve this math drawback: 2x + 3 = 11”, the mannequin will attempt to present the way it solves the equation by breaking it down into easier steps.

Implementing Chain-of-thought prompting in Python

Goal: We take a troublesome query and break it into smaller, related questions. Then, we information the module by way of these smaller questions, one after the other. Every step helps the module to grasp extra and get nearer to the ultimate reply.

Instance

I present all the small print in regards to the pizza order, following the steps outlined to interrupt down the consumer’s request and tackle their pizza supply info.

Comply with the steps to reply the query.

Step 1. First, verify if the product is within the product current in step 2 or not.

Step 2. If the product is current in Step 2, then verify whether or not the product is current in Step 3 or not.

All obtainable pizzas

  1. Pizza: Supreme Delight
    Sort: Supreme
    Restaurant: Pizzarella’s
    Dimension: Giant
    Toppings: Pepperoni, sausage, mushrooms, onions, bell peppers
    Description: A flavorful mixture of savory toppings.
    Worth: $15.99
  2. Pizza: Veggie Backyard
    Sort: Vegetarian
    Restaurant: GreenSlice Pizzeria
    Dimension: Medium
    Toppings: Spinach, tomatoes, black olives, feta cheese
    Description: A garden-fresh delight for vegetarians.
    Worth: $12.99
  3. Pizza: Meat Lover’s Feast
    Sort: Meat Lover’s
    Restaurant: Carnivore Crust
    Dimension: Further Giant
    Toppings: Bacon, ham, floor beef, sausage, pepperoni
    Description: A carnivore’s dream with an abundance of meaty goodness.
    Worth: $19.99
  4. Pizza: Hawaiian Luau
    Sort: Hawaiian
    Restaurant: TropiPizza
    Dimension: Small
    Toppings: Ham, pineapple, mozzarella, marinara sauce
    Description: A tropical twist with candy and savory flavors.
    Worth: $10.99
  5. Pizza: Margherita Basic
    Sort: Margherita
    Restaurant: Bella Napoli
    Dimension: Private
    Toppings: Recent basil, tomatoes, mozzarella, olive oil
    Description: A traditional Italian favourite with simplicity and class.
    Worth: $9.99

Step 3. Choose the product from step 2 and discover the no of the merchandise in step 1.

Step 4. Calculate the whole worth of the product in step 3.

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

As soon as the steps have been outlined, I set the consumer message.

Immediate

I want to order a Margherita Pizza Basic Dimension Small and Meat Lover’s Feast Dimension Further Giant.

Anticipated Response

Upon submitting the message to the mannequin, we obtained the next.

Step 1. Confirm if each the Margherita Pizza Basic and Meat Lover’s Feast are in our checklist of pizzas (step 2).

Each pizzas are certainly obtainable.

Step 2. If the product is current in Step 2, verify the product is current in Step 3.

Each pizzas (Margherita Pizza Basic and Meat Lover’s Feast) are current in step 3.

Step 3. Discover the numbers of the pizzas. Margherita Pizza Basic is not any.5 and Meat Lover’s Feast is not any.3

Step 4. Calculate the whole worth for the pizzas. Margherita Pizza Basic is $9.99, and Meat Lover’s Feast is $19.99. Complete is $9.99 + $19.99 = $29.98.

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

{
    "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"
        }
    ],
    "Complete Worth": "$29.98"
}

The consumer immediate is totally different.

If the consumer’s immediate differs from our system message.

user_message = f"""
I want to order a espresso"""

You will get totally different outputs.

I am sorry, however in response to steps 1 and a pair of, the product you're looking for is just not within the obtainable checklist. Please select a product that's current within the checklist.

Full Supply 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"""
Comply with the steps to reply the query:

step 1: First verify if the product is in product current within the step 2 or not.

step 2: If the product is current within the step 2 then verify the product is current within the step 3 or not.
All obtainable pizzas:
1. Pizza: Supreme Delight
   Sort: Supreme
   Restaurant: Pizzarella's
   Dimension: Giant
   Toppings: Pepperoni, sausage, mushrooms, onions, bell peppers
   Description: A flavorful mixture of savory toppings.
   Worth: $15.99

2. Pizza: Veggie Backyard
   Sort: Vegetarian
   Restaurant: GreenSlice Pizzeria
   Dimension: Medium
   Toppings: Spinach, tomatoes, black olives, feta cheese
   Description: A garden-fresh delight for vegetarians.
   Worth: $12.99

3. Pizza: Meat Lover's Feast
   Sort: Meat Lover's
   Restaurant: Carnivore Crust
   Dimension: Further Giant
   Toppings: Bacon, ham, floor beef, sausage, pepperoni
   Description: A carnivore's dream with an abundance of meaty goodness.
   Worth: $19.99

4. Pizza: Hawaiian Luau
   Sort: Hawaiian
   Restaurant: TropiPizza
   Dimension: Small
   Toppings: Ham, pineapple, mozzarella, marinara sauce
   Description: A tropical twist with candy and savory flavors.
   Worth: $10.99

5. Pizza: Margherita Basic
   Sort: Margherita
   Restaurant: Bella Napoli
   Dimension: Private
   Toppings: Recent basil, tomatoes, mozzarella, olive oil
   Description: A traditional Italian favourite with simplicity and class.
   Worth: $9.99

step 3: choose the product from the step 2 and discover the no of the merchandise within the step 1 

step 4: calculate the whole worth of the product within the step 3 

step 5: returns the json string of the product within the step 4 and whole 

"""

user_message = f"""
I want to order a Margherita Pizza Basic Dimension small and Meat Lover's Feast Dimension Further Giant"""

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

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

Within the subsequent article, we are going to discover further immediate engineering methods.

Know extra about our firm at Skrots. Know extra about our providers at Skrots Providers, Additionally checkout all different blogs at Weblog at Skrots

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button