#Blank output when calling API

3 messages · Page 1 of 1 (latest)

wheat ridge
#

I am trying to use the API in my swift app however whenever. I make a call i either get a blank output or some "code".
Here is the output I get -> rewrittenText: Data: 239 bytes response: ["created": 1676810031, "object": text_completion, "id": cmpl-6ldB1QF7oTHOrTjiMA7DqHnVKIuEI, "choices": <__NSSingleObjectArrayI 0x6000038191c0>( { "finish_reason" = stop; index = 0; logprobs = "<null>"; text = ""; } ) , "usage": { "prompt_tokens" = 12; "total_tokens" = 12; }, "model": text-davinci-002]

#

Here is my code ` let endpoint = URL(string: "https://api.openai.com/v1/engines/text-davinci-002/completions")!

    let headers = [
        "Content-Type": "application/json",
        "Authorization": "Bearer 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"
    ]

    
    let prompt = "Can you give me some examples of things you can do?"
    let parameters: [String: Any] = [
        "prompt": prompt,
        "max_tokens": 256,//64,
        "n": 1,
        "stop": ["\n"]
    ]

    
    var request = URLRequest(url: endpoint)
    request.httpMethod = "POST"
    request.allHTTPHeaderFields = headers
    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)

    
    let task = URLSession.shared.dataTask(with: request) { data, response, error in
        if let error = error {
            print("Error: \(error.localizedDescription)")
        } else if let data = data {
            if let response = try? JSONSerialization.jsonObject(with: data, options: []) as? [String: Any],
               let choices = response["choices"] as? [[String: Any]],
               let text = choices.first?["text"] as? String {
                let rewrittenText = text.replacingOccurrences(of: prompt, with: "").trimmingCharacters(in: .whitespacesAndNewlines)
                print("rewrittenText: \(rewrittenText)")
                print("Data: \(data)")
                print("response: \(response)")
            } else {
                print("Invalid API response")
            }
        }
    }

    task.resume()`
dense lantern