gpt4 book ai didi

swift - OpenAI GPT-3 API 错误 : "You must provide a model parameter"

转载 作者:行者123 更新时间:2023-12-02 05:46:37 25 4
gpt4 key购买 nike

我正在尝试通过 SWIFT 向 openAI API 发布问题。如果我通过 Postman 使用相同的有效载荷,它工作正常,但在 Xcode-Condole 中,我从 openAI 得到以下响应:

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

这是我的代码:

 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()

}`

如果我打印 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
}

我试图在我的 Postman 请求中使用相同的负载。它在这里工作得很好。我也尝试使用不同的编码,但它总是抛出相同的错误。

不确定,我做错了什么。也许有人可以提供帮助?

提前谢谢你。

赌注,托比

最佳答案

您的 HTTP 请求揭示了问题。您需要添加 'Content-Type: application/json'

根据 GeeksforGeeks :

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

关于swift - OpenAI GPT-3 API 错误 : "You must provide a model parameter",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75567331/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com