Skip to content

Commit

Permalink
Sync max context value (#11)
Browse files Browse the repository at this point in the history
* maxLength

* update

* test slider value

* update

* update

* update

* update
  • Loading branch information
swuecho authored Mar 20, 2023
1 parent b0808a0 commit 3a3d6e0
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 73 deletions.
8 changes: 6 additions & 2 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,15 @@ func (h *ChatHandler) OpenAIChatCompletionAPIWithStreamHandler(w http.ResponseWr
http.Error(w, "Error: '"+err.Error()+"'", http.StatusBadRequest)
return
}

chat_session, err := h.chatService.q.GetChatSessionByUUID(ctx, chatSessionUuid)
if err != nil {
http.Error(w, "Error: '"+err.Error()+"'", http.StatusBadRequest)
return
}
msgs, err := h.chatService.q.GetLastNChatMessages(ctx,
sqlc_queries.GetLastNChatMessagesParams{
Uuid: chatUuid,
Limit: 10,
Limit: chat_session.MaxLength,
})
if err != nil {
http.Error(w, "Error: '"+err.Error()+"'", http.StatusBadRequest)
Expand Down
1 change: 1 addition & 0 deletions api/chat_message_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func (h *ChatMessageHandler) GetChatMessageByUUID(w http.ResponseWriter, r *http
http.Error(w, err.Error(), http.StatusNotFound)
return
}

json.NewEncoder(w).Encode(message)
}

Expand Down
13 changes: 11 additions & 2 deletions api/chat_session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func (h *ChatSessionHandler) Register(router *mux.Router) {
router.HandleFunc("/chat_sessions/{id}", h.DeleteChatSession).Methods(http.MethodDelete)
router.HandleFunc("/chat_sessions", h.GetAllChatSessions).Methods(http.MethodGet)

router.HandleFunc("/uuid/chat_sessions/max_conversions/{uuid}", h.UpdateSessionMaxLength).Methods("PUT")
router.HandleFunc("/uuid/chat_sessions/max_length/{uuid}", h.UpdateSessionMaxLength).Methods("PUT")
router.HandleFunc("/uuid/chat_sessions/topic/{uuid}", h.UpdateChatSessionTopicByUUID).Methods("PUT")
router.HandleFunc("/uuid/chat_sessions/{uuid}", h.GetChatSessionByUUID).Methods("GET")
router.HandleFunc("/uuid/chat_sessions/{uuid}", h.CreateOrUpdateChatSessionByUUID).Methods("PUT")
Expand Down Expand Up @@ -136,7 +136,14 @@ func (h *ChatSessionHandler) GetChatSessionByUUID(w http.ResponseWriter, r *http
http.Error(w, err.Error(), http.StatusNotFound)
return
}
json.NewEncoder(w).Encode(session)
session_resp := &ChatSessionResponse{
Uuid: session.Uuid,
Topic: session.Topic,
MaxLength: session.MaxLength,
CreatedAt: session.CreatedAt,
UpdatedAt: session.UpdatedAt,
}
json.NewEncoder(w).Encode(session_resp)
}

// CreateChatSessionByUUID creates a chat session by its UUID
Expand All @@ -155,6 +162,7 @@ func (h *ChatSessionHandler) CreateChatSessionByUUID(w http.ResponseWriter, r *h
return
}
sessionParams.UserID = int32(userIDInt)
sessionParams.MaxLength = 10
session, err := h.service.CreateChatSession(r.Context(), sessionParams)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down Expand Up @@ -216,6 +224,7 @@ func (h *ChatSessionHandler) CreateOrUpdateChatSessionByUUID(w http.ResponseWrit
return
}
sessionParams.Uuid = uuid
sessionParams.MaxLength = 10

ctx := r.Context()
userIDStr := ctx.Value(userContextKey).(string)
Expand Down
1 change: 0 additions & 1 deletion api/chat_session_service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ func TestChatSessionService(t *testing.T) {
updated_params := sqlc_queries.UpdateChatSessionParams{ID: session.ID,
UserID: session.UserID,
Topic: "Updated Test Session",
Active: session.Active, MaxLength: session.MaxLength,
}
if _, err := service.UpdateChatSession(context.Background(), updated_params); err != nil {
t.Fatalf("failed to update chat session: %v", err)
Expand Down
25 changes: 25 additions & 0 deletions api/models.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package main

import (
"time"
)

type ErrorResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Expand Down Expand Up @@ -39,3 +43,24 @@ type SimpleChatSession struct {
IsEdit bool `json:"isEdit"`
Title string `json:"title"`
}

type ChatMessageResponse struct {
Uuid string `json:"uuid"`
ChatSessionUuid string `json:"chatSessionUuid"`
Role string `json:"role"`
Content string `json:"content"`
Score float64 `json:"score"`
UserID int32 `json:"userId"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
CreatedBy int32 `json:"createdBy"`
UpdatedBy int32 `json:"updatedBy"`
}

type ChatSessionResponse struct {
Uuid string `json:"uuid"`
Topic string `json:"topic"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
MaxLength int32 `json:"maxLength"`
}
12 changes: 6 additions & 6 deletions api/sqlc/queries/chat_session.sql
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: UpdateChatSession :one
UPDATE chat_session SET user_id = $2, topic = $3, updated_at = now(), active = $4, max_length = $5
UPDATE chat_session SET user_id = $2, topic = $3, updated_at = now(), active = $4
WHERE id = $1
RETURNING *;

Expand All @@ -26,21 +26,21 @@ WHERE active = true and uuid = $1
order by updated_at;

-- name: CreateChatSessionByUUID :one
INSERT INTO chat_session (user_id, uuid, topic, created_at, active, max_length)
INSERT INTO chat_session (user_id, uuid, topic, created_at, active, max_length)
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING *;

-- name: UpdateChatSessionByUUID :one
UPDATE chat_session SET user_id = $2, topic = $3, updated_at = now(), max_length = $4
UPDATE chat_session SET user_id = $2, topic = $3, updated_at = now()
WHERE uuid = $1
RETURNING *;

-- name: CreateOrUpdateChatSessionByUUID :one
INSERT INTO chat_session(uuid, user_id, topic)
VALUES ($1, $2, $3)
INSERT INTO chat_session(uuid, user_id, topic, max_length)
VALUES ($1, $2, $3, $4)
ON CONFLICT (uuid)
DO UPDATE SET
-- topic = EXCLUDED.topic,
max_length = EXCLUDED.max_length,
topic = CASE WHEN chat_session.topic IS NULL THEN EXCLUDED.topic ELSE chat_session.topic END,
updated_at = now()
returning *;
Expand Down
50 changes: 24 additions & 26 deletions api/sqlc_queries/chat_session.sql.go

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

18 changes: 0 additions & 18 deletions e2e/tests/example.spec.ts

This file was deleted.

1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"highlight.js": "^11.7.0",
"html2canvas": "^1.4.1",
"katex": "^0.16.4",
"lodash": "^4.17.21",
"markdown-it": "^13.0.1",
"moment": "^2.29.4",
"naive-ui": "^2.34.3",
Expand Down
2 changes: 2 additions & 0 deletions web/pnpm-lock.yaml

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

22 changes: 21 additions & 1 deletion web/src/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export async function fetchSignUp(email: string, password: string) {
}
}

export const getChatSessionsByUserId = async () => {
export const getChatSessionsByUser = async () => {
try {
const response = await request.get('/chat_sessions/users')
return response.data
Expand Down Expand Up @@ -168,6 +168,26 @@ export const clearSessionChatMessages = async (sessionUuid: string) => {
}
}

export const getChatSessionMaxContextLength = async (sessionUuid: string) => {
try {
const response = await request.get(`/uuid/chat_sessions/${sessionUuid}`)
return response.data.maxLength
}
catch (error) {
console.error(error)
}
}

export const setChatSessionMaxContextLength = async (uuid: string, maxLength: number) => {
try {
const response = await request.put(`/uuid/chat_sessions/max_length/${uuid}`, { uuid, maxLength })
return response.data
}
catch (error) {
console.error(error)
throw error
}
}

export const deleteChatMessage = async (uuid: string) => {
try {
Expand Down
4 changes: 2 additions & 2 deletions web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
deleteChatData,
deleteChatSession,
getChatMessagesBySessionUUID as getChatSessionHistory,
getChatSessionsByUserId,
getChatSessionsByUser,
getUserActiveChatSession,
renameChatSession,
} from '@/api'
Expand Down Expand Up @@ -50,7 +50,7 @@ export const useChatStore = defineStore('chat-store', {
},

async syncChatSessions() {
const sessions = await getChatSessionsByUserId()
const sessions = await getChatSessionsByUser()
if (sessions.length <= 0)
return

Expand Down
Loading

0 comments on commit 3a3d6e0

Please sign in to comment.