-
Notifications
You must be signed in to change notification settings - Fork 61
feat: Add Vercel AI Gateway as a Provider #768
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
tarai-dl
wants to merge
1
commit into
Merit-Systems:master
Choose a base branch
from
tarai-dl:rn/vercel-ai-gateway
base: master
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
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
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
124 changes: 124 additions & 0 deletions
124
packages/app/server/src/providers/VercelAIGatewayProvider.ts
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,124 @@ | ||
| import { LlmTransactionMetadata, Transaction } from '../types'; | ||
| import { BaseProvider } from './BaseProvider'; | ||
| import { ProviderType } from './ProviderType'; | ||
| import { getCostPerToken } from '../services/AccountingService'; | ||
| import logger from '../logger'; | ||
| import { env } from '../env'; | ||
|
|
||
| interface CompletionStateBody { | ||
| id: string; | ||
| usage: { | ||
| prompt_tokens: number; | ||
| completion_tokens: number; | ||
| total_tokens: number; | ||
| }; | ||
| } | ||
|
|
||
| interface StreamingChunkBody { | ||
| id: string; | ||
| choices: { | ||
| index: number; | ||
| delta: { | ||
| content?: string; | ||
| }; | ||
| finish_reason: string | null; | ||
| }[]; | ||
| usage: { | ||
| prompt_tokens: number; | ||
| completion_tokens: number; | ||
| total_tokens: number; | ||
| } | null; | ||
| } | ||
|
|
||
| const parseSSEGPTFormat = (data: string): StreamingChunkBody[] => { | ||
| // Split by double newlines to separate events | ||
| const events = data.split('\n\n'); | ||
| const chunks: StreamingChunkBody[] = []; | ||
|
|
||
| for (const event of events) { | ||
| if (!event.trim()) continue; | ||
|
|
||
| // Each event should start with 'data: ' | ||
| if (event.startsWith('data: ')) { | ||
| const jsonStr = event.slice(6); // Remove 'data: ' prefix | ||
|
|
||
| // Skip [DONE] marker | ||
| if (jsonStr.trim() === '[DONE]') continue; | ||
|
|
||
| try { | ||
| const parsed = JSON.parse(jsonStr); | ||
| chunks.push(parsed); | ||
| } catch (error) { | ||
| logger.error(`Error parsing SSE chunk: ${error}`); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return chunks; | ||
| }; | ||
|
|
||
| export class VercelAIGatewayProvider extends BaseProvider { | ||
| getType(): ProviderType { | ||
| return ProviderType.VERCEL_AI_GATEWAY; | ||
| } | ||
|
|
||
| getBaseUrl(): string { | ||
| return this.VERCEL_AI_GATEWAY_BASE_URL; | ||
| } | ||
|
|
||
| getApiKey(): string | undefined { | ||
| return env.VERCEL_AI_GATEWAY_API_KEY; | ||
| } | ||
|
|
||
| async handleBody(data: string): Promise<Transaction> { | ||
| try { | ||
| let prompt_tokens = 0; | ||
| let completion_tokens = 0; | ||
| let total_tokens = 0; | ||
| let providerId = 'null'; | ||
|
|
||
| if (this.getIsStream()) { | ||
| const chunks = parseSSEGPTFormat(data); | ||
|
|
||
| for (const chunk of chunks) { | ||
| if (chunk.usage && chunk.usage !== null) { | ||
| prompt_tokens += chunk.usage.prompt_tokens; | ||
| completion_tokens += chunk.usage.completion_tokens; | ||
| total_tokens += chunk.usage.total_tokens; | ||
| } | ||
| providerId = chunk.id || 'null'; | ||
| } | ||
| } else { | ||
| const parsed = JSON.parse(data) as CompletionStateBody; | ||
| prompt_tokens += parsed.usage.prompt_tokens; | ||
| completion_tokens += parsed.usage.completion_tokens; | ||
| total_tokens += parsed.usage.total_tokens; | ||
| providerId = parsed.id || 'null'; | ||
| } | ||
|
|
||
| const cost = getCostPerToken( | ||
| this.getModel(), | ||
| prompt_tokens, | ||
| completion_tokens | ||
| ); | ||
|
|
||
| const metadata: LlmTransactionMetadata = { | ||
| providerId: providerId, | ||
| provider: this.getType(), | ||
| model: this.getModel(), | ||
| inputTokens: prompt_tokens, | ||
| outputTokens: completion_tokens, | ||
| totalTokens: total_tokens, | ||
| }; | ||
|
|
||
| return { | ||
| metadata: metadata, | ||
| rawTransactionCost: cost, | ||
| status: 'success', | ||
| }; | ||
| } catch (error) { | ||
| logger.error(`Error processing data: ${error}`); | ||
| throw error; | ||
| } | ||
| } | ||
| } |
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
140 changes: 140 additions & 0 deletions
140
packages/sdk/ts/src/supported-models/chat/vercel-ai-gateway.ts
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,140 @@ | ||
| import { SupportedModel } from '../types'; | ||
|
|
||
| // Union type of all valid Vercel AI Gateway model IDs | ||
| // The Vercel AI Gateway provides access to models from multiple providers | ||
| // through a single OpenAI-compatible API endpoint | ||
| export type VercelAIGatewayModel = | ||
| | 'gpt-4o' | ||
| | 'gpt-4o-mini' | ||
| | 'gpt-4-turbo' | ||
| | 'gpt-4' | ||
| | 'gpt-3.5-turbo' | ||
| | 'claude-3-opus-20240229' | ||
| | 'claude-3-sonnet-20240229' | ||
| | 'claude-3-haiku-20240307' | ||
| | 'claude-3-5-sonnet-20240620' | ||
| | 'gemini-1.5-pro' | ||
| | 'gemini-1.5-flash' | ||
| | 'gemini-1.0-pro' | ||
| | 'mistral-large-latest' | ||
| | 'mistral-medium-latest' | ||
| | 'mistral-small-latest' | ||
| | 'llama-3.1-405b-instruct' | ||
| | 'llama-3.1-70b-instruct' | ||
| | 'llama-3.1-8b-instruct'; | ||
|
|
||
| export const VercelAIGatewayModels: SupportedModel[] = [ | ||
| // OpenAI models via Vercel AI Gateway | ||
| { | ||
| model_id: 'gpt-4o', | ||
| input_cost_per_token: 0.000005, | ||
| output_cost_per_token: 0.000015, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gpt-4o-mini', | ||
| input_cost_per_token: 1.5e-7, | ||
| output_cost_per_token: 6e-7, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gpt-4-turbo', | ||
| input_cost_per_token: 0.00001, | ||
| output_cost_per_token: 0.00003, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gpt-4', | ||
| input_cost_per_token: 0.00003, | ||
| output_cost_per_token: 0.00006, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gpt-3.5-turbo', | ||
| input_cost_per_token: 5e-7, | ||
| output_cost_per_token: 0.0000015, | ||
| provider: 'Vercel', | ||
| }, | ||
| // Anthropic models via Vercel AI Gateway | ||
| { | ||
| model_id: 'claude-3-opus-20240229', | ||
| input_cost_per_token: 0.000015, | ||
| output_cost_per_token: 0.000075, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'claude-3-sonnet-20240229', | ||
| input_cost_per_token: 0.000003, | ||
| output_cost_per_token: 0.000015, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'claude-3-haiku-20240307', | ||
| input_cost_per_token: 2.5e-7, | ||
| output_cost_per_token: 0.00000125, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'claude-3-5-sonnet-20240620', | ||
| input_cost_per_token: 0.000003, | ||
| output_cost_per_token: 0.000015, | ||
| provider: 'Vercel', | ||
| }, | ||
| // Google models via Vercel AI Gateway | ||
| { | ||
| model_id: 'gemini-1.5-pro', | ||
| input_cost_per_token: 0.0000035, | ||
| output_cost_per_token: 0.0000105, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gemini-1.5-flash', | ||
| input_cost_per_token: 3.5e-7, | ||
| output_cost_per_token: 0.00000105, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'gemini-1.0-pro', | ||
| input_cost_per_token: 5e-7, | ||
| output_cost_per_token: 0.0000015, | ||
| provider: 'Vercel', | ||
| }, | ||
| // Mistral models via Vercel AI Gateway | ||
| { | ||
| model_id: 'mistral-large-latest', | ||
| input_cost_per_token: 0.000004, | ||
| output_cost_per_token: 0.000012, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'mistral-medium-latest', | ||
| input_cost_per_token: 0.0000027, | ||
| output_cost_per_token: 0.0000081, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'mistral-small-latest', | ||
| input_cost_per_token: 0.000001, | ||
| output_cost_per_token: 0.000003, | ||
| provider: 'Vercel', | ||
| }, | ||
| // Meta Llama models via Vercel AI Gateway | ||
| { | ||
| model_id: 'llama-3.1-405b-instruct', | ||
| input_cost_per_token: 0.000005, | ||
| output_cost_per_token: 0.000016, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'llama-3.1-70b-instruct', | ||
| input_cost_per_token: 9e-7, | ||
| output_cost_per_token: 0.0000009, | ||
| provider: 'Vercel', | ||
| }, | ||
| { | ||
| model_id: 'llama-3.1-8b-instruct', | ||
| input_cost_per_token: 2e-7, | ||
| output_cost_per_token: 2e-7, | ||
| provider: 'Vercel', | ||
| }, | ||
| ]; | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Vercel AI Gateway models use the same model IDs as native providers (OpenAI, Anthropic), causing them to silently override the native provider mappings when
VercelAIGatewayModelsis spread last intoALL_SUPPORTED_MODELS.