# OPENAICHAT

## Overview

The **OPENAICHAT** workflow application lets you interact with an OpenAI chat model.

## How it works

* The application allows you to interact with OpenAI completion models.
* Application logs are available. These can be specified by setting the value of the `OpenAIChatLogLevel` parameter in the `web.config` file to `0` to deactivate logs, `1` for error logs, `2` for information logs, or `3` for debug logs; the default value is `0`.

## Required parameters

<table data-full-width="false"><thead><tr><th width="238.32476806640625" valign="top">Parameter</th><th width="110.18804931640625" valign="top">Type</th><th width="110.04266357421875" valign="top">Direction</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>MODEL</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">ID of the model to use<br><br>You can find available models at the following link: <a href="https://platform.openai.com/docs/models/">https://platform.openai.com/docs/models/</a>; the endpoint used by default is <code>/v1/chat/completions</code>.</td></tr></tbody></table>

You can use either of the following configurations: with system/user messages, with a message number, or with a JSON message array.

### With system/user messages

<table data-full-width="false"><thead><tr><th width="238.32489013671875" valign="top">Parameter</th><th width="110.18798828125" valign="top">Type</th><th width="110.042724609375" valign="top">Direction</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>SYSTEM_MESSAGE</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">The system message content</td></tr><tr><td valign="top"><code>USER_MESSAGE</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">The user message content</td></tr></tbody></table>

### With a message number

<table data-full-width="false"><thead><tr><th width="238.79205322265625" valign="top">Parameter</th><th width="110.18804931640625" valign="top">Type</th><th width="110.04266357421875" valign="top">Direction</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>MESSAGE_ROLEx</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">The type of the message, where <code>x</code> corresponds to the message number; the<br>value should be <code>assistant</code>, <code>system</code>, or <code>user</code></td></tr><tr><td valign="top"><code>MESSAGE_CONTENTx</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">The user message content, where <code>x</code> corresponds to the message number</td></tr></tbody></table>

### With a JSON message array

<table data-full-width="false"><thead><tr><th width="238.32489013671875" valign="top">Parameter</th><th width="110.18798828125" valign="top">Type</th><th width="110.04278564453125" valign="top">Direction</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>MESSAGE_JSON</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>The JSON array message object; the structure should match the following:</p><pre class="language-json"><code class="lang-json">[
    {
        "role": "assistant/system/user",
        "content": "First message content"
    },
    {
        "role": "assistant/system/user",
        "content": "Second message content"
    }
]
</code></pre></td></tr></tbody></table>

## Optional parameters

<table data-full-width="false"><thead><tr><th width="237.63812255859375" valign="top">Parameters</th><th width="111" valign="top">Type</th><th width="104" valign="top">Direction</th><th valign="top">Description</th></tr></thead><tbody><tr><td valign="top"><code>API_KEY</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">OpenAI API key<br><br>By default, this value comes from the <code>OpenAIApiKey</code> parameter in the <code>web.config</code> file.</td></tr><tr><td valign="top"><code>URL</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">API endpoint; this value comes from the <code>OpenAIChatApiUrl</code> parameter in the <code>web.config</code> file, if it's been defined</td></tr><tr><td valign="top"><code>TEMPERATURE</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>Sampling temperature, between <code>0</code> and <code>1</code></p><p></p><p><strong>Default value:</strong> <code>1</code></p><p></p><p>Higher values (e.g. <code>0.8</code>) will make the output more random, while lower values (e.g. <code>0.2</code>) will make it more focused and deterministic.</p></td></tr><tr><td valign="top"><code>TOP_P</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with <code>top_p</code> probability mass</p><p></p><p><strong>Default value:</strong> <code>1</code></p><p></p><p>📌 <strong>Example:</strong> A value of <code>0.1</code> means only the tokens comprising the top 10% probability mass are considered.</p></td></tr><tr><td valign="top"><code>FREQUENCY_PENALTY</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>Number between <code>-2.0</code> and <code>2.0</code></p><p></p><p><strong>Default value:</strong> <code>0</code></p><p></p><p>Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.</p></td></tr><tr><td valign="top"><code>MAX_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>Maximum number of tokens that can be generated in the chat completion</p><p></p><p><strong>Default value:</strong> <code>256</code></p><p></p><p>✏️ <strong>Note:</strong> For reasoning models (o1, o3, GPT-5 series), use <code>MAX_COMPLETION_TOKENS</code> instead. If both <code>MAX_TOKENS</code> and <code>MAX_COMPLETION_TOKENS</code> are specified, <code>MAX_COMPLETION_TOKENS</code> takes precedence.</p></td></tr><tr><td valign="top"><code>MAX_COMPLETION_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>Maximum number of tokens to generate in the completion </p><p></p><p>This parameter is required for reasoning models (o1, o3, GPT-5 series) and takes precedence over <code>MAX_TOKENS</code> when both are specified. For reasoning models, this value includes both reasoning tokens and visible completion tokens.</p></td></tr><tr><td valign="top"><code>REASONING_EFFORT</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>Controls the effort level for reasoning models; only applicable to reasoning models (o1, o3, GPT-5 series). </p><p></p><p><strong>Values:</strong> <code>low</code>, <code>medium</code>, or <code>high</code></p><p></p><p>⚠️ <strong>Important:</strong> Do not use this parameter with non-reasoning models (e.g. GPT-4o, GPT-4-turbo) as this will cause an API error.</p></td></tr><tr><td valign="top"><code>PRESENCE_PENALTY</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top"><p>Number between <code>-2.0</code> and <code>2.0</code></p><p><br><strong>Default value:</strong> <code>0</code></p><p></p><p>Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics<br></p></td></tr><tr><td valign="top"><code>RESPONSE_FORMAT</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>Format of the response: <code>json_object</code>, <code>text</code>, or <code>json_schema</code><br></p><p></p><p>When the value is <code>json_object</code>, the system prompt should contain the JSON keyword. When the value is <code>json_schema</code>, the expected schema must be provided in the  <code>RESPONSE_FORMAT_JSON_SCHEMA</code> parameter.</p></td></tr><tr><td valign="top"><code>RESPONSE_FORMAT_JSON_SCHEMA</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>The JSON schema that will be used by the model to respond.<br></p><p>See the <a href="#response_format_json_schema"><code>RESPONSE_FORMAT_JSON_SCHEMA</code></a> section below for an example.</p></td></tr><tr><td valign="top"><code>APP_RESPONSE_IGNORE_ERROR</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>Specifies (<code>Y</code> or <code>N</code>) if error should be ignored</p><p></p><p><strong>Default value:</strong> <code>N</code><br></p><p>✏️ <strong>Note:</strong> In case of error, if the parameter has <code>Y</code> as its value, the error will be ignored and defined OUT parameters (<code>APP_RESPONSE_STATUS</code> or <code>APP_RESPONSE_CONTENT</code>) will be mapped. Otherwise, an exception will be thrown.</p></td></tr><tr><td valign="top"><code>TOOLS</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">List of tools available to the model, formatted in JSON and compliant with OpenAI's format: <a href="https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools">https://platform.openai.com/docs/api-reference/chat/create#chat-create-tools</a><br>See the <a href="#tools"><code>TOOLS</code></a> section below for an example.</td></tr><tr><td valign="top"><code>TOOL_CHOICE_REQUIRED</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>Specifies whether the model must necessarily choose a tool</p><p></p><p><strong>Values:</strong> <code>Y</code> or <code>N</code> (default).</p></td></tr><tr><td valign="top"><code>PARALLEL_TOOL</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top"><p>Specifies whether the model can choose multiple tools. </p><p></p><p><strong>Values:</strong> <code>Y</code> (default) or <code>N</code>.</p></td></tr><tr><td valign="top"><code>MESSAGE_HISTORY</code></td><td valign="top">TEXT</td><td valign="top">INOUT</td><td valign="top"><p>The message history in JSON format</p><p></p><p>The reference structure follows OpenAI's documentation for the <code>messages</code> object:<br><a href="https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages">https://platform.openai.com/docs/api-reference/chat/create#chat-create-messages</a></p></td></tr><tr><td valign="top"><code>SELECTED_TOOLS</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">The list of selected tool names, separated by commas</td></tr><tr><td valign="top"><code>SELECTED_TOOLS_PARAM</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top"><p>A JSON array representing the list of selected tools along with their parameters </p><p></p><p>See the <a href="#selected_tools_params"><code>SELECTED_TOOLS_PARAMS</code></a> section below for an example output.</p></td></tr><tr><td valign="top"><code>SELECTED_TOOLS_COUNT</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">The number of selected tools</td></tr><tr><td valign="top"><code>RESULT</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">Chat result call</td></tr><tr><td valign="top"><code>RESULT_CONTENT</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">Content of the assistant message</td></tr><tr><td valign="top"><code>RESULT_TOTAL_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">OUT</td><td valign="top">Total of tokens used for generation</td></tr><tr><td valign="top"><code>RESULT_COMPLETION_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">OUT</td><td valign="top">Total of tokens used for generation</td></tr><tr><td valign="top"><code>RESULT_PROMPT_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">OUT</td><td valign="top">Total of token used for the prompt</td></tr><tr><td valign="top"><code>RESULT_REASONING_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">OUT</td><td valign="top"><p>Number of tokens used for internal reasoning by reasoning models (o1, o3, GPT-5 series)</p><p></p><p>These tokens are not visible in the response but count toward usage and billing. Returns <code>0</code> for non-reasoning models.</p></td></tr><tr><td valign="top"><code>RESULT_CACHED_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">OUT</td><td valign="top"><p>Number of prompt tokens that were served from cache</p><p></p><p>Cached tokens are billed at a reduced rate. This is useful for understanding cost optimization from prompt caching.</p></td></tr><tr><td valign="top"><code>APP_RESPONSE_STATUS</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">Response status code</td></tr><tr><td valign="top"><code>APP_RESPONSE_CONTENT</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">Response payload or error message</td></tr></tbody></table>

## Reasoning models

OpenAI offers reasoning models (o1, o3, GPT-5 series) that use internal reasoning before generating a response. These models have specific parameter requirements:

### Parameters

<table><thead><tr><th valign="top">Parameter</th><th>Description</th></tr></thead><tbody><tr><td valign="top"><code>MAX_COMPLETION_TOKENS</code> </td><td><strong>Required for reasoning models.</strong> Specifies the maximum tokens for the completion, including both reasoning tokens and visible output tokens</td></tr><tr><td valign="top"><code>REASONING_EFFORT</code></td><td><strong>Optional.</strong> Controls the depth of reasoning: <code>low</code>, <code>medium</code>, or <code>high</code>.<br><br>Higher values use more reasoning tokens but may produce better results.</td></tr></tbody></table>

{% hint style="warning" %}

* **Use `MAX_COMPLETION_TOKENS` instead of `MAX_TOKENS`** for reasoning models. The `MAX_TOKENS` parameter is deprecated for these models.
* **Do not use `REASONING_EFFORT` with non-reasoning models** (e.g., GPT-4o, GPT-4-turbo). These models don't support this parameter and the API will return an error.
* **When using `REASONING_EFFORT`, always set `MAX_COMPLETION_TOKENS`**. If `REASONING_EFFORT` is specified without `MAX_COMPLETION_TOKENS`, no token limit is sent to the API (the API will use its default limits). This is intentional because reasoning models reject the `MAX_TOKENS` parameter.
* **Reasoning tokens are not visible** in the response content but are included in the token usage count and billing.
  {% endhint %}

### Output parameters

<table><thead><tr><th valign="top">Parameter</th><th>Description</th></tr></thead><tbody><tr><td valign="top"><code>RESULT_REASONING_TOKENS</code></td><td><p>Number of tokens used for internal reasoning</p><p></p><p>Only populated for reasoning models; returns <code>0</code> for other models.</p></td></tr><tr><td valign="top"><code>RESULT_CACHED_TOKENS</code></td><td>Number of prompt tokens served from cache (applies to all models with prompt caching)</td></tr></tbody></table>

### Example of reasoning model use

```
MODEL = gpt-5-mini
USER_MESSAGE = "Solve this math problem: If a train travels 120 km in 2 hours, what is its average speed?"
MAX_COMPLETION_TOKENS = 500
REASONING_EFFORT = medium
```

#### Output

```
RESULT_CONTENT = "Average speed = distance ÷ time = 120 km ÷ 2 h = 60 km/h."
RESULT_TOTAL_TOKENS = 124
RESULT_COMPLETION_TOKENS = 95
RESULT_PROMPT_TOKENS = 29
RESULT_REASONING_TOKENS = 64
RESULT_CACHED_TOKENS = 0
```

## JSON schema use case

Using a JSON schema as a response format enforces the application to respond in a structured manner that aligns with the schema.&#x20;

You can directly extract the returned values to populate specific data; simply specify the name of the property to extract as the parameter name and set the target data in OUT.

## Examples

### `TOOLS`

<pre class="language-json"><code class="lang-json"><strong>[
</strong>    {
        "name": "GET_STOCK_INFORMATION",
        "description": "Get stock information about a product. If the product is not found, return an error. If the stock is less than 10, return a warning and a purchase order should be done.",      
        "parameters": {
            "type": "object",
            "properties": {
                "product_name": {
                    "type": "string",
                    "description": "The product name"
                },
                "serial_number": {
                    "type": "string",
                    "description": "The product serial number"
                }
            },
            "additionalProperties": false,
            "required": ["serial_number"]
        }
    },
    {
        "name": "PURCHASE_ORDER",
        "description": "Make a purchase order for a product.",    
        "parameters": {
            "type": "object",
            "properties": {
                "product_name": {
                    "type": "string",
                    "description": "The product name"
                },
                "serial_number": {
                    "type": "string",
                    "description": "The product serial number"
                },
                "quantity": {
                    "type": "number",
                    "description": "The quantity of the product to purchase"
                }
            },
            "additionalProperties": false,
            "required": ["serial_number", "quantity"]
        }
    }
]
</code></pre>

### `SELECTED_TOOLS_PARAMS`

```json
[
    {
        "name": "GET_STOCK_INFORMATION",
        "id": "call_Vuc2Ga8jP7vUksxG9C0fwpY8",
        "parameters": {
            "product_name": "vis",
            "serial_number": "V45645"
        }
        
    },
    {
        "name": "GET_STOCK_INFORMATION",
        "id": "call_nq3SCVUk0FjAHCeqOZGNXpC8",
        "parameters": {
            "product_name": "boulons",
            "serial_number": "b456"
        }
    }
]
```

### `RESPONSE_FORMAT_JSON_SCHEMA`

```json
{
  "name": "schema",
  "schema": {
    "$schema": "http://json-schema.org/draft-04/schema#",
    "type": "object",
    "properties": {
      "advice": {
        "type": "string"
      },
      "next_action": {
        "type": "string",
        "enum": ["expert","sales","support","logistics",null]
      },
      "confidence_level": {
        "type": "number"
      }
    },
    "required": ["advice", "next_action","confidence_level"]
  }
}
```

## OpenAI-compatible providers

The OPENAICHAT application can be used with OpenAI-compatible providers by specifying a custom API endpoint using the `URL` parameter or the `OpenAIChatApiUrl` setting in `web.config`.

### Compatible providers

<table><thead><tr><th valign="top">Provider</th><th valign="top">Type</th></tr></thead><tbody><tr><td valign="top">Azure OpenAI</td><td valign="top">Cloud</td></tr><tr><td valign="top">Ollama</td><td valign="top">Self-hosted</td></tr><tr><td valign="top">vLLM</td><td valign="top">Self-hosted</td></tr><tr><td valign="top">LocalAI</td><td valign="top">Self-hosted</td></tr><tr><td valign="top">LM Studio</td><td valign="top">Self-hosted</td></tr><tr><td valign="top">Together AI</td><td valign="top">Cloud</td></tr><tr><td valign="top">Groq</td><td valign="top">Cloud</td></tr><tr><td valign="top">Mistral AI</td><td valign="top">Cloud</td></tr><tr><td valign="top">OpenRouter</td><td valign="top">Cloud</td></tr></tbody></table>

{% hint style="warning" %}

* Do not use `MAX_COMPLETION_TOKENS` and `REASONING_EFFORT` with non-OpenAI providers unless you know the provider supports these parameters.
* Use `MAX_TOKENS` for token limits when using non-OpenAI providers.
* Some providers may ignore unsupported parameters, while others may return an error.
  {% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.workflowgen.com/admin/9.6/workflow-applications/openai/openaichat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
