Skip to content

Commit

Permalink
uuid v7 (#474)
Browse files Browse the repository at this point in the history
* uuid v7

* uuid types

* uuid types
  • Loading branch information
swuecho authored Jun 18, 2024
1 parent 2b65cf2 commit 884260a
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 62 deletions.
11 changes: 9 additions & 2 deletions api/auth/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ import (
"time"

jwt "github.com/golang-jwt/jwt/v5"

"github.com/google/uuid"
)

func NewUUID() string {
uuidv7, err := uuid.NewV7()
if err != nil {
return uuid.NewString()
}
return uuidv7.String()
}

var ErrInvalidToken = errors.New("invalid token")

func GenJwtSecretAndAudience() (string, string) {
Expand Down Expand Up @@ -43,7 +50,7 @@ func GenerateToken(userID int32, role string, secret, jwt_audience string, lifet
"user_id": strconv.FormatInt(int64(userID), 10),
"exp": expires,
"role": role,
"jti": uuid.NewString(),
"jti": NewUUID(),
"iss": issuer,
"nbf": notBefore,
"aud": jwt_audience,
Expand Down
15 changes: 7 additions & 8 deletions api/chat_main_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"time"

mapset "github.com/deckarep/golang-set/v2"
"github.com/google/uuid"
"github.com/rotisserie/eris"
"github.com/samber/lo"
openai "github.com/sashabaranov/go-openai"
Expand Down Expand Up @@ -710,7 +709,7 @@ func (h *ChatHandler) chatStreamClaude(w http.ResponseWriter, chatSession sqlc_q
break
}
if answer_id == "" {
answer_id = uuid.NewString()
answer_id = NewUUID()
}
var response ClaudeResponse
_ = json.Unmarshal(line, &response)
Expand Down Expand Up @@ -864,7 +863,7 @@ func (h *ChatHandler) chatStreamClaude3(w http.ResponseWriter, chatSession sqlc_
return "", "", true
}
if answer_id == "" {
answer_id = uuid.NewString()
answer_id = NewUUID()
}
if bytes.HasPrefix(line, []byte("{\"type\":\"content_block_start\"")) {
answer = claude.AnswerFromBlockStart(line)
Expand Down Expand Up @@ -996,7 +995,7 @@ func (h *ChatHandler) chatOllamStram(w http.ResponseWriter, chatSession sqlc_que
break
}
if answer_id == "" {
answer_id = uuid.NewString()
answer_id = NewUUID()
}

if len(answer) < 200 || len(answer)%2 == 0 {
Expand Down Expand Up @@ -1124,7 +1123,7 @@ func (h *ChatHandler) customChatStream(w http.ResponseWriter, chatSession sqlc_q
break
}
if answer_id == "" {
answer_id = uuid.NewString()
answer_id = NewUUID()
}
var response CustomModelResponse
_ = json.Unmarshal(line, &response)
Expand All @@ -1149,7 +1148,7 @@ func (h *ChatHandler) chatStreamTest(w http.ResponseWriter, chatSession sqlc_que

answer_id := chatUuid
if !regenerate {
answer_id = uuid.NewString()
answer_id = NewUUID()
}
setSSEHeader(w)

Expand Down Expand Up @@ -1197,7 +1196,7 @@ func NewChatCompletionRequest(chatSession sqlc_queries.ChatSession, chat_compele
Messages: openai_message,
//MaxTokens: maxOutputToken,
Temperature: float32(chatSession.Temperature),
TopP: float32(chatSession.TopP)-0.01,
TopP: float32(chatSession.TopP) - 0.01,
N: int(chatSession.N),
Stream: true,
}
Expand Down Expand Up @@ -1275,7 +1274,7 @@ func (h *ChatHandler) chatStreamGemini(w http.ResponseWriter, chatSession sqlc_q
var answer string
answer_id := chatUuid
if !regenerate {
answer_id = uuid.NewString()
answer_id = NewUUID()
}

var headerData = []byte("data: ")
Expand Down
3 changes: 1 addition & 2 deletions api/chat_main_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"log"
"time"

"github.com/google/uuid"
"github.com/rotisserie/eris"
"github.com/samber/lo"
models "github.com/swuecho/chat_backend/models"
Expand Down Expand Up @@ -74,7 +73,7 @@ func (s *ChatService) CreateChatPromptSimple(chatSessionUuid string, newQuestion
tokenCount, _ := getTokenCount(newQuestion)
chatPrompt, err := s.q.CreateChatPrompt(context.Background(),
sqlc_queries.CreateChatPromptParams{
Uuid: uuid.NewString(),
Uuid: NewUUID(),
ChatSessionUuid: chatSessionUuid,
Role: "system",
Content: newQuestion,
Expand Down
4 changes: 2 additions & 2 deletions api/chat_session_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ func (h *ChatSessionHandler) createChatSessionFromSnapshot(w http.ResponseWriter
}

_, err = h.service.q.CreateChatPrompt(r.Context(), sqlc_queries.CreateChatPromptParams{
Uuid: uuid.NewString(),
Uuid: NewUUID(),
ChatSessionUuid: sessionUUID,
Role: "system",
Content: promptMsg.Text,
Expand All @@ -303,7 +303,7 @@ func (h *ChatSessionHandler) createChatSessionFromSnapshot(w http.ResponseWriter

messageParam := sqlc_queries.CreateChatMessageParams{
ChatSessionUuid: sessionUUID,
Uuid: uuid.NewString(),
Uuid: NewUUID(),
Role: message.GetRole(),
Content: message.Text,
UserID: userID,
Expand Down
3 changes: 1 addition & 2 deletions api/chat_snapshot_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"net/http/httptest"
"testing"

"github.com/google/uuid"
"github.com/gorilla/mux"
"github.com/swuecho/chat_backend/sqlc_queries"
"gotest.tools/v3/assert"
Expand All @@ -32,7 +31,7 @@ func TestChatSnapshot(t *testing.T) {
userID := 1

// Generate a random UUID for the snapshot
snapshotUUID := uuid.NewString()
snapshotUUID := NewUUID()

// Create a test snapshot
snapshot, err := h.service.q.CreateChatSnapshot(context.Background(), sqlc_queries.CreateChatSnapshotParams{
Expand Down
8 changes: 8 additions & 0 deletions api/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,18 @@ import (
"net/http"
"strconv"

"github.com/google/uuid"
"github.com/pkoukk/tiktoken-go"
"github.com/rotisserie/eris"
)

func NewUUID() string {
uuidv7, err := uuid.NewV7()
if err != nil {
return uuid.NewString()
}
return uuidv7.String()
}
func getTokenCount(content string) (int, error) {
encoding := "cl100k_base"
tke, err := tiktoken.GetEncoding(encoding)
Expand Down
72 changes: 38 additions & 34 deletions web/package-lock.json

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

8 changes: 4 additions & 4 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,25 @@
"markdown-it": "^13.0.1",
"naive-ui": "^2.38.2",
"pinia": "^2.0.32",
"uuid": "^9.0.0",
"uuid": "^10.0.0",
"vue": "3.4.27",
"vue-i18n": "^9.2.2",
"vue-router": "4.1.6"
},
"devDependencies": {
"@tanstack/vue-query-devtools": "^5.40.1",
"@antfu/eslint-config": "^0.35.3",
"@commitlint/cli": "^17.4.4",
"@commitlint/config-conventional": "^17.4.4",
"@iconify/vue": "^4.1.0",
"@tanstack/vue-query-devtools": "^5.40.1",
"@types/crypto-js": "^4.1.1",
"@types/file-saver": "^2.0.5",
"@types/katex": "^0.16.0",
"@types/lodash-es": "^4.17.7",
"@types/luxon": "^3.3.0",
"@types/markdown-it": "^12.2.3",
"@types/node": "^18.14.6",
"@types/uuid": "^9.0.1",
"@types/uuid": "^9.0.8",
"@vitejs/plugin-vue": "^4.0.0",
"autoprefixer": "^10.4.13",
"axios": "^1.3.4",
Expand All @@ -80,4 +80,4 @@
"pnpm lint:fix"
]
}
}
}
5 changes: 3 additions & 2 deletions web/src/api/chat_session.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { v4 as uuidv4 } from 'uuid'
// @ts-ignore
import { v7 as uuidv7 } from 'uuid'
import { fetchDefaultChatModel } from './chat_model'
import request from '@/utils/request/axios'

export const getChatSessionDefault = async (title: string): Promise<Chat.Session> => {
const default_model = await fetchDefaultChatModel()
const uuid = uuidv4()
const uuid = uuidv7()
return {
title,
isEdit: false,
Expand Down
5 changes: 1 addition & 4 deletions web/src/store/modules/chat/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { defineStore } from 'pinia'
import { v4 as uuidv4 } from 'uuid'

import { check_chat, getLocalState, } from './helper'
import { router } from '@/router'
import {
Expand Down Expand Up @@ -176,9 +174,8 @@ export const useChatStore = defineStore('chat-store', {
const [keys] = check_chat(this.chat, false)
if (!uuid) {
if (this.history.length === 0) {
const uuid = uuidv4()
const default_model_parameters = await getChatSessionDefault(new_chat_text)

const uuid = default_model_parameters.uuid;
createChatSession(uuid, chat.text, default_model_parameters.model)
this.history.push({ uuid, title: chat.text, isEdit: false })
// first chat message is prompt
Expand Down
Loading

0 comments on commit 884260a

Please sign in to comment.