# Creating a script with AI

## Introduction

The custom script editor includes an integrated AI agent, so you can get real-time assistance creating and updating scripts!

Our AI integration knows about expected keys and value types (payload, variables, meta, etc.), so generated scripts will be in a form that's ready to use in process flows.

{% hint style="info" %}
For the best results from AI, use PHP, JavaScript, Python, or GO. AI does work for other languages however, you will need to provide significantly more context in your requests.
{% endhint %}

<details>

<summary><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FpLMO12yvTCxi9PorCt53%2Ficons8-film.svg?alt=media&#x26;token=e59864bc-2d97-4f98-966c-a083c18e60db" alt="" data-size="line"> Show me</summary>

<figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FRlqbbGlDWQBV7ujJ69eD%2FAI%20scripts%20demo.gif?alt=media&#x26;token=111d5e0c-691d-43d0-ac05-9f347594df76" alt=""><figcaption></figcaption></figure>

</details>

## Need to know

* The maximum memory size for a custom script is 512MB
* The maximum size of a custom script is 4GB
* Up to 5 AI requests can be made per day for any company profile, free of charge.&#x20;
* A [subscription tier bolt-on](https://doc.wearepatchworks.com/product-documentation/getting-started/core-subscription-tiers) can be purchased if you'd like to increase this allowance.
* You can view the number of AI requests used and remaining from your [AI conversation history](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-with-ai/ai-conversation-history).
* AI prompts can be up to 1500 characters.
* Sample payloads for an AI request can be up to 6000 characters.

## Using AI to create a script

Follow the steps below:

**Step 1**\
Log in to the [Patchworks dashboard](https://app.wearepatchworks.com/scripts) and select `scripts` from the left-hand navigation bar.

**Step 2**\
Select the `create script` button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2Frq05aPxsoslqqCxPeEwz%2Fcreate%20script%201.png?alt=media&#x26;token=427a948b-ec37-4ff8-bd53-28df13d7f183" alt=""><figcaption></figcaption></figure></div>

**Step 3**\
Add a unique `name` and a `description` and then choose your preferred coding language:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FXbo4m6E4va8emHLSnmmS%2Fadd%20new%20ai%20script%201.png?alt=media&#x26;token=e56f53cd-7a1e-4007-9db0-b9816cd92bc4" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Script names must be unique - you can't add two scripts with the same name.
{% endhint %}

**Step 4**\
Move down to the Patchworks AI panel and enter your prompt (be as clear and specific as possible) - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FxKmTiOkZZD4DkPVVSbI5%2Fadd%20new%20ai%20script%202.png?alt=media&#x26;token=c5cd57ca-de02-4b19-83d7-43c533736b24" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
The Patchworks AI panel is displayed as soon as your coding language is selected in the previous step.
{% endhint %}

**Step 5**\
If you have a sample payload and would like to provide it for clarity, select the `provide an example payload?` checkbox and paste in your payload - for example:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FvkaHkSi6BxlFDlY1uJvP%2Fadd%20new%20ai%20script%203.png?alt=media&#x26;token=3b51509c-d97d-480d-9eb3-c90192a40aa1" alt=""><figcaption></figcaption></figure></div>

We recommend providing a sample payload whenever possible, as it provides valuable context to the AI agent.&#x20;

Sample payloads don't have to be perfect (or valid code) - as long as the structure and format are described correctly, the AI agent should have enough to work with. For example, if you don't have a JSON payload available, you might enter something like:

{% code overflow="wrap" %}

```
The payload comprises a JSON object containing multiple fields and associated values. There are no arrays. 
```

{% endcode %}

**Step 6**\
Click the `generate` button:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2F4BMD9d1A5E5ttsiU2EHW%2Fadd%20new%20ai%20script%204.png?alt=media&#x26;token=e5ec6b29-bde6-4479-9f8b-5e45d53aced9" alt=""><figcaption></figcaption></figure></div>

{% hint style="warning" %}
Each time you `generate` a script, the [AI request allowance associated with your subscription tier](https://doc.wearepatchworks.com/product-documentation/getting-started/core-subscription-tiers) is decremented. You can view the number of AI requests used and remaining from your [AI conversation history](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/creating-a-script-with-ai/ai-conversation-history).
{% endhint %}

**Step 7**\
AI generates a first pass of the script, which is displayed in an overlay:

<div align="left"><figure><img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FJXOjQPrgWixzzSfAjz1H%2Fadd%20new%20ai%20script%205.png?alt=media&#x26;token=5096ee05-79eb-49d9-ade1-d2297ec14c1d" alt=""><figcaption></figcaption></figure></div>

{% hint style="info" %}
If your request is ambiguous (for example, you indicate that you require some specific fields to be added but don't stipulate what these are), you may be prompted for clarification:\
\
![](https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FIDXSkHMGZZwngfL21YJR%2Fclarification.png?alt=media\&token=7468bc34-b06b-4374-9a17-84b265eda178)

In this case, enter your response in the comments field and click `send reply`.<br>
{% endhint %}

From here:

* If you are satisfied that the script is correct, or that you at least have enough to work with independently, click `accept script`:\
  \ <img src="https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FqJQmS82q16zPOY5JS01L%2Fadd%20new%20ai%20script%206a.png?alt=media&#x26;token=48aa832c-63d2-4747-8dfb-92ac8441df53" alt="" data-size="original"><br>

  <div data-gb-custom-block data-tag="hint" data-style="info" class="hint hint-info"><p>You can't edit the generated script in the AI 'overlay' window - you need to accept it and then make any required updates in the <a href="../creating-a-script-manually#adding-and-editing-script-code">standard script editor</a>.</p></div>

  \
  Having accepted a script, it opens in the [standard script editor](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-a-script-manually#adding-and-editing-script-code) so you can [save](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-a-script-manually#adding-and-editing-script-code), [edit](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-a-script-manually#adding-and-editing-script-code) and [test](https://doc.wearepatchworks.com/product-documentation/developer-hub/custom-scripting/creating-and-testing-custom-scripts/testing-scripts) the code as needed. &#x20;

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Always <a href="testing-scripts">test</a> your scripts to ensure that the results are as expected.</p></div>

* If you want to refine your requirements and ask AI to generate again, update the `comments` field and click `send reply`:\
  \
  ![](https://2440044887-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FLYNcUBVQwSkOMG6KjZfz%2Fuploads%2FnJhJk1DheYddyFZNMNsx%2Fadd%20new%20ai%20script%206b.png?alt=media\&token=20ef5a38-e8c0-4c3f-aa02-1e1bd8d98c2a)<br>

  <div data-gb-custom-block data-tag="hint" data-style="warning" class="hint hint-warning"><p>Each time you refine the generated output with <code>send reply</code>, the <a href="../../../getting-started/core-subscription-tiers">AI request allowance associated with your subscription tier</a> is decremented. You can view the number of AI requests used and remaining from your <a href="creating-a-script-with-ai/ai-conversation-history">AI conversation history</a>.</p></div>

  \
  Then, if you are satisfied that the script is correct, or that you at least have enough to work with independently, click `accept script` - otherwise you can refine your requirements again.&#x20;

* If you want to close the script and exit back to your original prompt, click `close`.<br>

* If you feel that the generated response is not appropriate/helpful, click `report`. This marks the conversation for review by Patchworks.
