-
Notifications
You must be signed in to change notification settings - Fork 784
feature: inference profiles for bedrock #1118
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
Changes from all commits
0fc1cfa
d1e5b61
5c9e26a
96217dd
6d1e469
e982ce3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -10,7 +10,7 @@ import { | |||||||||||||||||||||||||||||||||||||||||
| } from './chatComplete'; | ||||||||||||||||||||||||||||||||||||||||||
| import { Options } from '../../types/requestBody'; | ||||||||||||||||||||||||||||||||||||||||||
| import { GatewayError } from '../../errors/GatewayError'; | ||||||||||||||||||||||||||||||||||||||||||
| import { BedrockFinetuneRecord } from './types'; | ||||||||||||||||||||||||||||||||||||||||||
| import { BedrockFinetuneRecord, BedrockInferenceProfile } from './types'; | ||||||||||||||||||||||||||||||||||||||||||
| import { FinetuneRequest } from '../types'; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const generateAWSHeaders = async ( | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -404,3 +404,80 @@ export const populateHyperParameters = (value: FinetuneRequest) => { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return hyperParameters; | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const getInferenceProfile = async ( | ||||||||||||||||||||||||||||||||||||||||||
| inferenceProfileIdentifier: string, | ||||||||||||||||||||||||||||||||||||||||||
| awsRegion: string, | ||||||||||||||||||||||||||||||||||||||||||
| awsAccessKeyId: string, | ||||||||||||||||||||||||||||||||||||||||||
| awsSecretAccessKey: string, | ||||||||||||||||||||||||||||||||||||||||||
| awsSessionToken?: string | ||||||||||||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||||||||||||
| const url = `https://bedrock.${awsRegion}.amazonaws.com/inference-profiles/${encodeURIComponent(decodeURIComponent(inferenceProfileIdentifier))}`; | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+408
to
+415
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔒 Security Issue Fix Issue: The getInferenceProfile function doesn't validate the inferenceProfileIdentifier before using it in the URL, which could potentially lead to URL manipulation issues.
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const headers = await generateAWSHeaders( | ||||||||||||||||||||||||||||||||||||||||||
| undefined, | ||||||||||||||||||||||||||||||||||||||||||
| { 'content-type': 'application/json' }, | ||||||||||||||||||||||||||||||||||||||||||
| url, | ||||||||||||||||||||||||||||||||||||||||||
| 'GET', | ||||||||||||||||||||||||||||||||||||||||||
| 'bedrock', | ||||||||||||||||||||||||||||||||||||||||||
| awsRegion, | ||||||||||||||||||||||||||||||||||||||||||
| awsAccessKeyId, | ||||||||||||||||||||||||||||||||||||||||||
| awsSecretAccessKey, | ||||||||||||||||||||||||||||||||||||||||||
| awsSessionToken | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const response = await fetch(url, { | ||||||||||||||||||||||||||||||||||||||||||
| method: 'GET', | ||||||||||||||||||||||||||||||||||||||||||
| headers, | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (!response.ok) { | ||||||||||||||||||||||||||||||||||||||||||
| throw new Error( | ||||||||||||||||||||||||||||||||||||||||||
| `Failed to get inference profile: ${response.status} ${response.statusText}` | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return (await response.json()) as BedrockInferenceProfile; | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| console.error('Error getting inference profile:', error); | ||||||||||||||||||||||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| export const getFoundationModelFromInferenceProfile = async ( | ||||||||||||||||||||||||||||||||||||||||||
| c: Context, | ||||||||||||||||||||||||||||||||||||||||||
| inferenceProfileIdentifier: string, | ||||||||||||||||||||||||||||||||||||||||||
| providerOptions: Options | ||||||||||||||||||||||||||||||||||||||||||
| ) => { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| const getFromCacheByKey = c.get('getFromCacheByKey'); | ||||||||||||||||||||||||||||||||||||||||||
| const putInCacheWithValue = c.get('putInCacheWithValue'); | ||||||||||||||||||||||||||||||||||||||||||
| const cacheKey = `bedrock-inference-profile-${inferenceProfileIdentifier}`; | ||||||||||||||||||||||||||||||||||||||||||
| const cachedFoundationModel = getFromCacheByKey | ||||||||||||||||||||||||||||||||||||||||||
| ? await getFromCacheByKey(env(c), cacheKey) | ||||||||||||||||||||||||||||||||||||||||||
| : null; | ||||||||||||||||||||||||||||||||||||||||||
| if (cachedFoundationModel) { | ||||||||||||||||||||||||||||||||||||||||||
| return cachedFoundationModel; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| const inferenceProfile = await getInferenceProfile( | ||||||||||||||||||||||||||||||||||||||||||
| inferenceProfileIdentifier || '', | ||||||||||||||||||||||||||||||||||||||||||
| providerOptions.awsRegion || '', | ||||||||||||||||||||||||||||||||||||||||||
| providerOptions.awsAccessKeyId || '', | ||||||||||||||||||||||||||||||||||||||||||
| providerOptions.awsSecretAccessKey || '', | ||||||||||||||||||||||||||||||||||||||||||
| providerOptions.awsSessionToken || '' | ||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // modelArn is always like arn:aws:bedrock:us-east-1::foundation-model/anthropic.claude-v2:1 | ||||||||||||||||||||||||||||||||||||||||||
| const foundationModel = inferenceProfile?.models?.[0]?.modelArn | ||||||||||||||||||||||||||||||||||||||||||
| ?.split('/') | ||||||||||||||||||||||||||||||||||||||||||
| ?.pop(); | ||||||||||||||||||||||||||||||||||||||||||
| if (putInCacheWithValue) { | ||||||||||||||||||||||||||||||||||||||||||
| putInCacheWithValue(env(c), cacheKey, foundationModel, 86400); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| return foundationModel; | ||||||||||||||||||||||||||||||||||||||||||
| } catch (error) { | ||||||||||||||||||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||||||
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.
🛠️ Code Refactor
Issue: The code doesn't handle the case where foundationModel extraction fails but still attempts to use it.
Fix: Add a check to ensure foundationModel is defined before setting it in params.
Impact: Prevents potential undefined values from being used in the model parameter.