OpenAI: Run Assistant Function action

The OpenAI: Run Assistant function action unlocks a new level of capability for your OpenAI Assistant. It utilizes the OpenAI Assistants API, actively enabling assistants to call upon external functionalities during conversations. This feature acts as a bridge, actively connecting your assistant to a vast toolbox of additional powers.

Prerequisites

  • You must have the Bot creator role to use the OpenAI Run Assistant function action in an automation.
  • Ensure that you have the necessary credentials to send a request and have included Authenticate action before calling any OpenAI actions.
  • You must have a OpenAI: Run Assistant action prior to calling OpenAI Run Assistant function.

This example delves into using OpenAI Run assistant functions that extends your assistant's capabilities. You will explore how to call an existing assistant within the Run environment, extract crucial information (Thread ID, Call ID, and Run ID), and leverage these IDs to define a tailored function response. For instance, here a Stock market assistant is loaded and the function results are defined using the tools output list (Function response).

Procedure

  1. In the Automation Anywhere Control Room, navigate to the Actions pane, select Generative AI > OpenAI, drag Authenticate action, and place it in the canvas. To authenticate you need an API Key. For more information on generating an API key and authentication, see Authenticate action.OpenAI: Run Assistant Function
  2. Place the Open AI: Run Assistant action below the Authenticate action and use Load assistants, to load an assistant (Stock market assistant). Enter a Message (Get Stock price for AAINC). The response is saved to a string variable str_Response. For more information on using Open AI: Run Assistant action, see OpenAI: Run Assistant action.OpenAI: Run Assistant - Action
    Note: For illustrative purposes only, the fictional stock code AAINC is used in this example. The price of $3200 assigned to AAINC is a sample value and does not represent the price of any real stock.

    The Stock market assistant loaded from OpenAI contains the following instruction and function:

    You are a stock market assistant. When asked a question related to stocks use the function and get the stock price.
    {
       "name":"get_stock_price",
       "description":"Get the current stock price",
       "parameters":{
          "type":"object",
          "properties":{
             "symbol":{
                "type":"string",
                "description":"The stock symbol"
             }
          },
          "required":[
             "symbol"
          ]
       }
    }
  3. From the Actions pane, drag the JSON > Start session action and drop it into the canvas below the Open AI: Run Assistant action.
    1. In the Data Source field, select Text and enter$str_Response$.
    2. In the JSON Object session field, select Local session and enter Default.
      JSON Decode
      You now have the entire JSON. The next task is to extract the required fields from the body. The following snippet shows a sample JSON:
      {
         "data":{
            "thread_id":"thread_hiCr3najXMSxuuiIntJDXDXd",
            "tool_calls":[
               {
                  "id":"call_Beu0XFPGR91RZGoXBA8ODSke",
                  "type":"function",
                  "function":{
                     "name":"get_stock_price",
                     "arguments":"{\n\"symbol\": \"AAINC\"\n}"
                  }
               }
            ],
            "id":"run_LnR669bHLcMAaIGOcvIE0nVa"
         },
         "status":"requires_action"
      }
    Note: The tool_calls and tool_outputs within an OpenAI Assistant message can handle scenarios where the user makes multiple requests in a single message. For instance, Get the stock price of AAINC and BBINC. This single message actually contains two distinct requests: finding the stock price for AAINC and finding the stock price for BBINC.
  4. Extract the required fields from the JSON to execute the OpenAI: Run Assistant Function.
    1. From the Actions pane, drag the Json > Get node value action and drop it into the canvas below the Json > Get node value action. For more information on JSON: Get node value action, see Get node value action in JSON.
    2. Specify the nodes to be extracted. For example, to extract the thread_id, you can use data.thread_id
    3. Create a variable thread_id and assign the output to this variable.
    4. Similarly extract run_id and call_id(data.id and data.tool_calls[0].id) and save it in a variable run_id and call_id.
  5. Navigate to the Actions pane, select Generative AI > OpenAI, drag OpenAI: Run Assistant Function action, and place it in under the last Get node value action.
    1. Use the extracted $thread_id$ in the Thread ID field.
    2. Use the extracted $run_id$ in the Run ID field.
    3. Define a Function response using the standard schema below. The schema is a JSON object containing information about the output of a tool (function) used by the OpenAI Assistant.
      {
         "tool_outputs":[
            {
               "tool_call_id":"Enter the call ID",
               "output":"{Enter your output}"
            }
         ]
      }

      After processing your request and running the appropriate function, you need to communicate the results back to the OpenAI Assistant API. To do this, you need to create a special list as shown above called the tools output list. This list contains the following properties:

      • tool_call_id (string): This property holds a unique identifier for the specific call of the tool (function) that generated the output. This ID helps track and reference the specific execution instance of the tool.
      • output (any): This property holds the actual output data generated by the tool. The data type of output can vary depending on the specific tool and its purpose. It could be:
        • A string (such as a text generated by a creative writing function)
        • A number (such as a calculation result)
        • A list (such as a collection of retrieved data points)
        • A dictionary (such as complex data structure with multiple values)
        • Any other valid JSON data type

      For instance, in this example input the following JSON:

      {
         "tool_outputs":[
            {
               "tool_call_id":"$call_id
      quot;, "output":"{3200}" } ] }
    4. Enter Default as the session name to confine to the current session.
    5. Save the response to a variable. In this example, the response is saved to a string variable str_FunctionResponse. The output is a JSON that displays the value you asked to output and here below is a sample output from the above run:
      :
      {
         "data":[
            {
               "metadata":{
                  
               },
               "role":"assistant",
               "content":[
                  {
                     "type":"text",
                     "text":{
                        "value":"The current stock price for AAINC is $3200.",
                        "annotations":[
                           
                        ]
                     }
                  }
               ]
            }
         ],
         "status":"completed"
      }}
    The OpenAI: Run Assistant Function action revolutionizes how OpenAI Assistants operate, transforming them from simple information providers to powerful tools that can take action and fulfill requests. By leveraging external functionalities, assistants can now tackle complex tasks, deliver personalized recommendations as shown in the above example, and ultimately provide a more comprehensive and helpful user experience.