This guide is suitable for entry-level software developers who want to learn how to use the Roli platform to build powerful backends.
What you’re building
This tutorial walks you through building a chatbot on Roli that consists of a public internet-facing WebSocket API routing conversational calls to an LLM registered with the Model Registry. Completing with a code-generate, documented SDK you can distribute (npm module).
The remainder of the commands will be run from the tutorial directory. Change to it.
Setup Roli Tools
Run the command to install the roli-tools package
Set Roli’s connection information so it can talk to the backend platform installation.
Login to Roli. This will open a browser window.
Initialize the service
Init the service code directory
Write an endpoint class
Edit chatbot/service/config.ts to import the Endpoint class from the roli-runtime package.
Write a class named ChatbotApi that extends the Endpoint class.
At this point we have an Endpoint but it’s not very useful because there’s no methods for clients to call. We’ll get back to this after we create a Session.
Write a session class
Import the Session class from roli-runtime.
Write a class named ChatbotSession that extends the Session class.
Add an endpoint method that returns a session
Add createSession to the list of imports
Add a method to ChatbotApi that creates the session, sets userName and returns the session.
Call the Model Registry
Add getModel to the list of imports
Add a method to the session that clients will call to interact with the model
Call the Model Registry to get the model
Construct a basic executable prompt
Add ChatModelResponse, Prompt, and Program to the list of imports
Create a prompt with a fun system message, a user message that comes from the message argument, and a callback to handle the output
Create a program passing the model and the prompt, execute it, and return the results.
Make the chatbot conversational
Add Step and Instruction to the list of imports
Add a property to ChatbotSession to hold the conversation history
Modify the tell method to construct a list of steps for the program starting with this._history if it exists
Make it so the system message is only added to the first prompt
Pass the whole list of steps instead of just the single prompt to the program and add the newly created Instruction to the history after the Program’s execution.
Deploy the service
Use the Roli SDK to deploy the service from the service directory
Code generate a client package
Generate a client package so our client code can talk to the service we just deployed.
Answer npm when asked what package manager to use.
Integrate service with the client code
In the file chatbot/client/src/index.ts import createRoliClient, ServiceOptions, and the roli client key.
The default for Roli clients is to log lots of things we don’t want to see when using a CLI so we need to turn off verbose logging.
Create an instance of the roli client by calling createRoliClient passing the key and ServiceOptions.
Use the Roli client to get a reference to the ChatbotApi endpoint
Get the session from the endpoint, output the sessionId, and send the typed chat text to its tell method.
At this point both the service and client are close but there’s still one step left before it’s usable. We need to register an LLM with the Model Registry.
Register a ModelSpecification
Edit the file chatbot/model.json, add your API key.
Register the ModelSpecification with the Model Registry
Test it all out
Build and run the chatbot example client.
Login with your name and then say hello
Conclusion
You’ve created a conversational AI chatbot and deployed it to a service running on the Roli platform. This service is versatile, and could be used from any web UI or microservice using similar client code.