Mastering the Chain of Dictionary Technique in Prompt Engineering

Sahitya Arya Last Updated : 20 Jul, 2024
9 min read

Introduction

The ability to be quick has become increasingly important in the rapidly developing fields of artificial intelligence and natural language processing. Experts and amateurs in AI are finding great success with the Chain of Dictionary method, one potent methodology. This article will thoroughly cover this intriguing strategy’s implementation, advantages, and applications. Prepare yourself to discover new avenues for your AI exchanges!

Chain of Dictionaries

Overview

  1. The Chain of Dictionary technique organizes a series of linked dictionaries or JSON objects to guide AI through tasks or conversations.
  2. It offers structured data, contextual clarity, flexibility, and greater control over AI responses.
  3. Using this method, an example demonstrates generating a story in multiple steps, ensuring structured creativity and contextual continuity.
  4. Another example showcases a multilingual travel assistant, translating information into different languages while maintaining cultural nuances.
  5. Key benefits include modularity, clarity, scalability, and adaptability, making it suitable for various applications.
  6. Challenges to consider include token limitations, coherence throughout steps, and effective error handling.

The Chain of Dictionary Technique

A sophisticated smart prompt engineering technique called the Chain of Dictionary methodology entails building a network of linked dictionaries or JSON objects. The AI model is guided through a difficult task or conversation by the particular instructions, context, or data that each dictionary in the chain contains.

Here’s why you should use it:

  1. Structured Data: Structured data is information that can be presented to the AI in an organized and hierarchical manner.
  2. Contextual Clarity: Gives each process step a clear context.
  3. Flexibility: Simple to adjust for various scenarios or AI models.
  4. Greater Control: Offers more exact control over the AI’s reactions.

Let’s dig into a real-world scenario to show this strategy in action!

Example 1: Generating a Story in Multiple Steps

Here is the Pre Requisite and Setup:

Installation of dependencies

!pip install openai --upgrade

Importing libraries and setting up openAI client

import os
from openai import OpenAI
client = OpenAI()

Setting Api key configuration

os.environ["OPENAI_API_KEY"]= “Your open-API-Key”

Consider the scenario where we wish to design an AI-driven story generator that guides us through various phases of tale production. To help the AI with this, we’ll employ the Chain of Dictionary approach.

import openai
from IPython.display import display, Markdown, Image as IPImage
from PIL import Image, ImageDraw, ImageFont
import textwrap
import os

# Set up your OpenAI client (make sure you've set your API key)
client = openai.OpenAI()

# Define the steps for the story creation chain
story_chain = {
    "step1": {
        "instruction": "Generate a basic premise for a science fiction story",
        "context": "Think of a unique concept involving space exploration or advanced technology",
        "output": ""
    },
    "step2": {
        "instruction": "Develop the main character based on the premise",
        "context": "Consider their background, motivations, and challenges",
        "output": ""
    },
    "step3": {
        "instruction": "Create a plot outline",
        "context": "Include a beginning, middle, and end. Introduce conflict and resolution",
        "output": ""
    },
    "step4": {
        "instruction": "Write the opening paragraph",
        "context": "Set the tone and introduce the main elements of the story",
        "output": ""
    }
}

def generate_story_element(prompt):
    """
    Generate a story element based on the given prompt using OpenAI API.

    Args:
        prompt (str): The prompt to generate the story element.

    Returns:
        str: The generated story element in Markdown format.
    """
    response = client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a creative writing assistant. Format your responses in Markdown."},
            {"role": "user", "content": prompt + " Provide your response in Markdown format."}
        ],
        model="gpt-3.5-turbo",
    )
    return response.choices[0].message.content.strip()

def text_to_image(text, filename, title):
    """
    Convert text to an image and save it to a file.

    Args:
        text (str): The text to convert to an image.
        filename (str): The filename to save the image.
        title (str): The title to display on the image.
    """
    # Create a new image with white background
    img = Image.new('RGB', (800, 600), color='white')
    d = ImageDraw.Draw(img)
    
    # Use a default font
    font = ImageFont.load_default()
    title_font = ImageFont.load_default()
    
    # Draw the title
    d.text((10, 10), title, font=title_font, fill=(0, 0, 0))
    
    # Wrap the text
    wrapped_text = textwrap.wrap(text, width=70)
    
    # Draw the text
    y_text = 50
    for line in wrapped_text:
        d.text((10, y_text), line, font=font, fill=(0, 0, 0))
        y_text += 20
    
    # Save the image
    img.save(filename)

# Process each step in the chain
for step, content in story_chain.items():
    prompt = f"{content['instruction']}. {content['context']}"
    if step != "step1":
        prompt += f" Based on the previous: {story_chain[f'step{int(step[-1]) - 1}']['output']}"
    content['output'] = generate_story_element(prompt)
    
    # Display the output
    display(Markdown(f"### {step.upper()}:\n{content['output']}"))
    
    # Create and save an image for this step
    text_to_image(content['output'], f"{step}.png", step.upper())
    
    # Display the saved image
    display(IPImage(filename=f"{step}.png"))

# Final story compilation
final_story = f"""
## Premise:
{story_chain['step1']['output']}

## Main Character:
{story_chain['step2']['output']}

## Plot Outline:
{story_chain['step3']['output']}

## Opening Paragraph:
{story_chain['step4']['output']}
"""

# Display the final story
display(Markdown("# FINAL STORY ELEMENTS:\n" + final_story))

# Create and save an image for the final story
text_to_image(final_story, "final_story.png", "FINAL STORY ELEMENTS")

# Display the final story image
display(IPImage(filename="final_story.png"))

print("Images have been saved as PNG files in the current directory.")

Code Explanation

This code illustrates how we can direct an AI through the story-writing process by using the Chain of Dictionary approach. Let us dissect the current situation:

  1. We build a four-step `story_chain` dictionary with instructions and context for each stage.
  2. To obtain answers, the `generate_story_element` function queries the OpenAI API.
  3. We go through each chain stage iteratively to maintain consistency and improve on earlier outputs.
  4. Lastly, we combine all the pieces to create a seamless narrative framework.

Output

For step-by-step Output, you can check them here: GitHub Link

Benefits of This Strategy

  1. Structured Creativity: We segment the story-writing process into manageable sections to cover all important aspects.
  2. Contextual Continuity: Every action builds on the one before it, ensuring the narrative makes sense from beginning to end.
  3. Flexibility: In order to accommodate more intricate story structures, we may simply add or change steps in the chain.

Let’s look at one more example to demonstrate the flexibility of the Chain of Dictionary method.

Example 2: A Multilingual Tour Guide

Here, we’ll build an AI-powered travel helper that speaks several languages and can offer information.

import openai
from IPython.display import display, Markdown, Image as IPImage
from PIL import Image, ImageDraw, ImageFont
import textwrap
import os

# Set up your OpenAI client (make sure you've set your API key)
client = openai.OpenAI()

# Define the steps for the travel assistant
travel_assistant = {
    "step1": {
        "instruction": "Suggest a popular tourist destination",
        "context": "Consider a mix of culture, history, and natural beauty",
        "output": ""
    },
    "step2": {
        "instruction": "Provide key information about the destination",
        "context": "Include must-see attractions, best time to visit, and local cuisine",
        "output": ""
    },
    "step3": {
        "instruction": "Translate the information to French",
        "context": "Maintain the meaning and tone of the original text",
        "output": ""
    },
    "step4": {
        "instruction": "Translate the information to Spanish",
        "context": "Ensure cultural nuances are appropriately conveyed",
        "output": ""
    }
}

def generate_travel_info(prompt):
    """
    Generate travel information based on the given prompt using OpenAI API.

    Args:
        prompt (str): The prompt to generate travel information.

    Returns:
        str: The generated travel information in Markdown format.
    """
    response = client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a knowledgeable travel assistant. Format your responses in Markdown."},
            {"role": "user", "content": prompt + " Provide your response in Markdown format."}
        ],
        model="gpt-3.5-turbo",
    )
    return response.choices[0].message.content.strip()

def text_to_image(text, filename, title):
    """
    Convert text to an image and save it to a file.

    Args:
        text (str): The text to convert to an image.
        filename (str): The filename to save the image.
        title (str): The title to display on the image.
    """
    # Create a new image with white background
    img = Image.new('RGB', (800, 600), color='white')
    d = ImageDraw.Draw(img)
    
    # Use a default font
    font = ImageFont.load_default()
    title_font = ImageFont.load_default()
    
    # Draw the title
    d.text((10, 10), title, font=title_font, fill=(0, 0, 0))
    
    # Wrap the text
    wrapped_text = textwrap.wrap(text, width=70)
    
    # Draw the text
    y_text = 50
    for line in wrapped_text:
        d.text((10, y_text), line, font=font, fill=(0, 0, 0))
        y_text += 20
    
    # Save the image
    img.save(filename)

# Process each step in the chain
for step, content in travel_assistant.items():
    prompt = f"{content['instruction']}. {content['context']}"
    if step in ["step3", "step4"]:
        prompt += f" Based on the previous: {travel_assistant['step2']['output']}"
    content['output'] = generate_travel_info(prompt)
    
    # Display the output
    display(Markdown(f"### {step.upper()}:\n{content['output']}"))
    
    # Create and save an image for this step
    text_to_image(content['output'], f"{step}.png", step.upper())
    
    # Display the saved image
    display(IPImage(filename=f"{step}.png"))

# Final multi-lingual travel guide
travel_guide = f"""
## Destination:
{travel_assistant['step1']['output']}

## Information (English):
{travel_assistant['step2']['output']}

## Information (French):
{travel_assistant['step3']['output']}

## Information (Spanish):
{travel_assistant['step4']['output']}
"""

# Display the final travel guide
display(Markdown("# MULTI-LINGUAL TRAVEL GUIDE:\n" + travel_guide))

# Create and save an image for the final travel guide
text_to_image(travel_guide, "final_travel_guide.png", "MULTI-LINGUAL TRAVEL GUIDE")

# Display the final travel guide image
display(IPImage(filename="final_travel_guide.png"))

print("Images have been saved as PNG files in the current directory.")

Here is an example of a travel assistant we developed that can translate material into several languages and offer suggestions and information about possible destinations. This demonstrates the use of the Chain of Dictionary approach to develop more intricate, multifaceted artificial intelligence systems.

Code Explanation

This code builds a multi-lingual travel assistant that generates and translates travel information, displays it in Markdown, and saves the results as images.

  • The OpenAI client is initialized with client = openai.OpenAI().
  • The travel_assistant dictionary defines four steps with instructions, context, and output fields.
  • The generate_travel_info function calls the OpenAI API to generate text based on a prompt.
  • The text_to_image function converts text to an image using PIL and saves it.
  • The for loop iterates over each step in travel_assistant, generating and displaying text and images.
  • A final multi-lingual travel guide is created, displayed, and saved as an image.

Output

For the final output, check here: GitHub Link

Here are the Similar Reads

ArticleSource
Implementing the Tree of Thoughts Method in AILink
What are Delimiters in Prompt Engineering?Link
What is Self-Consistency in Prompt Engineering?Link
What is Temperature in Prompt Engineering?Link
What is Skeleton of Thoughts and its Python Implementation?Link
Chain of Verification: Prompt Engineering for Unparalleled AccuracyLink

Check more articles here – Prompt Engineering.

Principal Advantages of the Chain of Dictionaries Method

Here’s the principal advantage of the Chain of Dictionaries:

  1. Modularity: Every link in the chain is easily interchangeable, extendable, or modified without affecting the others.
  2. Clarity: The methodical approach facilitates comprehension and troubleshooting of the AI’s thinking process.
  3. Scalability: You can add as many phases as required to manage complicated jobs or dialogues.
  4. Adaptability: The method can be used in various contexts and use cases, from creative writing to language translation and beyond.

Difficulties and Things to Think About

Despite the effectiveness of the Chain of Dictionary approach, there are several potential drawbacks to be aware of:

  1. Token Limitations: You can run into token constraints that limit the duration of your prompts and responses, depending on the AI model you’re employing.
  2. Coherence Throughout Steps: Ascertain that each step’s output keeps in line with the task’s broader context.
  3. Error Handling: Use effective error handling to handle erroneous AI replies or problems with APIs.

Complex Applications With Chain of Dictionary

Even more complex applications are possible with the Chain of Dictionary technique:

  1. Interactive Storytelling: Write narratives with branching branches in which the user’s choices dictate the course of events.
  2. Multi-Model Interaction: To produce illustrated tales or trip guides, combine text-based artificial intelligence with models for creating images.
  3. Automated Research: Create a thorough report by synthesizing data from several sources and organizing it into a chain.

Conclusion

The Chain of Dictionary approach in rapid engineering opens up many opportunities for developing complex, context-aware AI systems. By decomposing complicated tasks into manageable parts and offering precise instructions and context at each turn, we can direct AI models to provide more accurate, pertinent, and inventive outputs.

As you practice using this method, remember that creating clear, simple instructions and ensuring a logical flow between each link in the chain is essential for success. You’ll be able to create AI interactions that are more perceptive, engaging, and effective with experience and imagination.

Frequently Asked Questions

Q1. What is the Chain of Dictionary technique in prompt engineering?

Ans. The Chain of Dictionary technique involves creating a sequence of linked dictionaries or JSON objects, each containing specific instructions, context, or data to guide an AI model through a complex task or conversation.

Q2. Why should I use the Chain of Dictionary technique?

Ans. This technique helps organize data in a structured and hierarchical manner, provides clear context for each process step, offers flexibility for various scenarios or AI models, and gives precise control over the AI’s responses.

Q3. How does the Chain of Dictionary technique improve AI-generated stories?

Ans. Breaking down the story-writing process into manageable steps ensures all key aspects are covered, maintains contextual continuity, and allows for flexibility in adding or modifying steps, leading to a coherent and engaging narrative.

Q4. What are some advanced applications of the Chain of Dictionary technique?

Ans. The technique can be used for interactive storytelling with branching paths, multi-model interactions combining text and image generation, and automated research by synthesizing information from multiple sources into a cohesive report.

Q5. What challenges might I face when using the Chain of Dictionary technique?

Ans. Potential challenges include token limitations that restrict the length of prompts and responses, ensuring coherence across steps, and handling errors or inconsistencies in AI responses effectively.

I'm Sahitya Arya, a seasoned Deep Learning Engineer with one year of hands-on experience in both Deep Learning and Machine Learning. Throughout my career, I've authored more than three research papers and have gained a profound understanding of Deep Learning techniques. Additionally, I possess expertise in Large Language Models (LLMs), contributing to my comprehensive skill set in cutting-edge technologies for artificial intelligence.

Responses From Readers

Clear

Congratulations, You Did It!
Well Done on Completing Your Learning Journey. Stay curious and keep exploring!

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details