OpenAI assistant streaming for function calling

153 views Asked by At

Now I am struggling how to implement function calling in streaming assistant run in my NestJS project. As you know, The example code is like this.

https://platform.openai.com/docs/assistants/overview?context=with-streaming

const run = openai.beta.threads.runs.createAndStream(thread.id, {
    assistant_id: assistant.id
  })
    .on('textCreated', (text) => process.stdout.write('\nassistant > '))
    .on('textDelta', (textDelta, snapshot) => process.stdout.write(textDelta.value))
    .on('toolCallCreated', (toolCall) => process.stdout.write(`\nassistant > ${toolCall.type}\n\n`))
    .on('toolCallDelta', (toolCallDelta, snapshot) => {
      if (toolCallDelta.type === 'code_interpreter') {
        if (toolCallDelta.code_interpreter.input) {
          process.stdout.write(toolCallDelta.code_interpreter.input);
        }
        if (toolCallDelta.code_interpreter.outputs) {
          process.stdout.write("\noutput >\n");
          toolCallDelta.code_interpreter.outputs.forEach(output => {
            if (output.type === "logs") {
              process.stdout.write(`\n${output.logs}\n`);
            }
          });
        }
      }
    });

It was able to get streaming working for regular text responses, but now I’m stuck on function calling. I would like to know about event handler of nodejs SDK for OpenAI. Is there anyone to know about that? especially function calling of streaming assistant?

1

There are 1 answers

4
Jeremy On

The function called will just be a name and some arguments as defined in your Assistant. You need to manually handle the function by checking the name and arguments like this:

 .on('toolCallDelta', (toolCallDelta, snapshot) => {
      if (toolCallDelta.type === 'function') {
        if (toolCallDelta.function.name === 'getWeather') {
            // process using your custom code and toolCallDelta.function.arguments, then submit the output back to the run
        }
      }
    });

psuedo code adaption for typescript

import { FunctionToolCallDelta } from "openai/resources/beta/threads/runs/steps.mjs";

 .on('toolCallDelta', (toolCallDelta, snapshot) => {
      if (toolCallDelta.type === 'function') {
        const functionToolCall = toolCall as FunctionToolCallDelta;
        if (functionToolCall.function.name === 'getWeather') {
            // process using your custom code and functionToolCall.function.arguments, then submit the output back to the run
        }
      }
    });