View takes 1 positional argument but 2 were given

尝试向openAI发送POST请求,输入为:

{"write hello world"}

但遇到了以下错误:

TypeError: View.__init__() takes 1 positional argument but 2 were given

这是我的视图代码:

def get_help(user_input):    response = openai.Completion.create(        engine="text-davinci-002",        prompt="user_input",        temperature=0.5,        max_tokens=1024,        top_p=1,        frequency_penalty=0,        presence_penalty=0    )    return response["choices"][0]["text"]@api_view(['POST'])class receive_response(View):    def post(self, request):        user_input = request.POST["user_input"]        response = get_help(user_input)        return HttpResponse(response)

这是我的urls.py文件:

urlpatterns = [    path("get", get_help, name="get_help"),    path("post", receive_response, name="post"),]

回答:

你的问题出在这行代码上: class receive_response(View):

(为什么receive_response要继承自View?)

本质上发生的事情是:

  • 接收到POST请求
  • 初始化request_received对象(使用POST的参数,其中必须有两个)
  • 由于它继承自View(并且没有指定__init__(),父类的__init__()会接收相同的输入)
  • 由于View.__init__()接受单个输入值,但它收到了两个,因此你得到了错误:TypeError: View.__init__() takes 1 positional argument but 2 were given

在你的类中添加def __init__(self, v1, v2):声明,并调试以查看v1/v2是什么(并决定将哪个/什么传递给super().__init__()(View的构造函数))

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中创建了一个多类分类项目。该项目可以对…

发表回复

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