I am using using OpenAISwift to work with chatGTP API, I am having an issue with the API that suddenly stopped working. Despite having a valid token and enough credit for API usage, I am not receiving any response from the API. I was previously using the default repo model, but it seems that many models have been shut down by OpenAI. I changed it to .chat (.chatgpt)
to use GPT-3 and also tested with the GPT-3 turbo model, but I still am not getting any data. This is a crucial issue for my AI-based app, and I would be grateful for any workaround. If it helps, here is my code.
@MainActor
class AIViewModel: ObservableObject {
private var client: OpenAISwift?
@Published var respnse = ""
@Published var isLoading = false
@Published var recievedError = false
init() { client = OpenAISwift(config: .makeDefaultOpenAI(apiKey: "sk-*******")) }
func ask(text: String) async {
isLoading = true
do {
let result = try await client?.sendCompletion(
with: text,
model: .chat(.chatgpt),
maxTokens: 200
)
let output = result?.choices?.first?.text.trimmingCharacters(in: .newlines) ?? "no answer"
//print(output)
self.respnse = output
} catch {
print(error.localizedDescription)
recievedError = true
}
isLoading = false
}
}
2
Answers
I checked the GitHub repo, and the problem is not caused by the model.
As you can see below,
chatgpt
uses thegpt-3.5-turbo
model (source), which has not been deprecated:The problem is probably caused by switching from the Completions API to the Chat Completions API. Message retrieval is different:
.text
.message.content
I don’t know Swift, but pay attention to this line:
Your problem is probably due to using the
OpenAI
legacy endpoint with completion (as mentioned already by Rok Benko)Try this approach using the more up-to-date
OpenAISwift
code fromSwift-Almanac OpenAISwift and with
sendChat(...)
instead.Working example code: