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

create bot #513

Merged
merged 6 commits into from
Aug 3, 2024
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
8 changes: 4 additions & 4 deletions .github/workflows/docker-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ jobs:
- name: change docker image tag
run: python scripts/replace_tag_to_test.py ${GITHUB_SHA}

- name: docker-compose
run: docker-compose up -d
- name: docker compose
run: docker compose up -d

- name: show docker ps
run: docker-compose ps
run: docker compose ps

- name: show docker logs
run: docker-compose logs
run: docker compose logs

# Setup cache for node_modules
- name: Cache node modules
Expand Down
19 changes: 19 additions & 0 deletions api/chat_snapshot_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func (h *ChatSnapshotHandler) Register(router *mux.Router) {
router.HandleFunc("/uuid/chat_snapshot/{uuid}", h.UpdateChatSnapshotMetaByUUID).Methods(http.MethodPut)
router.HandleFunc("/uuid/chat_snapshot/{uuid}", h.DeleteChatSnapshot).Methods(http.MethodDelete)
router.HandleFunc("/uuid/chat_snapshot_search", h.ChatSnapshotSearch).Methods(http.MethodGet)
router.HandleFunc("/uuid/chat_bot/{uuid}", h.CreateChatBot).Methods(http.MethodPost)
}

func (h *ChatSnapshotHandler) CreateChatSnapshot(w http.ResponseWriter, r *http.Request) {
Expand All @@ -46,6 +47,24 @@ func (h *ChatSnapshotHandler) CreateChatSnapshot(w http.ResponseWriter, r *http.

}

func (h *ChatSnapshotHandler) CreateChatBot(w http.ResponseWriter, r *http.Request) {
chatSessionUuid := mux.Vars(r)["uuid"]
user_id, err := getUserID(r.Context())
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error(), err)
return
}
uuid, err := h.service.CreateChatBot(r.Context(), chatSessionUuid, user_id)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, err.Error(), err)
}
json.NewEncoder(w).Encode(
map[string]interface{}{
"uuid": uuid,
})

}

func (h *ChatSnapshotHandler) GetChatSnapshot(w http.ResponseWriter, r *http.Request) {
uuidStr := mux.Vars(r)["uuid"]
snapshot, err := h.service.q.ChatSnapshotByUUID(r.Context(), uuidStr)
Expand Down
44 changes: 44 additions & 0 deletions api/chat_snapshot_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,47 @@ func (s *ChatSnapshotService) CreateChatSnapshot(ctx context.Context, chatSessio
return one.Uuid, nil

}


func (s *ChatSnapshotService) CreateChatBot(ctx context.Context, chatSessionUuid string, userId int32) (string, error) {
chatSession, err := s.q.GetChatSessionByUUID(ctx, chatSessionUuid)
if err != nil {
return "", err
}
// TODO: fix hardcode
simple_msgs, err := s.q.GetChatHistoryBySessionUUID(ctx, chatSessionUuid, int32(1), int32(10000))
text := lo.Reduce(simple_msgs, func(acc string, curr sqlc_queries.SimpleChatMessage, _ int) string {
return acc + curr.Text
}, "")
// save all simple_msgs to a jsonb field in chat_snapshot
if err != nil {
return "", err
}
// simple_msgs to RawMessage
simple_msgs_raw, err := json.Marshal(simple_msgs)
if err != nil {
return "", err
}
snapshot_uuid := uuid.New().String()
chatSessionMessage, err := json.Marshal(chatSession)
if err != nil {
return "", err
}
one, err := s.q.CreateChatBot(ctx, sqlc_queries.CreateChatBotParams{
Uuid: snapshot_uuid,
Model: chatSession.Model,
Typ: "chatbot",
Title: firstN(chatSession.Topic, 100),
UserID: userId,
Session: chatSessionMessage,
Tags: json.RawMessage([]byte("{}")),
Text: text,
Conversation: simple_msgs_raw,
})
if err != nil {
log.Println(err)
return "", err
}
return one.Uuid, nil

}
8 changes: 7 additions & 1 deletion api/sqlc/queries/chat_snapshot.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ INSERT INTO chat_snapshot (uuid, user_id, title, model, summary, tags, conversat
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING *;


-- name: CreateChatBot :one
INSERT INTO chat_snapshot (uuid, user_id, typ, title, model, summary, tags, conversation ,session, text )
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING *;

-- name: UpdateChatSnapshot :one
UPDATE chat_snapshot
SET uuid = $2, user_id = $3, title = $4, summary = $5, tags = $6, conversation = $7, created_at = $8
Expand All @@ -26,7 +32,7 @@ SELECT * FROM chat_snapshot WHERE uuid = $1;


-- name: ChatSnapshotMetaByUserID :many
SELECT uuid, title, summary, tags, created_at
SELECT uuid, title, summary, tags, created_at, typ
FROM chat_snapshot WHERE user_id = $1
order by created_at desc;

Expand Down
2 changes: 2 additions & 0 deletions api/sqlc/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ CREATE INDEX IF NOT EXISTS user_active_chat_session_user_id_idx ON user_active_c
-- for share chat feature
CREATE TABLE IF NOT EXISTS chat_snapshot (
id SERIAL PRIMARY KEY,
typ VARCHAR(255) NOT NULL default 'snapshot',
uuid VARCHAR(255) NOT NULL default '',
user_id INTEGER NOT NULL default 0,
title VARCHAR(255) NOT NULL default '',
Expand All @@ -252,6 +253,7 @@ CREATE TABLE IF NOT EXISTS chat_snapshot (
search_vector tsvector generated always as (setweight(to_tsvector('simple', coalesce(title, '')), 'A') || ' ' || setweight(to_tsvector('simple', coalesce(text, '')), 'B') :: tsvector) stored
);

ALTER TABLE chat_snapshot ADD COLUMN IF NOT EXISTS typ VARCHAR(255) NOT NULL default 'snapshot' ;
ALTER TABLE chat_snapshot ADD COLUMN IF NOT EXISTS model VARCHAR(255) NOT NULL default '' ;
ALTER TABLE chat_snapshot ADD COLUMN IF NOT EXISTS session JSONB DEFAULT '{}' NOT NULL;
ALTER TABLE chat_snapshot ADD COLUMN IF NOT EXISTS text text DEFAULT '' NOT NULL;
Expand Down
73 changes: 66 additions & 7 deletions api/sqlc_queries/chat_snapshot.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/sqlc_queries/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions web/src/api/chat_snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ export const createChatSnapshot = async (uuid: string): Promise<any> => {
}
}


export const createChatBot = async (uuid: string): Promise<any> => {
try {
const response = await request.post(`/uuid/chat_bot/${uuid}`)
return response.data
}
catch (error) {
console.error(error)
throw error
}
}

export const fetchChatSnapshot = async (uuid: string): Promise<any> => {
try {
const response = await request.get(`/uuid/chat_snapshot/${uuid}`)
Expand Down
23 changes: 18 additions & 5 deletions web/src/locales/en-US-more.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
{
"bot": {
"list": "Robot List",
"all": {
"title": "Robot List"
}
},
"chat": {
"uploader_title": "Upload File",
"uploader_button": "Upload",
"uploader_close": "Close",
"uploader_help_text": "Supported file types: text, image, audio, video"
"chatSettings": "Chat Settings",
"uploadFiles": "Upload Files",
"createBot": "Create Bot"
},
"admin": {
"chat_model": {
"enablePerModeRatelimit": "Enable Rate Limit Per Mode",
"isEnable": "Is Enabled"
}
}
}
}


Loading
Loading