-
Notifications
You must be signed in to change notification settings - Fork 3.6k
feat: add LiteLLM as AI gateway provider #4644
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
RheagalFire
wants to merge
4
commits into
simstudioai:main
Choose a base branch
from
RheagalFire:feat/add-litellm-provider
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
fe39f57
feat: add LiteLLM as AI gateway provider
RheagalFire 9667e88
fix: add litellm to attachments, provider store, utils, and block guards
RheagalFire 475820a
fix: add frontend model discovery pipeline for litellm provider
RheagalFire b4268ec
fix: remove azureEndpoint fallback from LiteLLM provider
RheagalFire File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,70 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { getErrorMessage } from '@sim/utils/errors' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { | ||
| providerModelsResponseSchema, | ||
| vllmUpstreamResponseSchema, | ||
| } from '@/lib/api/contracts/providers' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { filterBlacklistedModels, isProviderBlacklisted } from '@/providers/utils' | ||
|
|
||
| const logger = createLogger('LiteLLMModelsAPI') | ||
|
|
||
| export const GET = withRouteHandler(async (_request: NextRequest) => { | ||
| if (isProviderBlacklisted('litellm')) { | ||
| logger.info('LiteLLM provider is blacklisted, returning empty models') | ||
| return NextResponse.json({ models: [] }) | ||
| } | ||
|
|
||
| const baseUrl = (env.LITELLM_BASE_URL || '').replace(/\/$/, '') | ||
|
|
||
| if (!baseUrl) { | ||
| logger.info('LITELLM_BASE_URL not configured') | ||
| return NextResponse.json({ models: [] }) | ||
| } | ||
|
|
||
| try { | ||
| logger.info('Fetching LiteLLM models', { baseUrl }) | ||
|
|
||
| const headers: Record<string, string> = { | ||
| 'Content-Type': 'application/json', | ||
| } | ||
|
|
||
| if (env.LITELLM_API_KEY) { | ||
| headers.Authorization = `Bearer ${env.LITELLM_API_KEY}` | ||
| } | ||
|
|
||
| const response = await fetch(`${baseUrl}/v1/models`, { | ||
| headers, | ||
| next: { revalidate: 60 }, | ||
| }) | ||
|
|
||
| if (!response.ok) { | ||
| logger.warn('LiteLLM service is not available', { | ||
| status: response.status, | ||
| statusText: response.statusText, | ||
| }) | ||
| return NextResponse.json({ models: [] }) | ||
| } | ||
|
|
||
| const data = vllmUpstreamResponseSchema.parse(await response.json()) | ||
| const allModels = data.data.map((model) => `litellm/${model.id}`) | ||
| const models = filterBlacklistedModels(allModels) | ||
|
|
||
| logger.info('Successfully fetched LiteLLM models', { | ||
| count: models.length, | ||
| filtered: allModels.length - models.length, | ||
| models, | ||
| }) | ||
|
|
||
| return NextResponse.json(providerModelsResponseSchema.parse({ models })) | ||
| } catch (error) { | ||
| logger.error('Failed to fetch LiteLLM models', { | ||
| error: getErrorMessage(error, 'Unknown error'), | ||
| baseUrl, | ||
| }) | ||
|
|
||
| return NextResponse.json({ models: [] }) | ||
| } | ||
| }) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.