-1

I have a template prompt similar to this one:

prompt = f"""
Write a poem about the topic delimited by triple backticks if it starts with a consonant, 
otherwise say 
"foo". 
Topic: ```{topic}```
"""

and a list of topics:

topics = ['cuddly pandas', 'ugly bears', 'sketchy Elons']

I would like to query the OpenAI API with the same base prompt, for each topic in topics. How can I do that? This works, but it seems a bit inelegant to have to redefine the f-string at each iteration of the for loop:

for topic in topics:
    prompt = f"""
    Write a poem about the topic delimited by triple backticks if the first word of the topic  starts with a 
    consonant, 
    otherwise say 
    "foo". 
    Topic: ```{topic}```
     """
    print(prompt)

DeltaIV
  • 4,773
  • 12
  • 39
  • 86

1 Answers1

0

If I understand your problem correctly, you can try something like this:

topic_str = """
Write a poem about the topic delimited by triple backticks if the first word of the topic  starts with a 
consonant, 
otherwise say 
"foo". 
Topic:```"""

for topic in topics:
    prompt = topic_str+f"{topic}"+"```"
    print(prompt)
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45