Creating an Interactive AI Assistant for CSV Data with LangChain and gTTS
In this article, we’ll walk through creating an interactive AI assistant that can handle CSV data, respond to user queries, and even speak the responses aloud. This assistant is built using LangChain for natural language processing, Pandas for data manipulation, and Google Text-to-Speech (gTTS) for voice responses.
Overview
Get Ahsan Tariq’s stories in your inbox
Join Medium for free to get updates from this writer.
The AI assistant will:
- Load and process a CSV file.
- Accept user queries and respond based on the CSV data.
- Speak the responses if the user chooses.
The assistant will also handle rate limits gracefully and ensure responses are clean and well-formatted.
Prerequisites
Before we start, make sure you have the following Python libraries installed:
pip install pandas langchain-core langchain-groq gtts playsound pyttsx3import os
os.environ["GROQ_API_KEY"]="gsk_vy2kY2xzMixdze5uaObkWGdyb3FY4VZP2X..."Step-by-Step Implementation
1. Importing Libraries
First, we import the necessary libraries:
import pandas as pd
from langchain_core.prompts import ChatPromptTemplate
from langchain_groq import ChatGroq
import time
from gtts import gTTS
from playsound import playsound
import os
import threading2. Setting Up the Text-to-Speech Function
We define a function speak_text to handle text-to-speech conversion using gTTS and play the audio:
# Global flag to control speaking
speak_flag = True
# Function to speak the response using gTTS in a separate thread
def speak_text(text):
def speak():
global speak_flag
if speak_flag:
tts = gTTS(text=text, lang='en')
tts.save("response.mp3")
playsound("response.mp3")
os.remove("response.mp3")
# Start a new thread for the TTS
threading.Thread(target=speak).start()3. Loading the CSV Data
Next, we define a function to load the CSV file into a Pandas DataFrame:
# Load CSV data
def load_csv(file_path):
df = pd.read_csv(file_path)
return df
# Prepare data for prompts
def prepare_data_for_prompt(df):
return df.head().to_dict() # Example: Only use the first few rows for simplicity4. Setting Up LangChain
We set up the LangChain prompt and chain:
# Setup LangChain prompt and chain
def setup_chain():
chat = ChatGroq(temperature=0, model_name="llama3-8b-8192")
system = "You are a helpful assistant."
human = "{text}"
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", human)])
chain = prompt | chat
return chain5. Handling Rate Limits
To handle rate limits gracefully, we implement an exponential backoff wait function:
# Exponential backoff wait function
def wait_before_next_request(attempt):
wait_time = 2 ** attempt # Exponential backoff
print(f"Rate limit exceeded. Waiting for {wait_time} seconds before retrying...")
time.sleep(wait_time)We also create a safe_invoke function that retries the request if it encounters rate limit errors:
# Safe invoke with retry mechanism
def safe_invoke(chain, input_data):
retry_attempts = 5
for attempt in range(retry_attempts):
try:
return chain.invoke(input_data)
except Exception as e: # Catch general exceptions for rate limit issues
if "Rate limit" in str(e): # Basic check for rate limit errors
print(f"Rate limit exceeded. Attempt {attempt + 1}")
wait_before_next_request(attempt)
else:
raise e
raise Exception("Failed to process request after multiple attempts")6. Main Function to Handle User Interaction
The main function handles user input and outputs responses:
# Main function to handle user input and output
def main(file_path):
global speak_flag
df = load_csv(file_path)
data = prepare_data_for_prompt(df)
chain = setup_chain()
conversation_history = []
while True:
question = input("Enter your question (or type 'exit' to quit): ")
if question.lower() == 'exit':
print("Exiting the program.")
break
# Append the new question to the conversation history
conversation_history.append(f"User: {question}")
history_text = "\n".join(conversation_history)
response = safe_invoke(chain, {"text": f"{history_text}\nAssistant: {question} Here is the data: {data}"})
# Append the response to the conversation history
conversation_history.append(f"Assistant: {response}")
# Clean and format the response
response_text = str(response).strip()
formatted_response = "\n".join([line.strip() for line in response_text.splitlines() if line.strip()])
print("\nResponse from model:\n")
print(formatted_response, "\n")
print("="*40 + "\n") # Divider for better readability
# Ask user if they want the response to be spoken
speak_choice = input("Do you want to hear the response? (yes/no): ").strip().lower()
if speak_choice == 'yes':
speak_flag = True
speak_text(formatted_response)
else:
speak_flag = False
# Usage
file_path = 'Irish-Times-180.csv'
main(file_path)7. Running the Assistant
To run the assistant, ensure you have a CSV file (Irish-Times-180.csv in this example) in the working directory and execute the script. The assistant will load the CSV data, accept your questions, and respond based on the data. It will also ask if you want the response to be spoken aloud.
Conclusion
In this article, we created an interactive AI assistant capable of processing CSV data, answering queries, and speaking responses. By combining LangChain, Pandas, and gTTS, we built a versatile assistant that handles natural language processing, data manipulation, and text-to-speech conversion efficiently.







