Repeating Tasks with for Loops

Hi Everyone,
I did not quite understand the use of Flavor:{flavor} in the code below.
ice_cream_flavors = [
“Vanilla”,
“Chocolate”,
“Strawberry”,
“Mint Chocolate Chip”
]
for flavor in ice_cream_flavors:
prompt = f"““For the ice cream flavor listed below,
provide a captivating description that could be used for promotional purposes.
Flavor:{flavor}””"
print_llm_response(prompt)

It’was different when we were working on lists without using for loop. For example we only used the name of the list: {friends_list}

friends_list = [“Tommy”, “Isabel”, “Daniel”]
prompt = f"“”
Write a set of four line birthday poems for my friends {friends_list}.
The poems should be insipred by the first letter of each friend’s name.
“”"
print(prompt)
print_llm_response(prompt)

Can anyone tell me the difference?

Hi brukII,

good question. In a way you can do whatever you prefer to do.

You could also write

prompt = f"““For the ice cream flavors listed below,
provide a captivating description for each of them that could be used for promotional purposes.
Flavors: {ice_cream_flavors} ””"
print_llm_response(prompt)

or

for friend in friends_list:
prompt = f"“”
Write a four line birthday poem for my friend {friend}.
The poem should be inspired by the first letter of his/her name.
“”"
print_llm_response(prompt)

The llm understands that in the first case it gets a list and in the second case one item only.
Basically the result will be the same.

But it could make a difference when you might have another task for the llm that would involve asking the llm to take all the items into consideration. For example you might want the llm to also sort the poems or the descriptions of the ice cream flavors.

So it depends what you want the llm to do. In the latter case it needs the whole list whereas in the other case it only needs one item a time.

I hope that helps

Jochen

1 Like

Thank you so much Jochen!
Actually, what I still don’t understand is: when we write Flavor: {flavor}, what does Flavor represent? I know that the part inside the curly braces {flavor} corresponds to the flavorFrom the for"" loop for flavor in ice_cream_flavors: “”. But what exactly is the purpose of Flavor here?

For example, in this sentence:
“Write a set of four-line birthday poems for my friends {friends_list}.”
I understand the purpose of {friends_list} and what it represents here. However, in the previous example with Flavor: {flavor}, I still don’t understand what Flavor stands for or why it is being used.

Hi brukII,

in my understanding it helps the llm to understand that {flavor} is what is meant by the word flavor in the given task. For example you could write:

prompt= f""“please analyse the following text and
… more instructions …
Text: {text}
“””

or you could write

prompt= f""“please analyse the text {text} and
… more instructions …
“””

But in my opinion the first prompt is better because first you give the instructions in one block and the the input text (imagine you have a large text)

I think it helps to structure the prompt. It also could be that you also would like to give instructions about the output format or give examples.
Then you might have a structure like

prompt = f"“”
Task: … here you mention the word Text (or whatever that has to be processed) …
Format:
Examples:
Text: {text}
“”"

In a way it is a kind of best practice.
This is what we learn in prompt engineering how to design a prompt in order to give clear instructions.

I hope I could help

Jochen

1 Like

Hi brukII,

you can also think of it like that: The llm gets your request after the replacement:

prompt = f"““For the ice cream flavor listed below,
provide a captivating description that could be used for promotional purposes.
Flavor: Chocolade ””"

in contrast to

prompt = f"““For the ice cream flavor listed below,
provide a captivating description that could be used for promotional purposes.
Chocolade ””"

Flavor: refers to the word flavor in your task. That way the llm has better chances to understand what you want.

Jochen

1 Like

In this:
f"Flavor:{flavor}"

  • The Flavor: portion is just text that is included in the formatted text.
  • The curly braces enclose the name of a variable. The value of the variable will be inserted in the text. In this case its the flavor variable.

It’s just a bit confusing to read.

1 Like

Now I understand it better, thanks!

Thank you. I appreciate your time and effort.