Commit f905e865 authored by nanahira's avatar nanahira

chatgpt

parent 92fd82f4
......@@ -242,9 +242,12 @@
},
"chatgpt": {
"enabled": false,
"endpoint": "https://chatgpt.hanatan.net",
"token": "hanatan",
"session": "srvpro"
"endpoint": "https://api.openai.com",
"token": "sk-xxxx",
"model": "gpt-4o-mini",
"system_prompt": "",
"comment": "{{player}} and {{opponent}} will be replaced in the prompt",
"extra_opts": {}
},
"test_mode": {
"watch_public_hand": false,
......
......@@ -3674,17 +3674,23 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)->
ygopro.stoc_send_chat(client, "${chat_warn_level0}", ygopro.constants.COLORS.RED)
cancel = true
if not cancel and settings.modules.chatgpt.enabled and room.windbot and not client.is_post_watcher and client.pos < 2 and not client.is_local
session_key = "#{settings.modules.chatgpt.session}:#{settings.port}:#{CLIENT_get_authorize_key(client)}"
axios.post("#{settings.modules.chatgpt.endpoint}/api/chat", {
session: session_key,
text: msg
}, {
# session_key = "#{settings.modules.chatgpt.session}:#{settings.port}:#{CLIENT_get_authorize_key(client)}"
openai_req_body = {
messages: [
{ role: "user", content: msg }
],
model: settings.modules.chatgpt.model
}
if settings.modules.chatgpt.system_prompt
openai_req_body.messages.unshift { role: "system", content: settings.modules.chatgpt.system_prompt.replace }
Object.assign(openai_req_body, settings.modules.chatgpt.extra_opts)
axios.post("#{settings.modules.chatgpt.endpoint}/v1/chat/completions", openai_req_body, {
timeout: 300000,
headers: {
Authorization: "Bearer #{settings.modules.chatgpt.token}"
}
}).then((res) ->
text = res.data.data.text
text = res.data.choices[0].message.content
lines = text.split("\n")
for line in lines
if line
......@@ -3694,7 +3700,7 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)->
else
ygopro.stoc_send_chat_to_room(room, ' ', 1 - client.pos)
).catch((err) ->
log.error "CHATGPT ERROR", session_key, err
log.error "CHATGPT ERROR", err
)
return false
if !(room and (room.random_type or room.arena)) and not settings.modules.mycard.enabled
......
......@@ -4652,7 +4652,7 @@
//else
//log.info 'BIG BROTHER OK', response.statusCode, roomname, body
ygopro.ctos_follow('CHAT', true, async function(buffer, info, client, server, datas) {
var buy_result, cancel, ccolor, cip, cmd, cmsg, cname, code, color, cvalue, isVip, j, key, len, msg, name, oldmsg, player, ref, ref1, room, session_key, struct, sur_player, uname, windbot, word;
var buy_result, cancel, ccolor, cip, cmd, cmsg, cname, code, color, cvalue, isVip, j, key, len, msg, name, oldmsg, openai_req_body, player, ref, ref1, room, struct, sur_player, uname, windbot, word;
room = ROOM_all[client.rid];
if (!room) {
return;
......@@ -4907,18 +4907,31 @@
cancel = true;
}
if (!cancel && settings.modules.chatgpt.enabled && room.windbot && !client.is_post_watcher && client.pos < 2 && !client.is_local) {
session_key = `${settings.modules.chatgpt.session}:${settings.port}:${CLIENT_get_authorize_key(client)}`;
axios.post(`${settings.modules.chatgpt.endpoint}/api/chat`, {
session: session_key,
text: msg
}, {
// session_key = "#{settings.modules.chatgpt.session}:#{settings.port}:#{CLIENT_get_authorize_key(client)}"
openai_req_body = {
messages: [
{
role: "user",
content: msg
}
],
model: settings.modules.chatgpt.model
};
if (settings.modules.chatgpt.system_prompt) {
openai_req_body.messages.unshift({
role: "system",
content: settings.modules.chatgpt.system_prompt.replace
});
}
Object.assign(openai_req_body, settings.modules.chatgpt.extra_opts);
axios.post(`${settings.modules.chatgpt.endpoint}/v1/chat/completions`, openai_req_body, {
timeout: 300000,
headers: {
Authorization: `Bearer ${settings.modules.chatgpt.token}`
}
}).then(function(res) {
var chunk, chunks, l, len1, line, lines, results, text;
text = res.data.data.text;
text = res.data.choices[0].message.content;
lines = text.split("\n");
results = [];
for (l = 0, len1 = lines.length; l < len1; l++) {
......@@ -4940,7 +4953,7 @@
}
return results;
}).catch(function(err) {
return log.error("CHATGPT ERROR", session_key, err);
return log.error("CHATGPT ERROR", err);
});
return false;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment