Skip to content

Commit

Permalink
Add isEnable field to chat model for enabling/disabling functionali…
Browse files Browse the repository at this point in the history
…ty (#482)

* Add `isEnable` field to chat model for enabling/disabling functionality

* "Fix alignment and indentation issues in chat_model_handler.go and main.go"

* Fix typo and update field names for consistency in chat model handler and SQL queries.

* Add `isEnable` field to `AddModelForm.vue`

* Refactor locale keys and form references for consistency

* Update chat model handler to handle nil boolean for IsEnable and default to true

* Simplify `UpdateChatModel` handler by removing pointer logic for `IsEnable` field.

* Fix typo in field name `isEnable` to `IsEnable`

* Update test to ignore 'IsEnable' field in DeepEqual comparison
  • Loading branch information
swuecho authored Jun 30, 2024
1 parent bba91f3 commit dd611de
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 37 deletions.
3 changes: 3 additions & 0 deletions api/chat_model_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,15 @@ func (h *ChatModelHandler) UpdateChatModel(w http.ResponseWriter, r *http.Reques
DefaultToken int32
MaxToken int32
HttpTimeOut int32
IsEnable bool
}
err = json.NewDecoder(r.Body).Decode(&input)
if err != nil {
RespondWithError(w, http.StatusInternalServerError, eris.Wrap(err, "Failed to parse request body").Error(), err)
return
}


ChatModel, err := h.db.UpdateChatModel(r.Context(), sqlc_queries.UpdateChatModelParams{
ID: int32(id),
Name: input.Name,
Expand All @@ -162,6 +164,7 @@ func (h *ChatModelHandler) UpdateChatModel(w http.ResponseWriter, r *http.Reques
DefaultToken: input.DefaultToken,
MaxToken: input.MaxToken,
HttpTimeOut: input.HttpTimeOut,
IsEnable: input.IsEnable,
})

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion api/chat_model_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ func checkGetModels(t *testing.T, router *mux.Router, expectedResults []sqlc_que
t.Errorf("error parsing response body: %s", err.Error())
}
assert.Equal(t, len(results), 2)
assert.DeepEqual(t, lo.Reverse(expectedResults), results, cmpopts.IgnoreFields(sqlc_queries.ChatModel{}, "ID"))
assert.DeepEqual(t, lo.Reverse(expectedResults), results, cmpopts.IgnoreFields(sqlc_queries.ChatModel{}, "ID", "IsEnable"))
return results
}

Expand Down
2 changes: 0 additions & 2 deletions api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,6 @@ func main() {

apiRouter.HandleFunc("/tts", handleTTSRequest)



// Embed static/* directory
fs := http.FileServer(http.FS(static.StaticFiles))

Expand Down
2 changes: 1 addition & 1 deletion api/sqlc/queries/chat_model.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ RETURNING *;

-- name: UpdateChatModel :one
UPDATE chat_model SET name = $2, label = $3, is_default = $4, url = $5, api_auth_header = $6, api_auth_key = $7, enable_per_mode_ratelimit = $9,
max_token = $10, default_token = $11, order_number = $12, http_time_out = $13
max_token = $10, default_token = $11, order_number = $12, http_time_out = $13, is_enable = $14
WHERE id = $1 and user_id = $8
RETURNING *;

Expand Down
1 change: 1 addition & 0 deletions api/sqlc/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ ALTER TABLE chat_model ADD COLUMN IF NOT EXISTS max_token INTEGER NOT NULL defau
ALTER TABLE chat_model ADD COLUMN IF NOT EXISTS default_token INTEGER NOT NULL default 2048;
ALTER TABLE chat_model ADD COLUMN IF NOT EXISTS order_number INTEGER NOT NULL default 1;
ALTER TABLE chat_model ADD COLUMN IF NOT EXISTS http_time_out INTEGER NOT NULL default 120;
ALTER TABLE chat_model ADD COLUMN IF NOT EXISTS is_enable BOOLEAN DEFAULT true NOT NULL;


INSERT INTO chat_model(name, label, is_default, url, api_auth_header, api_auth_key, max_token, default_token, order_number)
Expand Down
28 changes: 19 additions & 9 deletions api/sqlc_queries/chat_model.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.

6 changes: 1 addition & 5 deletions web/src/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
"admin": {
"add_user_model_rate_limit": "Add User Session Count",
"chat_model": {
"EnablePerModeRatelimit": "Separate Throttling",
"actions": "Actions",
"apiAuthHeader": "Auth Header key",
"apiAuthKey": "API KEY corresponding to the environment variable",
"api_auth_header": "Auth Header key",
"api_auth_key": "Environment variable corresponding to API KEY",
"defaultToken": "Default token number",
"deleteModel": "Delete",
"deleteModelConfirm": "Confirm deletion",
"enable_per_model_rate_limit": "Enable per-model rate limit",
"enablePerModelRateLimit": "Enable per-model rate limit",
"isDefault": "Default?",
"is_default": "Default?",
"label": "Model name",
"maxToken": "Maximum token number",
"name": "ID",
Expand Down
7 changes: 2 additions & 5 deletions web/src/locales/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,13 @@
"deleteModelConfirm": "确认删除",
"name": "ID",
"label": "模型名称",
"is_default": "默认?",
"isDefault": "默认?",
"api_auth_header": "Auth Header key",
"apiAuthHeader": "Auth Header key",
"api_auth_key": "API KEY对应的环境变量",
"apiAuthKey": "API KEY对应的环境变量",
"actions": "操作",
"url": "访问接口(with full url path)",
"enable_per_model_rate_limit": "是否单独流控",
"EnablePerModeRatelimit": "是否单独流控",
"enablePerModeRatelimit": "是否单独流控",
"isEnable": "是否启用",
"orderNumber": "次序",
"maxToken": "最大token数量",
"defaultToken": "默认token数量"
Expand Down
6 changes: 1 addition & 5 deletions web/src/locales/zh-TW.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
"admin": {
"add_user_model_rate_limit": "添加用戶會話數量",
"chat_model": {
"EnablePerModeRatelimit": "是否單獨流控",
"actions": "操作",
"apiAuthHeader": "Auth Header 鍵",
"apiAuthKey": "API KEY對應的環境變量",
"api_auth_header": "Auth Header 鍵",
"api_auth_key": "API KEY對應的環境變數",
"defaultToken": "默認token數量",
"deleteModel": "刪除",
"deleteModelConfirm": "確認刪除",
"enable_per_model_rate_limit": "是否單獨流控",
"enablePerModelRateLimit": "是否單獨流控",
"isDefault": "默認?",
"is_default": "默認?",
"label": "模型名稱",
"maxToken": "最大token數量",
"name": "身分識別號",
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 @@ -63,6 +63,7 @@ declare namespace Chat {
name: string
url: string
enablePerModeRatelimit: boolean,
isEnable: boolean,
maxToken?: string,
defaultToken?: string,
orderNumber?: string,
Expand Down
9 changes: 5 additions & 4 deletions web/src/views/admin/model/AddModelForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const formData = ref<Chat.ChatModel>({
apiAuthHeader: '',
apiAuthKey: '',
enablePerModeRatelimit: false,
isEnable: true,
})
interface Emit {
Expand Down Expand Up @@ -50,16 +51,16 @@ async function addRow() {
<NFormItem path="url" :label="$t('admin.chat_model.url')">
<NInput v-model:value="formData.url" />
</NFormItem>
<NFormItem path="apiAuthHeader" :label="$t('admin.chat_model.api_auth_header')">
<NFormItem path="apiAuthHeader" :label="$t('admin.chat_model.apiAuthHeader')">
<NInput v-model:value="formData.apiAuthHeader" />
</NFormItem>
<NFormItem path="apiAuthKey" :label="$t('admin.chat_model.api_auth_key')">
<NFormItem path="apiAuthKey" :label="$t('admin.chat_model.apiAuthKey')">
<NInput v-model:value="formData.apiAuthKey" />
</NFormItem>
<NFormItem path="isDefault" :label="$t('admin.chat_model.is_default')">
<NFormItem path="isDefault" :label="$t('admin.chat_model.isDefault')">
<NSwitch v-model:value="formData.isDefault" />
</NFormItem>
<NFormItem path="enablePerModeRatelimit" :label="$t('admin.chat_model.enable_per_model_rate_limit')">
<NFormItem path="enablePerModeRatelimit" :label="$t('admin.chat_model.enablePerModeRatelimit')">
<NSwitch v-model:value="formData.enablePerModeRatelimit" />
</NFormItem>
</NForm>
Expand Down
24 changes: 21 additions & 3 deletions web/src/views/admin/model/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,17 @@ const deteteModelMutation = useMutation({
const UpdateRow = (row: Chat.ChatModel) => {
if (row.id) {
chatModelMutation.mutate({
const newRow = {
id: row.id,
data: {
...row,
orderNumber: parseInt(row.orderNumber || '0'),
defaultToken: parseInt(row.defaultToken || '0'),
maxToken: parseInt(row.maxToken || '0'),
},
})
}
console.log(newRow)
chatModelMutation.mutate(newRow)
}
}
Expand Down Expand Up @@ -213,7 +215,7 @@ function createColumns(): DataTableColumns<Chat.ChatModel> {
},
}
const perModelLimit = {
title: t('admin.chat_model.EnablePerModeRatelimit'),
title: t('admin.chat_model.enablePerModeRatelimit'),
key: 'enablePerModeRatelimit',
render(row: Chat.ChatModel, index: number) {
return h(NSwitch, {
Expand All @@ -227,6 +229,21 @@ function createColumns(): DataTableColumns<Chat.ChatModel> {
},
}
const isEnableColumn = {
title: t('admin.chat_model.isEnable'),
key: 'isEnable',
render(row: Chat.ChatModel, index: number) {
return h(NSwitch, {
value: row.isEnable,
onUpdateValue(v: boolean) {
// Assuming `data` is an array of FormData objects
data.value[index].isEnable = v
UpdateRow(data.value[index])
},
})
},
}
const actionField = {
title: t('admin.chat_model.actions'),
key: 'actions',
Expand Down Expand Up @@ -257,6 +274,7 @@ function createColumns(): DataTableColumns<Chat.ChatModel> {
apiAuthHeaderField,
isDefaultField,
perModelLimit,
isEnableColumn,
defaultToken,
maxToken,
orderNumber,
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/chat/components/ModelSelector.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const optionFromModel = (model: any) => {
}
}
const chatModelOptions = computed(() =>
data?.value ? data.value.map(optionFromModel) : []
data?.value ? data.value.filter((x: any) => x.isEnable).map(optionFromModel) : []
)
Expand Down
2 changes: 1 addition & 1 deletion web/src/views/chat/components/Session/SessionConfig.vue
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const { data, isLoading } = useQuery({
})
const chatModelOptions = computed(() =>
data?.value ? data.value.map(optionFromModel) : []
data?.value ? data.value.filter((x: any) => x.isEnable).map(optionFromModel) : []
)
const chatStore = useChatStore()
Expand Down

0 comments on commit dd611de

Please sign in to comment.