-
-
Notifications
You must be signed in to change notification settings - Fork 11k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat: support openai model fetcher
- Loading branch information
Showing
32 changed files
with
653 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { NextResponse } from 'next/server'; | ||
|
||
import { getPreferredRegion } from '@/app/api/config'; | ||
import { createErrorResponse } from '@/app/api/errorResponse'; | ||
import { ChatCompletionErrorPayload } from '@/libs/agent-runtime'; | ||
import { ChatErrorType } from '@/types/fetch'; | ||
|
||
import AgentRuntime from '../../agentRuntime'; | ||
import { checkAuth } from '../../auth'; | ||
|
||
export const runtime = 'edge'; | ||
|
||
export const preferredRegion = getPreferredRegion(); | ||
|
||
export const GET = checkAuth(async (req, { params, jwtPayload }) => { | ||
const { provider } = params; | ||
|
||
try { | ||
const agentRuntime = await AgentRuntime.initializeWithUserPayload(provider, jwtPayload); | ||
|
||
const list = await agentRuntime.models(); | ||
|
||
return NextResponse.json(list); | ||
} catch (e) { | ||
const { | ||
errorType = ChatErrorType.InternalServerError, | ||
error: errorContent, | ||
...res | ||
} = e as ChatCompletionErrorPayload; | ||
|
||
const error = errorContent || e; | ||
// track the error at server side | ||
console.error(`Route: [${provider}] ${errorType}:`, error); | ||
|
||
return createErrorResponse(errorType, { error, ...res, provider }); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
src/app/settings/llm/components/ProviderModelList/ModelFetcher.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Icon, Tooltip } from '@lobehub/ui'; | ||
import { Typography } from 'antd'; | ||
import { createStyles } from 'antd-style'; | ||
import dayjs from 'dayjs'; | ||
import { LucideLoaderCircle, LucideRefreshCcwDot } from 'lucide-react'; | ||
import { memo } from 'react'; | ||
import { Flexbox } from 'react-layout-kit'; | ||
|
||
import { useGlobalStore } from '@/store/global'; | ||
import { modelConfigSelectors } from '@/store/global/selectors'; | ||
import { GlobalLLMProviderKey } from '@/types/settings'; | ||
|
||
const useStyles = createStyles(({ css, token }) => ({ | ||
hover: css` | ||
cursor: pointer; | ||
padding: 4px 8px; | ||
border-radius: ${token.borderRadius}px; | ||
transition: all 0.2s ease-in-out; | ||
&:hover { | ||
color: ${token.colorText}; | ||
background-color: ${token.colorFillSecondary}; | ||
} | ||
`, | ||
})); | ||
|
||
interface ModelFetcherProps { | ||
provider: GlobalLLMProviderKey; | ||
} | ||
|
||
const ModelFetcher = memo<ModelFetcherProps>(({ provider }) => { | ||
const { styles } = useStyles(); | ||
const [useFetchProviderModelList] = useGlobalStore((s) => [ | ||
s.useFetchProviderModelList, | ||
s.setModelProviderConfig, | ||
]); | ||
const enabledAutoFetch = useGlobalStore(modelConfigSelectors.enabledAutoFetchModels(provider)); | ||
const latestFetchTime = useGlobalStore( | ||
(s) => modelConfigSelectors.providerConfig(provider)(s)?.latestFetchTime, | ||
); | ||
const totalModels = useGlobalStore( | ||
(s) => modelConfigSelectors.providerModelCards(provider)(s).length, | ||
); | ||
|
||
const { mutate, isValidating } = useFetchProviderModelList(provider, enabledAutoFetch); | ||
|
||
return ( | ||
<Typography.Text style={{ fontSize: 12 }} type={'secondary'}> | ||
<Flexbox align={'center'} gap={0} horizontal justify={'space-between'}> | ||
<div>共 {totalModels} 个模型可用</div> | ||
<Tooltip title={`上次更新时间:${dayjs(latestFetchTime).format('MM-DD HH:mm:ss')}`}> | ||
<Flexbox | ||
align={'center'} | ||
className={styles.hover} | ||
gap={4} | ||
horizontal | ||
onClick={() => mutate()} | ||
> | ||
<Icon | ||
icon={isValidating ? LucideLoaderCircle : LucideRefreshCcwDot} | ||
size={'small'} | ||
spin={isValidating} | ||
/> | ||
<div>{isValidating ? '正在获取模型列表...' : '获取模型列表'}</div> | ||
</Flexbox> | ||
</Tooltip> | ||
</Flexbox> | ||
</Typography.Text> | ||
); | ||
}); | ||
export default ModelFetcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.