swift let headers HTTPHeaders = Authorization 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 Content-Type applicationjson let parameters Par
The code provided is making a POST request to the OpenAI API to get completions from the Davinci Codex model. However, the error message suggests that an API key is missing.
To resolve this issue, you need to obtain an API key from the OpenAI platform (https://platform.openai.com/account/api-keys) and include it in the "Authorization" header of the request. The updated code should look like this:
let apiKey = "YOUR_API_KEY"
let headers: HTTPHeaders = [
"Authorization": "Bearer " + apiKey,
"Content-Type": "application/json"
]
let parameters: Parameters = [
"prompt": self.requestStr
]
AF.request("https://api.openai.com/v1/engines/davinci-codex/completions", method: .post, parameters: parameters, encoding: JSONEncoding.default, headers: headers).response(completionHandler: { response in
if let data = response.data {
do {
if let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments) as? NSDictionary {
// Handle the JSON response
print(json)
}
} catch {
// Handle the error
}
}
print("Response: \(response)")
})
Make sure to replace "YOUR_API_KEY" with your actual API key
原文地址: https://www.cveoy.top/t/topic/iGx4 著作权归作者所有。请勿转载和采集!