Commit f905e865 authored by nanahira's avatar nanahira

chatgpt

parent 92fd82f4
...@@ -242,9 +242,12 @@ ...@@ -242,9 +242,12 @@
}, },
"chatgpt": { "chatgpt": {
"enabled": false, "enabled": false,
"endpoint": "https://chatgpt.hanatan.net", "endpoint": "https://api.openai.com",
"token": "hanatan", "token": "sk-xxxx",
"session": "srvpro" "model": "gpt-4o-mini",
"system_prompt": "",
"comment": "{{player}} and {{opponent}} will be replaced in the prompt",
"extra_opts": {}
}, },
"test_mode": { "test_mode": {
"watch_public_hand": false, "watch_public_hand": false,
......
...@@ -3674,17 +3674,23 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)-> ...@@ -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) ygopro.stoc_send_chat(client, "${chat_warn_level0}", ygopro.constants.COLORS.RED)
cancel = true 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 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)}" # session_key = "#{settings.modules.chatgpt.session}:#{settings.port}:#{CLIENT_get_authorize_key(client)}"
axios.post("#{settings.modules.chatgpt.endpoint}/api/chat", { openai_req_body = {
session: session_key, messages: [
text: msg { 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, timeout: 300000,
headers: { headers: {
Authorization: "Bearer #{settings.modules.chatgpt.token}" Authorization: "Bearer #{settings.modules.chatgpt.token}"
} }
}).then((res) -> }).then((res) ->
text = res.data.data.text text = res.data.choices[0].message.content
lines = text.split("\n") lines = text.split("\n")
for line in lines for line in lines
if line if line
...@@ -3694,7 +3700,7 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)-> ...@@ -3694,7 +3700,7 @@ ygopro.ctos_follow 'CHAT', true, (buffer, info, client, server, datas)->
else else
ygopro.stoc_send_chat_to_room(room, ' ', 1 - client.pos) ygopro.stoc_send_chat_to_room(room, ' ', 1 - client.pos)
).catch((err) -> ).catch((err) ->
log.error "CHATGPT ERROR", session_key, err log.error "CHATGPT ERROR", err
) )
return false return false
if !(room and (room.random_type or room.arena)) and not settings.modules.mycard.enabled if !(room and (room.random_type or room.arena)) and not settings.modules.mycard.enabled
......
...@@ -4652,7 +4652,7 @@ ...@@ -4652,7 +4652,7 @@
//else //else
//log.info 'BIG BROTHER OK', response.statusCode, roomname, body //log.info 'BIG BROTHER OK', response.statusCode, roomname, body
ygopro.ctos_follow('CHAT', true, async function(buffer, info, client, server, datas) { 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]; room = ROOM_all[client.rid];
if (!room) { if (!room) {
return; return;
...@@ -4907,18 +4907,31 @@ ...@@ -4907,18 +4907,31 @@
cancel = true; cancel = true;
} }
if (!cancel && settings.modules.chatgpt.enabled && room.windbot && !client.is_post_watcher && client.pos < 2 && !client.is_local) { 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)}`; // session_key = "#{settings.modules.chatgpt.session}:#{settings.port}:#{CLIENT_get_authorize_key(client)}"
axios.post(`${settings.modules.chatgpt.endpoint}/api/chat`, { openai_req_body = {
session: session_key, messages: [
text: msg {
}, { 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, timeout: 300000,
headers: { headers: {
Authorization: `Bearer ${settings.modules.chatgpt.token}` Authorization: `Bearer ${settings.modules.chatgpt.token}`
} }
}).then(function(res) { }).then(function(res) {
var chunk, chunks, l, len1, line, lines, results, text; 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"); lines = text.split("\n");
results = []; results = [];
for (l = 0, len1 = lines.length; l < len1; l++) { for (l = 0, len1 = lines.length; l < len1; l++) {
...@@ -4940,7 +4953,7 @@ ...@@ -4940,7 +4953,7 @@
} }
return results; return results;
}).catch(function(err) { }).catch(function(err) {
return log.error("CHATGPT ERROR", session_key, err); return log.error("CHATGPT ERROR", err);
}); });
return false; 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