---
title: Generate Choice Options Using AI
product: Survey Creator
description: Learn how to integrate an AI service into Survey Creator to generate choice options for questions and fix grammar errors in question titles and choices. This JavaScript form builder demo uses the GPT-3.5 Turbo model.
framework: Vanilla JS
source: https://surveyjs.io/survey-creator/examples/ai-generated-choices/vanillajs
index: https://surveyjs.io/survey-creator/examples/overview.md
---

# Generate Choice Options Using AI (Vanilla JS)

AI-powered services have become ubiquitous in the modern digital world. Chatbots and assistants, such as ChatGPT, DeepSeek, and Microsoft Copilot, help users with writing and coding; voice recognition services transcribe speech to text; neural networks generate images based on descriptions. This demo shows how you can integrate an AI service into Survey Creator to generate choice options for Dropdown, Radio Button Group, and Checkboxes questions based on the provided question title. Click one of the questions on the design surface and select **AI** &rarr; **Generate choices by title** or **Generate more choices**. Another AI-based feature that may be useful for survey authors is spell correction. To test this functionality, make an error in a question title or choice option text and select **AI** &rarr; **Fix grammar errors**.

AI service integration starts with deploying a language model to your application and exposing an API for interaction with that model. Refer to tutorials dedicated to the model of your choice for instructions. This example uses the GPT-3.5 Turbo model deployed on the SurveyJS website.

A deployed AI model receives text prompts and responds with text completions. Your client-side code should construct the text prompts and send them to the model. For example, a prompt to fix spelling errors may look as shown below. Note the addition of the expected response format:

"Fix grammar errors in the 'choices' and 'title' fields: { title: "...", choices: [ ... ] }. Use JSON format as output."

Once the AI service sends back a text completion with a JSON object, update the required question properties (`title` and `choices` in this demo). Refer to the Code tab for a code example.

## Files

### `public/index.html`

```html
<!-- Uncomment the following lines to enable Ace Editor in the JSON Editor tab -->
<!-- 
<script src="https://unpkg.com/ace-builds/src-min-noconflict/ace.js"></script>
<script src="https://unpkg.com/ace-builds/src-min-noconflict/ext-searchbox.js"></script>
<script src="https://unpkg.com/ace-builds/src-min-noconflict/theme-clouds_midnight.js"></script>
-->

<div id="surveyCreatorContainer" style="position: absolute; height: 100%; width: 100%"></div>
```

### `src/index.css`

```css
/* You can add your custom CSS here. */
```

### `src/survey_json.js`

```js
export const surveyJSON = {
  "elements": [
    {
      "type": "dropdown",
      "name": "cars",
      "title": "Which is the brand of your car?",
      "description": "Select \"AI\" -> \"Generate choices by title\" or \"Generate more choices\" to populate the question with automatically created choice options."
    },
    {
      "type": "dropdown",
      "name": "grammar-errors",
      "title": "Tihs queston contain gramar and sppeling erors",
      "description": "Select \"AI\" -> \"Fix grammar errors\" to fix them automatically.",
      "choices": [
        "Itme 1",
        "Ittem 2",
        "Imte 3",
      ]
    },
    {
      "type": "checkbox",
      "name": "js-frameworks",
      "title": "Which JavaScript frameworks do you use?",
      "description": "Select \"AI\" -> \"Generate choices by title\" or \"Generate more choices\" to populate the question with automatically created choice options.",
      "colCount": 2
    },
    {
      "type": "radiogroup",
      "name": "product-discovery",
      "title": "How did you first discover our product?",
      "description": "Select \"AI\" -> \"Generate choices by title\" or \"Generate more choices\" to populate the question with automatically created choice options."
    }
  ]
};
```

### `src/index.js`

```js
import { SurveyCreator } from "survey-creator-js";
import "survey-core/survey.i18n";
import "survey-creator-core/survey-creator-core.i18n";
import { Serializer, Action, ComputedUpdater, createDropdownActionModel, QuestionSelectBase } from "survey-core";
import { surveyJSON } from "./survey_json";
import "survey-core/survey-core.css";
import "survey-creator-core/survey-creator-core.css";
import "./index.css";
import SurveyTheme from "survey-core/themes";
import { registerCreatorTheme } from "survey-creator-core";

registerCreatorTheme(SurveyTheme); // Add predefined Survey Creator UI themes

const creator = new SurveyCreator();

function queryAI(query, callback) {
    const url = `/api/AI/query`;
    try {
        fetch(url, {
            method: "POST",
            headers: {
                "Content-Type": "application/json",
            },
            body: JSON.stringify({ text: query }),
        })
        .then(x => x.json())
        .then(data => callback(data.text, data.error));
    } catch (error) {
        callback(null, error.message);
    }
}
function createAIAction(selectBaseQuestion) {
    const AIOptions = [
        { id: "title-to-choices", title: "Generate choices by title", query: "Generate unique choices by title and replace 'choices' field in this object: " },
        { id: "add-more-choices", title: "Generate more choices", query: "Generate more unique choices by title and add them to the 'choices' field in this object: " },
        { id: "randomize", title: "Randomize choice order", query: "Randomize choice order and replace them in 'choices' field: " },
        { id: "grammar", title: "Fix grammar errors", query: "Fix grammar errors in the 'choices' and 'title' fields: " }
    ];
    const action = createDropdownActionModel({
        id: "AI",   
        title: "AI",
        iconName: "icon-wand-24x24",
        visibleIndex: 8
    }, {
        items: AIOptions,
        onSelectionChanged: (item) => {
            action.title = "AI";
            let query = item.query;
            query += JSON.stringify({
                title: selectBaseQuestion.title,
                choices: selectBaseQuestion.choices.map(c => c.title)
            }) + "\n";
            query += "Use JSON format as output.";

            queryAI(query, (response, error) => {
                if (error) {
                    creator.notify(error, "error")
                } else {
                    const data = JSON.parse(response);
                    selectBaseQuestion.title = data.title;
                    selectBaseQuestion.choices = [];
                    selectBaseQuestion.choices = data.choices;
                }
            });
        },
        verticalPosition: "top",
        horizontalPosition: "center"
    });
    return action;
}
creator.onElementGetActions.add((_, options) => {
    const question = options.element;

    // Create the "AI" adorner for all choice-based questions
    if (question instanceof QuestionSelectBase) {
        const AIAdorner = createAIAction(question);
        options.actions.push(AIAdorner);
    }
});

creator.JSON = surveyJSON;
creator.render("surveyCreatorContainer");
```

### `package.json`

```json
{
  "dependencies": {
    "survey-core": "latest",
    "survey-js-ui": "latest",
    "survey-creator-core": "latest",
    "survey-creator-js": "latest"
  },
  "devDependencies": {
    "react-scripts": "latest"
  }
}
```

## Other Frameworks

- [Angular](https://surveyjs.io/survey-creator/examples/ai-generated-choices/angular.md)
- [React](https://surveyjs.io/survey-creator/examples/ai-generated-choices/reactjs.md)
- [Vue 3](https://surveyjs.io/survey-creator/examples/ai-generated-choices/vue3js.md)
- [jQuery](https://surveyjs.io/survey-creator/examples/ai-generated-choices/jquery.md)
