Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Build an AI SMS Chatbot with Replicate, LLaMA 2, and LangChain

Posted on Sep 12 Recently, Meta and Microsoft introduced the second generation of the Llama LLM (Large Language Model) to help developers and organizations to build generative AI-powered tools and experiences. Read on to learn how to build an AI SMS chatbot that answers questions like Ahsoka (from Star Wars) using LangChain templating, LLaMa 2, Replicate, and Twilio Programmable Messaging!Do you prefer learning via video more? Check out this TikTok summarizing this tutorial in 1 minute!⚠️ ngrok is needed for the development version of the application because your computer is likely behind a router or firewall, so it isn’t directly reachable on the Internet. You can also choose to automate ngrok as shown in this article.Replicate offers a cloud API and tools so you can more easily run machine learning models, abstracting away some lower-level machine learning concepts and handling infrastructure so you can focus more on your own applications. You can run open-source models that others have published, or package and publish your own, either publicly or privately.Since you will be installing some Python packages for this project, you will need to make a new project directory and a virtual environment.If you're using a Unix or macOS system, open a terminal and enter the following commands:If you're following this tutorial on Windows, enter the following commands in a command prompt window:Grab your default Replicate API Token or create a new one here.On the command line runNow it's time to write some code!Make a file called app.py and place the following import statements at the top.Though LLaMA 2 is tuned for chat, templates are still helpful so the LLM knows what behavior is expected of it. This starting prompt is similar to ChatGPT so it should behave similarly.Next, make a LLM Chain, one of the core components of LangChain. This allows us to chain together prompts and make a prompt history. The model is formatted as the model name followed by the version–in this case, the model is LlaMA 2, a 13-billion parameter language model from Meta fine-tuned for chat completions. max_length is 4096, the maximum number of tokens (called the context window) the LLM can accept as input when generating responses.Finally, make a Flask app to accept inbound text messages, pass that to the LLM Chain, and return the output as an outbound text message with Twilio Programmable Messaging.On the command line, run python app.py to start the Flask app.Now, your Flask app will need to be visible from the web so Twilio can send requests to it. ngrok lets you do this. With ngrok installed, run ngrok http 5000 in a new terminal tab in the directory your code is in.You should see the screen above. Grab that ngrok Forwarding URL to configure your Twilio number: select your Twilio number under Active Numbers in your Twilio console, scroll to the Messaging section, and then modify the phone number’s routing by pasting the ngrok URL with the /sms path in the textbox corresponding to when A Message Comes In as shown below:Click Save and now your Twilio phone number is configured so that it maps to your web application server running locally on your computer and your application can run. Text your Twilio number a question relating to the text file and get an answer from that file over SMS!You can view the complete code on GitHub here.There is so much fun for developers to have around building with LLMs! You can modify existing LangChain and LLM projects to use LLaMA 2 instead of GPT, build a web interface using Streamlit instead of SMS, fine-tune LLaMA 2 with your own data, and more! I can't wait to see what you build–let me know online what you're working on!Templates let you quickly answer FAQs or store snippets for re-use. Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink. Hide child comments as well Confirm For further actions, you may consider blocking this person and/or reporting abuse PARAM MITTAL - Jun 24 Bobur Umurzokov - Jul 27 Mohamad Albaker Kawtharani - Jun 23 MindsDB - Jul 27 Build the future of communications. Read more technical content and tutorials from Twilio. Once suspended, twilio will not be able to comment or publish posts until their suspension is removed. Once unsuspended, twilio will be able to comment and publish posts again. Once unpublished, all posts by twilio will become hidden and only accessible to themselves. If twilio is not suspended, they can still re-publish their posts from their dashboard. Note: Once unpublished, this post will become invisible to the public and only accessible to Lizzie Siegle. They can still re-publish the post if they are not suspended. Thanks for keeping DEV Community safe. Here is what you can do to flag twilio: twilio consistently posts content that violates DEV Community's code of conduct because it is harassing, offensive or spammy. Unflagging twilio will restore default visibility to their posts. DEV Community — A constructive and inclusive social network for software developers. With you every step of your journey. Built on Forem — the open source software that powers DEV and other inclusive communities.Made with love and Ruby on Rails. DEV Community © 2016 - 2023. We're a place where coders share, stay up-to-date and grow their careers.



This post first appeared on VedVyas Articles, please read the originial post: here

Share the post

Build an AI SMS Chatbot with Replicate, LLaMA 2, and LangChain

×

Subscribe to Vedvyas Articles

Get updates delivered right to your inbox!

Thank you for your subscription

×