ANTICHAT

ANTICHAT (https://forum.antichat.xyz/index.php)
-   Общие вопросы программирования (https://forum.antichat.xyz/forumdisplay.php?f=206)
-   -   нужно закинуть чат гпт на сайт (https://forum.antichat.xyz/showthread.php?t=1491347)

квадрат малечива 09.10.2023 22:09

Добрый вечер, как реализовать чат гпт на сайте, может через апишку как то, если можете помогите чем нибудь, пожалуйста!!!

YarmaK 09.10.2023 22:12

костыль через бота в тг с чатгпт как варик, проще апи телеги найти же😂

chapo 09.10.2023 23:17

https://forum.antichat.xyz/attachments/28390836/

index.html:





Код:


Submit

Clear history



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
+=
`USER:${input.value}`
;
const
result
=
await
getAnswer
(
input
.
value
)
;
input
.
value
=
''
;
chat
.
innerHTML
+=
`GPT:${result}`
;
}
)
;
clear
.
addEventListener
(
'click'
,
(
)
=>
{
messages
=
[
]
;
chat
.
innerHTML
=
''
;
}
)
;
}
)
;



Время: 02:26