我有一个与此类似的模板提示:
prompt = f"""
Write a poem about the topic delimited by triple backticks if it starts with a consonant,
otherwise say
"foo".
Topic: ```{topic}```
"""
和主题列表:
topics = ['cuddly pandas', 'ugly bears', 'sketchy Elons']
我想针对 topics
中的每个主题使用相同的基本提示查询 OpenAI API。我怎样才能做到这一点?这可行,但在 for 循环的每次迭代中都必须重新定义 f 字符串似乎有点不雅:
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)
最佳答案
如果我正确理解您的问题,您可以尝试如下操作:
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)
关于prompt - 如何以编程方式编写 GPT 提示符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76348006/