Guide: Create a Template
Step by step guide creating a new template
In this guide we'll go step by step creating a new template. We will create the Info Agent template. This template will allow creators to add some invisible context around their post so that everytime someone comments on the post Sage will automatically reply with answers about whatever they may ask.
Step 1: New File
Copy an existing template file such as artistPresent.ts
in the templates folder in client-bonsai and name it infoAgent.ts
. Rename the function infoAgent and update the default export at the bottom of the file to reflect the new function name.
Step 2: Update the clientMetadata
Update the clientMetadata
object:
Go to utils/types.ts
to add the new TemplateCategory
and TemplateName
. Then we set both of those properties on the object here along with the new display name, description, and we get a link to a new image.
For the options object we set allowPreview
to false since there won't be any media updates for this template type and no generative post content. Image is optional and content is required since creator's will need to create the original post content. This will prompt the dynamic form to allow the creator to write the post text.
Lastly update the templateData
object with the form items that creators will need to provide to create the post. In this case we will allow them to provide any info that may be relevant and urls that the AI can scrape such as docs for responding to comments.
Step 3: Update the Prompt Template
Next we need to update the template that is used to prompt the AI. We are going to batch comments so that it can respond to multiple at a time with one API request.
Here we instruct the AI to take in the comments and the info and urls provided by the creator and return a JSON object of responses to each one. We also need to create some new types.
Step 4: Update the Handler Function
If you copied a different template file then the handler function will start with these lines:
And then continue with a try catch statement. Delete everything inside the try block. In order to process comments and respond to them we need to:
fetch recent comments
fetch the original post content
generate responses to the comments
respond to the comments
return values
Step 4.1: Fetch recent comments
Use this code to fetch recent comments made since the last update.
Step 4.2: Fetch the original post content
Use this code to fetch the content of the original post.
Step 4.3: Generate responses to the comments
Compose the context of the AI request and then generate the object and track the token usage. Make sure to include the web search tool so that the model can use your urls.
Step 4.4: Respond to the comments
Loop through the comments and send a reply to each one.
Step 4.5: Return values
Lastly return from the handler. Since we aren't updating the metadata or uri those values will be undefined so that the server knows not to push anything new to Lens or trigger a metadata refresh.
Step 5: Update the server to broadcast the template
Update the initialize function in index.ts
to include the new template.
Testing
Import template and create post
Setup with ngrok
Install ngrok if you don't have it already
Authenticate
Route with ngrok
Get the ngrok url and add it to the
.env
of your Eliza server asDOMAIN
Start the server
Paste url into the import section of the studio
The new template should appear at the bottom of the list. Use a cover image with an aspect ratio of 3 to 2 for best results. The imported url will be saved in your browser's local storage until you either clear it or import a new url. Only one imported url will be saved at a time.
Click on "Create" and you should be directed to a new page with a form like the one you specified in the zod object of clientMetadata.templateData
.
You should be able to create a new post with the new template now. Then we'll be able to activate the post lifecycle on the Eliza server to create updates.
Trigger Updates
Now you can trigger an update to your post. First leave a comment or two on the post and then send a POST command to the update endpoint on the server.
You should see logs of the post content and comments being fetched and new content being generated and Lens content uris being created as responses are sent out to each of the comments.
That's it!
Last updated