Skip to content

Commit f57a758

Browse files
author
Kamil Sobol
authored
Use different questions when retrying AI use cases. (#2237)
1 parent f1db886 commit f57a758

File tree

3 files changed

+42
-26
lines changed

3 files changed

+42
-26
lines changed

.changeset/pink-students-allow.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/integration-tests/src/retry.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ export type RetryPredicate = (error: Error) => boolean;
77
* errors or temporary service unavailability.
88
*/
99
export const runWithRetry = async <T>(
10-
callable: () => Promise<T>,
10+
callable: (attempt: number) => Promise<T>,
1111
retryPredicate: RetryPredicate,
1212
maxAttempts = 3
1313
): Promise<T> => {
1414
const collectedErrors: Error[] = [];
1515

1616
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
1717
try {
18-
const result = await callable();
18+
const result = await callable(attempt);
1919
return result;
2020
} catch (error) {
2121
if (error instanceof Error) {

packages/integration-tests/src/test-project-setup/conversation_handler_project.ts

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { resolve } from 'path';
3434
import { fileURLToPath } from 'url';
3535
import * as bedrock from '@aws-sdk/client-bedrock-runtime';
3636
import { e2eToolingClientConfig } from '../e2e_tooling_client_config.js';
37+
import { runWithRetry } from '../retry.js';
3738

3839
// TODO: this is a work around
3940
// it seems like as of amplify v6 , some of the code only runs in the browser ...
@@ -279,13 +280,14 @@ class ConversationHandlerTestProject extends TestProjectBase {
279280
)
280281
);
281282

282-
await this.executeWithRetry(() =>
283+
await this.executeWithRetry((attempt) =>
283284
this.assertCustomConversationHandlerCanExecuteTurnWithParameterLessTool(
284285
backendId,
285286
authenticatedUserCredentials.accessToken,
286287
dataUrl,
287288
apolloClient,
288-
true
289+
true,
290+
attempt
289291
)
290292
);
291293

@@ -349,23 +351,25 @@ class ConversationHandlerTestProject extends TestProjectBase {
349351
)
350352
);
351353

352-
await this.executeWithRetry(() =>
354+
await this.executeWithRetry((attempt) =>
353355
this.assertDefaultConversationHandlerCanPropagateError(
354356
backendId,
355357
authenticatedUserCredentials.accessToken,
356358
dataUrl,
357359
apolloClient,
358-
true
360+
true,
361+
attempt
359362
)
360363
);
361364

362-
await this.executeWithRetry(() =>
365+
await this.executeWithRetry((attempt) =>
363366
this.assertDefaultConversationHandlerCanPropagateError(
364367
backendId,
365368
authenticatedUserCredentials.accessToken,
366369
dataUrl,
367370
apolloClient,
368-
false
371+
false,
372+
attempt
369373
)
370374
);
371375
}
@@ -870,7 +874,8 @@ class ConversationHandlerTestProject extends TestProjectBase {
870874
accessToken: string,
871875
graphqlApiEndpoint: string,
872876
apolloClient: ApolloClient<NormalizedCacheObject>,
873-
streamResponse: boolean
877+
streamResponse: boolean,
878+
attempt: number
874879
): Promise<void> => {
875880
const customConversationHandlerFunction = (
876881
await this.resourceFinder.findByBackendIdentifier(
@@ -880,13 +885,23 @@ class ConversationHandlerTestProject extends TestProjectBase {
880885
)
881886
)[0];
882887

888+
// Try different questions on retry.
889+
// Retrying same question in narrow time frame usually yields same answer.
890+
const questions = [
891+
'Give me a random number',
892+
'Give me a random number please',
893+
'Can you please give me a random number',
894+
'Generate and print random number',
895+
];
896+
const question = questions[attempt % questions.length];
897+
883898
const message: CreateConversationMessageChatInput = {
884899
conversationId: randomUUID().toString(),
885900
id: randomUUID().toString(),
886901
role: 'user',
887902
content: [
888903
{
889-
text: 'Give me a random number',
904+
text: question,
890905
},
891906
],
892907
};
@@ -919,7 +934,8 @@ class ConversationHandlerTestProject extends TestProjectBase {
919934
accessToken: string,
920935
graphqlApiEndpoint: string,
921936
apolloClient: ApolloClient<NormalizedCacheObject>,
922-
streamResponse: boolean
937+
streamResponse: boolean,
938+
attempt: number
923939
): Promise<void> => {
924940
const defaultConversationHandlerFunction = (
925941
await this.resourceFinder.findByBackendIdentifier(
@@ -929,13 +945,23 @@ class ConversationHandlerTestProject extends TestProjectBase {
929945
)
930946
)[0];
931947

948+
// Try different questions on retry.
949+
// Retrying same question in narrow time frame usually yields same answer.
950+
const questions = [
951+
'What is the value of PI?',
952+
'Give me the value of PI',
953+
'Give me the value of PI please',
954+
'Can you please give me the value of PI?',
955+
];
956+
const question = questions[attempt % questions.length];
957+
932958
const message: CreateConversationMessageChatInput = {
933959
id: randomUUID().toString(),
934960
conversationId: randomUUID().toString(),
935961
role: 'user',
936962
content: [
937963
{
938-
text: 'What is the value of PI?',
964+
text: question,
939965
},
940966
],
941967
};
@@ -1031,20 +1057,8 @@ class ConversationHandlerTestProject extends TestProjectBase {
10311057
* Therefore, we wrap transactions in retry loop.
10321058
*/
10331059
private executeWithRetry = async (
1034-
callable: () => Promise<void>,
1035-
maxAttempts = 3
1060+
callable: (attempt: number) => Promise<void>
10361061
) => {
1037-
const collectedErrors = [];
1038-
for (let i = 1; i <= maxAttempts; i++) {
1039-
try {
1040-
await callable();
1041-
// if successful return early;
1042-
return;
1043-
} catch (e) {
1044-
collectedErrors.push(e);
1045-
}
1046-
}
1047-
// if we got here there were only errors
1048-
throw new AggregateError(collectedErrors);
1062+
await runWithRetry(callable, () => true, 4);
10491063
};
10501064
}

0 commit comments

Comments
 (0)