MacPaw / OpenAI

Swift community driven package for OpenAI public API
MIT License
1.99k stars 324 forks source link

Use macros to automatically serialise Swift functions into OpenAI functions #79

Open pgorzelany opened 1 year ago

pgorzelany commented 1 year ago

A very recent feature added to the library are functions which are passed to the chat request as an array of ChatFunctionDeclaration. The problem is that it's very tedious to write those and then convert them back into real function calls. Maybe it would be possible to use macros to automatically serialise a Swift function into ChatFunctionDeclaration and then call a function automatically upon receiving a ChatFunctionCall.

Not sure if its possible and how would that work since I didn't use macros yet, but would be cool!

alelordelo commented 1 year ago

@pgorzelany , did you get this to work?

@Krivoblotsky , I am trying to do something like this, but the chat demo never returns the function, and then I found that this is because the choice.delta.role is wrong, and prints "assistant" when its actually a "function"

           let message = Message(
                        id: partialChatResult.id,
                        role: choice.delta.role ?? .assistant,
                        content: messageText,
                        createdAt: Date(timeIntervalSince1970: TimeInterval(partialChatResult.created))
                    )
                    print("message role is \"\(message.role)\"")
kalafus commented 7 months ago

@alelordelo , share your code?