我在这个链接上注册以获取OpenAI API的密钥。
我在R语言中使用了”chatgpt
“包。
library(chatgpt)
并设置环境变量:
Sys.setenv(OPENAI_API_KEY = "sk-YOURAPI")
我使用了以下函数:
chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please.")
有时候输出会缺少一些单词。例如:
*** ChatGPT input:How to make a strawberry pie to donate to my wife? Ingredients, please.[1] ... and your wife is"
在is
之后的文本没有完成。在R
中对此有什么解决方案吗?
在reddit上有一个类似的问题。
编辑
我尝试增加函数的处理时间(这样聊天就会完成所有内容的输入,而不会中途停止)。像这样:
for (i in 35) { print(chatgpt::ask_chatgpt("How to make a strawberry pie to donate to my wife? Ingredients, please.")) Sys.sleep(i)}
编辑者注:2023年3月1日之前,没有正式的ChatGPT API。
回答:
您遇到的是提示工程问题。GPT是一个复杂的自动补全引擎。
如果您没有得到正确的回应,您需要重新设计您的提示。
您始终可以在OpenAI的游乐场中测试您的提示:https://platform.openai.com/playground
我通过使用以下提示成功获得了成分和烹饪步骤的列表:
我怎样才能为我的妻子制作一个草莓派捐赠?请先提供一个带编号的成分列表,然后提供一个带编号的步骤列表。
这是我在游乐场中得到的输出:
Ingredients:1. 2 ½ cups of fresh or frozen strawberries2. 1 9-inch pre-made pie crust3. ¾ cup of granulated sugar4. 2 tablespoons of cornstarch5. ¼ teaspoon of salt6. 1 tablespoon of fresh lemon juiceSteps: 1. Preheat oven to 425 degrees F. 2. Place the pre-made pie crust in a 9-inch pie dish and set aside. 3. In a medium bowl, combine the strawberries, sugar, cornstarch, salt, and lemon juice. Stir until the mixture is combined. 4. Pour the strawberry mixture into the pre-made pie crust. 5. Place the pie dish on a baking sheet and bake for 15 minutes. 6. Reduce the oven temperature to 375 degrees F and bake for an additional 25 minutes. 7. Allow the pie to cool completely before serving.
另外,根据chatgpt R库的Github仓库,它指出“{chatgpt} R包提供了一组功能来协助R语言编码。”
参考:https://github.com/jcrodriguez1989/chatgpt
我建议直接使用OpenAI的API,这样您将对响应有更多的控制。我不是R语言专家,但这是OpenAI游乐场向我展示的做法。
library(httr)response <- GET("https://api.openai.com/v1/completions", query = list( prompt = "How can I make a strawberry pie to donate to my wife? Please provide first a numbered list of ingredients, and secondly a numbered lists of steps.", max_tokens = 200, model = 'text-davinci-003' ), add_headers(Authorization = "bearer YOUR_OPENAI_API_KEY"))content(response)
参考:OpenAI游乐场