Skip to content

Commit bd4ff4d

Browse files
author
Kamil Sobol
authored
Add memory setting to conversation handler (#2143)
* Add memory setting to conversation handler * pr feedback
1 parent 853b8b1 commit bd4ff4d

File tree

9 files changed

+128
-1
lines changed

9 files changed

+128
-1
lines changed

.changeset/red-candles-fly.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-function': patch
3+
---
4+
5+
Fix jsdocs that incorrectly state default memory settings

.changeset/tiny-ears-fly.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@aws-amplify/ai-constructs': patch
3+
'@aws-amplify/backend-ai': patch
4+
---
5+
6+
Add memory setting to conversation handler

packages/ai-constructs/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ type ConversationHandlerFunctionProps = {
5454
modelId: string;
5555
region?: string;
5656
}>;
57+
memoryMB?: number;
5758
outputStorageStrategy?: BackendOutputStorageStrategy<AIConversationOutput>;
5859
};
5960

packages/ai-constructs/src/conversation/conversation_handler_construct.test.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,4 +222,66 @@ void describe('Conversation Handler Function construct', () => {
222222
Handler: 'index.handler',
223223
});
224224
});
225+
226+
void describe('memory property', () => {
227+
void it('sets valid memory', () => {
228+
const app = new App();
229+
const stack = new Stack(app);
230+
new ConversationHandlerFunction(stack, 'conversationHandler', {
231+
models: [],
232+
memoryMB: 234,
233+
});
234+
const template = Template.fromStack(stack);
235+
236+
template.hasResourceProperties('AWS::Lambda::Function', {
237+
MemorySize: 234,
238+
});
239+
});
240+
241+
void it('sets default memory', () => {
242+
const app = new App();
243+
const stack = new Stack(app);
244+
new ConversationHandlerFunction(stack, 'conversationHandler', {
245+
models: [],
246+
});
247+
const template = Template.fromStack(stack);
248+
249+
template.hasResourceProperties('AWS::Lambda::Function', {
250+
MemorySize: 512,
251+
});
252+
});
253+
254+
void it('throws on memory below 128 MB', () => {
255+
assert.throws(() => {
256+
const app = new App();
257+
const stack = new Stack(app);
258+
new ConversationHandlerFunction(stack, 'conversationHandler', {
259+
models: [],
260+
memoryMB: 127,
261+
});
262+
}, new Error('memoryMB must be a whole number between 128 and 10240 inclusive'));
263+
});
264+
265+
void it('throws on memory above 10240 MB', () => {
266+
assert.throws(() => {
267+
const app = new App();
268+
const stack = new Stack(app);
269+
new ConversationHandlerFunction(stack, 'conversationHandler', {
270+
models: [],
271+
memoryMB: 10241,
272+
});
273+
}, new Error('memoryMB must be a whole number between 128 and 10240 inclusive'));
274+
});
275+
276+
void it('throws on fractional memory', () => {
277+
assert.throws(() => {
278+
const app = new App();
279+
const stack = new Stack(app);
280+
new ConversationHandlerFunction(stack, 'conversationHandler', {
281+
models: [],
282+
memoryMB: 256.2,
283+
});
284+
}, new Error('memoryMB must be a whole number between 128 and 10240 inclusive'));
285+
});
286+
});
225287
});

packages/ai-constructs/src/conversation/conversation_handler_construct.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ export type ConversationHandlerFunctionProps = {
3434
modelId: string;
3535
region?: string;
3636
}>;
37+
/**
38+
* An amount of memory (RAM) to allocate to the function between 128 and 10240 MB.
39+
* Must be a whole number.
40+
* Default is 512MB.
41+
*/
42+
memoryMB?: number;
3743
/**
3844
* @internal
3945
*/
@@ -86,6 +92,7 @@ export class ConversationHandlerFunction
8692
timeout: Duration.seconds(60),
8793
entry: this.props.entry ?? defaultHandlerFilePath,
8894
handler: 'handler',
95+
memorySize: this.resolveMemory(),
8996
bundling: {
9097
// Do not bundle SDK if conversation handler is using our default implementation which is
9198
// compatible with Lambda provided SDK.
@@ -153,4 +160,27 @@ export class ConversationHandlerFunction
153160
},
154161
});
155162
};
163+
164+
private resolveMemory = () => {
165+
const memoryMin = 128;
166+
const memoryMax = 10240;
167+
const memoryDefault = 512;
168+
if (this.props.memoryMB === undefined) {
169+
return memoryDefault;
170+
}
171+
if (
172+
!isWholeNumberBetweenInclusive(this.props.memoryMB, memoryMin, memoryMax)
173+
) {
174+
throw new Error(
175+
`memoryMB must be a whole number between ${memoryMin} and ${memoryMax} inclusive`
176+
);
177+
}
178+
return this.props.memoryMB;
179+
};
156180
}
181+
182+
const isWholeNumberBetweenInclusive = (
183+
test: number,
184+
min: number,
185+
max: number
186+
) => min <= test && test <= max && test % 1 === 0;

packages/backend-ai/API.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ type DefineConversationHandlerFunctionProps = {
5353
modelId: string | AiModel;
5454
region?: string;
5555
}>;
56+
memoryMB?: number;
5657
};
5758

5859
// @public (undocumented)

packages/backend-ai/src/conversation/factory.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,19 @@ void describe('ConversationHandlerFactory', () => {
188188
});
189189
});
190190
});
191+
192+
void it('passes memory setting to construct', () => {
193+
const factory = defineConversationHandlerFunction({
194+
entry: './test-assets/with-default-entry/handler.ts',
195+
name: 'testHandlerName',
196+
models: [],
197+
memoryMB: 271,
198+
});
199+
const lambda = factory.getInstance(getInstanceProps);
200+
const template = Template.fromStack(Stack.of(lambda.resources.lambda));
201+
template.resourceCountIs('AWS::Lambda::Function', 1);
202+
template.hasResourceProperties('AWS::Lambda::Function', {
203+
MemorySize: 271,
204+
});
205+
});
191206
});

packages/backend-ai/src/conversation/factory.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class ConversationHandlerFunctionGenerator
4343
};
4444
}),
4545
outputStorageStrategy: this.outputStorageStrategy,
46+
memoryMB: this.props.memoryMB,
4647
};
4748
const conversationHandlerFunction = new ConversationHandlerFunction(
4849
scope,
@@ -119,6 +120,12 @@ export type DefineConversationHandlerFunctionProps = {
119120
modelId: string | AiModel;
120121
region?: string;
121122
}>;
123+
/**
124+
* An amount of memory (RAM) to allocate to the function between 128 and 10240 MB.
125+
* Must be a whole number.
126+
* Default is 512MB.
127+
*/
128+
memoryMB?: number;
122129
};
123130

124131
/**

packages/backend-function/src/factory.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ export type FunctionProps = {
104104
/**
105105
* An amount of memory (RAM) to allocate to the function between 128 and 10240 MB.
106106
* Must be a whole number.
107-
* Default is 128MB.
107+
* Default is 512MB.
108108
*/
109109
memoryMB?: number;
110110

0 commit comments

Comments
 (0)