> For the complete documentation index, see [llms.txt](https://docs.advantys.com/workflowgen-administration-module-reference-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.advantys.com/workflowgen-administration-module-reference-guide/workflow-applications/openai/openaichat.md).

# 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="true"><thead><tr><th width="202" valign="top">Parameter</th><th width="76" valign="top">Type</th><th width="125" 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:<br><a href="https://platform.openai.com/docs/models/model-endpoint-compatibility">https://platform.openai.com/docs/models/model-endpoint-compatibility</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/developer/user messages

<table data-full-width="true"><thead><tr><th width="202" valign="top">Parameter</th><th width="76" valign="top">Type</th><th width="125" 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>DEVELOPER_MESSAGE</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">The developer 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="true"><thead><tr><th width="216" valign="top">Parameter</th><th width="76" valign="top">Type</th><th width="125" 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>, <code>user</code>, or <code>developer</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="true"><thead><tr><th width="202" valign="top">Parameter</th><th width="76" valign="top">Type</th><th width="125" 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 example:</p><pre class="language-json"><code class="lang-json">[
    {
        "role": "assistant",
        "content": "First message content"
    },
    {
        "role": "system",
        "content": "Second message content"
    }
]
</code></pre></td></tr></tbody></table>

## Optional parameters

<table data-full-width="true"><thead><tr><th width="311" 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; defaults to <code>https://api.openai.com/v1/audio/transcriptions</code></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>; defaults to <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">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. Therefore, <code>0.1</code> means only the tokens comprising the top 10% probability mass are considered.<br><br>Defaults to <code>1</code></td></tr><tr><td valign="top"><code>FREQUENCY_PENALTY</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top">Number between <code>-2.0</code> and <code>2.0</code>. 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.<br><br>Defaults to <code>0</code></td></tr><tr><td valign="top"><code>MAX_TOKENS</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top">Maximum number of tokens that can be generated in the chat completion; defaults to <code>256</code></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>; defaults to <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.</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>, <code>or json_schema</code><br></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">The JSON schema that will be used by the model to respond.<br>See the <a href="#response_format_json_schema"><code>RESPONSE_FORMAT_JSON_SCHEMA</code></a> section below for an example.</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; defaults to <code>N</code><br></p><p>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">Specifies whether the model must necessarily choose a tool. Values: <code>Y</code> or <code>N</code> (default).</td></tr><tr><td valign="top"><code>PARALLEL_TOOL</code></td><td valign="top">TEXT</td><td valign="top">IN</td><td valign="top">Specifies whether the model can choose multiple tools. Values: <code>Y</code> (default) or <code>N</code>.</td></tr><tr><td valign="top"><code>MESSAGE_HISTORY</code></td><td valign="top">TEXT</td><td valign="top">INOUT</td><td valign="top">The message history in JSON format. 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>.</td></tr><tr><td valign="top"><code>SELECTED_TOOL</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_TOOL_PARAM</code></td><td valign="top">TEXT</td><td valign="top">OUT</td><td valign="top">A JSON array representing the list of selected tools along with their parameters. See the <a href="#selected_tools_params"><code>SELECTED_TOOLS_PARAMS</code></a> section below for an example output.</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>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><tr><td valign="top"><code>TRIM_SIZE</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top">Number of messages from the end of the conversation history to be kept</td></tr><tr><td valign="top"><code>TRIM_SKIP</code></td><td valign="top">NUMERIC</td><td valign="top">IN</td><td valign="top">Number messages from the beginning of the conversation history to be kept</td></tr></tbody></table>

## 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"]
  }
}
```
