OpenAI GPT-3 API error: “You must provide a model parameter“

devtools/2024/10/22 9:31:15/

题意:OpenAI GPT-3 API 错误:“你必须提供一个模型参数”

问题背景:

I am trying to POST a question to openAI API via SWIFT. It works fine, if I use the same payload via Postman, but in the Xcode-Condole I got the following response from openAI:

我正在尝试通过 Swift 向 OpenAI API 发送一个 POST 请求。如果我在 Postman 中使用相同的负载,它工作正常,但在 Xcode 控制台中,我从 OpenAI 收到了以下响应:

Response data string:
{"error": {"message": "you must provide a model parameter","type": "invalid_request_error","param": null,"code": null}}

This is my code:        以下是我的代码        

 func getActivityAnalysis(){let url = URL(string: "https://api.openai.com/v1/completions")guard let requestUrl = url else { fatalError() }// Prepare URL Request Objectvar request = URLRequest(url: requestUrl)request.setValue("Bearer blaaaablaa", forHTTPHeaderField: "Authorization")request.httpMethod = "POST"let prompt = "just a test"let requestBody = OpenAIRequest(model: "text-davinci-003", prompt: prompt, max_tokens: 300, temperature: 0.5)let encoder = JSONEncoder()encoder.outputFormatting = .prettyPrintedlet data = try! encoder.encode(requestBody)print(String(data: data, encoding: .utf8)!)// Set HTTP Request Bodyrequest.httpBody = dataprint("\(request.httpMethod!) \(request.url!)")print(request.allHTTPHeaderFields!)print(String(data: request.httpBody ?? Data(), encoding: .utf8)!)// Perform HTTP Requestlet task = URLSession.shared.dataTask(with: request) { (data, response, error) in// Check for Errorif let error = error {print("Error took place \(error)")return}// Convert HTTP Response Data to a Stringif let data = data, let dataString = String(data: data, encoding: .utf8) {print("Response data string:\n \(dataString)")self.openAIResponse = dataString}}task.resume()}`

If I print the http request, it seems fine for me as well:

如果我打印出 HTTP 请求,它看起来也没有问题:

 POST https://api.openai.com/v1/completions["Authorization": "Bearer blaaaaa"]{"temperature" : 0.5,"model" : "text-davinci-003","prompt" : "just a test","max_tokens" : 300}

I tried to use the same payload in my Postman request. It worked fine here. I also tried to use different encodings, but it always throws the same error.

我尝试在 Postman 请求中使用相同的负载,它在这里工作正常。我还尝试了不同的编码方式,但总是抛出相同的错误。

Not sure, what I am doing wrong. Maybe someone can help?

不确定我哪里做错了。也许有人能帮忙?

Thank you in advance.        提前感谢你。

问题解决:

Your HTTP request reveals the problem. You need to add 'Content-Type: application/json'.

According to GeeksforGeeks:

你的 HTTP 请求暴露了问题。你需要添加 `'Content-Type: application/json'`。

根据 GeeksforGeeks 的说法:

Content-Type is an HTTP header that is used to indicate the media type of the resource and in the case of responses, it tells the browser about what actually content type of the returned content is.


http://www.ppmy.cn/devtools/115073.html

相关文章

了解 React 应用程序中的渲染和重新渲染:它们如何工作以及如何优化它们

当我们在 react 中创建应用程序时,我们经常会遇到术语“渲染”和“重新渲染组件”。虽然乍一看这似乎很简单,但当涉及不同的状态管理系统(如 usestate、redux)或当我们插入生命周期钩子(如 useeffect)时&am…

js 深入理解类-class

目录 概述1. 类的定义2. 类构造函数2.1. 实例化2.1.1 实例化流程2.1.2 带参实例化2.1.3 执行构造函数返回的两种对象2.1.4 类构造函数和普通构造函数的区别 2.2 把类当成特殊函数2.2.1 辨别是不是函数,使用 typeof2.2.2 辨别是不是函数,是否有prototype2…

OpenCV_图像旋转超详细讲解

图像转置 transpose(src, dst); transpose()可以实现像素下标的x和y轴坐标进行对调:dst(i,j)src(j,i),接口形式 transpose(InputArray src, // 输入图像OutputArray dst, // 输出 ) 图像翻转 flip(src, dst, 1); flip()函数可以实现对图像的水平翻转…

MySQL数据库备份与恢复

前言 数据库备份与恢复是数据库管理中的关键操作,它们确保了数据的安全性和在发生故障时的快速恢复。MySQL,作为广泛使用的开源关系型数据库管理系统,提供了多种备份和恢复策略。本文将详细介绍MySQL数据库的备份与恢复方法,包括…

记录Zabbix监控飞塔防火墙HA状态异常

1、Zabbix监控飞塔HA状态值0为正常,1为异常。 2、监控到HA不同步是由于防火墙策略修改导致,具体说明如下: 设备HA部署,当修改防火墙策略时,仅在一台设备上添加、删除或修改,该操作会导致防火墙…

软件测试工程师面试整理-编程与自动化

在软件测试领域,编程与自动化是提升测试效率、覆盖率和可靠性的关键因素。掌握编程技术和自动化测试框架,能够帮助测试人员有效地执行大量重复性测试任务,并迅速反馈软件的质量状况。以下是编程与自动化在测试中的主要应用及相关技术介绍: 1. 编程语言与自动化 ● 常用编程…

力扣300-最长递增子序列(Java详细题解)

题目链接:300. 最长递增子序列 - 力扣(LeetCode) 前情提要: 本题是子序列问题的开始篇,接下来我将更新子序列篇章的dp问题。 因为本人最近都来刷dp类的题目所以该题就默认用dp方法来做。 dp五部曲。 1.确定dp数组…

fastadmin 部署后前台会员中心出现404错误

访问前台会员中心出现404错误。 解决:nginx访问站点增加伪静态 location / {if (!-e $request_filename){rewrite ^(.*)$ /index.php?s$1 last; break;} }在phpstydy中增加伪静态,如图: