нужно закинуть чат гпт на сайт

Статус
В этой теме нельзя размещать новые ответы.

epileptic kid

Участник
Автор темы
47
10
Добрый вечер, как реализовать чат гпт на сайте, может через апишку как то, если можете помогите чем нибудь, пожалуйста!!!
 
Решение
1696878997289.png

index.html:
<body>
    <div class="container">
        <input id="prompt" type="text" placeholder="Вилкой в глаз или в жопу раз?">
        <button id="submit">Submit</button>
        <button id="clear">Clear history</button>
        <div id="chat"></div>
    </div>
    <script src="index.js"></script>
</body>
index.js:
const messages = [];
const TOKEN = 'ТВОЙ ТОКЕН';

const randomInt = (max) => Math.random() * max;
async function getAnswer(prompt) {
    if (!prompt)
        return alert('Ошибка, введите текст!');
    messages.push({ role: 'user', content: prompt });
    const response = await fetch('https://api.openai.com/v1/chat/completions', {
        headers: {
            Authorization: `Bearer ${TOKEN}`,
            'Content-Type'...

chapo

🫡 В армии с 17.10.2023. В ЛС НЕ ОТВЕЧАЮ
Друг
8,777
11,225
1696878997289.png

index.html:
<body>
    <div class="container">
        <input id="prompt" type="text" placeholder="Вилкой в глаз или в жопу раз?">
        <button id="submit">Submit</button>
        <button id="clear">Clear history</button>
        <div id="chat"></div>
    </div>
    <script src="index.js"></script>
</body>
index.js:
const messages = [];
const TOKEN = 'ТВОЙ ТОКЕН';

const randomInt = (max) => Math.random() * max;
async function getAnswer(prompt) {
    if (!prompt)
        return alert('Ошибка, введите текст!');
    messages.push({ role: 'user', content: prompt });
    const response = await fetch('https://api.openai.com/v1/chat/completions', {
        headers: {
            Authorization: `Bearer ${TOKEN}`,
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            model: 'gpt-3.5-turbo',
            messages: messages,
            temperature: 1,
            max_tokens: 256,
            top_p: 1,
            frequency_penalty: 0,
            presence_penalty: 0
        }),
        method: 'POST'
    });
    
    const text = await response.text();
    const data = JSON.parse(text);
    if (response.status != 200)
        return alert(data?.error?.message ?? `Unknown error, code ${response.status}\n${text}`);
    return data?.choices?.[randomInt(0, data.choices.length - 1)].message.content ?? text;
}

addEventListener('DOMContentLoaded', () => {
    const input = document.getElementById('prompt');
    const submit = document.getElementById('submit');
    const clear = document.getElementById('clear');
    const chat = document.getElementById('chat');
    submit.addEventListener('click', async () => {
        chat.innerHTML += `<p>USER: ${input.value}</p>`;
        const result = await getAnswer(input.value);
        input.value = '';
        chat.innerHTML += `<p>GPT: ${result}</p>`;
    });
    clear.addEventListener('click', () => {
        messages = [];
        chat.innerHTML = '';
    });
});
 
  • Нравится
Реакции: Quasper и epileptic kid
Статус
В этой теме нельзя размещать новые ответы.