dylanshine / openai-kit

A community Swift package used to interact with the OpenAI API
https://platform.openai.com/docs/api-reference
MIT License
692 stars 107 forks source link

keyNotFound #22

Closed mhimayath closed 1 year ago

mhimayath commented 1 year ago

After adding the package I copied the below code and added the function call inside a

func data() { Task{ do { let completion = try await openAIClient.completions.create( model: Model.GPT3.textAda001, prompts: ["Write a haiku"]) } catch { print("Error") } } }

After it triggered, Im the getting this error in the console: keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil))

dylanshine commented 1 year ago

@mhimayath This is very odd, I just ran your code in the test suite and it's producing a completion.

dylanshine commented 1 year ago

Do you have the response that cause this error to occur?

roddymunro commented 1 year ago

I'm coming across the same issue. Parsing the response body to APIErrorResponse displays this:

▿ APIErrorResponse
  ▿ error : APIError
    - message : "This is a chat model and not supported in the v1/completions endpoint. Did you mean to use v1/chat/completions?"
    - type : "invalid_request_error"
    ▿ param : Optional<String>
      - some : "model"
    - code : nil
mhimayath commented 1 year ago

@dylanshine Right after adding the package with "Exact Version" -> "1.3.2" I ran this code but still the same issue. I even tried with "Exact Version" -> "1.0.0" but still the same

The complete code i ran:

import UIKit
import OpenAIKit
import AsyncHTTPClient

var apiKey: String = ...

var organization: String = ...

let httpClient = HTTPClient(eventLoopGroupProvider: .createNew)
let configuration = Configuration(apiKey: apiKey, organization: organization)
let openAIClient = OpenAIKit.Client(httpClient: httpClient, configuration: configuration)

class ViewController: UIViewController {

       @IBAction func clickAction(_ sender: UIButton) {
        data()
    }

    func data() {
    Task{
    do {
        let completion = try await openAIClient.completions.create(
            model: Model.GPT3.textAda001,
            prompts: ["Write a haiku"])
        print("Executed!!")
        } catch {
        print("Error")
            }
        }
    }
}

The output is keyNotFound(CodingKeys(stringValue: "id", intValue: nil), Swift.DecodingError.Context(codingPath: [], debugDescription: "No value associated with key CodingKeys(stringValue: \"id\", intValue: nil) (\"id\").", underlyingError: nil))

dylanshine commented 1 year ago

@mhimayath do { return try decoder.decode(T.self, from: byteBuffer) } catch { print(error) throw try decoder.decode(APIErrorResponse.self, from: byteBuffer) } It seems as if this is not properly handling the error case. I will look into this more later.

dylanshine commented 1 year ago

@mhimayath What I'm suspecting is happening is the print line is executing the decoding error you're seeing. If you print out the error itself, it should be correctly formatted as an APIError. I'll remove the print statement to avoid confusion

dylanshine commented 1 year ago

@roddymunro I think what you bring up is a separate issue. Essentially the framework currently allows you to use a variety of Models that conform to ModelID as valid input, but will error out in certain scenarios such as the one you posted above. I future refactor that allows the correct usage of the models will be in order.

dylanshine commented 1 year ago

@roddymunro If you want to interact with the ""/v1/chat/completions" endpoint, use ChatProvider not CompletionProvider as I see you've updated in your fork.

roddymunro commented 1 year ago

@dylanshine Thanks, I see now! It might be worth highlighting that in the readme.