Setting up the Application - JamestheCog/chatbot_for_sics GitHub Wiki

Because the application is primarily written in Ruby, Windows users will need to first install Ruby onto their machines prior to cloning this repository onto it (i.e., Ruby should come as a pre-installed program for iOS users). Not to worry though - those who need Ruby installed can install the said language on its official homepage (i.e., there should be a download button right smack in the middle of its homepage); simply follow the instructions that come with the installer and you should be set!

Cloning this Repository

This repository is publicly-available and will always be (well, it kinda has to anyways lest Render is unable to perform continuous deployment) - short of heading to this repository's codebase and downloading everything as a .zip file, you can clone this repository onto your machine with the repository's following URL: https://github.com/JamestheCog/chatbot_for_sics.git. In other words, do navigate to your preferred working location on your machine via the command line and run the following:

git clone https://github.com/JamestheCog/chatbot_for_sics.git

And you should be good to go! The instructions for running the application locally should be available in the repository's README.md file; any dependencies used are also listed in the Gemfile present!

Environment Variables

The project itself has a number of dependencies in its .env file - specifically, the following in the below table:

Dependency Purpose (of the Dependency)
SQLitecloud The database service used to log messages sent by the user and the chatbot
Gemini The underlying AI model the chatbot is based off of
Sinatra The application's framework
MailerSend Send fetched conversations to the specified email address

If you are a collaborator of this project (i.e., either from SUTD's side of things and / or a newly-joined lab member who's now on this project), you should have also received an .env file containing important environment variables to be edited for your specific setup (do note that environment variables that are user-agnostic have values that are still there). I will explain which values to change and how to change them for your specific setup in a table!

SQLitecloud

Environment Variable to Change Purpose
SQLITECLOUD_CONNECTION_STRING The connection string needed to be used to connect to your SQLitecloud database
SQLITECLOUD_API_KEY The apikey value in the connection string - the string of numbers and characters after the ?apikey= part of the connection string
SQLITECLOUD_PROJECT_ID Another part of your connection string - this should appear at the beginning of your connection string in the following format sqlitecloud://<project ID>.g2.sqlite.cloud:<rest of the connection string>...

First things first, head onto SQLitecloud's official webpage and create an account with them if you don't already have. Then, perform the following:

  1. Create a new project with the dark blue "Create Project" button on the upper right hand corner of the UI. Ensure that your project is of the "free" type before giving it a name of your choice (e.g., I named mine sic_conversations)!

  2. Click into the project that you've just created above - in it, create a new database with "Create Database" in the upper right hand corner. Do also give your database an appropriate name (e.g., I named mine convos)!

  3. Then, enter the Studio (on the left side bar menu) and in the SQL console, enter the following bit of SQL:

    CREATE TABLE patient_conversations (
       user_id         TEXT
       conversation_id TEXT
       role            TEXT
       time_messaged   TEXT
       message         TEXT
    )

    The above block defines the schema of the only table in the database (for now) - a table titled patient_conversations to store all serious illness conversations (or "SICs" for short) had with the chatbot. The table also has a total of five text columns (some of the data types could be changed, but TEXT type fields were the most convenient at the time of creation) to store user and conversation identifiers, raw messages sent, and who a message was mentioned by (i.e., the user or the chatbot).

  4. Once a project, a database, and a table have been created, click on the "Connect" button on the bottom left hand side of SQLitecloud's side menu. This should bring up a popup menu that will display several pieces of information, including but not limited to your connection key. Save this string (and its subsequent copies) into your copy of the .env file!

If you're not seeing your connection string and / or SQLitecloud is returning an error message when you try to access your connection string, then do ensure that your selected user in the pop-up menu is an administrative role (if not one that has edit access to the database table)!

Gemini

Environment Variable to Change Purpose
GEMINI_API_KEY Needed to access Gemini's API (i.e., so that the application's core functionality is present
PROMPT_PATH The relative file path (from the project's root directory) to the base_bot_prompt.txt prompt. This text file contains the encrypted prompt used to construct the underlying AI assistant that the application wraps around.

Gemini's services have both free and paid tiers - but at least during the time of writing - I found that its free-tier services were more than sufficient for our purposes at the time (i.e., we were merely trialing the project out on willing and able caretakers and their care recipients). Nevertheless - to generate your own Gemini API key:

  1. Head onto Google's AI Studio, login to it with your Google account (or create one if you don't already have one), and click on the "Get API Key" option on the bottom left hand corner of the webpage.
  2. Then, access the "Projects" page in the same left sidebar menu and create a new project - this project can be named anything.
  3. Return to the "Get API Key" page and create a new (free) API key under your newly-created project. Paste this newly-generated key into your .env file!

Gemini's free-tier rate limits might change in the future, but it's pretty generous last I checked - so much so that it accommodates up to 1,000 requests per day (per project)! As for that PROMPT_PATH variable - well - so long as you are able to access a different location on your project setup where base_bot_prompt.txt resides, feel free to change this to anything else you want (or leave the value of this environment as is if you're satisfied with it)!

Sinatra

There's nothing much to do here - merely ensure that Sinatra is installed on your machine lest this don't function as expected! Sinatra and the rest of the application's Ruby dependencies (i.e., "gems" as we rubyists call them) are all listed in the Gemfile file in the project's root directory. Run the following block of code in the command line to install those said dependencies; as always, please ensure that your command line is operating in the project's root directory before running the following:

gem install bundle   # <-- Bundler functions similarly to gem install <insert gem name here>, but ensures that gem versions are the same.
bundle install       # <-- Installs the gems outlined in `Gemfile`.

And you should be set after this!

MailerSend

Environment Variable to Change Purpose
SEMDMAIL_API_TOKEN The API token from Mailersend used to send emails
SENDMAIL_EMAIL The email to send Mailersend mails to
SENDMAIL_NAME The name of the recipient whose email you want to send mail to

This dependency might not seem super obvious on first glance, but this dependency is crucial in the conversation_fetching.rb route in the application. When a user is finished interacting with the bot, their conversation with the chatbot would need to be fetched - hence this route! One would traditionally need to manually retrieve it via code and a connection string, but because I got lazy, I came up with this solution! So, every day at 6:00 PM or so, a cron job sends a POST request (with a specific JSON payload for authentication purposes) to the endpoint specified in the ConversationRoutes module. If there are any conversations captured by the chatbot at a given date, an email with those conversations in text files (i.e., text file attachments) will be sent along with the email too.

Nevertheless, I initially ran with Mailersend as it was the first service I landed on while researching "free" mail API services to use - its usage limits for free-tier users seem good enough for our purposes. Otherwise, I did the following to set up the SENDMAIL variables' values:

  1. Visit Mailersend's official webpage and log in (or create an account) to the platform. Once you are there:
  2. Create a new account / project once you've logged into your account - there should be an option up top that'll allow you to create a new account / project.
  3. Then, click on the "API" button on the lower right corner of the page - doing so should bring you to an API key creation page. Ensure that your API key has "full access" (i.e., we probably won't need all features of the API, but it's better to have too much access than too little if ever our needs change) - this is important as API keys cannot be edited or viewed post-creation.
  4. Copy the API key that is generated and paste it into your copy of the .env file. And while you're here, do also enter your email and the name associated with the email!

FAQs

Chances are that you may have some questions pertaining to the overall setup of this project, in which case, I'll do my best to answer them! Rest assured - if your question is not here, do feel free to contact Kevin if need be!

  1. Why was Ruby chosen to build this application? Why not Python, JavaScript, or something more mainstream?

    Honestly? There's no reason in particular why Ruby was chosen - it was one of the very first languages Kevin picked up during his teenage days (i.e., when he was barely learning how to code), so when he was asked to build something fast for this project, Ruby was just the language he defaulted to! Fortunately for him, this application is still pretty small so far - but a full-scale migration to another framework and / or another language altogether might be in the horizon depending on the project's future (though that's another story for another day)!

  2. Is MailerSend the same thing as those SENDMAIL variables in the .env file template?

    Yep - they are! That's just an oversight on Kevin's part; he mis-remembered and -called MailerSend as "SendMail" for a period of time! Rest assured though - this will not affect anything in the chatbot's operation!

⚠️ **GitHub.com Fallback** ⚠️