Compare commits
4 commits
7d4ff3ce64
...
a398641172
Author | SHA1 | Date | |
---|---|---|---|
a398641172 | |||
4ce0f70d5d | |||
568e175208 | |||
e6b7f10b87 |
20 changed files with 75 additions and 57 deletions
|
@ -56,7 +56,7 @@ public class DiscordBot implements Runnable {
|
||||||
if (server.isEmpty()) {
|
if (server.isEmpty()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var tokens = this.serverDBService.tokensOfLast30Days(server.get().getId());
|
var tokens = this.serverDBService.tokensOfLast30Days(String.valueOf(server.get().getId()));
|
||||||
interactionOriginalResponseUpdater.setContent("" + tokens).update();
|
interactionOriginalResponseUpdater.setContent("" + tokens).update();
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class ServerDBService {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<ServerConfig> getConfig(long serverId) {
|
public Optional<ServerConfig> getConfig(String serverId) {
|
||||||
var getServerConfig = """
|
var getServerConfig = """
|
||||||
SELECT * FROM server_configs WHERE server_id = ?
|
SELECT * FROM server_configs WHERE server_id = ?
|
||||||
""";
|
""";
|
||||||
|
@ -32,7 +32,7 @@ public class ServerDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, serverId);
|
pstmt.setString(1, serverId);
|
||||||
ResultSet resultSet = pstmt.executeQuery();
|
ResultSet resultSet = pstmt.executeQuery();
|
||||||
Iterable<ServerConfig> iterable = () -> new ResultSetIterator<>(resultSet, new ServerConfig.ServerConfigResultSetTransformer());
|
Iterable<ServerConfig> iterable = () -> new ResultSetIterator<>(resultSet, new ServerConfig.ServerConfigResultSetTransformer());
|
||||||
return StreamSupport.stream(iterable.spliterator(), false).findFirst();
|
return StreamSupport.stream(iterable.spliterator(), false).findFirst();
|
||||||
|
@ -89,7 +89,7 @@ public class ServerDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, newServerConfig.serverId());
|
pstmt.setString(1, newServerConfig.serverId());
|
||||||
pstmt.setString(2, newServerConfig.systemMessage());
|
pstmt.setString(2, newServerConfig.systemMessage());
|
||||||
pstmt.setInt(3, newServerConfig.rateLimit());
|
pstmt.setInt(3, newServerConfig.rateLimit());
|
||||||
int affectedRows = pstmt.executeUpdate();
|
int affectedRows = pstmt.executeUpdate();
|
||||||
|
@ -111,7 +111,7 @@ public class ServerDBService {
|
||||||
) {
|
) {
|
||||||
pstmt.setString(1, newServerConfig.systemMessage());
|
pstmt.setString(1, newServerConfig.systemMessage());
|
||||||
pstmt.setInt(2, newServerConfig.rateLimit());
|
pstmt.setInt(2, newServerConfig.rateLimit());
|
||||||
pstmt.setLong(3, newServerConfig.serverId());
|
pstmt.setString(3, newServerConfig.serverId());
|
||||||
pstmt.setLong(4, id);
|
pstmt.setLong(4, id);
|
||||||
int affectedRows = pstmt.executeUpdate();
|
int affectedRows = pstmt.executeUpdate();
|
||||||
if (affectedRows == 0) {
|
if (affectedRows == 0) {
|
||||||
|
@ -140,7 +140,7 @@ public class ServerDBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int countMessagesInLastMinute(long serverId) {
|
public int countMessagesInLastMinute(String serverId) {
|
||||||
var getServerConfig = """
|
var getServerConfig = """
|
||||||
SELECT count(*) FROM server_messages WHERE server_id = ? AND time <= ? and time >= ?
|
SELECT count(*) FROM server_messages WHERE server_id = ? AND time <= ? and time >= ?
|
||||||
""";
|
""";
|
||||||
|
@ -148,7 +148,7 @@ public class ServerDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, serverId);
|
pstmt.setString(1, serverId);
|
||||||
var now = Instant.now();
|
var now = Instant.now();
|
||||||
pstmt.setTimestamp(2, Timestamp.from(now));
|
pstmt.setTimestamp(2, Timestamp.from(now));
|
||||||
pstmt.setTimestamp(3, Timestamp.from(now.minus(1, ChronoUnit.MINUTES)));
|
pstmt.setTimestamp(3, Timestamp.from(now.minus(1, ChronoUnit.MINUTES)));
|
||||||
|
@ -172,7 +172,7 @@ public class ServerDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, serverMessage.serverId());
|
pstmt.setString(1, serverMessage.serverId());
|
||||||
pstmt.setLong(2, serverMessage.userId());
|
pstmt.setLong(2, serverMessage.userId());
|
||||||
pstmt.setInt(3, serverMessage.tokens());
|
pstmt.setInt(3, serverMessage.tokens());
|
||||||
int affectedRows = pstmt.executeUpdate();
|
int affectedRows = pstmt.executeUpdate();
|
||||||
|
@ -184,7 +184,7 @@ public class ServerDBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long tokensOfLast30Days(long serverId) {
|
public long tokensOfLast30Days(String serverId) {
|
||||||
var countTokensOfLast30Days = """
|
var countTokensOfLast30Days = """
|
||||||
SELECT sum(tokens) FROM server_messages WHERE server_id = ? AND time < ? AND time >= ?
|
SELECT sum(tokens) FROM server_messages WHERE server_id = ? AND time < ? AND time >= ?
|
||||||
""";
|
""";
|
||||||
|
@ -192,7 +192,7 @@ public class ServerDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
|
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, serverId);
|
pstmt.setString(1, serverId);
|
||||||
var now = Instant.now();
|
var now = Instant.now();
|
||||||
pstmt.setTimestamp(2, Timestamp.from(now));
|
pstmt.setTimestamp(2, Timestamp.from(now));
|
||||||
pstmt.setTimestamp(3, Timestamp.from(now.minus(30, ChronoUnit.DAYS)));
|
pstmt.setTimestamp(3, Timestamp.from(now.minus(30, ChronoUnit.DAYS)));
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class UserDBService {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Optional<UserConfig> getConfig(long userId) {
|
public Optional<UserConfig> getConfig(String userId) {
|
||||||
var getServerConfig = """
|
var getServerConfig = """
|
||||||
SELECT * FROM user_configs WHERE user_id = ?
|
SELECT * FROM user_configs WHERE user_id = ?
|
||||||
""";
|
""";
|
||||||
|
@ -32,7 +32,7 @@ public class UserDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, userId);
|
pstmt.setString(1, userId);
|
||||||
ResultSet resultSet = pstmt.executeQuery();
|
ResultSet resultSet = pstmt.executeQuery();
|
||||||
Iterable<UserConfig> iterable = () -> new ResultSetIterator<>(resultSet, new UserConfig.UserConfigResultSetTransformer());
|
Iterable<UserConfig> iterable = () -> new ResultSetIterator<>(resultSet, new UserConfig.UserConfigResultSetTransformer());
|
||||||
return StreamSupport.stream(iterable.spliterator(), false).findFirst();
|
return StreamSupport.stream(iterable.spliterator(), false).findFirst();
|
||||||
|
@ -90,7 +90,7 @@ public class UserDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, newUserConfig.userId());
|
pstmt.setString(1, newUserConfig.userId());
|
||||||
pstmt.setString(2, newUserConfig.systemMessage());
|
pstmt.setString(2, newUserConfig.systemMessage());
|
||||||
pstmt.setInt(3, newUserConfig.contextLength());
|
pstmt.setInt(3, newUserConfig.contextLength());
|
||||||
pstmt.setInt(4, newUserConfig.rateLimit());
|
pstmt.setInt(4, newUserConfig.rateLimit());
|
||||||
|
@ -105,7 +105,7 @@ public class UserDBService {
|
||||||
|
|
||||||
public void updateConfig(long id, UserConfig.NewUserConfig newUserConfig) throws DBException {
|
public void updateConfig(long id, UserConfig.NewUserConfig newUserConfig) throws DBException {
|
||||||
var getServerConfig = """
|
var getServerConfig = """
|
||||||
UPDATE user_configs SET system_message = ?, context_length = ?, rate_limit = ? WHERE id = ?
|
UPDATE user_configs SET system_message = ?, context_length = ?, rate_limit = ?, user_id = ? WHERE id = ?
|
||||||
""";
|
""";
|
||||||
try (Connection con = DriverManager
|
try (Connection con = DriverManager
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
|
@ -114,7 +114,8 @@ public class UserDBService {
|
||||||
pstmt.setString(1, newUserConfig.systemMessage());
|
pstmt.setString(1, newUserConfig.systemMessage());
|
||||||
pstmt.setInt(2, newUserConfig.rateLimit());
|
pstmt.setInt(2, newUserConfig.rateLimit());
|
||||||
pstmt.setLong(3, newUserConfig.contextLength());
|
pstmt.setLong(3, newUserConfig.contextLength());
|
||||||
pstmt.setLong(4, id);
|
pstmt.setString(4, newUserConfig.userId());
|
||||||
|
pstmt.setLong(5, id);
|
||||||
int affectedRows = pstmt.executeUpdate();
|
int affectedRows = pstmt.executeUpdate();
|
||||||
if (affectedRows == 0) {
|
if (affectedRows == 0) {
|
||||||
logger.error("No config update with id: " + id);
|
logger.error("No config update with id: " + id);
|
||||||
|
@ -142,7 +143,7 @@ public class UserDBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public int countMessagesInLastMinute(long userId) {
|
public int countMessagesInLastMinute(String userId) {
|
||||||
var getServerConfig = """
|
var getServerConfig = """
|
||||||
SELECT count(*) FROM user_messages WHERE user_id = ? AND time <= ? and time >= ?
|
SELECT count(*) FROM user_messages WHERE user_id = ? AND time <= ? and time >= ?
|
||||||
""";
|
""";
|
||||||
|
@ -150,7 +151,7 @@ public class UserDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, userId);
|
pstmt.setString(1, userId);
|
||||||
var now = Instant.now();
|
var now = Instant.now();
|
||||||
pstmt.setTimestamp(2, Timestamp.from(now));
|
pstmt.setTimestamp(2, Timestamp.from(now));
|
||||||
pstmt.setTimestamp(3, Timestamp.from(now.minus(1, ChronoUnit.MINUTES)));
|
pstmt.setTimestamp(3, Timestamp.from(now.minus(1, ChronoUnit.MINUTES)));
|
||||||
|
@ -174,7 +175,7 @@ public class UserDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
PreparedStatement pstmt = con.prepareStatement(getServerConfig)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, newUserMessage.userId());
|
pstmt.setString(1, newUserMessage.userId());
|
||||||
pstmt.setString(2, newUserMessage.question());
|
pstmt.setString(2, newUserMessage.question());
|
||||||
pstmt.setString(3, newUserMessage.answer());
|
pstmt.setString(3, newUserMessage.answer());
|
||||||
pstmt.setInt(4, newUserMessage.tokens());
|
pstmt.setInt(4, newUserMessage.tokens());
|
||||||
|
@ -187,7 +188,7 @@ public class UserDBService {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public long tokensOfLast30Days(long userId) {
|
public long tokensOfLast30Days(String userId) {
|
||||||
var countTokensOfLast30Days = """
|
var countTokensOfLast30Days = """
|
||||||
SELECT sum(tokens) FROM user_messages WHERE user_id = ? AND time < ? AND time >= ?
|
SELECT sum(tokens) FROM user_messages WHERE user_id = ? AND time < ? AND time >= ?
|
||||||
""";
|
""";
|
||||||
|
@ -195,7 +196,7 @@ public class UserDBService {
|
||||||
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
.getConnection(this.jdbcConnectionString, this.username, this.password);
|
||||||
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
|
PreparedStatement pstmt = con.prepareStatement(countTokensOfLast30Days)
|
||||||
) {
|
) {
|
||||||
pstmt.setLong(1, userId);
|
pstmt.setString(1, userId);
|
||||||
var now = Instant.now();
|
var now = Instant.now();
|
||||||
pstmt.setTimestamp(2, Timestamp.from(now));
|
pstmt.setTimestamp(2, Timestamp.from(now));
|
||||||
pstmt.setTimestamp(3, Timestamp.from(now.minus(30, ChronoUnit.DAYS)));
|
pstmt.setTimestamp(3, Timestamp.from(now.minus(30, ChronoUnit.DAYS)));
|
||||||
|
|
|
@ -8,14 +8,14 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record ServerConfig(long id, long serverId, String systemMessage, int rateLimit, Instant time) {
|
public record ServerConfig(long id, String serverId, String systemMessage, int rateLimit, Instant time) {
|
||||||
|
|
||||||
public static class ServerConfigResultSetTransformer implements ResultSetTransformer<ServerConfig> {
|
public static class ServerConfigResultSetTransformer implements ResultSetTransformer<ServerConfig> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerConfig transform(ResultSet resultSet) throws SQLException {
|
public ServerConfig transform(ResultSet resultSet) throws SQLException {
|
||||||
var id = resultSet.getLong("id");
|
var id = resultSet.getLong("id");
|
||||||
var serverId = resultSet.getLong("server_id");
|
var serverId = resultSet.getString("server_id");
|
||||||
var systemMessage = resultSet.getString("system_message");
|
var systemMessage = resultSet.getString("system_message");
|
||||||
var rateLimit = resultSet.getInt("rate_limit");
|
var rateLimit = resultSet.getInt("rate_limit");
|
||||||
var time = resultSet.getTimestamp("time").toInstant();
|
var time = resultSet.getTimestamp("time").toInstant();
|
||||||
|
@ -24,6 +24,6 @@ public record ServerConfig(long id, long serverId, String systemMessage, int rat
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public record NewServerConfig(long serverId, String systemMessage, @Nullable int rateLimit) {
|
public record NewServerConfig(String serverId, String systemMessage, @Nullable int rateLimit) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,14 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record ServerMessage(long id, long serverId, long userId, int tokens, Instant time) {
|
public record ServerMessage(long id, String serverId, long userId, int tokens, Instant time) {
|
||||||
|
|
||||||
public static class ServerMessageResultSetTransformer implements ResultSetTransformer<ServerMessage> {
|
public static class ServerMessageResultSetTransformer implements ResultSetTransformer<ServerMessage> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ServerMessage transform(ResultSet resultSet) throws SQLException {
|
public ServerMessage transform(ResultSet resultSet) throws SQLException {
|
||||||
var id = resultSet.getLong("id");
|
var id = resultSet.getLong("id");
|
||||||
var serverId = resultSet.getLong("server_id");
|
var serverId = resultSet.getString("server_id");
|
||||||
var userId = resultSet.getLong("user_id");
|
var userId = resultSet.getLong("user_id");
|
||||||
var tokens = resultSet.getInt("tokens");
|
var tokens = resultSet.getInt("tokens");
|
||||||
var time = resultSet.getTimestamp("time").toInstant();
|
var time = resultSet.getTimestamp("time").toInstant();
|
||||||
|
@ -23,6 +23,6 @@ public record ServerMessage(long id, long serverId, long userId, int tokens, Ins
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public record NewServerMessage(long serverId, long userId, int tokens) {
|
public record NewServerMessage(String serverId, long userId, int tokens) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -8,14 +8,14 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record UserConfig(long id, long userId, String systemMessage, int contextLength, int rateLimit, Instant time) {
|
public record UserConfig(long id, String userId, String systemMessage, int contextLength, int rateLimit, Instant time) {
|
||||||
|
|
||||||
public static class UserConfigResultSetTransformer implements ResultSetTransformer<UserConfig> {
|
public static class UserConfigResultSetTransformer implements ResultSetTransformer<UserConfig> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserConfig transform(ResultSet resultSet) throws SQLException {
|
public UserConfig transform(ResultSet resultSet) throws SQLException {
|
||||||
var id = resultSet.getLong("id");
|
var id = resultSet.getLong("id");
|
||||||
var userId = resultSet.getLong("user_id");
|
var userId = resultSet.getString("user_id");
|
||||||
var systemMessage = resultSet.getString("system_message");
|
var systemMessage = resultSet.getString("system_message");
|
||||||
var contextLength = resultSet.getInt("context_length");
|
var contextLength = resultSet.getInt("context_length");
|
||||||
var rateLimit = resultSet.getInt("rate_limit");
|
var rateLimit = resultSet.getInt("rate_limit");
|
||||||
|
@ -25,7 +25,7 @@ public record UserConfig(long id, long userId, String systemMessage, int context
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public record NewUserConfig(long userId, String systemMessage, @Nullable int contextLength,
|
public record NewUserConfig(String userId, String systemMessage, @Nullable int contextLength,
|
||||||
@Nullable int rateLimit) {
|
@Nullable int rateLimit) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,14 @@ import java.sql.ResultSet;
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
|
|
||||||
public record UserMessage(long id, long userId, String question, String answer, int tokens, Instant time) {
|
public record UserMessage(long id, String userId, String question, String answer, int tokens, Instant time) {
|
||||||
|
|
||||||
public static class UserMessageResultSetTransformer implements ResultSetTransformer<UserMessage> {
|
public static class UserMessageResultSetTransformer implements ResultSetTransformer<UserMessage> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public UserMessage transform(ResultSet resultSet) throws SQLException {
|
public UserMessage transform(ResultSet resultSet) throws SQLException {
|
||||||
var id = resultSet.getLong("id");
|
var id = resultSet.getLong("id");
|
||||||
var userId = resultSet.getLong("user_id");
|
var userId = resultSet.getString("user_id");
|
||||||
var question = resultSet.getString("question");
|
var question = resultSet.getString("question");
|
||||||
var answer = resultSet.getString("answer");
|
var answer = resultSet.getString("answer");
|
||||||
var tokens = resultSet.getInt("tokens");
|
var tokens = resultSet.getInt("tokens");
|
||||||
|
@ -24,6 +24,6 @@ public record UserMessage(long id, long userId, String question, String answer,
|
||||||
}
|
}
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown = true)
|
@JsonIgnoreProperties(ignoreUnknown = true)
|
||||||
public record NewUserMessage(long userId, String question, String answer, int tokens) {
|
public record NewUserMessage(String userId, String question, String answer, int tokens) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,7 +24,7 @@ public class MessageCreateHandler implements MessageCreateListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (this.messageHandler.exceedsRate(event)) {
|
if (this.messageHandler.exceedsRate(event)) {
|
||||||
event.getChannel().sendMessage("Rate limit hit - cooling down now...");
|
event.getChannel().sendMessage("Rate limit hit - cooling down...");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.messageHandler.handle(event);
|
this.messageHandler.handle(event);
|
||||||
|
|
|
@ -29,6 +29,8 @@ public class ServerMessageHandler implements MessageHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MessageCreateEvent event) {
|
public void handle(MessageCreateEvent event) {
|
||||||
String content = extractContent(event);
|
String content = extractContent(event);
|
||||||
|
var serverId = event.getServer().get().getId();
|
||||||
|
var systemMessage = this.serverDBService.getConfig(String.valueOf(serverId)).get().systemMessage();
|
||||||
var request = event.getMessage().getType() == MessageType.REPLY ?
|
var request = event.getMessage().getType() == MessageType.REPLY ?
|
||||||
new ChatGPTRequestBuilder()
|
new ChatGPTRequestBuilder()
|
||||||
.contextRequest(event.getMessage()
|
.contextRequest(event.getMessage()
|
||||||
|
@ -37,8 +39,8 @@ public class ServerMessageHandler implements MessageHandler {
|
||||||
.flatMap(m -> m)
|
.flatMap(m -> m)
|
||||||
.map(Message::getReadableContent)
|
.map(Message::getReadableContent)
|
||||||
.stream().toList(),
|
.stream().toList(),
|
||||||
content) :
|
content, systemMessage) :
|
||||||
new ChatGPTRequestBuilder().simpleRequest(content);
|
new ChatGPTRequestBuilder().simpleRequest(content, systemMessage);
|
||||||
try {
|
try {
|
||||||
var response = this.chatGPTService.submit(request);
|
var response = this.chatGPTService.submit(request);
|
||||||
if (response.choices().size() < 1) {
|
if (response.choices().size() < 1) {
|
||||||
|
@ -61,7 +63,7 @@ public class ServerMessageHandler implements MessageHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
var serverId = event.getServer().get().getId();
|
var serverId = event.getServer().get().getId();
|
||||||
var config = this.serverDBService.getConfig(serverId);
|
var config = this.serverDBService.getConfig(String.valueOf(serverId));
|
||||||
if (config.isEmpty()) {
|
if (config.isEmpty()) {
|
||||||
logger.debug("Not allowed with id: " + serverId);
|
logger.debug("Not allowed with id: " + serverId);
|
||||||
return false;
|
return false;
|
||||||
|
@ -71,7 +73,7 @@ public class ServerMessageHandler implements MessageHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exceedsRate(MessageCreateEvent event) {
|
public boolean exceedsRate(MessageCreateEvent event) {
|
||||||
var serverId = event.getServer().get().getId();
|
var serverId = String.valueOf(event.getServer().get().getId());
|
||||||
var config = this.serverDBService.getConfig(serverId);
|
var config = this.serverDBService.getConfig(serverId);
|
||||||
if (config.isEmpty()) {
|
if (config.isEmpty()) {
|
||||||
logger.error("Missing configuration for server with id: " + serverId);
|
logger.error("Missing configuration for server with id: " + serverId);
|
||||||
|
@ -92,7 +94,7 @@ public class ServerMessageHandler implements MessageHandler {
|
||||||
var serverId = event.getServer().map(DiscordEntity::getId).get();
|
var serverId = event.getServer().map(DiscordEntity::getId).get();
|
||||||
var userId = event.getMessageAuthor().getId();
|
var userId = event.getMessageAuthor().getId();
|
||||||
|
|
||||||
var serverMessage = new ServerMessage.NewServerMessage(serverId, userId, tokens);
|
var serverMessage = new ServerMessage.NewServerMessage(String.valueOf(serverId), userId, tokens);
|
||||||
this.serverDBService.addMessage(serverMessage);
|
this.serverDBService.addMessage(serverMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,7 +24,9 @@ public class UserMessageHandler implements MessageHandler {
|
||||||
@Override
|
@Override
|
||||||
public void handle(MessageCreateEvent event) {
|
public void handle(MessageCreateEvent event) {
|
||||||
String content = event.getReadableMessageContent();
|
String content = event.getReadableMessageContent();
|
||||||
var request = new ChatGPTRequestBuilder().simpleRequest(content);
|
var userId = event.getMessageAuthor().getId();
|
||||||
|
var systemMessage = this.userDBService.getConfig(String.valueOf(userId)).get().systemMessage();
|
||||||
|
var request = new ChatGPTRequestBuilder().simpleRequest(content, systemMessage);
|
||||||
try {
|
try {
|
||||||
var response = this.chatGPTService.submit(request);
|
var response = this.chatGPTService.submit(request);
|
||||||
if (response.choices().size() < 1) {
|
if (response.choices().size() < 1) {
|
||||||
|
@ -47,7 +49,7 @@ public class UserMessageHandler implements MessageHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
var userId = event.getMessageAuthor().getId();
|
var userId = event.getMessageAuthor().getId();
|
||||||
var config = this.userDBService.getConfig(userId);
|
var config = this.userDBService.getConfig(String.valueOf(userId));
|
||||||
if (config.isEmpty()) {
|
if (config.isEmpty()) {
|
||||||
logger.debug("Not allowed with id: " + userId);
|
logger.debug("Not allowed with id: " + userId);
|
||||||
return false;
|
return false;
|
||||||
|
@ -57,7 +59,7 @@ public class UserMessageHandler implements MessageHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean exceedsRate(MessageCreateEvent event) {
|
public boolean exceedsRate(MessageCreateEvent event) {
|
||||||
var userId = event.getMessageAuthor().getId();
|
var userId = String.valueOf(event.getMessageAuthor().getId());
|
||||||
var config = this.userDBService.getConfig(userId);
|
var config = this.userDBService.getConfig(userId);
|
||||||
if (config.isEmpty()) {
|
if (config.isEmpty()) {
|
||||||
logger.error("Missing configuration for userId with id: " + userId);
|
logger.error("Missing configuration for userId with id: " + userId);
|
||||||
|
@ -77,7 +79,7 @@ public class UserMessageHandler implements MessageHandler {
|
||||||
private void logUserMessage(MessageCreateEvent event, String question, String answer, int tokens) {
|
private void logUserMessage(MessageCreateEvent event, String question, String answer, int tokens) {
|
||||||
var userId = event.getMessageAuthor().getId();
|
var userId = event.getMessageAuthor().getId();
|
||||||
|
|
||||||
var userMessage = new UserMessage.NewUserMessage(userId, question, answer, tokens);
|
var userMessage = new UserMessage.NewUserMessage(String.valueOf(userId), question, answer, tokens);
|
||||||
this.userDBService.addMessage(userMessage);
|
this.userDBService.addMessage(userMessage);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,14 +7,11 @@ import java.util.List;
|
||||||
|
|
||||||
public class ChatGPTRequestBuilder {
|
public class ChatGPTRequestBuilder {
|
||||||
private final String model = "gpt-3.5-turbo";
|
private final String model = "gpt-3.5-turbo";
|
||||||
private final String systemMessage = """
|
|
||||||
You are Jarvis, a helpful and friendly AI. People interact with you over Discord, a chatting platform. Your default language to answer is German. You format your responses in markdown and your answers don´t need to be formal.
|
|
||||||
""";
|
|
||||||
|
|
||||||
public ChatGPTRequestBuilder() {
|
public ChatGPTRequestBuilder() {
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatGPTRequest simpleRequest(String content) {
|
public ChatGPTRequest simpleRequest(String content, String systemMessage) {
|
||||||
return new ChatGPTRequest(
|
return new ChatGPTRequest(
|
||||||
model,
|
model,
|
||||||
List.of(new ChatGPTRequest.Message("system", systemMessage), new ChatGPTRequest.Message("user", content)),
|
List.of(new ChatGPTRequest.Message("system", systemMessage), new ChatGPTRequest.Message("user", content)),
|
||||||
|
@ -22,7 +19,7 @@ public class ChatGPTRequestBuilder {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatGPTRequest contextRequest(List<String> contextMessages, String message) {
|
public ChatGPTRequest contextRequest(List<String> contextMessages, String message, String systemMessage) {
|
||||||
List<ChatGPTRequest.Message> messages = new ArrayList<>();
|
List<ChatGPTRequest.Message> messages = new ArrayList<>();
|
||||||
messages.add(new ChatGPTRequest.Message("system", systemMessage));
|
messages.add(new ChatGPTRequest.Message("system", systemMessage));
|
||||||
var context = contextMessages.stream()
|
var context = contextMessages.stream()
|
||||||
|
|
|
@ -106,3 +106,19 @@ DROP TABLE IF EXISTS allowed_servers;
|
||||||
DROP TABLE IF EXISTS allowed_users;
|
DROP TABLE IF EXISTS allowed_users;
|
||||||
|
|
||||||
COMMIT;
|
COMMIT;
|
||||||
|
|
||||||
|
BEGIN;
|
||||||
|
|
||||||
|
ALTER TABLE server_configs
|
||||||
|
ALTER COLUMN server_id TYPE text USING server_id::text;
|
||||||
|
|
||||||
|
ALTER TABLE server_messages
|
||||||
|
ALTER COLUMN server_id TYPE text USING server_id::text;
|
||||||
|
|
||||||
|
ALTER TABLE user_configs
|
||||||
|
ALTER COLUMN user_id TYPE text USING user_id::text;
|
||||||
|
|
||||||
|
ALTER TABLE user_messages
|
||||||
|
ALTER COLUMN user_id TYPE text USING user_id::text;
|
||||||
|
|
||||||
|
COMMIT;
|
||||||
|
|
|
@ -20,7 +20,7 @@ const submitAction = async () => {
|
||||||
<h1 class="text-xl normal-case">Add Server Config</h1>
|
<h1 class="text-xl normal-case">Add Server Config</h1>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">Server ID</label>
|
<label class="mr-4">Server ID</label>
|
||||||
<input v-model.number="newConfig.serverId" class="input input-bordered w-full max-w-xs" />
|
<input v-model="newConfig.serverId" class="input input-bordered w-full max-w-xs" />
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">System Message</label>
|
<label class="mr-4">System Message</label>
|
||||||
|
|
|
@ -27,7 +27,7 @@ const modalId = "edit-" + id
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">Server ID</label>
|
<label class="mr-4">Server ID</label>
|
||||||
<input v-model.number="configToEdit.serverId" class="input input-bordered w-full max-w-xs" />
|
<input v-model="configToEdit.serverId" class="input input-bordered w-full max-w-xs" />
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">System Message</label>
|
<label class="mr-4">System Message</label>
|
||||||
|
|
|
@ -17,7 +17,7 @@ const submitAction = async () => {
|
||||||
<h1 class="text-xl normal-case">Add User Config</h1>
|
<h1 class="text-xl normal-case">Add User Config</h1>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">User ID</label>
|
<label class="mr-4">User ID</label>
|
||||||
<input v-model.number="newConfig.userId" class="input input-bordered w-full max-w-xs" />
|
<input v-model="newConfig.userId" class="input input-bordered w-full max-w-xs" />
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">System Message</label>
|
<label class="mr-4">System Message</label>
|
||||||
|
|
|
@ -19,14 +19,14 @@ const modalId = "edit-" + id
|
||||||
<template>
|
<template>
|
||||||
<ModalComponent :modalId="modalId" :openModal="{ class: 'btn btn-sm btn-info m-1', label: 'Edit' }"
|
<ModalComponent :modalId="modalId" :openModal="{ class: 'btn btn-sm btn-info m-1', label: 'Edit' }"
|
||||||
:submitAction="submitAction" :onLoadAction="onLoadAction">
|
:submitAction="submitAction" :onLoadAction="onLoadAction">
|
||||||
<h1 class="text-xl normal-case">Edit Server Config</h1>
|
<h1 class="text-xl normal-case">Edit User Config</h1>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">ID</label>
|
<label class="mr-4">ID</label>
|
||||||
<input v-model.number="configToEdit.id" class="input input-bordered w-full max-w-xs" disabled />
|
<input v-model.number="configToEdit.id" class="input input-bordered w-full max-w-xs" disabled />
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">Server ID</label>
|
<label class="mr-4">User ID</label>
|
||||||
<input v-model.number="configToEdit.userId" class="input input-bordered w-full max-w-xs" />
|
<input v-model="configToEdit.userId" class="input input-bordered w-full max-w-xs" />
|
||||||
</div>
|
</div>
|
||||||
<div class="m-2">
|
<div class="m-2">
|
||||||
<label class="mr-4">System Message</label>
|
<label class="mr-4">System Message</label>
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export type ServerConfig = {
|
export type ServerConfig = {
|
||||||
id: number,
|
id: number,
|
||||||
serverId: number,
|
serverId: string,
|
||||||
systemMessage: string,
|
systemMessage: string,
|
||||||
rateLimit: number,
|
rateLimit: number,
|
||||||
time: string
|
time: string
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
export type UserConfig = {
|
export type UserConfig = {
|
||||||
id: number,
|
id: number,
|
||||||
userId: number,
|
userId: string,
|
||||||
systemMessage: string,
|
systemMessage: string,
|
||||||
contextLength: number,
|
contextLength: number,
|
||||||
rateLimit: number,
|
rateLimit: number,
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import type { ServerConfig } from "@/models/server";
|
import type { ServerConfig } from "@/models/server";
|
||||||
|
|
||||||
const configUrl = "/api/servers/configs/"
|
const configUrl = "http://localhost:7070/api/servers/configs/"
|
||||||
|
|
||||||
export async function getConfigs(): Promise<ServerConfig[]> {
|
export async function getConfigs(): Promise<ServerConfig[]> {
|
||||||
return fetch(configUrl)
|
return fetch(configUrl)
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
import type { UserConfig } from '@/models/user'
|
import type { UserConfig } from '@/models/user'
|
||||||
|
|
||||||
const configUrl = "/api/users/configs/"
|
const configUrl = "http://localhost:7070/api/users/configs/"
|
||||||
|
|
||||||
export async function getConfigs(): Promise<UserConfig[]> {
|
export async function getConfigs(): Promise<UserConfig[]> {
|
||||||
return fetch(configUrl)
|
return fetch(configUrl)
|
||||||
|
|
Loading…
Reference in a new issue