Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

soft delete msg, prompt #67

Merged
merged 4 commits into from
Apr 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 21 additions & 12 deletions api/sqlc/queries/chat_message.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
-- name: GetAllChatMessages :many
SELECT * FROM chat_message ORDER BY id;
SELECT * FROM chat_message
WHERE is_deleted = false
ORDER BY id;

-- name: GetChatMessagesBySessionUUID :many
SELECT cm.*
FROM chat_message cm
INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid
WHERE cs.active = true and cs.uuid = $1
WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1
ORDER BY cm.id
OFFSET $2
LIMIT $3;
Expand All @@ -15,14 +17,15 @@ LIMIT $3;
SELECT cm.*
FROM chat_message cm
INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid
WHERE cs.active = true and cs.uuid = $1
WHERE cm.is_deleted = false and cs.active = true and cs.uuid = $1
ORDER BY cm.id
OFFSET $2
LIMIT $1;


-- name: GetChatMessageByID :one
SELECT * FROM chat_message WHERE id = $1;
SELECT * FROM chat_message
WHERE is_deleted = false and id = $1;


-- name: CreateChatMessage :one
Expand All @@ -36,12 +39,15 @@ WHERE id = $1
RETURNING *;

-- name: DeleteChatMessage :exec
DELETE FROM chat_message WHERE id = $1;
UPDATE chat_message set is_deleted = true, updated_at = now()
WHERE id = $1;

-- by uuid

---- UUID ----

-- name: GetChatMessageByUUID :one
SELECT * FROM chat_message WHERE uuid = $1;
SELECT * FROM chat_message
WHERE is_deleted = false and uuid = $1;

-- name: CreateChatMessageByUUID :one
INSERT INTO chat_message (uuid, chat_session_uuid, role, content, score, user_id, created_by, updated_by, raw)
Expand All @@ -54,15 +60,16 @@ WHERE uuid = $1
RETURNING *;

-- name: DeleteChatMessageByUUID :exec
DELETE FROM chat_message WHERE uuid = $1;
UPDATE chat_message SET is_deleted = true, updated_at = now()
WHERE uuid = $1;


-- name: HasChatMessagePermission :one
SELECT COUNT(*) > 0 as has_permission
FROM chat_message cm
INNER JOIN chat_session cs ON cm.chat_session_uuid = cs.uuid
INNER JOIN auth_user au ON cs.user_id = au.id
WHERE cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true;
WHERE cm.is_deleted = false and cm.id = $1 AND (cs.user_id = $2 OR au.is_superuser) and cs.active = true;


-- name: GetLatestMessagesBySessionUUID :many
Expand All @@ -72,7 +79,7 @@ Where chat_message.id in
(
SELECT chat_message.id
FROM chat_message
WHERE chat_message.chat_session_uuid = $1
WHERE chat_message.chat_session_uuid = $1 and chat_message.is_deleted = false
ORDER BY created_at DESC
LIMIT $2
)
Expand All @@ -82,7 +89,7 @@ ORDER BY created_at;
-- name: GetFirstMessageBySessionUUID :one
SELECT *
FROM chat_message
WHERE chat_session_uuid = $1
WHERE chat_session_uuid = $1 and is_deleted = false
ORDER BY created_at
LIMIT 1;

Expand All @@ -94,6 +101,7 @@ WHERE chat_message.id in (
FROM chat_message cm
WHERE cm.chat_session_uuid = $3
AND cm.id < (SELECT id FROM chat_message WHERE chat_message.uuid = $1)
AND cm.is_deleted = false
ORDER BY cm.created_at DESC
LIMIT $2
)
Expand All @@ -107,7 +115,8 @@ WHERE uuid = $1 ;


-- name: DeleteChatMessagesBySesionUUID :exec
DELETE FROM chat_message
UPDATE chat_message
SET is_deleted = true, updated_at = now()
WHERE chat_session_uuid = $1;


Expand Down
27 changes: 17 additions & 10 deletions api/sqlc/queries/chat_prompt.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
-- name: GetAllChatPrompts :many
SELECT * FROM chat_prompt ORDER BY id;
SELECT * FROM chat_prompt
WHERE is_deleted = false
ORDER BY id;

-- name: GetChatPromptByID :one
SELECT * FROM chat_prompt WHERE id = $1;
SELECT * FROM chat_prompt
WHERE is_deleted = false and id = $1;

-- name: CreateChatPrompt :one
INSERT INTO chat_prompt (uuid, chat_session_uuid, role, content, user_id, created_by, updated_by)
Expand All @@ -16,35 +19,37 @@ RETURNING *;

-- name: UpdateChatPromptByUUID :one
UPDATE chat_prompt SET content = $2, updated_at = now()
WHERE uuid = $1
WHERE uuid = $1 and is_deleted = false
RETURNING *;

-- name: DeleteChatPrompt :exec
DELETE FROM chat_prompt WHERE id = $1;
UPDATE chat_prompt
SET is_deleted = true, updated_at = now()
WHERE id = $1;

-- name: GetChatPromptsByUserID :many
SELECT *
FROM chat_prompt
WHERE user_id = $1
WHERE user_id = $1 and is_deleted = false
ORDER BY id;

-- name: GetChatPromptsBysession_uuid :many
SELECT *
FROM chat_prompt
WHERE chat_session_uuid = $1
WHERE chat_session_uuid = $1 and is_deleted = false
ORDER BY id;


-- name: GetChatPromptsBySessionUUID :many
SELECT *
FROM chat_prompt
WHERE chat_session_uuid = $1
WHERE chat_session_uuid = $1 and is_deleted = false
ORDER BY id;

-- name: GetOneChatPromptBySessionUUID :one
SELECT *
FROM chat_prompt
WHERE chat_session_uuid = $1
WHERE chat_session_uuid = $1 and is_deleted = false
ORDER BY id
LIMIT 1;

Expand All @@ -55,8 +60,10 @@ LIMIT 1;
SELECT COUNT(*) > 0 as has_permission
FROM chat_prompt cp
INNER JOIN auth_user au ON cp.user_id = au.id
WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser);
WHERE cp.id = $1 AND (cp.user_id = $2 OR au.is_superuser) AND cp.is_deleted = false;


-- name: DeleteChatPromptByUUID :exec
DELETE FROM chat_prompt WHERE uuid = $1;
UPDATE chat_prompt
SET is_deleted = true, updated_at = now()
WHERE uuid = $1;
15 changes: 12 additions & 3 deletions api/sqlc/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ CREATE TABLE IF NOT EXISTS chat_message (
updated_at timestamp DEFAULT now() Not NULL,
created_by integer NOT NULL,
updated_by integer NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false,
raw jsonb default '{}' NOT NULL

);

-- alter table chat_message add column chat_session_uuid character varying(255) NOT NULL DEFAULT '';
Expand All @@ -68,7 +68,8 @@ CREATE TABLE IF NOT EXISTS chat_prompt (
created_at timestamp DEFAULT now() NOT NULL ,
updated_at timestamp DEFAULT now() NOT NULL,
created_by integer NOT NULL,
updated_by integer NOT NULL
updated_by integer NOT NULL,
is_deleted BOOLEAN NOT NULL DEFAULT false
-- raw jsonb default '{}' NOT NULL
);

Expand All @@ -94,8 +95,16 @@ CREATE TABLE IF NOT EXISTS user_active_chat_session (

-- ALTER TABLE chat_prompt RENAME COLUMN topic TO session_uuid;

-- chat_session
ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS temperature float DEFAULT 1.0 NOT NULL;
ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS top_p float DEFAULT 1.0 NOT NULL;
ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS max_tokens int DEFAULT 512 NOT NULL;
ALTER TABLE chat_session ADD COLUMN IF NOT EXISTS debug boolean DEFAULT false NOT NULL;
ALTER TABlE chat_session ADD COLUMN IF NOT EXISTS model character varying(255) NOT NULL DEFAULT 'gpt-3.5-turbo';
ALTER TABlE chat_session ADD COLUMN IF NOT EXISTS model character varying(255) NOT NULL DEFAULT 'gpt-3.5-turbo';


-- chat_messages
ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false;

-- chat prompt
ALTER TABLE chat_prompt ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL DEFAULT false;
Loading