Creating a solution (AWS SDKs) - Amazon Personalize

Creating a solution (AWS SDKs)

Important

After you create a solution, you can’t change its configuration. By default, all new solutions use automatic training. With automatic training, you incur training costs while your solution is active. To avoid unnecessary costs, make sure to delete the solution when you are finished. For information about training costs, see Amazon Personalize pricing.

To create a solution with AWS SDKs, use the CreateSolution API operation. The following code shows you how to create a solution that uses automatic training. It automatically creates a new solution version every five days.

To use the code, update it to give the solution a name, specify the Amazon Resource Name (ARN) of your dataset group, optionally change the training frequency, and specify the ARN of the recipe that you want to use. For information about recipes, see Choosing a recipe.

SDK for Python (Boto3)
import boto3 personalize = boto3.client('personalize') create_solution_response = personalize.create_solution( name = 'solution name', recipeArn = 'recipe ARN', datasetGroupArn = 'dataset group ARN', performAutoTraining = True, solutionConfig = { "autoTrainingConfig": { "schedulingExpression": "rate(5 days)" } } ) solution_arn = create_solution_response['solutionArn'] print('solution_arn: ', solution_arn)
SDK for JavaScript v3
import { CreateSolutionCommand, PersonalizeClient, } from "@aws-sdk/client-personalize"; // create client const personalizeClient = new PersonalizeClient({ region: "REGION" }); // set the solution parameters export const solutionParam = { datasetGroupArn: "DATASET_GROUP_ARN" /* required */, recipeArn: "RECIPE_ARN" /* required */, name: "SOLUTION_NAME" /* required */, performAutoTraining: true /* optional, default is true */, solutionConfig: { autoTrainingConfig: { schedulingExpression: "rate(5 days)" /* optional, default is every 7 days */, }, }, }; export const run = async () => { try { const response = await personalizeClient.send( new CreateSolutionCommand(solutionParam) ); console.log("Success", response); return response; // For unit tests. } catch (err) { console.log("Error", err); } }; run();

After you create the solution, record the solution ARN for future use. With automatic training, solution version creation starts within one after the solution is ACTIVE. If you manually create a solution version within the hour, the solution skips the first automatic training. After training starts, you can get the solution version's Amazon Resource Name (ARN) with the ListSolutionVersions API operation. To get its status, use the DescribeSolutionVersion API operation.

You can use the following Python code to wait for automatic training to start. The wait_for_training_to_start method returns the ARN of the first solution version.

import time import boto3 def wait_for_training_to_start(new_solution_arn): max_time = time.time() + 3 * 60 * 60 # 3 hours while time.time() < max_time: list_solution_versions_response = personalize.list_solution_versions( solutionArn=new_solution_arn ) solution_versions = list_solution_versions_response.get('solutionVersions', []) if solution_versions: new_solution_version_arn = solution_versions[0]['solutionVersionArn'] print(f"Solution version ARN: {new_solution_version_arn}") return new_solution_version_arn else: print(f"Training hasn't started yet. Training will start within the next hour.") time.sleep(60) personalize = boto3.client('personalize') solution_arn = "solution_arn" solution_version_arn = wait_for_training_to_start(solution_arn)

When the solution version is ACTIVE, you are ready to use it to get recommendations. How you use an active solution version depends on how you get recommendations: