您的位置:首页 > 汽车 > 新车 > 互联网直播营销大赛主题_广州网站seo_国内seo公司_百度上海推广优化公司

互联网直播营销大赛主题_广州网站seo_国内seo公司_百度上海推广优化公司

2024/9/22 16:35:52 来源:https://blog.csdn.net/suiusoar/article/details/142260462  浏览:    关键词:互联网直播营销大赛主题_广州网站seo_国内seo公司_百度上海推广优化公司
互联网直播营销大赛主题_广州网站seo_国内seo公司_百度上海推广优化公司

题意: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.

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com