Complete chat with OpenAI

You can complete the chat via OpenAI and use the result in your bot conversation.

Step 1. Add and set up the OpenAI integration.

Navigate to the organization integrations section:

Add the OpenAI integration:

Step 2. Create an assistant.

Assistants are created and edited in the Control Room, not at OpenAI. Open Integrations → your OpenAI integration → Assistants → Create, or create one straight from a bot’s Chat Completion variable.

Give it a name, choose the model that answers, and write its instructions. Under Knowledge you attach the documents it answers from, and under Tools you choose what it may do beyond answering — web search, the code interpreter, image generation and function tools.

Assistants that existed before August 2026, together with their storages and documents, were migrated automatically when OpenAI removed its Assistants API. An assistant whose instructions had to be reconstructed is marked Review in the list — open it and check that the instructions still describe what you want.

Everything an assistant can do, and who in your organization may change it, is described in Assistants. Its knowledge can also be kept up to date automatically with the OpenAI integration automations, learn more here.

Step 3. Add bot variable of type Chat Completion:

Screenshot2024-03-07at15.03.43.png

Select the previously configured integration. Select the previously configured Assistant.

Set the initial and follow-up parameters:

{
    "input": [
        {
            "role": "user",
            "content": "{{chat_prompt}}"
        }
    ]
}

Follow-up parameters take the same shape. A follow-up continues the previous response, so send only the new turn rather than the conversation so far:

{
    "input": [
        {
            "role": "user",
            "content": "{{chat_prompt}}"
        }
    ]
}

Each entry in input is a message, and three roles are accepted: user for the visitor’s message, system for instructions to the model, and assistant for a previous reply you want to seed the conversation with.

A system message steers the model in addition to the assistant’s own instructions — both are sent, and one does not replace the other. Use the assistant’s instructions for who it is, and a system message here for how it should behave in this one variable:

{
    "input": [
        {
            "role": "system",
            "content": "Always answer in Czech. Keep answers to two sentences."
        },
        {
            "role": "user",
            "content": "{{chat_prompt}}"
        }
    ]
}

Add the system message to both the initial and the follow-up parameters. The visitor’s replies go through the follow-up parameters, so a system message set only on the initial ones stops applying after the first turn.

A role other than user, system or assistant is treated as user without reporting an error, so check the spelling if a system message appears to be ignored.

The older thread/messages form is still accepted, so bots written before August 2026 keep working unchanged:

{
    "thread": {
        "messages": [
            {
                "role": "user",
                "content": "{{chat_prompt}}"
            }
        ]
    }
}

Adjust the parameters according to the documentation Bot variable syntax is allowed in the parameters so that they can be dynamic.

The result of such a bot variable is the completed chat that can be used in the bot conversation, for example, in the response.

In our example, the variable will create a response from a bot based on the user input, which is represented by the chat_prompt variable.

Step 4. Add a step for the user input:

Note that the code name for this step is chat_prompt - this automatically creates a bot variable with the same name.

Step 5. Add a step response with the chat completion:

Screenshot2024-03-07at15.56.58.png

This response uses the completed_chat_assistant variable created in Step 3.

Trying an assistant before a bot uses it

Each assistant has a Sandbox: a conversation that runs exactly the path a live bot does, without touching a bot. Open it from the assistant, or from Open the sandbox on its page.

For every answer it shows what actually ran — which tools fired, what the file search matched, what a function was asked for and what it returned, which model served the request, how many tokens it cost and how long it took. Conversations are kept for 30 days after they were last used, so you can come back and compare how an assistant answers before and after a change to its instructions.

Streaming

For the improved user experience with sometimes longer time that it takes for the AI to generate a response, you can use dedicated Variable Stream bot chat text type. It will display the AI response in real time as it comes.

Function calling

OpenAI allows function calling as a helper tool for the assistant-based chat completion. This can be integrated with the bot variables, and using the scripting, you can, for example, call external services and use the returned content in the chat completion.

Declare one or more functions on the assistant, under Function tools: the name, what the function does, and its parameters as a JSON Schema object.

A function’s schema used to live on the assistant object at OpenAI and was lost when that API was removed, so an assistant migrated from it will answer without ever calling its functions until they are declared here again. The bot variable’s function script below is unchanged — only the declaration moved.

Declaring a function under Function tools on the assistant

The Parameters field takes the JSON Schema for the function’s arguments — the name and the description are their own fields beside it:

{
  "type": "object",
  "properties": {
    "location": {
      "type": "string",
      "description": "The city and state e.g. San Francisco, CA"
    }
  },
  "required": [
    "location"
  ]
}

Enable the function script in the chat completion variable settings:

Screenshot2024-03-12at09.27.04.png
function (name, arguments, getVariableValue, callback) {
  switch (name) {
    case "getCurrentWeather":
      var url = new quriobot.URL("http://api.weatherapi.com/v1/current.json", true)
      url.query.q = arguments.location
      url.query.key = "YOUR_API_KEY"
      quriobot.ajax(url.toString(), function(responseText){
        var response = JSON.parse(responseText);
        var weather = response?.current?.condition?.text;
        callback(weather);
      })
      break;
    default:
      callback(null);
  }
}

Function script receives a name and the arguments of the requested function to submit the results of. This function will be called each time the model asks for the results of a function call. As a helper, there’s a getVariableValue(name, callback) function** **to allow getting the bot variables if they are needed for the function result. In our example, we check the requested function name and if it’s getCurrentWeather, we make an AJAX call to the weather API service and return the textual representation of the weather condition in the requested location. The response then might look like this:

Screenshot2024-03-12at09.44.05.png
ou can also have multiple functions processed by the same script by having logic branches depending on the provided name argument.

File uploads

You can use files uploaded via the File upload for the chat completions. Currently, those use cases are supported:

  • Assistant completions that use the code interpreter A file is passed as its own part of the message content, alongside the text. The file_ids field of the old format is no longer read, so a bot still using it will send the message without its file:
{
    "input": [
        {
            "role": "user",
            "content": [
                {"type": "input_text", "text": "{{hi_and_welcome}}"},
                {"type": "input_file", "file_id": "{{upload.value}}"}
            ]
        }
    ]
}

Each file gets its own input_file part. An image is passed the same way with input_image.

  • Non-Assistant chat completions with Vision In order to use the uploaded image for the vision, use image_url message type and use {{variable.value}} syntax:
{
  "messages": [
    {
      "role": "system",
      "content": "You are a helpful assistant."
    },
    {
      "role": "user",
      "content": [
        {"type": "text", "text": "{{hi_and_welcome}}"},
        {
          "type": "image_url",
          "image_url": {
            "url": "{{upload.value}}"
          }
        }
      ]
    }
  ]
}

Edit this page

Categories
Tags
See also