Skip to content

Commit

Permalink
Add model field to chat messages for tracking LLM model used in each …
Browse files Browse the repository at this point in the history
…message.
  • Loading branch information
swuecho committed Sep 13, 2024
1 parent 3575c9c commit e1ed0fd
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 23 deletions.
6 changes: 3 additions & 3 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
}

if existingPrompt {
_, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, userID, baseURL, chatSession.SummarizeMode)
_, err := h.service.CreateChatMessageSimple(ctx, chatSession.Uuid, chatUuid, "user", newQuestion, chatSession.Model, userID, baseURL, chatSession.SummarizeMode)
if err != nil {
http.Error(w,
eris.Wrap(err, "fail to create message: ").Error(),
Expand Down Expand Up @@ -260,7 +260,7 @@ func genAnswer(h *ChatHandler, w http.ResponseWriter, chatSessionUuid string, ch
h.service.logChat(chatSession, msgs, answerText)
}

if _, err := h.service.CreateChatMessageSimple(ctx, chatSessionUuid, answerID, "assistant", answerText, userID, baseURL, chatSession.SummarizeMode); err != nil {
if _, err := h.service.CreateChatMessageSimple(ctx, chatSessionUuid, answerID, "assistant", answerText, chatSession.Model, userID, baseURL, chatSession.SummarizeMode); err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "failed to create message").Error(), nil)
return
}
Expand Down Expand Up @@ -292,7 +292,7 @@ func genBotAnswer(h *ChatHandler, w http.ResponseWriter, session sqlc_queries.Ch
}

ctx := context.Background()
if _, err := h.service.CreateChatMessageSimple(ctx, session.Uuid, answerID, "assistant", answerText, userID, baseURL, session.SummarizeMode); err != nil {
if _, err := h.service.CreateChatMessageSimple(ctx, session.Uuid, answerID, "assistant", answerText, session.Model, userID, baseURL, session.SummarizeMode); err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "failed to create message").Error(), nil)
return
}
Expand Down
3 changes: 2 additions & 1 deletion api/chat_main_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (s *ChatService) CreateChatPromptSimple(chatSessionUuid string, newQuestion
}

// CreateChatMessage creates a new chat message.
func (s *ChatService) CreateChatMessageSimple(ctx context.Context, sessionUuid, uuid, role, content string, userId int32, baseURL string, is_summarize_mode bool) (sqlc_queries.ChatMessage, error) {
func (s *ChatService) CreateChatMessageSimple(ctx context.Context, sessionUuid, uuid, role, content, model string, userId int32, baseURL string, is_summarize_mode bool) (sqlc_queries.ChatMessage, error) {
numTokens, err := getTokenCount(content)
if err != nil {
log.Println(eris.Wrap(err, "failed to get token count: "))
Expand All @@ -105,6 +105,7 @@ func (s *ChatService) CreateChatMessageSimple(ctx context.Context, sessionUuid,
Uuid: uuid,
Role: role,
Content: content,
Model: model,
UserID: userId,
CreatedBy: userId,
UpdatedBy: userId,
Expand Down
4 changes: 2 additions & 2 deletions api/sqlc/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ SELECT * FROM chat_message WHERE chat_session_id = $1 ORDER BY id;
SELECT * FROM chat_message WHERE id = $1;

-- name: CreateChatMessage :one
INSERT INTO chat_message (chat_session_id, role, content, score, user_id, created_at, updated_at, created_by, updated_by, raw)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
INSERT INTO chat_message (chat_session_id, role, content, model, score, user_id, created_at, updated_at, created_by, updated_by, raw)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
RETURNING *;

-- name: UpdateChatMessage :one
Expand Down
4 changes: 2 additions & 2 deletions api/sqlc/queries/chat_message.sql
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ WHERE is_deleted = false and id = $1;


-- name: CreateChatMessage :one
INSERT INTO chat_message (chat_session_uuid, uuid, role, content, token_count, score, user_id, created_by, updated_by, llm_summary, raw)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)
INSERT INTO chat_message (chat_session_uuid, uuid, role, content, model, token_count, score, user_id, created_by, updated_by, llm_summary, raw)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
RETURNING *;

-- name: UpdateChatMessage :one
Expand Down
2 changes: 2 additions & 0 deletions api/sqlc/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ CREATE TABLE IF NOT EXISTS chat_message (
chat_session_uuid character varying(255) NOT NUll,
role character varying(255) NOT NULL,
content character varying NOT NULL,
model character varying(255) NOT NULL DEFAULT '',
llm_summary character varying(1024) NOT NULL DEFAULT '',
score double precision NOT NULL,
user_id integer NOT NULL,
Expand All @@ -168,6 +169,7 @@ ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS is_deleted BOOLEAN NOT NULL D
ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS token_count INTEGER DEFAULT 0 NOT NULL;
ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS is_pin BOOLEAN NOT NULL DEFAULT false;
ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS llm_summary character varying(1024) NOT NULL DEFAULT '';
ALTER TABLE chat_message ADD COLUMN IF NOT EXISTS model character varying(255) NOT NULL DEFAULT '';

-- add hash index on uuid
CREATE INDEX IF NOT EXISTS chat_message_uuid_idx ON chat_message using hash (uuid) ;
Expand Down
39 changes: 26 additions & 13 deletions api/sqlc_queries/chat_message.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.

2 changes: 2 additions & 0 deletions api/sqlc_queries/zz_custom_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type SimpleChatMessage struct {
Uuid string `json:"uuid"`
DateTime string `json:"dateTime"`
Text string `json:"text"`
Model string `json:"model"`
Inversion bool `json:"inversion"`
Error bool `json:"error"`
Loading bool `json:"loading"`
Expand Down Expand Up @@ -54,6 +55,7 @@ func (q *Queries) GetChatHistoryBySessionUUID(ctx context.Context, uuid string,
Uuid: message.Uuid,
DateTime: message.UpdatedAt.Format(time.RFC3339),
Text: message.Content,
Model: message.Model,
Inversion: message.Role == "user",
Error: false,
Loading: false,
Expand Down
1 change: 1 addition & 0 deletions web/src/typings/chat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare namespace Chat {
uuid: string,
dateTime: string
text: string
model?: string
inversion?: boolean
error?: boolean
loading?: boolean
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/chat/components/MessageList.vue
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<template>
<Message v-for="(item, index) of dataSources" :key="index" :date-time="item.dateTime"
:model="chatSession?.model" :text="item.text" :inversion="item.inversion" :error="item.error"
:model="item?.model || chatSession?.model" :text="item.text" :inversion="item.inversion" :error="item.error"
:is-prompt="item.isPrompt" :is-pin="item.isPin" :loading="item.loading" :index="index"
@regenerate="onRegenerate(index)" @toggle-pin="handleTogglePin(index)" @delete="handleDelete(index)" @after-edit="handleAfterEdit" />
</template>
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/snapshot/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ function onScrollToTop() {
<div id="image-wrapper" class="w-full max-w-screen-xl m-auto dark:bg-[#101014]"
:class="[isMobile ? 'p-2' : 'p-4']">
<Message v-for="(item, index) of snapshot_data.conversation" :key="index" :date-time="item.dateTime"
:model="snapshot_data.model" :text="item.text" :inversion="item.inversion" :error="item.error"
:model="item?.model || snapshot_data.model" :text="item.text" :inversion="item.inversion" :error="item.error"
:loading="item.loading" :index="index" />
</div>
</div>
Expand Down

0 comments on commit e1ed0fd

Please sign in to comment.