I currently have made a REST API using Alamofire in swiftUI. The request is a post method to this URL: https://api.openai.com/v1/engines/text-davinci-002/completions, which is basically sending a question to the OpenAI API and supposed to get the answer in JSON format. I have made an API key in OpenAI and have included it in my code.
The problem I am facing is that when getting the input, I recieve the following error:
Error: Response status code was unacceptable: 400.
I first tried to make sure if my API key was correct and it was. I also tried to see if a cURL statement would work to check if it was an error with the URL.
I used this cURL statement:
curl -X POST \
https://api.openai.com/v1/engines/text-davinci-002/completions \
-H 'Authorization: Bearer my-api-key' \
-H 'Content-Type: application/json' \
-d '{
"prompt": "What is quantum mechanics?",
"temperature": 0.7,
"max_tokens": 20,
"top_p": 1,
"frequency_penalty": 0,
"presence_penalty": 0
}'
Which worked fine and gave me a correct output:
{"id":"cmpl-6hhWBlzZHrLtV4RKCtjL63CLoTvPi","object":"text_completion","created":1675873407,"model":"text-davinci-002","choices":[{"text":"\n\nQuantum mechanics is a branch of physics that studies the behavior of matter and energy in the","index":0,"logprobs":null,"finish_reason":"length"}],"usage":{"prompt_tokens":5,"completion_tokens":20,"total_tokens":25}}
If it helps, here's my code:
//
// chat.swift
// titan
//
// Created by Refluent on 08/02/2023.
//
import SwiftUI
import Alamofire
struct Message: Hashable {
let sender: String
let content: String
}
struct OpenAIResponse: Decodable {
let completions: [Completion]
struct Completion: Decodable {
let text: String
}
}
struct chatView: View {
@State private var response: String = ""
@State private var messages: [Message] = []
@State private var userInput: String = ""
let apiKey = "my-api-key"
let model = "text-davinci-002"
var body: some View {
VStack {
Text("Response from OpenAI API:")
List(messages, id: \.self) { message in
Text("\(message.sender): \(message.content)")
}
TextField("Enter your question", text: $userInput)
.padding()
Button(action: {
self.sendRequest()
}) {
Text("Send")
}
}
}
func sendRequest() {
let headers: HTTPHeaders = ["Authorization": "Bearer \(apiKey)", "Content-Type": "application/json"]
let parameters: Parameters = ["prompt": userInput, "model": model]
messages.append(Message(sender: "Me", content: userInput))
userInput = ""
AF.request("https://api.openai.com/v1/engines/text-davinci-002/completions", method: .post, parameters: parameters, headers: headers)
.validate()
.responseDecodable(of: OpenAIResponse.self) { response in
switch response.result {
case .success(let value):
let text = value.completions[0].text
self.messages.append(Message(sender: "ChatGPT", content: text))
case .failure(let error):
self.messages.append(Message(sender: "ChatGPT", content: "Error: \(error.localizedDescription)"))
}
}
}
}
For more context about my code, here it is:
I'm sending a post request to this URL, then getting the output and saving it in an array alongside the original input. The input and the output is then displayed as a chat.
Does anyone know why I am recieving this error? Did I miss a crucial part in my code?
Thanks in advance btw.
After reading through the comments, I realised that this isn't exactly something to do in Swift
, so I will be implementing this in the server side. I apoligize for wasting your time.