Create a chat agent app - Amazon SageMaker Unified Studio

Amazon SageMaker Unified Studio is in preview release and is subject to change.

Create a chat agent app

In this section, you learn how create a simple Amazon Bedrock IDE chat agent app that creates playlists for a radio station. Later, you add the following features.

  • A guardrail to prevent songs with inappropriate song titles.

  • A data source that lets the app create playlists using your unique song information.

  • A function that gets today's top 10 songs.

Step 1: Create the initial chat agent app

In this step you create a chat agent app that generates playlists for a radio station.

To create the app, you first need to create an Amazon SageMaker Unified Studio project. A project can contain multiple apps and is also where you can add the Amazon Bedrock components that you want your apps to use. Later you will add guardail, data source, and function components to your app. You can share a project with other users and groups of users. For more information, see Share an Amazon Bedrock IDE chat agent app.

In the app, you use a system prompt to specify that the model should behave as an app that creates playlists for a radio station that plays rock and pop music. A system prompt is a type of prompt that provides instructions or context to the model about the task it should perform, or the persona it should adopt during the conversation. Users can then create playlists of rock and pop songs based on different themes, such as songs that are related by artist.

To help guide users of the app, you can set user interface (UI) text, such as hint text for the beginning of a chat.

In the app, you can experiment with the randomness and diversity of the response that the model returns by changing the inference parameters.

While you develop your app, you work on the current draft. You can save the current draft to the app history. Later you might want to restart work from a previous draft. For more information, see Use app history to view and restore versions of an app.

Warning

Generative AI may give inaccurate responses. Avoid sharing sensitive information. Chats may be visible to others in your organization.

To create an Amazon Bedrock IDE chat agent app
  1. Navigate to the Amazon SageMaker Unified Studio landing page by using the URL from your admininstrator.

  2. Access Amazon SageMaker Unified Studio using your IAM or single sign-on (SSO) credentials. For more information, see Access Amazon SageMaker Unified Studio.

  3. On the Amazon SageMaker Unified Studio home page, in the Amazon Bedrock IDE tile, choose Build chat agent app to create a new chat agent app. The Select or create a new project to continue dialog box opens.

  4. In the Select or create a new project to continue dialog box, do one of the following:

    • If you want to use a new project, follow the instructions at Create a new project. For the Project profile in step 1, choose Generative AI application development.

    • If you want to use an existing project, select the project that you want to use and then choose Continue.

  5. In Untitled App - nnnn, enter Radio show as the name for your app.

  6. In the Configs pane, do the following:

    1. For Models, select a model that supports Guardrails, Data, and Function components. The description of the model tells you the components that a model supports. If you don't have access to an appropriate model, contact your administrator. Different models might not support all features.

    2. For Enter a system prompt in Instructions for chat agent & examples, enter You are a chat agent app that creates 2 hour long playlists for a radio station that plays rock and pop music..

    3. In the UI section, update the user interface for the app by doing the following:

      1. In Hint text for empty chat enter Hi! I'm your radio show playlist creator..

      2. In Hint text for user input enter Enter a prompt that describes the playlist that you want..

      3. In Quick start prompts choose Edit.

      4. Choose Reset to clear the list of quick start prompts

      5. For Quick-start prompt 1, enter Create a playlist of pop music songs..

      6. (Optional). Enter quick start prompts of your choice in the remaining quick start prompt text boxes.

      7. Choose Back to configs.

  7. Choose Save to save the current draft of your app.

  8. In the Quick start prompts section of the Preview pane, run the quick start prompt that you just created by choosing the prompt.

    The app shows the prompt and the response from the model in the Preview pane.

  9. In the prompt text box (the text should read Enter a prompt that describes the playlist that you want), enter Create a playlist of songs where each song on the list is related to the next song, by musician, bands, or other connections. Be sure to explain the connection from one song to the next. .

  10. Choose the run button (or press Enter on your keyboard) to send the prompt to the model.

  11. Experiment with influencing the model response by doing the following:

    1. In the Inference parameters section change the inference parameters. For example, include less familiar songs in the playlist by increasing the Temperature inference parameter.

      The inference parameters you can change are Temperature, Top P, and Top K. Not all models support each of these inference parameters. For more information, see Influence model responses with inference parameters.

    2. Run the prompt again to see the effect of your changes.

  12. (Optional) Share a snapshot of your app with others by following the instructions at Share an Amazon Bedrock IDE chat agent app.

  13. (Optional) Export your snapshot from Amazon SageMaker Unified Studio by following the instructions at Use your app outside of Amazon SageMaker Unified Studio.

  14. Next step: Add a guardrail to your app by following the instructions at Step 2: Add a guardrail to your chat agent app.

Step 2: Add a guardrail to your chat agent app

Guardrails for Amazon Bedrock lets you implement safeguards for your Amazon Bedrock IDE app based on your use cases and responsible AI policies. You can create multiple guardrails tailored to different use cases and apply them across multiple foundation models, providing a consistent user experience and standardizing safety controls across generative AI apps. You can configure denied topics to disallow undesirable topics and content filters to block harmful content in the prompts you send to a model and to the responses you get from a model. You can use guardrails with text-only foundation models. For more information, see Safeguard your Amazon Bedrock IDE chat agent app with a guardrail.

Add a guardrail

This procedure shows you how to use a guardrail to safeguard the app you created in Step 1: Create the initial chat agent app. The guardrail prevents inappropriate language in song titles and filters out unwanted music genres.

To add a guardrail to an Amazon Bedrock IDE app
  1. Open the app that you created in Step 1: Create the initial chat agent app.

  2. In the Configs pane, choose Guardrails and then Create new guardrail.

  3. For Guardrail name, enter prevent_unwanted_songs.

  4. For Guardrail description, enter Prevents inappropriate or undesirable songs..

  5. In Content filters make sure Enable content filters is selected. For more information, see Content filters.

  6. In Filter for prompts make sure the filter for each category is set to High.

  7. Make sure Apply the same filters for responses is selected.

  8. In Blocked messsaging do the following.

    1. For Blocked messaging for prompts, enter Sorry, your prompt contained inappropriate text..

    2. Clear Apply the same message for blocked responses.

    3. For Blocked messaging for responses, enter Sorry, but I can't respond with information that contains inappropriate text..

  9. Choose Create to create the guardrail.

  10. In the Configs pane, in the Guardrails section, select the guardrail that you just created (prevent_unwanted_songs). It might take a minute for the guardrail to appear in the list.

  11. Test the guardrail by entering Create a list of 10 songs where each song has a swear word in the title. In the prompt edit box.

  12. Choose the run button to send the prompt to the model. The model should respond with the message Sorry, but I can't respond with information that contains inappropriate text.

  13. Use a denied topic filter to prevent requests for music from a specific music genere. For information about denied topics, see Denied topics.

    To add the filter, do the following.

    1. In the Guardrails section of the Configs pane, select the guardrail and choose Preview.

    2. Choose Edit to edit the guardrail.

    3. In Denied topics, choose Add topic.

    4. For Name, enter heavy metal.

    5. For Definition for topic, enter Avoid mentioning songs that are from the heavy metal genre of music..

    6. In Sample phrases - optional, enter Create a playlist of heavy metal songs.

    7. (Optional) Choose Add phrase to add other phrases.

    8. Choose Save.

    9. Om the Edit guardrail page, choose Update to update the guardrail.

    10. Test the guardrail by entering Create a list of heavy metal songs. in the prompt edit box.

    11. Choose the run button to send the prompt to the model. The model should respond with the message Sorry, your prompt contained inappropriate text.

  14. Next step: Add a data source to your app by following the instructions at Step 3: Add a document data source to your chat agent app.

Step 3: Add a document data source to your chat agent app

You can use your own data into your application by adding a data source to your app. Doing this allows your app to access to information that is only available to you. When your app passes a query to a data source, Amazon Bedrock IDE generates a response that includes the query results from the data source. A data source can be a single file or an Amazon Bedrock Knowledge Base. For more information, see Add a data source to your app.

In this topic, you update the app you created in Step 1: Create the initial chat agent app to use a CSV file as a data source. You can add the CSV file as single file data source, or use the CSV file to create a knowledge base data source. The CSV file includes information about bands that don't have public metadata such as song length, or music genre. The user can use the app to create a playlist based on criteria such as song length or music genre.

To add your own data to an Amazon Bedrock IDE app
  1. Create a CSV file name songs.csv and fill with the following ficticious CSV data.

    song,artist,genre,length-seconds "Celestial Odyssey","Starry Renegades","Cosmic Rock",240 "Neon Rapture","Synthwave Siren","Synthwave Pop",300 "Wordsmith Warriors","Lyrical Legions","Lyrical Flow",180 "Nebula Shredders","Galactic Axemen","Cosmic Rock",270 "Electro Euphoria","Neon Nomads","Synthwave Pop",210 "Rhythm Renegades","Percussive Pioneers","Lyrical Flow",240 "Stardust Rift","Cosmic Crusaders","Cosmic Rock",180 "Synthwave Serenade","Electro Enchanters","Synthwave Pop",300 "Lyrical Legends","Rhyme Royale","Lyrical Flow",240 "Supernova Shredders","Amplified Ascension","Cosmic Rock",300 "Celestial Chords","Ethereal Echoes","Cosmic Rock",240 "Neon Nirvana","Synthwave Sirens","Synthwave Pop",270 "Verbal Virtuoso","Lyrical Maestros","Lyrical Flow",210 "Cosmic Collision","Stellar Insurgents","Cosmic Rock",180 "Pop Paradox","Melodic Mavericks","Synthwave Pop",240 "Flow Fusion","Verbal Virtuosos","Lyrical Flow",300 "Shredding Shadows","Crimson Crusaders","Cosmic Rock",270 "Synth Serenade","Electro Enchanters","Synthwave Pop",180 "Wordsmith Warlords","Lyrical Legionnaires","Lyrical Flow",240 "Sonic Supernova","Amplified Ascension","Cosmic Rock",210 "Celestial Symphony","Ethereal Ensemble","Cosmic Rock",300 "Electro Euphoria","Neon Nomads","Synthwave Pop",180 "Lyrical Legends","Rhyme Royale","Lyrical Flow",270 "Crimson Crescendo","Scarlet Serenaders","Cosmic Rock",240 "Euphoric Tides","Melodic Mystics","Synthwave Pop",210 "Rhythm Renegades","Percussive Pioneers","Lyrical Flow",180 "Cosmic Collision","Stellar Insurgents","Cosmic Rock",300 "Stardust Serenade","Celestial Crooners","Synthwave Pop",240 "Wordsmith Warriors","Lyrical Legions","Lyrical Flow",270 "Sonic Supernova III","Amplified Ascension","Cosmic Rock",180
  2. Open the app that you created in Step 1: Create the initial chat agent app.

  3. If you don't want to create a knowledge base, do the following:

    1. In Data choose Use single file. Not all models support the use of a single file as a data source.

    2. Choose Click to upload and upload the CSV file that you created in step 1. Alternatively, add the CSV by dragging and dropping the document from your computer.

      For more information, see Single file in a chat agent app.

  4. If you do want to use a knowledge base:

    1. In Data choose Use Knowledge Base and then Create Knowledge Base. The Create Knowledge Base pane is shown. If you've previously created the knowledge base, go to step 4.h. and select the knowledge base.

    2. For Name, enter a name for the Knowledge Base.

    3. For Description, enter a description for the Knowledge Base.

    4. In Add data sources, choose Local file.

    5. Choose Click to upload and upload the CSV file that you created in step 1. Alternatively, add the CSV by dragging and dropping the document from your computer.

      For more information, see Document data source.

    6. For Embeddings model, choose a model for converting your data into vector embeddings.

    7. Choose Create. It might take Amazon Bedrock IDE a few minutes to create the knowledge base.

    8. For Select Knowledge Base, select the Knowledge Base that you just created.

  5. Test the data source by entering Create a playlist of songs in the Lyrical Flow genre in the prompt text box.

  6. Choose the run button to send the prompt to the model. The model should respond with a playlist of songs from the Lyrical Flow genre that the CSV file contains.

  7. Choose Save to save the app.

Step 4: Add a function call to your chat agent app

Amazon Bedrock IDE functions let a model include information that it has no previous knowledge of in its response. For example, you can use a function to include dynamic information in a model's response such as a weather forecast, sports results, or traffic conditions. To use a function in Amazon Bedrock IDE you add a function component to your app. For more information, see Call functions from your chat agent app.

In Amazon Bedrock IDE, a function calls an API hosted outside of Amazon Bedrock IDE. You either create the API yourself, or use an existing API. To create an API, you can use Amazon API Gateway.

In this procedure, you add a function to the app that you created in Step 1: Create the initial chat agent app so that users can get a list of the top 10 songs played on the radio station that day.

To add a function to an Amazon Bedrock IDE app
  1. Create a HTTPS server that implements a TopSongsToday function. Make sure the function adheres to the following schema.

    openapi: 3.0.0 info: title: Top Songs API description: API to retrieve the top 10 songs played today version: 1.0.0 paths: /top-songs: get: operationId: TopSongsToday summary: Get the top 10 songs played today description: > This endpoint returns an array of the top 10 songs played today, ordered by popularity. The first element in the array (index 0) represents the most popular song, and the last element (index 9) represents the 10th most popular song. responses: '200': description: Successful response content: application/json: schema: $ref: '#/components/schemas/TopSongs' components: schemas: TopSongs: type: array items: $ref: '#/components/schemas/Song' description: > An array containing the top 10 songs played today. The first element (index 0) is the most popular song, and the last element (index 9) is the 10th most popular song. example: - title: 'Song Title 1' artist: 'Artist Name 1' album: 'Album Name 1' - title: 'Song Title 2' artist: 'Artist Name 2' album: 'Album Name 2' # ... up to 10 songs Song: type: object properties: title: type: string description: The title of the song artist: type: string description: The name of the artist or band album: type: string description: The name of the album the song is from required: - title - artist - album
  2. Open the app that you created in Step 1: Create the initial chat agent app.

  3. In Models, choose a model that supports functions. If you don't have access to an appropriate model, contact your administrator.

  4. In Functions, choose Create new function.

  5. In the Create function pane, do the following.

    1. For in Function name, enter Top_ten_songs_today.

    2. For Function description (optional), enter Today's top 10 songs..

    3. For Function schema, enter the OpenAPI schema from step one.

    4. Choose Validate schema to validate the schema.

    5. In Authentication method choose the authentication method for your HTTP server. For more information, see Authentication methods.

    6. In API servers, enter the URL for your server in Server URL. This value is autopopulated if the server URL is in the schema.

    7. Choose Create to create your function. It might take a few minutes to create the function.

  6. For Enter a system prompt, update the system prompt so that it describes the function. Use the following text: You are an app that creates 2 hour long playlists for a radio station that plays rock and pop music. The function Top_ten_songs_today gets the most popular song played on the radio station..

  7. Test the function by doing the following.

    1. Enter What are today's top 10 songs? in the prompt edit box.

    2. Choose the run button to send the prompt to the model. The model should respond with the list of today's top 10 songs.