如何在R语言中避免OpenAI API的字数限制?

我在这个链接上注册以获取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游乐场

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注