How to Use Llama 3 with PandasAI and Ollama Locally

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
Large models have huge potential for data analysis. Today, we'll cover how to use Llama 3 with PandasAI and Ollama. We'll run this model locally, so we won't leverage any api key like OpenAI. The tools we'll use in this video are completely open-source. Let me briefly explain these tools. When it comes to data manipulation, as you know, pandas is king. You can think of Pandas AI as a smart version of pandas. Pandas AI is nothing but a Python tool that allows you to explore, clean, and analyze your data using generative AI. This means that you can talk to your data using this tool. Cool, right? Another amazing tool in generative AI is Ollama. As you know, it is difficult to work with large models locally. This is where Ollama comes into play. Ollama helps you run LLMs locally. Awesome, right? Here is the app we'll create. First, we'll load our dataset. The dataset we'll use is the Titanic dataset. Here, we'll enter our prompt to chat with our dataset. And then the app will return a response. This is a simple app but great to see the power of Llama-3. Let's take a look at the topics we'll handle. First, we'll create a virtual environment and then install the tools we'll use. Next, we'll initialize Llama 3 with Ollama. After that, we'll load the dataset. And then we'll build the app. Lastly, we'll chat with the dataset using PandasAI. Nice, we've seen the topics we'll cover. Let's go ahead and start with the setup. To write our codes, we're going to use the vs code editor. Now, let's create a virtual environment. To do this, we're going to use conda. Let's write, conda create -n let's name genai Let me press enter. I already created this. After that, we're going to activate this environment. conda activate genai Here you go. Our environment is ready to use. The next step we're going to do is install the tools we'll use. To do this, let's create a requirements.txt file. We're going to click on the new file and then name it. requirements.txt Okay. Let's write the libraries we'll use here. To work with PandasAI, let's write, pandas, and pandasai. Next, to build the app, we'll use streamlit. Let's write streamlit. Okay, these tools are enough to build the app. We're going to now install these. It's simple to do this with pip. Let's type, pip install -r requirements.txt Let me press enter. Here you go. Our tools are ready to use. To write our codes, we're going to create a Python file. Let me click on the new file and then give a name. Let's say, app.py Yeah, our file is ready. So far, we created a virtual environment and installed the tools. Let's go ahead and initialize the model with Ollama. When it comes to working with llms locally, Ollama is king. This tool allows you to run open-source LLMs, such as Llama 3 and Mistral. Trust me, it's very easy to use Ollama. All you need to do is download it from the website and then install it on your computer. After installing you can use Ollama in the terminal. First, let's start it. Let's go to the terminal and then Write, ollama serve I already started ollama. Ollama is ready to use. You can use many large models with Ollama. Let me show you these models. Let's click the model. There you go. Here, you can choose any model you want. It is a piece of cake to load a model with ollama. To install a model, we're going to use the terminal. Let me show you. Let's say, we want to install llama 3. ollama pull llama3 Let me press enter. There you go. As you can see, llama 3 is loaded. You can see the installed models with the list command. ollama list There you go. As you can see I loaded many models. The model we'll use is the Llama 3 8B version. Awesome, Ollama is ready to go. What we're going to do is initialize the model with Ollama. To do this, we're going to use the LocalLLM. First, let's import this class. from pandasai.llm.local_llm import LocalLLM Okay. What we're going to do now is instantiate an LLM object by specifying the model name. To do this, we're going to use compatibility. This helps the app to connect to Ollama. Let write, model = LocalLLM() Let's connect Ollama. api_base="http://localhost:11434/v1" Okay, next, let's specify the model. model="llama3" Nice, our model is ready. Let's go ahead and initialize the app with streamlit. First, let's import this tool. import streamlit as st Next, let's give the app a title. st.title("Data analysis with PandasAI") After that, let's run this app. To do this, in the terminal let's write, streamlit run app.py Let me press enter. Yeah, the app opens automatically in the browser. This is simple, right? You can see the title here. Let's go ahead and create a widget to load the dataset. To do this, we can use the file_uploader method. Let's say, uploaded_file = st.file_uploader() Let's give a text, "Upload a CSV file", Next, let's specify the file type, type=['csv']) Okay. Nice, we created a widget. Let's test this widget. Go to the app and click rerun. There you go. To load the dataset, let's click over here. And then select our dataset. Yeah, the file is uploaded. Easy peasy, right? Let's go ahead and take a look at the first rows of the dataset. To do this, let's use the if statement. if uploaded_file is not None: After that, let's convert data into the pandas dataframe. First, we're going to import pandas. import pandas as pd Next, let's read the dataset. data = pd.read_csv(uploaded_file) Now, let's write the first rows. st.write(data.head(3)) Let's test the app. To do this, let's go to the app and click rerun. There you go. You can see the first rows of the Titanic dataset. What we need to do now is convert the dataset into SmartDataframe. To do this, we're going to use the SmartDataframe class. Let's go back and write, from pandasai import SmartDataframe Next, let's get an object from this class. df = SmartDataframe() Let's pass our data, Let's set the config parameter, let's pass {"llm": model} Nice, our smart dataframe is ready. Next, to get the input from the user, let's create a text area. prompt = st.text_area("Enter your prompt:") Nice, our text area is ready. What we're going to do now is create a button. For this, let's use the button method with the if statement. if st.button("Generate"): When pressing this button, we want to run the prompt. if prompt: If the prompt is true, let's display a message while running the code. To do this, let's use the spinner method using the with keyword. with st.spinner("Generating response..."): Now all we need to do is return the response. To do this, let's write, st.write() Let's invoke the chat method for a response. df.chat(prompt)) Awesome, our app is ready. Now it's time to make inferences. Let me go to the app and then click rerun. There you go. All we need to do is write a prompt to chat with the dataset. Let's write, How many rows and columns are in the dataset? Let me click on the generate button. Yeah, the app is up and running. Voila! We got the output by talking to the dataset. Amazing, right? Now, we can explore our dataset using this app. Let's want to learn the average age. What is the average age? Let me click generate. There you go. The average age is about twenty-nine. Let's go ahead and find out How many people have more than 3 siblings? How many people have more than 3 siblings? Let me press generate. There you go. There are 30 people with more than 3 siblings. Let's move on and want to see how many people died and how many survived. How many people died and how many survived? Let me click generate. There you go. Here are the numbers of people who survived and died. This was very easy to find out with Llama 3 right? Let's go ahead and want to calculate the percentage of surviving passengers by gender. Return the percentage of passengers by gender Let me click generate. Yeah, it's done. The percentage of male passengers is 64.76% and and for female passengers, the percentage is 35.24%. As you can see, it's easy peasy to explore the dataset with Llama-3. Let's go ahead and plot charts to understand the dataset. Let's write, Draw a bar chart of the sex column Let me click generate. There you go. As you can see, men more than women. Okay. Let's go ahead and want to plot a pie plot. Plot a pie chart of the pclass column Let me click generate. It's done. Here is the pie chart. You can see percentages here. Let's go ahead and want to visualize the distribution of the fare column. Visualize the distribution of the fare column Let me click generate. There you go. Here is the distribution of the fare column. Now, we want to plot the histogram of the age column. Draw the histogram of the age column. Let me click generate. There you go. Here is the distribution of the age column. Let's go ahead and plot a histogram of the fare by sex. Draw the histogram of the fare column separated by the sex column. Let me click generate. There you go. Here is the distribution of the fare column by sex. You can plot any chart you want. Lastly, let's want to plot the heatmap chart of numerical variables Draw the heatmap of numerical variables. Let me click generate. There you go. Here is the heatmap chart. Note that garbage in, garbage out. So good prompt means good output. You may need to try a few prompts to find a good one. Yeah, that's it. In this video, we covered how to use PandasAI with Llama-3. To show this, we built an app step-by-step with Streamlit. Hope you enjoyed it. Thanks for watching. Don't forget to subscribe, like the video, and leave a comment. See you in the next video. Bye for now.
Info
Channel: Tirendaz AI
Views: 17,124
Rating: undefined out of 5
Keywords: tirendaz academy, data science, machine learning, deep learning, artificial intelligence, data analysis, ai, generative ai, generative ai python, generative ai tutorial, llama 3, llama 380, llama 3 local, llama 3 8b, llama 3 vs gpt 4, llama 3 ai, llama 3 meta, llama 3 llm, ollama, ollama windows, ollama rag, ollama langchain, ollama docker, ollama api, ollama agents, llama ai, ollama basics, ollama beginner, ollama best models, pandasai, pandas, pandas ai
Id: _dDaNgBDoHY
Channel Id: undefined
Length: 13min 55sec (835 seconds)
Published: Fri May 03 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.