OpenAI icon indicating copy to clipboard operation
OpenAI copied to clipboard

Use macros to automatically serialise Swift functions into OpenAI functions

Open pgorzelany opened this issue 2 years ago • 2 comments

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!

pgorzelany avatar Jun 20 '23 20:06 pgorzelany

@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)\"")

alelordelo avatar Aug 25 '23 10:08 alelordelo

@alelordelo , share your code?

kalafus avatar Feb 08 '24 04:02 kalafus