mdwiltfong / doc-inspector

1 stars 0 forks source link

Add function to Assistant to send generated document based on template during run #14

Open Jamesllllllllll opened 9 months ago

Jamesllllllllll commented 9 months ago

I started working on the Assistant constructor. I added a second type object in the tools property array:

The changes are not live.

static async createAssistant(
    assistantName = 'DocInspector',
    instructions = '',
    model = 'gpt-3.5-turbo-1106'
  ) {
    try {
      const newAssistant = await openai.beta.assistants.create({
        name: assistantName,
        instructions: instructions,
        tools: [
          {
            type: 'retrieval',
          },
          {
            type: 'function',
            function: {
              name: 'sendGeneratedDocument',
              description:
                'Sends the generated document to be added to state for saving as a PDF',
              parameters: {
                type: 'object',
                properties: {
                  document: {
                    type: 'string',
                    description:
                      'A single string of markdown that is the updated version of the uploaded template with information from the resource added to it',
                  },
                },
                required: ['document'],
              },
            },
          },
        ],
        model: model,
      });
      return new OpenAIAssistant(
        model,
        instructions,
        newAssistant.id,
        newAssistant.functions
      );
    } catch (error) {
      console.log(error);
    }
  }
Jamesllllllllll commented 9 months ago

In OpenAI's example, all the keys in the tools object have quotations. Is this required in our back-end?

I generated the steps based on OpenAI's documentation:

1. Define Your Functions When Creating an Assistant

const assistant = await openai.beta.assistants.create({
  // ... other assistant configurations ...
  tools: [{
    "type": "function",
    "function": {
      "name": "sendDocumentString",
      // ... other function parameters and descriptions ...
    }
  }]
  // ... possibly more functions ...
});

2. Initiate a Run That Triggers the Function

When a user sends a message that triggers the sendDocumentString function, the run will enter a requires_action state. You can check this by retrieving the run.

3. Retrieve the Run and Read the Required Actions

Fetch the run details to see which functions are required. The run's details will include the required_action object, which specifies the function calls needed.

4. Execute the Required Function

Based on the function name and arguments provided in the required_action, execute the corresponding function in your backend. For example, if sendDocumentString is called, run that function with the provided arguments.

5. Submit Function Outputs

After executing the function, submit the outputs back to the Assistant's API. Use the tool_call_id to match the output with each function call. Here's how you might do it for sendDocumentString:

const run = await openai.beta.threads.runs.submitToolOutputs(
  thread.id,
  run.id,
  {
    tool_outputs: [
      {
        tool_call_id: callIdForSendDocumentString,
        output: "Success message or relevant output from sendDocumentString",
      },
      // ... outputs for other tool calls, if any ...
    ],
  }
);

6. The Run Continues

After submitting the outputs, the run will enter a queued state and then continue its execution, processing the output you provided.

Jamesllllllllll commented 9 months ago

I updated retrieveRun to the following

 static async retrieveRun(threadId, runId) {
    const retrievedThread = await openai.beta.threads.runs.retrieve(
      threadId,
      runId
    );
    return new Run(
      retrievedThread.id,
      retrievedThread.thread_id,
      retrievedThread.assistant_id,
      retrievedThread.status,
      retrievedThread.required_action // This is the only new line
    );
  }