prompt - 如何以编程方式编写 GPT 提示符?

标签 prompt openai-api f-string chatgpt-api

我有一个与此类似的模板提示:

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/

相关文章:

reactjs - TypeError : openai__WEBPACK_IMPORTED_MODULE_1__. OpenAIAPI 不是构造函数

token - OpenAi 是否也在答案中计算 token ?

python - openai.error.APIConnectionError : Error communicating with OpenAI

python - Python 中的自定义字符串插值

python - 如何在fstrings中使用.loc?

python - 从普通字符串构建 f 字符串

postgresql - psql 提示符更改为 "-#"并且该语句未提交

windows - 如何从命令提示符终止进程及其父进程

iOS 始终有一个 'detail' 栏可用

Javascript:不知道为什么这个解决方案有效