# Integrating OpenAI to a wix form 

**URL:** <https://forum.wixstudio.com/t/integrating-openai-to-a-wix-form/59033>\
**Category:** Ask the community\
**Tags:** wix-forms, question, code\
**Created:** [May 7, 2024, 3:13pm UTC](https://forum.wixstudio.com/t/integrating-openai-to-a-wix-form/59033 "2024-05-07T15:13:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rebecca\_Tyrstrup](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/rebecca_tyrstrup/32/91653_2.png) [@Rebecca\_Tyrstrup](https://forum.wixstudio.com/u/Rebecca_Tyrstrup)\
**Post date:** [May 7, 2024, 3:13pm UTC](https://forum.wixstudio.com/t/integrating-openai-to-a-wix-form/59033/1 "2024-05-07T15:13:33Z")

</div>

**Question:**  
How do i connect a form to open AI:s API?

**Product:**  
Wix website editor

**What are you trying to achieve:**  
I want to create a simple form where the user puts in one word and gets a response from an assistant or GPT from OpenAI. I have created a GPT which will provide the response I want.

**What have you already tried:**

_I have this code in the Velos Page Code section_

// Add this code in Velo’s Page Code section

import { chatGPTQuery } from ‘backend/chat-gpt-module’;

$w.onReady(function () {  
// When the user submits the job title  
$w(“#submitButton”).onClick(async function () {  
const jobTitle = $w(“#jobTitleInput”).value;

```
    // Check if the job title is not empty
    if (jobTitle.trim() === "") {
        $w("#responseText").text = "Please enter a job title.";
        return;
    }

    try {
        $w("#responseText").text = "Loading...";

        // This calls the backend function
        const response = await getGPTResponse(jobTitle);

        // Display the ChatGPT response
        $w("#responseText").text = response;
    } catch (error) {
        $w("#responseText").text = "Error: " + error.message;
    }
});

```

});

/\*\*

- Calls the backend function which interacts with the ChatGPT API.
- @param {string} jobTitle The job title entered by the user.
- @return {Promise} The ChatGPT response as a string.  
\*/  
function getGPTResponse(jobTitle) {  
chatGPTQuery(jobTitle)  
.then(response =\> {  
return response;  
})  
.catch(err =\> {  
console.error(err);  
throw new Error(‘There was an error processing your request.’);  
});  
return wixBackendFunction(‘chatGPTQuery’, { jobTitle });  
}

_I have the following code in a js file in the back-end section. I have inserted an API key from Open AI wherer it says “API-Key”_

import {fetch} from ‘wix-fetch’;

/// WHAT URL SHOULD BE USED?

export function chatGPTQuery(message) {  
const url = “[https://api.openai.com/v1/chat/completions](https://api.openai.com/v1/chat/completions)”;  
const headers = {  
“Authorization”: `Bearer API-KEY,  
“Content-Type”: “application/json”  
};  
const body = JSON.stringify({  
model: “gpt-3.5-turbo”, // Specify the model version  
messages: [{role: “user”, content: message}]  
});

```
return fetch(url, {method: 'POST', headers: headers, body: body})
    .then(response => response.json())
    .then(data => data.choices[0].message.content);

```

}

**Additional information:**  
I have created an AI assistant in open ai:s platform which is connected to the API key. I can’t figure out what is wrong.

---

<div class="post-metadata">

**Author:** ![CODE-NINJA](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/code-ninja/32/110588_2.png) [@CODE-NINJA](https://forum.wixstudio.com/u/CODE-NINJA)\
**Post date:** [May 7, 2024, 10:50pm UTC](https://forum.wixstudio.com/t/integrating-openai-to-a-wix-form/59033/2 "2024-05-07T22:50:43Z")

</div>

> [@Rebecca\_Tyrstrup](#):
>
> chatGPTQuery

Your SETUP could look like…

 ![2024-05-08 00_43_57-Window](https://us1.discourse-cdn.com/wix/original/3X/5/2/52d93c56e51ebfba734240a28473c820cdfa1992.png)

I would recommend to implement the following functions aswell…

1. Dropdown for choice of the GPT-Model  
a) Dalle-2  
b) Dalle-3  
c) Turbo-3.5  
d) and so on…  

2. Implementing a kind of CALCULATOR → calculating the used tokens and saving them is a history-log or something similar, to have an overview over the already used TOKENS.  
The more used tokens → the more you will pay for OAI.

3. Implementing your MICROPHONE AND SOUND, or even your Webcam for…  
a) Freehand conversations with CHAT-GPT  
b) Freehand Command-Control-Panel  
c) Using Webcam for SCANNING processes or RECOGNITION (IMAGES, BARCODES, or QR-CODES for example).

`$w('#ddnModel').onChange(async()=>{options = await get_oaiOptions(options, $w('#ddnModel').value);});`

```javascript
function get_oaiOptions(options, value) {console.log(options); console.log(value);
	options.model = value;
	if($w('#ddnModel').value==='dall-e-2') {
		options.keyName = 'OpenAi-KEY';
		options.apiUrl = 'https://api.openai.com/v1/images/generations';
		return options;
	}
	else if($w('#ddnModel').value==='dall-e-3') {
		options.keyName = 'OpenAi-KEY';
		options.apiUrl = 'https://api.openai.com/v1/images/generations';
		return options;
	}
	else if($w('#ddnModel').value==='gpt-3.5-turbo-instruct') {
		options.keyName = 'OpenAi-KEY';
		options.apiUrl = 'https://api.openai.com/v1/completions';
		return options;
	} else {console.log('else');}
}

```

Some more ideas → you will find here…

> [@\*\*\*Text to Speech\*\*\* ElevenLabs (JARVIS)](https://forum.wixstudio.com/t/text-to-speech-elevenlabs-jarvis/54919/2):
>
> !!! O M G!!! GOT THAT —\> S H I T \<— to work !!! The real → ARRAY-BUFFER \<— [2023-12-31 03\_28\_03-Chat-GPT \_ WixWorld] shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face shushing_face !!! Direct text to speech !!! → Let’s activate AI-based-JARVIS grinning upside_down_face crazy_face with a HIGH-QUA…

I have this CHAPTER already behind me.

**You can either type-in the questions into the input-box, or speak with the AI 😊**

And here some additional INFO for you…

> [@Microphone, Camera, Sound](https://forum.wixstudio.com/t/microphone-camera-sound/55327):
>
> I have now spent a lot of time investigating the issue with iFrames and the problem with … -allowing microphone (mic-activation) -allowing sound playback (sound-activation) -allowing camera (camera-activation) …inside the HTML-Component. Since there are no options given inside the VELO-APIs for that… …only → allow-fullscreen is given… …so it is not really possible to access microphone or activate sound and camerea inside…

**Good luck!**
