如何在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

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

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