Livekit
Memobase can be used to Voice Agent for many use cases, e.g. AI Companion, customer support, etc. This document gives a code breakdown of how to build a Voice Agent using Memobase and Livekit.
Setup
- Go to Memobase for your Memobase API Key or launch a local server
- Make sure to have a Livekit and Deepgram account. You can find these variables
LIVEKIT_URL
,LIVEKIT_API_KEY
andLIVEKIT_API_SECRET
from LiveKit Cloud Console and for more information you can refer this website LiveKit Documentation. ForDEEPGRAM_API_KEY
you can get from Deepgram Console refer this website Deepgram Documentation for more details. - Set up the environment variables:
- Install dependencies:
Code Breakdown
Full code is available here. We’re using livekit 1.0 SDK.
The main code is pretty simple, livekit offers a Agent
class to contorl the behavior of Voice Agent.
Memobase needs toreceive the current chat history and modify the system prompt to inject the user information.
First we need to do some setup work:
We initialized the Memobase client from env.
Then we inherit the livekit Agent
class:
In init method, we obtain the possible user name first, the user name will be used in Memobase to identify the user.
Then, we override the llm_node
method(which will be called before the messages are sent to the LLM):
In this method, we first get the user from Memobase for inserting messages and getting memories. We first check if there’re new messages from the last time we inserted messages. If there are, we insert them into Memobase.
Then we get the context from Memobase and add it to the system prompt.
Finally, we call the default llm_node
method to get the response from the LLM.
The context will look like this:
Memobase has some types of memories, e.g. User Profile, User Event, etc. In here, we called the Context API in Memobase to pack all types of memories in a prompt.
At last, we need to build the entrypoint for livekit:
Run the code
First you need to download the necessary files to run livekit in your local machine:
Then you can start to chat with the agent:
You can talk about yourself, like your name, your favorite sports, etc. The agent will remember these information.
You can then start a new chat console to see if the agent can remember you.