Compare commits

..

No commits in common. "a398641172fae4755781cc7d46223c1c36dfa5e6" and "7d4ff3ce64aea2d776eb0bced9acef2e895763cc" have entirely different histories.

20 changed files with 57 additions and 75 deletions

View file

@ -56,7 +56,7 @@ public class DiscordBot implements Runnable {
if (server.isEmpty()) { if (server.isEmpty()) {
return; return;
} }
var tokens = this.serverDBService.tokensOfLast30Days(String.valueOf(server.get().getId())); var tokens = this.serverDBService.tokensOfLast30Days(server.get().getId());
interactionOriginalResponseUpdater.setContent("" + tokens).update(); interactionOriginalResponseUpdater.setContent("" + tokens).update();
}); });
return; return;

View file

@ -24,7 +24,7 @@ public class ServerDBService {
this.password = password; this.password = password;
} }
public Optional<ServerConfig> getConfig(String serverId) { public Optional<ServerConfig> getConfig(long 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.setString(1, serverId); pstmt.setLong(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.setString(1, newServerConfig.serverId()); pstmt.setLong(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.setString(3, newServerConfig.serverId()); pstmt.setLong(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(String serverId) { public int countMessagesInLastMinute(long 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.setString(1, serverId); pstmt.setLong(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.setString(1, serverMessage.serverId()); pstmt.setLong(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(String serverId) { public long tokensOfLast30Days(long 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.setString(1, serverId); pstmt.setLong(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)));

View file

@ -24,7 +24,7 @@ public class UserDBService {
this.password = password; this.password = password;
} }
public Optional<UserConfig> getConfig(String userId) { public Optional<UserConfig> getConfig(long 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.setString(1, userId); pstmt.setLong(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.setString(1, newUserConfig.userId()); pstmt.setLong(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 = ?, user_id = ? WHERE id = ? UPDATE user_configs SET system_message = ?, context_length = ?, rate_limit = ? 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,8 +114,7 @@ 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.setString(4, newUserConfig.userId()); pstmt.setLong(4, id);
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);
@ -143,7 +142,7 @@ public class UserDBService {
} }
} }
public int countMessagesInLastMinute(String userId) { public int countMessagesInLastMinute(long 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 >= ?
"""; """;
@ -151,7 +150,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.setString(1, userId); pstmt.setLong(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)));
@ -175,7 +174,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.setString(1, newUserMessage.userId()); pstmt.setLong(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());
@ -188,7 +187,7 @@ public class UserDBService {
} }
} }
public long tokensOfLast30Days(String userId) { public long tokensOfLast30Days(long 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 >= ?
"""; """;
@ -196,7 +195,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.setString(1, userId); pstmt.setLong(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)));

View file

@ -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, String serverId, String systemMessage, int rateLimit, Instant time) { public record ServerConfig(long id, long 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.getString("server_id"); var serverId = resultSet.getLong("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, String serverId, String systemMessage, int r
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public record NewServerConfig(String serverId, String systemMessage, @Nullable int rateLimit) { public record NewServerConfig(long serverId, String systemMessage, @Nullable int rateLimit) {
} }
} }

View file

@ -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, String serverId, long userId, int tokens, Instant time) { public record ServerMessage(long id, long 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.getString("server_id"); var serverId = resultSet.getLong("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, String serverId, long userId, int tokens, I
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public record NewServerMessage(String serverId, long userId, int tokens) { public record NewServerMessage(long serverId, long userId, int tokens) {
} }
} }

View file

@ -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, String userId, String systemMessage, int contextLength, int rateLimit, Instant time) { public record UserConfig(long id, long 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.getString("user_id"); var userId = resultSet.getLong("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, String userId, String systemMessage, int conte
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public record NewUserConfig(String userId, String systemMessage, @Nullable int contextLength, public record NewUserConfig(long userId, String systemMessage, @Nullable int contextLength,
@Nullable int rateLimit) { @Nullable int rateLimit) {
} }
} }

View file

@ -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, String userId, String question, String answer, int tokens, Instant time) { public record UserMessage(long id, long 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.getString("user_id"); var userId = resultSet.getLong("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, String userId, String question, String answer
} }
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public record NewUserMessage(String userId, String question, String answer, int tokens) { public record NewUserMessage(long userId, String question, String answer, int tokens) {
} }
} }

View file

@ -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..."); event.getChannel().sendMessage("Rate limit hit - cooling down now...");
return; return;
} }
this.messageHandler.handle(event); this.messageHandler.handle(event);

View file

@ -29,8 +29,6 @@ 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()
@ -39,8 +37,8 @@ public class ServerMessageHandler implements MessageHandler {
.flatMap(m -> m) .flatMap(m -> m)
.map(Message::getReadableContent) .map(Message::getReadableContent)
.stream().toList(), .stream().toList(),
content, systemMessage) : content) :
new ChatGPTRequestBuilder().simpleRequest(content, systemMessage); new ChatGPTRequestBuilder().simpleRequest(content);
try { try {
var response = this.chatGPTService.submit(request); var response = this.chatGPTService.submit(request);
if (response.choices().size() < 1) { if (response.choices().size() < 1) {
@ -63,7 +61,7 @@ public class ServerMessageHandler implements MessageHandler {
} }
var serverId = event.getServer().get().getId(); var serverId = event.getServer().get().getId();
var config = this.serverDBService.getConfig(String.valueOf(serverId)); var config = this.serverDBService.getConfig(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;
@ -73,7 +71,7 @@ public class ServerMessageHandler implements MessageHandler {
@Override @Override
public boolean exceedsRate(MessageCreateEvent event) { public boolean exceedsRate(MessageCreateEvent event) {
var serverId = String.valueOf(event.getServer().get().getId()); var serverId = 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);
@ -94,7 +92,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(String.valueOf(serverId), userId, tokens); var serverMessage = new ServerMessage.NewServerMessage(serverId, userId, tokens);
this.serverDBService.addMessage(serverMessage); this.serverDBService.addMessage(serverMessage);
} }

View file

@ -24,9 +24,7 @@ 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 userId = event.getMessageAuthor().getId(); var request = new ChatGPTRequestBuilder().simpleRequest(content);
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) {
@ -49,7 +47,7 @@ public class UserMessageHandler implements MessageHandler {
} }
var userId = event.getMessageAuthor().getId(); var userId = event.getMessageAuthor().getId();
var config = this.userDBService.getConfig(String.valueOf(userId)); var config = this.userDBService.getConfig(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;
@ -59,7 +57,7 @@ public class UserMessageHandler implements MessageHandler {
@Override @Override
public boolean exceedsRate(MessageCreateEvent event) { public boolean exceedsRate(MessageCreateEvent event) {
var userId = String.valueOf(event.getMessageAuthor().getId()); var userId = 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);
@ -79,7 +77,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(String.valueOf(userId), question, answer, tokens); var userMessage = new UserMessage.NewUserMessage(userId, question, answer, tokens);
this.userDBService.addMessage(userMessage); this.userDBService.addMessage(userMessage);
} }
} }

View file

@ -7,11 +7,14 @@ 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, String systemMessage) { public ChatGPTRequest simpleRequest(String content) {
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)),
@ -19,7 +22,7 @@ public class ChatGPTRequestBuilder {
); );
} }
public ChatGPTRequest contextRequest(List<String> contextMessages, String message, String systemMessage) { public ChatGPTRequest contextRequest(List<String> contextMessages, String message) {
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()

View file

@ -106,19 +106,3 @@ 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;

View file

@ -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="newConfig.serverId" class="input input-bordered w-full max-w-xs" /> <input v-model.number="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>

View file

@ -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="configToEdit.serverId" class="input input-bordered w-full max-w-xs" /> <input v-model.number="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>

View file

@ -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="newConfig.userId" class="input input-bordered w-full max-w-xs" /> <input v-model.number="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>

View file

@ -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 User Config</h1> <h1 class="text-xl normal-case">Edit Server 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">User ID</label> <label class="mr-4">Server ID</label>
<input v-model="configToEdit.userId" class="input input-bordered w-full max-w-xs" /> <input v-model.number="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>

View file

@ -1,6 +1,6 @@
export type ServerConfig = { export type ServerConfig = {
id: number, id: number,
serverId: string, serverId: number,
systemMessage: string, systemMessage: string,
rateLimit: number, rateLimit: number,
time: string time: string

View file

@ -1,6 +1,6 @@
export type UserConfig = { export type UserConfig = {
id: number, id: number,
userId: string, userId: number,
systemMessage: string, systemMessage: string,
contextLength: number, contextLength: number,
rateLimit: number, rateLimit: number,

View file

@ -1,6 +1,6 @@
import type { ServerConfig } from "@/models/server"; import type { ServerConfig } from "@/models/server";
const configUrl = "http://localhost:7070/api/servers/configs/" const configUrl = "/api/servers/configs/"
export async function getConfigs(): Promise<ServerConfig[]> { export async function getConfigs(): Promise<ServerConfig[]> {
return fetch(configUrl) return fetch(configUrl)

View file

@ -1,6 +1,6 @@
import type { UserConfig } from '@/models/user' import type { UserConfig } from '@/models/user'
const configUrl = "http://localhost:7070/api/users/configs/" const configUrl = "/api/users/configs/"
export async function getConfigs(): Promise<UserConfig[]> { export async function getConfigs(): Promise<UserConfig[]> {
return fetch(configUrl) return fetch(configUrl)