jamesrochabrun / SwiftOpenAI

An open-source Swift package for interacting with OpenAI's public API.
MIT License
355 stars 54 forks source link

Fetch functionCallOutput.argument on a message? #59

Closed alelordelo closed 3 months ago

alelordelo commented 4 months ago

Thanks for this amazing library! : )

On Assistant V2 API with streaming support, how can we fetch a specific functionCallOutput.argument on a message?

Example:

         if !provider.functionCallOutput.isEmpty {
               Text("Function Call")
                Text(provider.functionCallOutput.argument) //fetch a specific argument here

                  .font(.title3)
                  .foregroundColor(.pink)
                  .fontDesign(.monospaced)
                  .bold()
               Text(provider.functionCallOutput)
                  .font(.body)
            }
jamesrochabrun commented 3 months ago

If you run the demo you can trigger function call by saying "Create an image of a cow" You will see that the arguments is just a string, a JSON string. So for example for "Create an image of a cow" prompt the arguments will be:

{"count": 1, "prompt": "cow"}

This will be a string so you need to convert the string to a dictionary, and then you will be able to get the specific arguments as you would get values from a dictionary.

NOTE: the arguments are defined in the tool that you send in the request.

alelordelo commented 2 months ago

thanks @jamesrochabrun , got it working now.

The challenge is to parse in realtime the streamed JSON chunks. Did you parsing this to work in realtime? Im woking on it, buts its quite challenging...

this library handles that, but its JS.... https://www.mikeborozdin.com/post/json-streaming-from-openai

jamesrochabrun commented 2 months ago

I see what you are saying, nope I did not found a Swift solution to handle JSON on stream, I may find time to build something but not on my current plans, if you find something let me know!

alelordelo commented 2 months ago

@jamesrochabrun I found this: https://zachwaugh.com/posts/streaming-messages-chatgpt-swift-asyncsequence

Not sure if it would also work for parsing function call arguments, will have a look

alelordelo commented 2 months ago

I also created a stack overflow thread so we can maybe get some help

https://stackoverflow.com/questions/78928283/json-parsing-in-chunks-in-swift#78928283