How to Build a Chatbot Using Streamlit and Llama 2 – MUO – MakeUseOf
Llama 2 is an open-source large language model (LLM) developed by Meta. It is a competent open-source large language model, arguably better than some closed models like GPT-3.5 and PaLM 2. It consists of three pre-trained and fine-tuned generative text model sizes, including the 7 billion, 13 billion, and 70 billion parameter models.
You will explore Llama 2s conversational capabilities by building a chatbot using Streamlit and Llama 2.
How different is Llama 2 from its predecessor large language model, Llama 1?
Llama 2 significantly outperforms its predecessor in all respects. These characteristics make it a potent tool for many applications, such as chatbots, virtual assistants, and natural language comprehension.
To start building your application, you have to set up a development environment. This is to isolate your project from the existing projects on your machine.
First, start by creating a virtual environment using the Pipenv library as follows:
Next, install the necessary libraries to build the chatbot.
Streamlit: It is an open-source web app framework that renders machine learning and data science applications quickly.
Replicate: It is a cloud platform that provides access to large open-source machine-learning models for deployment.
To get a Replicate token key, you must first register an account on Replicate using your GitHub account.
Once you have accessed the dashboard, navigate to the Explore button and search for Llama 2 chat to see the llama-270b-chat model.
Click on the llama-270b-chat model to view the Llama 2 API endpoints. Click the API button on the llama-270b-chat models navigation bar. On the right side of the page, click on the Python button. This will provide you with access to the API token for Python Applications.
Copy the REPLICATE_API_TOKEN and store it safe for future use.
First, create a Python file called llama_chatbot.py and an env file (.env). You will write your code in llama_chatbot.py and store your secret keys and API tokens in the .env file.
On the llama_chatbot.py file, import the libraries as follows.
Next, set the global variables of the llama-270b-chat model.
On the .env file, add the Replicate token and model endpoints in the following format:
Paste your Replicate token and save the .env file.
Create a pre-prompt to start the Llama 2 model depending on what task you want it to do. In this case, you want the model to act as an assistant.
Set up the page configuration for your chatbot as follows:
Write a function that initializes and sets up session state variables.
The function sets the essential variables like chat_dialogue, pre_prompt, llm, top_p, max_seq_len, and temperature in the session state. It also handles the selection of the Llama 2 model based on the user's choice.
Write a function to render the sidebar content of the Streamlit app.
The function displays the header and the setting variables of the Llama 2 chatbot for adjustments.
Write the function that renders the chat history in the main content area of the Streamlit app.
The function iterates through the chat_dialogue saved in the session state, displaying each message with the corresponding role (user or assistant).
Handle the user's input using the function below.
This function presents the user with an input field where they can enter their messages and questions. The message is added to the chat_dialogue in the session state with the user role once the user submits the message.
Write a function that generates responses from the Llama 2 model and displays them in the chat area.
The function creates a conversation history string that includes both user and assistant messages before calling the debounce_replicate_run function to obtain the assistant's response. It continually modifies the response in the UI to give a real-time chat experience.
Write the main function responsible for rendering the entire Streamlit app.
It calls all the defined functions to set up the session state, render the sidebar, chat history, handle user input, and generate assistant responses in a logical order.
Write a function to invoke the render_app function and start the application when the script is executed.
Now your application should be ready for execution.
Create a utils.py file in your project directory and add the function below:
The function performs a debounce mechanism to prevent frequent and excessive API queries from a users input.
Next, import the debounce response function into your llama_chatbot.py file as follows:
Now run the application:
Expected output:
The output shows a conversation between the model and a human.
Some real-world examples of Llama 2 applications include:
With closed models like GPT-3.5 and GPT-4, it is pretty difficult for small players to build anything of substance using LLMs since accessing the GPT model API can be quite expensive.
Opening up advanced large language models like Llama 2 to the developer community is just the beginning of a new era of AI. It will lead to more creative and innovative implementation of the models in real-world applications, leading to an accelerated race toward achieving Artificial Super Intelligence (ASI).
More here:
How to Build a Chatbot Using Streamlit and Llama 2 - MUO - MakeUseOf
- Qubetics: Investor's Favorite & Top Crypto to Invest in Today, Plus Kaspa Blockchain Tech & Artificial Super Intelligence Alliance Networks -... - March 28th, 2025 [March 28th, 2025]
- Qubetics Among The Best Crypto ICOs to Invest In Now, Solanas Rise, and Artificial Super Intelligence Alliance Dominance - Analytics Insight - March 3rd, 2025 [March 3rd, 2025]
- Elon Musk Is Not Taking Over the GovernmentAI Is: The NSA and the Emergence of Artificial Super Intelligence - substack.com - March 3rd, 2025 [March 3rd, 2025]
- Next Bull Run Crypto: How Qubetics, Artificial Super Intelligence Alliance, and Solana Are Shaping the Future of Blockchain - Techpoint Africa - February 27th, 2025 [February 27th, 2025]
- The Best Crypto to Buy This Week: Qubetics, Stacks, and Artificial Super Intelligence AllianceWhich One Deserves Your Investment? - MSN - February 27th, 2025 [February 27th, 2025]
- Learn How Solana And Artificial Super Intelligence Alliance Are Gaining Traction And Why Qubetics ($TICS) Could Be the Next Bull Run Crypto With... - February 16th, 2025 [February 16th, 2025]
- The Singularity Is Near: How Super Artificial Intelligence Will Reshape The World - Space Daily - January 26th, 2025 [January 26th, 2025]
- 5 Top Altcoins to Invest in This Week: Qubetics ($TICS), Arweave, Ondo, ZIGnaly And Artificial Super Intelligence Alliance Are On Spotlight -... - January 26th, 2025 [January 26th, 2025]
- The Best Altcoin with 100x Potential: Qubetics ($TICS) Has Earned the Trust of Over 14,000 Holders, While Artificial Super Intelligence Alliance... - January 13th, 2025 [January 13th, 2025]
- SoftBank's Masayoshi Son says artificial super intelligence to exist by 2035 - MSN - November 2nd, 2024 [November 2nd, 2024]
- SoftBank's Son says artificial super intelligence to exist by 2035 - MSN - November 2nd, 2024 [November 2nd, 2024]
- Qubetics Leads the Charge Against Quantum Threats, Fantom Soars and Artificial Super Intelligence Alliance Set for Growth: Guest Post by TheCoinrise... - October 12th, 2024 [October 12th, 2024]
- $OCEAN, $AGIX, And $FET Merge To Propel The Development Of Artificial Super Intelligence - The Merkle News - September 10th, 2024 [September 10th, 2024]
- Specter of Artificial Super Intelligence Looms in Camden Discussion - Freepress Online - August 25th, 2024 [August 25th, 2024]
- AI Coin Price: Will Artificial Superintelligence Alliance Have Bullish Impact? - Bankless Times - July 6th, 2024 [July 6th, 2024]
- 3 crypto firms are combining into one AI token - Morning Brew - June 16th, 2024 [June 16th, 2024]
- Could This New Artificial Intelligence (AI) Crypto Token Be a Millionaire Maker? - The Motley Fool - June 16th, 2024 [June 16th, 2024]
- Former OpenAI researcher outlines AI advances expectations in the next decade - Windows Central - June 16th, 2024 [June 16th, 2024]
- Creepy Study Suggests AI Is The Reason We've Never Found Aliens - ScienceAlert - May 11th, 2024 [May 11th, 2024]
- Beyond Human Cognition: The Future of Artificial Super Intelligence - Medium - January 16th, 2024 [January 16th, 2024]
- AI can easily be trained to lie and it can't be fixed, study says - Yahoo New Zealand News - January 16th, 2024 [January 16th, 2024]
- OpenAI's Ilya Sutskever Has a Plan for Keeping Super-Intelligent AI in Check - WIRED - December 17th, 2023 [December 17th, 2023]
- Sam Altman on OpenAI and Artificial General Intelligence - TIME - December 17th, 2023 [December 17th, 2023]
- Will AIs Next Wave of Super Intelligence Replace Human Ingenuity? Its Complicated - Grit Daily - December 17th, 2023 [December 17th, 2023]
- New Novel Skillfully Weaves Artificial Intelligence, Martial Arts and ... - Lakenewsonline.com - November 14th, 2023 [November 14th, 2023]
- Googles artificial intelligence predicts the weather around the globe in just one minute - EL PAS USA - November 14th, 2023 [November 14th, 2023]
- Nick Bostrom: Will AI lead to tyranny? - UnHerd - November 14th, 2023 [November 14th, 2023]
- Appeals court mulls whether to revive Wynn FARA case - POLITICO - November 14th, 2023 [November 14th, 2023]
- The AI Revolution From Evolution to Super intelligence - Cryptopolitan - October 21st, 2023 [October 21st, 2023]
- AI Symposium Explores Flaws and Potential of Artificial Intelligence - The Skanner - October 21st, 2023 [October 21st, 2023]
- Artificial intelligence has surprising pick to win 2024 Super Bowl - ClutchPoints - October 21st, 2023 [October 21st, 2023]
- Artificial Intelligence isn't taking over anything - Talon Marks - October 21st, 2023 [October 21st, 2023]
- AI and You: The Chatbots Are Talking to Each Other, AI Helps ... - CNET - October 21st, 2023 [October 21st, 2023]
- ONU's Polar SURF undergraduate research projects expand into the ... - Northern News - October 21st, 2023 [October 21st, 2023]
- Why Artificial Intelligence Needs to Consider the Unique Needs of ... - Women's eNews - September 27th, 2023 [September 27th, 2023]
- What Is Image-to-Image Translation? | Definition from TechTarget - TechTarget - September 27th, 2023 [September 27th, 2023]
- There is probably an 80% consensus that free will is actually ... - CTech - September 27th, 2023 [September 27th, 2023]
- Meta is planning on introducing dozens of chatbot personas ... - TechRadar - September 27th, 2023 [September 27th, 2023]
- We Cannot Trust AI With Control Of Our Bombs - Fair Observer - August 26th, 2023 [August 26th, 2023]
- AI: is the end nigh? | Laura Dodsworth - The Critic - August 26th, 2023 [August 26th, 2023]
- "Most Beautiful Car in the World" Alfa Romeo Asks People To ... - autoevolution - August 26th, 2023 [August 26th, 2023]
- Managing Past, Present and Future Epidemics - Australian Institute ... - Australian Institute of International Affairs - August 26th, 2023 [August 26th, 2023]
- The Best Games From Rare Per Metacritic - GameRant - August 26th, 2023 [August 26th, 2023]
- AI is the Scariest Beast Ever Created, Says Sci-Fi Writer Bruce Sterling - Newsweek - July 2nd, 2023 [July 2nd, 2023]
- Lets focus on AIs risks rather than existential threats - Business Plus - July 2nd, 2023 [July 2nd, 2023]
- Risks of artificial intelligence must be considered as the technology ... - University of Toronto - July 2nd, 2023 [July 2nd, 2023]
- Best Evil Technology Movies, From Terminator to M3GAN - CBR - Comic Book Resources - July 2nd, 2023 [July 2nd, 2023]
- 15 Super Cool Wallpapers for iPhone and Android - YMWC 18 - YTECHB - July 2nd, 2023 [July 2nd, 2023]
- PUB CHAT: Changing lives congrats to all grads and those who ... - Finger Lakes Times - July 2nd, 2023 [July 2nd, 2023]
- AI poses an existential threat, according to Munk Debates crowd ... - The Hub - July 2nd, 2023 [July 2nd, 2023]
- The Cautionary Tale of J. Robert Oppenheimer - Alta Magazine - July 2nd, 2023 [July 2nd, 2023]
- Virgin Voyages and JLo Bust on A.I. To Sell Vacations - We Got This Covered - July 2nd, 2023 [July 2nd, 2023]
- Cannes Diary: Will Artificial Intelligence Democratize Creativity or Lead to Certain Doom? - Hollywood Reporter - May 20th, 2023 [May 20th, 2023]
- Schools 'bewildered' by very fast rate of change in AI education ... - The Irish News - May 20th, 2023 [May 20th, 2023]
- Sam Altman is plowing ahead with nuclear fusion and his eye-scanning crypto ventureand, oh yeah, OpenAI - Fortune - May 20th, 2023 [May 20th, 2023]
- The Future of War Is AI - The Nation - May 20th, 2023 [May 20th, 2023]
- NFL fans outraged after ChatGPT names best football teams since 2000 including a surprise at No 1... - The US Sun - May 20th, 2023 [May 20th, 2023]
- We need to prepare for the public safety hazards posed by artificial intelligence - The Conversation - May 20th, 2023 [May 20th, 2023]
- What are the four main types of artificial intelligence? Find out how future AI programs can change the world - Fox News - May 20th, 2023 [May 20th, 2023]
- Did Tom Hanks Say He Will Use AI to Make Films After His Death? - Snopes.com - May 20th, 2023 [May 20th, 2023]
- These are the top 10 athletes of all time from the state of Iowa, according to ChatGPT - KCCI Des Moines - May 20th, 2023 [May 20th, 2023]
- Inside The High-Tech Homes Of The Super-Rich: Smart Systems, Security Fortresses And Personalized Gadgets - Yahoo Finance - May 20th, 2023 [May 20th, 2023]
- ChatGPT cant think consciousness is something entirely different to today's AI - The Conversation - May 20th, 2023 [May 20th, 2023]
- IIT-Mandi startup develops AI-based affordable solution to detect respiratory, genetic disorders - The Hindu - May 2nd, 2023 [May 2nd, 2023]
- Horrors Best And Scariest Uses of Artificial Intelligence - Dread Central - May 2nd, 2023 [May 2nd, 2023]
- Artificial intelligence or active imagination with ChatGPT? - Irish Examiner - May 2nd, 2023 [May 2nd, 2023]
- Reggie Watts on Late Late Show and Artificial Intelligence - Vulture - May 2nd, 2023 [May 2nd, 2023]
- Centaur Labs CEO: Unlocking AI for Healthcare Requires Expert Annotation - PYMNTS.com - May 2nd, 2023 [May 2nd, 2023]
- Super Active 32-Year-Old Dealmaker Is Japan's Newest Billionaire - Forbes - May 2nd, 2023 [May 2nd, 2023]
- Kevin McKenna meets tech thinker Margaret Totten | HeraldScotland - HeraldScotland - May 2nd, 2023 [May 2nd, 2023]
- Those 'Mrs. Davis' Sneakers Are Real and You Can Buy Them Now - Yahoo News - May 2nd, 2023 [May 2nd, 2023]
- Norway's $1.4tn wealth fund calls for state regulation of AI - Financial Times - May 2nd, 2023 [May 2nd, 2023]
- Macquarie chief Shemara Wikramanayake believes greater ... - The Australian Financial Review - May 2nd, 2023 [May 2nd, 2023]