Skip to content

Commit

Permalink
feat: model caching alpha
Browse files Browse the repository at this point in the history
  • Loading branch information
zmh-program committed Feb 13, 2024
1 parent caefd5d commit 65acc65
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 6 deletions.
4 changes: 4 additions & 0 deletions adapter/chatgpt/chat.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,9 @@ func (c *ChatInstance) CreateStreamChatRequest(props *ChatProps, callback global
return err.Error
}

if props.Buffer.IsEmpty() {
return errors.New("no response")
}

return nil
}
7 changes: 7 additions & 0 deletions app/src/admin/api/system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ export type SiteState = {
};

export type CommonState = {
cache: string[];
expire: number;
size: number;

article: string[];
generation: string[];
};
Expand Down Expand Up @@ -135,5 +139,8 @@ export const initialSystemState: SystemProps = {
common: {
article: [],
generation: [],
cache: [],
expire: 3600,
size: 1,
},
};
2 changes: 1 addition & 1 deletion app/src/components/Tips.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ function Tips({
</DropdownMenuTrigger>
<DropdownMenuContent
className={cn(
"px-3 py-1.5 cursor-pointer text-sm min-w-0",
"px-3 py-1.5 cursor-pointer text-sm min-w-0 max-w-[90vw]",
classNamePopup,
)}
side={`top`}
Expand Down
1 change: 0 additions & 1 deletion app/src/components/home/ChatSpace.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { useSelector } from "react-redux";
import { isSubscribedSelector } from "@/store/subscription.ts";
import { Button } from "@/components/ui/button.tsx";
import { ChevronRight, FolderKanban, Newspaper, Users2 } from "lucide-react";
import router from "@/router.tsx";
Expand Down
12 changes: 11 additions & 1 deletion app/src/resources/i18n/cn.json
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,17 @@
"articleTip": "批量文章生成功能分组,勾选后当前用户组可使用批量文章生成功能",
"generate": "AI 项目生成器分组",
"generateTip": "AI 项目生成器分组,勾选后当前用户组可使用 AI 项目生成器",
"groupPlaceholder": "已选 {{length}} 个分组"
"groupPlaceholder": "已选 {{length}} 个分组",
"cache": "可缓存的模型",
"cacheTip": "可缓存的模型,勾选后当前模型可被缓存并击中缓存",
"cachePlaceholder": "已选 {{length}} 个模型",
"cacheAll": "设为全部可缓存",
"cacheFree": "设为免费模型可缓存",
"cacheNone": "设为全部不缓存",
"cacheExpired": "缓存过期时间",
"cacheExpiredTip": "缓存过期时间(单位:秒),默认 1 小时",
"cacheSize": "最大缓存可能性区大小",
"cacheSizeTip": "最大缓存可能性大小,即同一类型入参的最大缓存可能性大小,若参数为 1, 则最大缓存的内容为 1 个,后请求的内容会被直接击中,若参数为 4, 则有 4 种返回的内容,后请求的内容会被击中其中一个"
},
"logger": {
"title": "服务日志",
Expand Down
12 changes: 11 additions & 1 deletion app/src/resources/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,17 @@
"articleTip": "Batch post generation function grouping, after checking the current user group can use batch post generation function",
"generate": "AI Project Builder Grouping",
"generateTip": "AI project generator grouping, after checking the current user group can use AI project generator",
"groupPlaceholder": "{{length}} groups selected"
"groupPlaceholder": "{{length}} groups selected",
"cache": "Cacheable Model",
"cacheTip": "Cacheable model, after checking the current model can be cached and hit the cache",
"cachePlaceholder": "{{length}} models selected",
"cacheAll": "Make All Cacheable",
"cacheFree": "Make free model cacheable",
"cacheNone": "Make All Uncached",
"cacheExpired": "Cache Expiration Time",
"cacheExpiredTip": "Cache expiration time (in seconds), default 1 hour",
"cacheSize": "Max Cache Likelihood Size",
"cacheSizeTip": "Maximum cache likelihood, that is, the maximum cache likelihood of the same type of input parameter. If the parameter is 1, the maximum cache content is 1, and the requested content will be directly hit. If the parameter is 4, there are 4 returned contents, and the requested content will be hit one of them."
},
"user": "User Management",
"invitation-code": "Invitation Code",
Expand Down
12 changes: 11 additions & 1 deletion app/src/resources/i18n/ja.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,17 @@
"articleTip": "バッチポストジェネレーション機能のグループ化、現在のユーザーグループを確認した後、バッチポストジェネレーション機能を使用することができます",
"generate": "AIプロジェクトビルダーグループ",
"generateTip": "AIプロジェクトジェネレータグループ、現在のユーザーグループを確認した後、AIプロジェクトジェネレータを使用することができます",
"groupPlaceholder": "{{length}}グループが選択されました"
"groupPlaceholder": "{{length}}グループが選択されました",
"cache": "キャッシュ可能なモデル",
"cacheTip": "キャッシュ可能なモデル、現在のモデルをチェックした後、キャッシュしてキャッシュをヒットすることができます",
"cachePlaceholder": "{{length}}モデルが選択されました",
"cacheAll": "すべてのキャッシュ可能にする",
"cacheFree": "フリーモデルをキャッシュ可能にする",
"cacheNone": "キャッシュされていないものをすべて作成",
"cacheExpired": "キャッシュの有効期限",
"cacheExpiredTip": "キャッシュの有効期限(秒単位)、デフォルト1時間",
"cacheSize": "最大キャッシュ尤度サイズ",
"cacheSizeTip": "最大キャッシュ尤度、つまり、同じ種類の入力パラメーターの最大キャッシュ尤度。パラメーターが1の場合、最大キャッシュコンテンツは1で、要求されたコンテンツは直接ヒットします。パラメーターが4の場合、4つの返されたコンテンツがあり、要求されたコンテンツのいずれかがヒットします。"
},
"user": "ユーザー管理",
"invitation-code": "招待コード",
Expand Down
50 changes: 50 additions & 0 deletions app/src/routes/admin/System.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { cn } from "@/components/ui/lib/utils.ts";
import { Switch } from "@/components/ui/switch.tsx";
import { MultiCombobox } from "@/components/ui/multi-combobox.tsx";
import { allGroups } from "@/utils/groups.ts";
import { channelModels } from "@/admin/channel.ts";

type CompProps<T> = {
data: T;
Expand Down Expand Up @@ -543,6 +544,55 @@ function Common({ data, dispatch, onChange }: CompProps<CommonState>) {
configParagraph={true}
isCollapsed={true}
>
<ParagraphItem>
<Label className={`flex flex-row items-center`}>
{t("admin.system.cache")}
<Tips content={t("admin.system.cacheTip")} />
</Label>
<MultiCombobox
value={data.cache}
onChange={(value) => {
dispatch({ type: "update:common.cache", value });
}}
list={channelModels}
placeholder={t("admin.system.cachePlaceholder", {
length: (data.cache ?? []).length,
})}
/>
</ParagraphItem>
<ParagraphItem>
<Label>
{t("admin.system.cacheExpired")}
<Tips
className={`inline-block`}
content={t("admin.system.cacheExpiredTip")}
/>
</Label>
<NumberInput
value={data.expire}
onValueChange={(value) =>
dispatch({ type: "update:common.expire", value })
}
min={0}
/>
</ParagraphItem>
<ParagraphItem>
<Label>
{t("admin.system.cacheSize")}
<Tips
className={`inline-block`}
content={t("admin.system.cacheSizeTip")}
/>
</Label>
<NumberInput
value={data.size}
onValueChange={(value) =>
dispatch({ type: "update:common.size", value })
}
min={0}
/>
</ParagraphItem>
<ParagraphSpace />
<ParagraphItem>
<Label className={`flex flex-row items-center`}>
{t("admin.system.article")}
Expand Down
29 changes: 28 additions & 1 deletion channel/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,11 @@ type searchState struct {
}

type commonState struct {
Cache []string `json:"cache" mapstructure:"cache"`
Article []string `json:"article" mapstructure:"article"`
Generation []string `json:"generation" mapstructure:"generation"`
Cache []string `json:"cache" mapstructure:"cache"`
Expire int64 `json:"expire" mapstructure:"expire"`
Size int64 `json:"size" mapstructure:"size"`
}

type SystemConfig struct {
Expand All @@ -85,6 +87,10 @@ func (c *SystemConfig) Load() {

globals.ArticlePermissionGroup = c.Common.Article
globals.GenerationPermissionGroup = c.Common.Generation
globals.CacheAcceptedModels = c.Common.Cache

globals.CacheAcceptedExpire = c.GetCacheAcceptedExpire()
globals.CacheAcceptedSize = c.GetCacheAcceptedSize()
}

func (c *SystemConfig) SaveConfig() error {
Expand Down Expand Up @@ -218,3 +224,24 @@ func (c *SystemConfig) GetAppLogo() string {

return logo
}

func (c *SystemConfig) GetCacheAcceptedModels() []string {
return c.Common.Cache
}

func (c *SystemConfig) GetCacheAcceptedExpire() int64 {
if c.Common.Expire <= 0 {
// default 1 hour
return 3600
}

return c.Common.Expire
}

func (c *SystemConfig) GetCacheAcceptedSize() int64 {
if c.Common.Size < 1 {
return 1
}

return c.Common.Size
}
3 changes: 3 additions & 0 deletions globals/variables.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ var AllowedOrigins = []string{
var NotifyUrl = ""
var ArticlePermissionGroup []string
var GenerationPermissionGroup []string
var CacheAcceptedModels []string
var CacheAcceptedExpire int64
var CacheAcceptedSize int64

func OriginIsAllowed(uri string) bool {
instance, _ := url.Parse(uri)
Expand Down

0 comments on commit 65acc65

Please sign in to comment.