OpenAI: Run Assistant action

The OpenAI: Run Assistant action uses OpenAI's Assistant API (v2) to build AI assistants within your own applications. An Assistant has instructions and can leverage models, tools, and knowledge to respond to user queries. The v2 Assistants API currently supports three types of tools: Code Interpreter, File search, and Function calling.

Prerequisites

  • You must have the Bot creator role to use the OpenAI Run Assistant action in an automation.
  • Ensure that you have the necessary credentials to send a request and have included OpenAI: Authenticate action before calling any OpenAI actions.
  • Understanding the assistant limitations: Knowing the assistant's capabilities and limitations helps you to set realistic expectations.

This example shows how to load an existing assistant in the OpenAI Run Assistant action and get an appropriate response. If you are interested to learn how to create your own assistant, see OpenAI Create Assistant.

Procedure

  1. In the Automation Anywhere Control Room, navigate to the Actions pane, select Generative AI > OpenAI, drag OpenAI: Run Assistant, and place it in the canvas.
  2. Enter or select the following fields:

    OpenAI: Run Assistant

    1. Click Load assistants, to select an existing assistant. You need to authenticate ( see OpenAI: Authenticate action) with the API Key.Open AI: Connect
      1. Connect to Open AI appears. You can input the API Key either through a saved credential in the Credential tab or select Insecure string tab to directly paste the API Key.
      2. The Assistant ID of the assistant selected is displayed in the Assistant ID field.
      Note: Understanding an assistant's limitations before selecting it is crucial for a smooth user experience. Imagine requesting a flight reservation, only to discover the assistant cannot access booking systems. By familiarizing yourself with the assistant's capabilities beforehand, you can avoid such frustrating scenarios.
    2. Enter a Message to use by the model to generate a response.
      Note:

      The Run Assistantaction retains the results of the previous action within the same conversation. If you call Run Assistantactions consecutively, the assistant will remember the context of the conversation within the same thread and be able to relate subsequent messages to the previous ones. However, this conversation history is cleared once the session ends. A new thread is created for each new session.

    3. Enter the name for the session to limit the session to the current session. Use the same name used in the Authentication action.
    4. To manage the optional parameters, select Yes under Show more options to add additional parameters such as:Model, Instructions, Additional instructions, Files and Metadata. You can also enable Code interpreter, File search or Function options to expand the capabilities of your assistant and allow it to perform more complex tasks. For information about these optional parameters, see OpenAI Create Run.
      Note: By selecting these optional parameters, to define Model(for example:gpt-4o), Instructions and so on allows you to fine-tune the behavior of the assistant by overriding its default settings.
      • Code Interpreter: Enables assistants to execute custom Python code, facilitating data manipulation, logic integration, and automation tasks. For example
      • File search: Allows assistants to search for and process information from external sources, enriching conversations and providing contextually relevant responses.
      • Function: Allows assistants to search for and process information from external sources, enriching conversations and providing contextually relevant responses.
    5. Save the response to a variable. In this example, the response is saved to a string variablestr_asst_Response. The output is a JSON and here below is a sample output from the above run:
      {
         "metadata":{
            
         },
         "data":[
            {
               "role":"assistant",
               "content":[
                  {
                     "type":"text",
                     "text":{
                        "value":"New York is a bustling metropolis known as \"The Big Apple,\" renowned for its iconic skyline, cultural diversity, and status as a major global financial, entertainment, and cultural center.",
                        "annotations":[
                           
                        ]
                     }
                  }
               ]
            }
         ],
         "status":"completed"
      }
Code interpreter:

The following example demonstrates how to use the assistant with the code interpreter enabled to generate the first eight terms of the Fibonacci sequence.

OpenAI Run Assistant Code Interpreter

The response of the above automation is as follows:

{
  "metadata": {},
  "data": [
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "The first 8 terms of the Fibonacci sequence are: \n\n\\[0, 1, 1, 2, 3, 5, 8, 13\\]",
            "annotations": []
          }
        }
      ]
    },
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1. Let's generate the first 8 terms of the Fibonacci sequence.",
            "annotations": []
          }
        }
      ]
    }
  ],
  "status": "completed"
}
File Search:

The following example demonstrates how to use the assistant with File search enabled. In this scenario, the assistant reads the attached file to generate the response.

OpenAI Run Assistant File search

The response of the above automation is as follows:

{
  "metadata": {},
  "data": [
    {
      "role": "assistant",
      "content": [
        {
          "type": "text",
          "text": {
            "value": "Here are the holidays listed in the attached file:\n\n1. New Year's Day\n2. Martin Luther King Jr. Day\n3. Presidents' Day\n4. Memorial Day\n5. Independence Day\n6. Labor Day\n7. Columbus Day\n8. Veterans Day\n9. Thanksgiving Day\n10. Christmas Day\n\nThese holidays were found within the attached document .",
            "annotations": []
          }
        }
      ]
    }
  ],
  "status": "completed"
}
Note:
  • To upload multiple files, you can add a list of File IDs. These File IDs are unique identifiers assigned to files uploaded using the OpenAI. For more information on uploading and retrieving Files in a Assistant, see OpenAI Files Operations
  • The Control Room files or desktop files uploaded to OpenAI can be found on the OpenAI storage.
.

OpenAI Run Assistant multiple File IDs

Function:

The following example demonstrates how to use the assistant with a function. In this scenario, imagine you want your assistant to retrieve weather information. By feeding it a new function named get_weather (along with a description and parameters), you equip the assistant with this new skill.

{
    "name": "get_weather",
    "description": "Determine weather in my location",
    "parameters": 
    {
       "type": "object",
       "properties": {
          "location": {
             "type": "string",
             "description": "The city and state e.g. San Francisco, CA"
          },
          "unit": {
             "type": "string",
             "enum": [
                "c",
                "f"
             ]
          }
       },
       "required": [
          "location"
       ]
    }
 }

The JSON output following the below image highlights the assistant's status as requires_action. This indicates that the assistant awaits your input on how to proceed. You can now leverage the OpenAI: Run Assistant Function action to instruct the assistant on how to use the newly introduced get_weather function and formulate an appropriate response based on the retrieved data.

Open AI Run Assistant Action Function

The response of the above automation is as follows:

{
   "data":{
      "thread_id":"thread_Sizn8HNIKH5NzDYXQ61n05RC",
      "tool_calls":[
         {
            "id":"call_wCWp3g9fdWLszmSvUMqglyW5",
            "type":"function",
            "function":{
               "name":"get_weather",
               "arguments":"{\"location\":\"Chicago, IL\"}"
            }
         }
      ],
      "id":"run_MBq3pioUk9K0NaREPOIY4qFG"
   },
   "status":"requires_action"
}
You can use the thread id, call id and run id from the JSON to call the OpenAI: Run Assistant Function action.