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!
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:
Let’s dig into a real-world scenario to show this strategy in action!
Here is the Pre Requisite and Setup:
!pip install openai --upgrade
import os
from openai import OpenAI
client = OpenAI()
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.")
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:
Output
For step-by-step Output, you can check them here: GitHub Link
Let’s look at one more example to demonstrate the flexibility of the Chain of Dictionary method.
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.
This code builds a multi-lingual travel assistant that generates and translates travel information, displays it in Markdown, and saves the results as images.
Output
For the final output, check here: GitHub Link
Article | Source |
Implementing the Tree of Thoughts Method in AI | Link |
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 Accuracy | Link |
Check more articles here – Prompt Engineering.
Here’s the principal advantage of the Chain of Dictionaries:
Despite the effectiveness of the Chain of Dictionary approach, there are several potential drawbacks to be aware of:
Even more complex applications are possible with the Chain of Dictionary technique:
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.
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.
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.
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.
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.
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.