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

题意: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 收到了以下响应:

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

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

Swift 复制代码
 func getActivityAnalysis(){
    
    let url = URL(string: "https://api.openai.com/v1/completions")
    guard let requestUrl = url else { fatalError() }
    
    // Prepare URL Request Object
    var 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 = .prettyPrinted
    let data = try! encoder.encode(requestBody)
    print(String(data: data, encoding: .utf8)!)
    
     
    // Set HTTP Request Body
    request.httpBody = data
    
    print("\(request.httpMethod!) \(request.url!)")
    print(request.allHTTPHeaderFields!)
    print(String(data: request.httpBody ?? Data(), encoding: .utf8)!)
    
    
    
    // Perform HTTP Request
    let task = URLSession.shared.dataTask(with: request) { (data, response, error) in
            
            // Check for Error
            if let error = error {
                print("Error took place \(error)")
                return
            }
     
            // Convert HTTP Response Data to a String
            if 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 请求,它看起来也没有问题:

Swift 复制代码
 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.

相关推荐
东坡肘子3 小时前
肘子的 Swift 周报 #063|异种肾脏移植取得突破
swiftui·swift·apple
AIGC大时代3 小时前
如何使用ChatGPT辅助文献综述,以及如何进行优化?一篇说清楚
人工智能·深度学习·chatgpt·prompt·aigc
hunteritself11 小时前
AI Weekly『12月16-22日』:OpenAI公布o3,谷歌发布首个推理模型,GitHub Copilot免费版上线!
人工智能·gpt·chatgpt·github·openai·copilot
新智元19 小时前
LeCun 八年前神预言,大模型路线再颠覆?OpenAI 宣告:强化学习取得稳定性突破
人工智能·openai
测试者家园20 小时前
ChatGPT生成接口文档的方法与实践
软件测试·chatgpt·测试用例·接口测试·接口文档·ai赋能·用chatgpt做软件测试
威化饼的一隅21 小时前
【多模态】swift-3框架使用
人工智能·深度学习·大模型·swift·多模态
小虚竹1 天前
如何利用ChatGPT生成不同类型的文章大纲
chatgpt
AI小欧同学1 天前
【AIGC-ChatGPT进阶副业提示词】育儿锦囊:化解日常育儿难题的实用指南
chatgpt·aigc
测试者家园1 天前
ChatGPT接口测试用例生成的流程
软件测试·chatgpt·测试用例·接口测试·测试图书·质量效能·用chatgpt做测试
m0_748256562 天前
如何使用Python WebDriver爬取ChatGPT内容(完整教程)
开发语言·python·chatgpt