/
Custom Agent Tutorial 1 - Social Media Assistant Agent
  • Ready for review
  • Custom Agent Tutorial 1 - Social Media Assistant Agent

    In this tutorial, we will build a custom AI Agent that assists us with making social media posts for our business.

    We want our agent to have a few behaviors:

    1. Able to talk to an individual and interview them about a subject area they are interested in

    2. Prepare a nicely formatted social media post containing the interview-subjects ideas and thoughts

    3. Save the newly created post to a local database that we can later view.

     

    Step 1 - Registering & Logging In

    We will start by logging into the Custom Agent demo environment at https://custom.prosperalabs.ai/app.

    If your account is fresh, then you might get a home screen that looks something like this:

    image-20241229-182356.png

    Step 2 - Creating the data schema to store posts

    We need to create a custom database to store the posts that our AI Agent generates. To do that, we will go down to the Custom Data Schemas section and create a new data schema.

    image-20241229-182642.png

    This will bring you to a screen that allows you to start configuring your new custom data schema.

    image-20241229-182737.png

    For our purposes, we want our social media posts to have a few fields:

     

    1. Subject - The general subject area of the post.

    2. Title - The title of the post. Not included in the post itself but can be useful for searching

    3. Content - The main text containing the content for the post.

     

    To do this, we will use the following schema:

    { "title": "Social Media Post", "properties": { "subject": { "show_in_table_view": true, "table_view_order": 1, "title": "Subject", "description": "The subject of the social media post, in the form of a general topic area.", "type": "string" }, "title": { "show_in_table_view": true, "table_view_order": 2, "title": "Title", "description": "The title of the social media post.", "type": "string" }, "content": { "description": "The main content of the social media post. This should follow a style that is appropriate for LinkedIn.", "multiline": true, "rows": 4, "show_in_table_view": false, "table_view_order": 3, "title": "Content", "type": "string" } }, "type": "object" }

     

    Pasting that into the Custom Data Schema editor, we get this:

    image-20241229-183357.png

    For the icon, you can choose from among any of the icons contained within the Material UI Icon Library, which you can locate here: Material Icons - Material UI. The name should be a lowercase version of the name given in the library.

    For our social media agent, I decided to use the classic share icon for my DB:

    image-20241229-183556.png
    image-20241229-183646.png

    Punching that into our custom data schema, you will see this:

    image-20241229-183717.png

    Now you can hit the create button to create your new custom data schema.

    At this point you may have to press “Reload” on your browser window. You should observe your new custom data schema show up as an option on the left side menu. Voila!

    image-20241229-183920.png

    You will find that it works just like any other data table in the system, allowing you to create, update, search, and delete entries.

    image-20241229-184026.png
    image-20241229-184120.png

    You can now proceed to the second step of making your custom agent.

    Step 3 - Create a Custom Smart Chain

    Now we need to customize our AI agent behavior. To do this, we will go to the smart chain system.

    image-20241229-184330.png

    It’s usually easiest to start from an existing smart chain and then customize it, so that is what we will do. We will load up default receptionist_english smart chain, as it provides a good, simple baseline to work from.

    To do this, click on the receptionist_english row to load up the smart chain.

    image-20241229-184457.png

    You should get a view that looks something like this:

    image-20241229-184529.png

    If you open up the step list, you will observe that this smart chain has three basic steps. These are the three minimum steps required for any AI Agent. Your AI Agent can have various different ways of accomplishing these three tasks, but at the core, the agent must do three things to function: Selecting an action, Executing the Action, and Recording the Action.

    image-20241229-184704.png

    For the purposes of customizing your agent, we are mainly concerned with the first two steps. The third step - record_action, can be usually left in the default configuration.

    Lets start by giving our new custom smart chain its own unique name.

    image-20241229-184847.png

    Type in the new name here. The name can only contain letters, numbers, and underscores. E.g. it must be a valid variable name. So we will choose social_media_agent

    image-20241229-184946.png

    After the rename, we save our new custom smart chain.

    image-20241229-185041.png

    It’s a good habit to be in to save regularly. Saves are not automatic - this is by design because changes to the text of a smart chain can impact the behavior of your actual live agent.

    If we go back to the smart chain list page, we will see our new custom social media agent there:

    image-20241229-185203.png

    Coming back to this view is usually a good idea after creating a new smart chain, because the system will refresh the URL it uses. Lets click back into that social_media_agent smart chain.

    The very first step of the agent defines a system prompt that is the main way to customize the behavior of simple chat-bot style agents. We are going to replace everything we have here with a very simple system prompt:

    You are an editor, journalist, and interviewer who is conducting an interview with a user about a topic area that they are interested in. You will start the conversation by asking the user what topic area they would like to talk about. Then you will start asking them random questions on that topic area. Every-time the user replies to your question, you will dive into the most interesting, unusual, or different aspect of their response. Then you will come up with a follow-up question to dive deeper and pose it to them. You will continue asking follow-up questions indefinitely.

    Once you punch this into the smart chain window, you should see something like this:

    image-20241229-185737.png

    Make sure to save your custom smart chain!

    Step 4 - Adjust the Smart Chain Binding

    Now there is only one more thing we need to do before we can start chatting with our new custom agent. If you are using the Custom Agent demo environment, then the default smart chain for any agents is defined by the custom_agent binding. You can see the binding under the smart chain binding list:

    image-20241229-190032.png
    lets ignore the “invalid date” on this screenshot for now

    By default the custom agent binding points to the receptionist smart chain. But now we want it to use our new custom social media agent smart chain. So we click on the chain name and search for our new smart chain.

    image-20241229-190237.png

    Once you have set the chain_name, if you reload the browser page, you should see your chain name referenced. The binding will also now have a user_id, indicating that you have modified the default binding for your specific user.

    image-20241229-190405.png

    So at this point, we are all set! We should now be able to see our agent start responding to us based on our new instructions.

    Step 5 - Testing the conversational behavior

    The fastest way to test the behavior of the bot is to go to the “Chat with your Bot” page under the web chat module

    image-20241229-190530.png

    If the bot does not automatically greet you, you can say hello yourself to kick off the conversation.

    image-20241229-191034.png

    Voila! We now have our custom agent responding based on our instructions!

    Step 6 - Generating the social media post

    There is currently one problem with our AI Agent. It doesn’t actually generate any social media posts! The Social Media Posts table is empty …

    image-20241229-191245.png

    At this point, we have to do the critical step of connecting our AI Agent to the database table that we created to store our social media posts.

    When you are designing your AI Agent, there are often multiple different ways that you can design things. Here we have two options for making our AI Agent interact with this table:

     

    1. Always Generate - we can put our agent in an “always generate” style configuration. In this type of configuration, the agent would be continuously updating the generated social media post every time that you message it. This hard the advantage of simplicity and reliability, but the disadvantage that it can be costly in terms of tokens.

    2. Custom Action - we could instead define a custom action to generate the social media post, and then provide prompting to the bot on when to choose that action. This has the advantage of saving tokens by only generating the social media post at a few specific spots in the conversation, but has the disadvantage that it introduces a new potential spot of unreliability - whether or not the agent chooses the correct action at the correct spot in the conversation.

     

    You should always assume that anytime you introduce an AI decision or AI computation, it will only be 90% accurate. And if you chain together multiple prompts, the errors compound. One prompt may be 90% accurate, but two prompts will only be 81% accurate. And three only 73%. Therefore, as a general rule of thumb on how to build AI Agents, the fewer critical decisions that the AI makes, the more reliable your agent will be.

    For our AI agent, we will prioritize reliability over cost and select the first option. We will put the bot into a configuration where its continuously updating its generated post in the background.

    To do this, we will go back to our custom smart chain:

    image-20241229-192232.png

    And we will now add a new step. Let’s call this the “generate_post” step.

    image-20241229-192304.png

    Rename the step using the pencil icon:

    image-20241229-192342.png

    For this action, we need to use a type of AI prompt called a structured output prompt. Use the drop down to select the Conversational Structured Output step type:

    image-20241229-192456.png

    Now we need to type in our prompt. For our purposes, we will keep it brief. At this point we aren’t actually trying to make it generate a good post, which might require more elaborate multi-step prompt chains. For now we just need anything so that we can observe our agent work.

    Use the following prompt:

    You will generate a social media post, including subject, title, and main content, based on the opinions provided to you by the user.

    Putting that into the UI, you should see this:

    image-20241229-192752.png

    Now we will need to define our output schema. Go to the “Output Schema” tab.

    image-20241229-192927.png

    The interface can be a bit awkward and we are always working on improving it! For now we will need to create a similar schema as to what we defined in the original object.

    If you punch in the same field names as defined in our custom data schema, set the types to String Field and copy in the descriptions, then you should see a form that looks like this:

    image-20241229-193224.png

    Don’t forget to save your changes!

    image-20241229-193310.png

    If you reload the browser page and come back to the Output Schema tab on the fourth generate_post step, you should still see all the data for the schema you just configured still.

    image-20241229-211612.png

     

    Ok now we are in the home stretch. We need to add one more step to our smart chain to ensure that the data for the social media post actually gets written to the database. Now we add yet another step to the end of our smart chain, naming it save_post

    image-20241229-193605.png

    In the step type selector, find the data write action. Hint! This widget supports search, so if you just type two letters, da, it should come up.

    image-20241229-193709.png

    Now we should be looking at the Options tab for the smart chain step:

    image-20241229-193750.png

    For the data_model_name, we will need to set this to the machine name for our custom data schema, which can be seen here on the custom data schema page.

    image-20241229-193857.png

    Punching that in, we get this:

    image-20241229-193932.png

    For now we leave overwrite_existing_by_conversation_id set to true. The data needs to refer to the data we generated in the previous step, and it needs to be converted into JSON step. To do this, we will need to put in a variable substitution. If you aren’t sure what the name of the variable is that you need to reference, you can always press the button to view the prompt context:

    image-20241229-194151.png

    We use the Jinja template engine (see here: https://jinja.palletsprojects.com/en/stable/ ) so the syntax to reference a variable uses double curly braces. However, you will need to apply the tojson filter.

    So you should set the “data” field like so:

    {{generate_post | tojson}}

    When you punch that in, you should see this:

    image-20241229-194352.png

    Make sure you save! Saving is not automatic.

    image-20241229-194431.png

    Step 7 - Test the post generation

    Your simple social media assistant agent is all set! Go back to the “Chat with your Bot” screen in order to test it:

    image-20241229-194711.png

    Now if we go into the Social Media Posts database, we can see the social media post that the bot has generated for us:

    image-20241229-201534.png

    and if we click in, we can see the exact text that it has generated, ready for me to copy and paste into LinkedIn

    image-20241229-201608.png

     

    Related content