Skip to content
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

fix: Deployments should be cached for non-model-information #102

Merged
merged 9 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 57 additions & 34 deletions packages/gen-ai-hub/src/utils/deployment-cache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,42 +28,65 @@ describe('deployment cache', () => {
});
});

it('should cache all deployments independent of potentially given models', () => {
const opts = {
scenarioId: 'foundation-models',
model: { name: 'gpt-4o', version: 'latest' }
};
describe('should cache all deployments independent of potentially given models', () => {
beforeEach(() => {
deploymentCache.setAll(
{
scenarioId: 'foundation-models',
model: { name: 'gpt-4o', version: 'latest' }
},
[
mockAiDeployment('deployment-id1', {
name: 'gpt-35-turbo',
version: 'latest'
}),
mockAiDeployment('deployment-id2', {
name: 'gpt-35-turbo',
version: '123'
})
]
);
});

deploymentCache.setAll(opts, [
mockAiDeployment('deployment-id1', {
name: 'gpt-35-turbo',
version: 'latest'
}),
mockAiDeployment('deployment-id2', {
name: 'gpt-35-turbo',
version: '123'
})
]);
it('cache nothing for unlisted model names', () => {
expect(
deploymentCache.get({
scenarioId: 'foundation-models',
model: { name: 'gpt-4o', version: 'latest' }
})
).toBeUndefined();
});

expect(deploymentCache.get(opts)).toBeUndefined();
expect(
deploymentCache.get({
...opts,
model: {
name: 'gpt-35-turbo',
version: 'latest'
}
})?.id
).toEqual('deployment-id1');
expect(
deploymentCache.get({
...opts,
model: {
name: 'gpt-35-turbo',
version: '123'
}
})?.id
).toEqual('deployment-id2');
it('retrieve the deployment matching the model name and version', () => {
expect(
deploymentCache.get({
scenarioId: 'foundation-models',
model: {
name: 'gpt-35-turbo',
version: '123'
}
})?.id
).toEqual('deployment-id2');
});

it('retrieve the first deployment matching the model name when version is missing', () => {
expect(
deploymentCache.get({
scenarioId: 'foundation-models',
model: {
name: 'gpt-35-turbo'
}
})?.id
).toEqual('deployment-id1');
});

it('retrieve the deployment when model is missing', () => {
expect(
deploymentCache.get({
scenarioId: 'foundation-models'
})?.id
).toEqual('deployment-id1');
});
});

it('should cache only the first deployments for equal models and versions', () => {
Expand Down
22 changes: 16 additions & 6 deletions packages/gen-ai-hub/src/utils/deployment-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function getCacheKey({
model,
resourceGroup = 'default'
}: DeploymentResolutionOptions) {
return `${scenarioId}-${executableId}-${model?.name ?? ''}-${model?.version ?? ''}-${resourceGroup}`;
return `${scenarioId}:${executableId}:${model?.name ?? ''}:${model?.version ?? ''}:${resourceGroup}`;
}

interface Deployment {
Expand Down Expand Up @@ -51,15 +51,25 @@ function createDeploymentCache(cache: Cache<Deployment>) {
* @param deployments - Deployments to retrieve the IDs and models from.
*/
setAll: (
opts: Omit<DeploymentResolutionOptions, 'model'>,
opts: DeploymentResolutionOptions,
deployments: AiDeployment[]
): void => {
// go backwards to cache the first deployment ID for each model
[...deployments].reverse().forEach(deployment => {
cache.set(getCacheKey({ ...opts, model: extractModel(deployment) }), {
entry: transformDeploymentForCache(deployment)
[...deployments]
.reverse()
.map(deployment => transformDeploymentForCache(deployment))
.flatMap(entry => [
entry,
{ id: entry.id },
...(entry.model
? [{ id: entry.id, model: { name: entry.model.name } }]
: [])
])
.forEach(entry => {
cache.set(getCacheKey({ ...opts, model: entry.model }), {
entry
});
});
});
},
clear: () => cache.clear()
};
Expand Down
12 changes: 6 additions & 6 deletions packages/gen-ai-hub/src/utils/deployment-resolver.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,23 @@ describe('deployment resolver', () => {
const id = await resolveDeploymentId({
scenarioId: 'foundation-models'
});
expect(id).toBe('1');
expect(id).toEqual('1');
});

it('should return the first deployment with the correct model name', async () => {
const id = await resolveDeploymentId({
scenarioId: 'foundation-models',
model: { name: 'gpt-4o' }
});
expect(id).toBe('1');
expect(id).toEqual('1');
});

it('should return the deployment with the correct model name and version', async () => {
const id = await resolveDeploymentId({
scenarioId: 'foundation-models',
model: { name: 'gpt-4o', version: '0613' }
});
expect(id).toBe('2');
expect(id).toEqual('2');
});

it('should retrieve deployment from cache if available', async () => {
Expand All @@ -55,8 +55,8 @@ describe('deployment resolver', () => {
};
deploymentCache.set(opts, { id: '1' } as AiDeployment);
const id = await resolveDeploymentId(opts);
expect(id).toBe('1');
expect(nock.isDone()).toBe(false);
expect(id).toEqual('1');
expect(nock.isDone()).toEqual(false);
});

it('should throw in case no deployment with the given model name is found', async () => {
Expand Down Expand Up @@ -131,7 +131,7 @@ describe('deployment resolver', () => {
resourceGroup: 'otherId'
});

expect(id).toBe('5');
expect(id).toEqual('5');
});
});

Expand Down
Loading