Files
new-api/web/default/src/features/usage-logs/components/model-badge.tsx
T

227 lines
6.1 KiB
TypeScript
Vendored

/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
For commercial licensing, please contact support@quantumnous.com
*/
import { Route } from 'lucide-react'
import { useTranslation } from 'react-i18next'
import { CopyableStatusBadge, StatusBadge } from '@/components/status-badge'
import {
Popover,
PopoverContent,
PopoverTrigger,
} from '@/components/ui/popover'
import { getIdentityColorClass } from '@/lib/colors'
import { getLobeIcon } from '@/lib/lobe-icon'
import { cn } from '@/lib/utils'
interface ModelBadgeProps {
modelName: string
actualModel?: string
className?: string
}
interface ModelProvider {
icon: string
label: string
}
function resolveModelProvider(modelName: string): ModelProvider | null {
const model = modelName.toLowerCase()
const hasAny = (keywords: string[]) =>
keywords.some((keyword) => model.includes(keyword))
if (
hasAny([
'gpt-',
'chatgpt-',
'text-embedding-',
'omni-moderation',
'dall-e',
'whisper',
'tts-',
]) ||
/\bo[134](?:-|$)/.test(model)
) {
return { icon: 'OpenAI.Color', label: 'OpenAI' }
}
if (hasAny(['claude-', 'anthropic'])) {
return { icon: 'Claude.Color', label: 'Claude' }
}
if (hasAny(['gemini-', 'learnlm-'])) {
return { icon: 'Gemini.Color', label: 'Gemini' }
}
if (hasAny(['grok-', 'xai-'])) {
return { icon: 'Grok.Color', label: 'Grok' }
}
if (hasAny(['deepseek-'])) {
return { icon: 'DeepSeek.Color', label: 'DeepSeek' }
}
if (hasAny(['qwen', 'qwq-'])) {
return { icon: 'Qwen.Color', label: 'Qwen' }
}
if (hasAny(['doubao-', 'volcengine'])) {
return { icon: 'Doubao.Color', label: 'Doubao' }
}
if (hasAny(['moonshot-', 'kimi-'])) {
return { icon: 'Moonshot.Color', label: 'Moonshot' }
}
if (hasAny(['minimax', 'abab'])) {
return { icon: 'Minimax.Color', label: 'MiniMax' }
}
if (hasAny(['glm-', 'chatglm', 'cogview', 'cogvideo'])) {
return { icon: 'Zhipu.Color', label: 'Zhipu' }
}
if (hasAny(['mimo-'])) {
return { icon: 'XiaomiMiMo', label: 'MiMo' }
}
if (hasAny(['ernie'])) {
return { icon: 'Wenxin.Color', label: 'Baidu' }
}
if (hasAny(['spark'])) {
return { icon: 'Spark.Color', label: 'iFlyTek' }
}
if (hasAny(['hunyuan'])) {
return { icon: 'Hunyuan.Color', label: 'Tencent' }
}
if (hasAny(['baichuan'])) {
return { icon: 'Baichuan.Color', label: 'Baichuan' }
}
if (hasAny(['internlm'])) {
return { icon: 'InternLM.Color', label: 'InternLM' }
}
if (hasAny(['step-'])) {
return { icon: 'Stepfun.Color', label: 'StepFun' }
}
if (hasAny(['yi-'])) {
return { icon: 'Yi.Color', label: 'Yi' }
}
if (hasAny(['mistral-', 'mixtral-'])) {
return { icon: 'Mistral.Color', label: 'Mistral' }
}
if (hasAny(['llama-', 'meta-'])) {
return { icon: 'Meta.Color', label: 'Meta' }
}
if (hasAny(['command-', 'cohere-'])) {
return { icon: 'Cohere.Color', label: 'Cohere' }
}
return null
}
function ModelBadgeContent(
props: ModelBadgeProps & {
staticOnly: boolean
}
) {
const provider = resolveModelProvider(props.modelName)
const colorClassName = getIdentityColorClass(props.modelName)
const content = [
provider ? (
<span
key='provider'
data-icon='inline-start'
className='flex items-center justify-center'
aria-label={provider.label}
>
{getLobeIcon(provider.icon, 12)}
</span>
) : null,
<span key='model' className='whitespace-nowrap'>
{props.modelName}
</span>,
]
if (props.staticOnly) {
return (
<StatusBadge
variant='neutral'
appearance='soft'
size='sm'
className={cn(
'h-5! max-w-none shrink-0 whitespace-nowrap! [&_[data-slot=status-badge-label]]:whitespace-nowrap!',
colorClassName,
props.className
)}
>
{content}
</StatusBadge>
)
}
return (
<CopyableStatusBadge
value={props.modelName}
variant='neutral'
appearance='soft'
size='sm'
className={cn(
'h-5! max-w-none shrink-0 whitespace-nowrap! [&_[data-slot=status-badge-label]]:whitespace-nowrap!',
colorClassName,
props.className
)}
>
{content}
</CopyableStatusBadge>
)
}
export function ModelBadge(props: ModelBadgeProps) {
const { t } = useTranslation()
if (!props.actualModel) {
return <ModelBadgeContent {...props} staticOnly={false} />
}
return (
<Popover>
<PopoverTrigger
render={
<button type='button' className='inline-flex items-center gap-1' />
}
>
<ModelBadgeContent {...props} staticOnly />
<Route className='text-muted-foreground size-3 shrink-0' />
</PopoverTrigger>
<PopoverContent className='w-72'>
<div className='space-y-2'>
<div className='flex items-start justify-between gap-3'>
<span className='text-muted-foreground text-xs'>
{t('Request Model:')}
</span>
<ModelBadgeContent
modelName={props.modelName}
staticOnly
className='max-w-[11rem]'
/>
</div>
<div className='flex items-start justify-between gap-3'>
<span className='text-muted-foreground text-xs'>
{t('Actual Model:')}
</span>
<ModelBadgeContent
modelName={props.actualModel}
staticOnly
className='max-w-[11rem]'
/>
</div>
</div>
</PopoverContent>
</Popover>
)
}