Skip to content

Commit

Permalink
major db rework, moving to psql
Browse files Browse the repository at this point in the history
  • Loading branch information
Ifiht committed Sep 11, 2024
1 parent 7aa7bc4 commit 299324d
Show file tree
Hide file tree
Showing 6 changed files with 151 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/modules/chat/chat.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ def format_question(prompt)
"stream" => false, # keep false, breaks if true
"seed" => i, # Set the random number generator (RNG) seed.
"n_predict" => 500, # notes
"temperature" => 0.54, # was:0, def:0-1, higher is more creative (0.49 failed to answer question about unicorns)
"temperature" => 0.56, # was:0, def:0-1, higher is more creative (0.49 failed to answer question about unicorns)
"stop" => ["\n@User:"], # notes
"repeat_last_n" => 128, # Last n tokens to consider for penalizing repetition. 0 is disabled and -1 is ctx-size.
"repeat_penalty" => 1.1, # Control the repetition of token sequences in the generated text.
"top_k" => 32, # def:40, Limit the next token selection to the K most probable tokens.
"top_p" => 0.9, # def:0.95, higher finds better predictions, but slower
"repeat_penalty" => 1.2, # Control the repetition of token sequences in the generated text.
"top_k" => 34, # def:40, Limit the next token selection to the K most probable tokens.
"top_p" => 0.92, # def:0.95, higher finds better predictions, but slower
"min_p" => 0.06, # def:0.05, The minimum probability for a token to be considered, relative to the probability of the most likely token.
"tfs_z" => 1, # def:1(disabled) https://www.trentonbricken.com/Tail-Free-Sampling/
"typical_p" => 1, # def:1(disabled)
Expand Down
Empty file added src/modules/database/README.md
Empty file.
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions src/modules/database/init_db.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
require 'sqlite3'
# OS Requirements:
#> sudo apt install -y postgresql-common
#> sudo /usr/share/postgresql-common/pgdg/apt.postgresql.org.sh
#> sudo apt install postgresql-16

# Open a database
db = SQLite3::Database.new "XPlatform_Chat.db"

# Example: Insert a new participant
db.execute("INSERT INTO Personas (name, type) VALUES (?, ?)", ["John Doe", "human"])
participant_id = db.last_insert_row_id

# Example: Insert participant identities for different platforms
platforms = {
"discord" => "john_doe#1234",
"twitter" => "@john_doe",
"speech_to_text" => "voice_profile_123"
}

platforms.each do |platform, platform_id|
db.execute("INSERT INTO participant_identities (participant_id, platform, platform_id) VALUES (?, ?, ?)",
[participant_id, platform, platform_id])
end

# Example: Insert an AI participant
db.execute("INSERT INTO Personas (name, type) VALUES (?, ?)", ["Claude", "ai"])
ai_participant_id = db.last_insert_row_id
db.execute("INSERT INTO participant_identities (participant_id, platform, platform_id) VALUES (?, ?, ?)",
[ai_participant_id, "anthropic", "claude_3.5"])

# Example: Create a conversation on Discord
db.execute("INSERT INTO Conversations (title, platform) VALUES (?, ?)", ["Discord Chat", "discord"])
conversation_id = db.last_insert_row_id

# Get participant identity IDs
discord_identity_id = db.get_first_value("SELECT id FROM participant_identities WHERE participant_id = ? AND platform = ?",
[participant_id, "discord"])
ai_identity_id = db.get_first_value("SELECT id FROM participant_identities WHERE participant_id = ?", [ai_participant_id])

# Link Personas to the conversation
db.execute("INSERT INTO conversation_participants (conversation_id, participant_identity_id) VALUES (?, ?)",
[conversation_id, discord_identity_id])
db.execute("INSERT INTO conversation_participants (conversation_id, participant_identity_id) VALUES (?, ?)",
[conversation_id, ai_identity_id])

# Example: Insert messages
db.execute("INSERT INTO messages (conversation_id, participant_identity_id, content) VALUES (?, ?, ?)",
[conversation_id, discord_identity_id, "Hey Claude, how's it going?"])
db.execute("INSERT INTO messages (conversation_id, participant_identity_id, content) VALUES (?, ?, ?)",
[conversation_id, ai_identity_id, "Hello! I'm doing well, thank you for asking. How can I assist you today?"])

# Close the database
db.close
93 changes: 93 additions & 0 deletions src/modules/database/init_pgre.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* shikigami.sql skeleton database
*/

BEGIN;

-- Drop existing tables if they exist
DROP TABLE IF EXISTS ConversationParticipants;
DROP TABLE IF EXISTS Messages;
DROP TABLE IF EXISTS PlatformIdentities;
DROP TABLE IF EXISTS Personas;
DROP TABLE IF EXISTS Conversations;

-- Create Conversations table
CREATE TABLE Conversations (
id SERIAL PRIMARY KEY, -- Use SERIAL for auto-incrementing IDs
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, -- DATETIME == TIMESTAMP for PostgreSQL
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
title TEXT,
platform TEXT NOT NULL
);

-- Create Personas table
CREATE TABLE Personas (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
type TEXT NOT NULL
);

-- Create PlatformIdentities table
CREATE TABLE PlatformIdentities (
id SERIAL PRIMARY KEY,
participant_id INTEGER NOT NULL,
platform TEXT NOT NULL,
platform_id TEXT NOT NULL,
FOREIGN KEY (participant_id) REFERENCES Personas(id) ON DELETE CASCADE, -- Enforce foreign key constraint
UNIQUE (platform, platform_id)
);

-- Create Messages table
CREATE TABLE Messages (
id SERIAL PRIMARY KEY,
conversation_id INTEGER NOT NULL,
persona_platform_id INTEGER NOT NULL,
content TEXT NOT NULL,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES Conversations(id) ON DELETE CASCADE, -- ON DELETE CASCADE ensures consistency
FOREIGN KEY (persona_platform_id) REFERENCES PlatformIdentities(id) ON DELETE CASCADE
);

-- Create ConversationParticipants table
-- + many-to-many junction table
-- + tracks who is involved in which conversations
CREATE TABLE ConversationParticipants (
conversation_id INTEGER NOT NULL,
participant_id INTEGER NOT NULL,
joined_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (conversation_id) REFERENCES Conversations(id) ON DELETE CASCADE,
FOREIGN KEY (participant_id) REFERENCES PlatformIdentities(id) ON DELETE CASCADE,
PRIMARY KEY (conversation_id, participant_id)
);

-- Create Indexes for Foreign Keys to improve query perf:
CREATE INDEX idx_conversation_id ON Messages(conversation_id);
CREATE INDEX idx_persona_platform_id ON Messages(persona_platform_id);
CREATE INDEX idx_conversation_participants_conversation ON ConversationParticipants(conversation_id);
CREATE INDEX idx_conversation_participants_participant ON ConversationParticipants(participant_id);

-- Function to update each conversation according to latest activity:
CREATE OR REPLACE FUNCTION update_conversation_timestamp()
RETURNS TRIGGER AS $$
BEGIN
UPDATE Conversations
SET updated_at = NOW()
WHERE id = NEW.conversation_id;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Latest activity trigger for Messages table
CREATE TRIGGER update_conversation_on_new_message
AFTER INSERT ON Messages
FOR EACH ROW
EXECUTE FUNCTION update_conversation_timestamp();

-- Latest activity trigger for ConversationParticipants table
CREATE TRIGGER update_conversation_on_new_participant
AFTER INSERT ON ConversationParticipants
FOR EACH ROW
EXECUTE FUNCTION update_conversation_timestamp();

COMMIT;
-- .quit

0 comments on commit 299324d

Please sign in to comment.