Transforming Customer Experiences with Intelligent AI Assistants
Today’s customers increasingly demand detailed, accurate, and readily available information about products before making purchasing decisions. Whether it’s checking specifications, reading reviews, or asking general questions, businesses can significantly enhance customer experience by offering intelligent assistants that quickly retrieve and present relevant data.
In this project, we’ll demonstrate how to build an AI-powered assistant designed to provide personalized experiences for customers exploring Apple phones. This assistant will efficiently retrieve detailed phone specifications, extract and summarize user reviews, and answer general queries, all within an interactive chatbot interface. To achieve this, we’ll use:
- LangGraph: A versatile workflow orchestration tool that simplifies designing and managing complex reasoning workflows.
- LangChain: A robust framework that enables seamless integration of natural language processing (NLP) models with external data sources and facilitates multi-step reasoning.
- Gradio: An intuitive library for creating customizable, user-friendly interfaces for AI applications, such as chatbots.
By leveraging these technologies, we will build a smart system that can efficiently classify queries, manage data retrieval, and provide context-aware responses tailored to users' needs.
Overview of the Data Sources
The assistant will draw insights from two main data repositories:
- Apple Phone Specifications: Data on features, technical specs, and other key details about Apple phones, extracted from datasets or documents.
- Customer Reviews: User feedback and opinions on Apple phones, also sourced from structured datasets or text documents.
Key Features of the AI Assistant
The assistant will categorize and respond to three primary query types:
- Specifications Queries: Retrieves detailed information about Apple phone features, such as display, camera, and performance.
- Review-Based Queries: Summarizes or retrieves customer opinions and ratings to help users make informed decisions.
- General Queries: Handles broader questions like product comparisons or FAQs about Apple phones.
This categorization ensures users receive the most relevant and precise information for their needs.
Tools and Technologies
- LangGraph
LangGraph helps us orchestrate the entire query-to-response workflow by defining clear decision-making paths. It allows us to create modular workflows where each query type (specifications, reviews, general) triggers a specific sub-task. LangGraph ensures our system can efficiently handle multi-step queries, such as combining specification details with a summary of user reviews. - LangChain
LangChain integrates large language models (LLMs) with the assistant’s data sources. By using LangChain’s capabilities, we can connect our assistant to structured datasets, perform in-depth query analysis, and ensure accurate, multi-stage reasoning for nuanced responses. Its tools for chaining tasks make it an ideal choice for processing complex queries involving both specifications and reviews. - Gradio
Gradio provides the user interface for our assistant. With its simple yet powerful framework, we’ll build an interactive chatbot interface that allows users to input queries, view detailed responses, and interact seamlessly. Gradio’s flexibility ensures the chatbot interface is not only functional but also visually appealing and intuitive.
Step-by-Step Process to Build the Assistant
Launching an E2E Node
Get started with E2E Cloud here. Here are some screenshots to help you navigate through the platform. Go to the Nodes option on the left side of the screen and open the dropdown menu. In our case, 100GB will work.
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/6715e05126aa29535905aecb_AD_4nXd4JGmb0aOduxS7GUwreh64i2s0fPjTnrshl6wpd5Do9NOYzRypxLgm1f3-YjYIszSTsYFa63rCnWPEg2_hmCV6C1bDtHCKR_O4ePWGlDP47wiWY_rQZlP0l5es_bzaHTRIfFZixSQWEK172LwebcFLJkfu.png)
Select the size of your disk as 50GB – it works just fine for our use case. But you might need to increase it if your use case changes.
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/6715e051809c171b16f30d98_AD_4nXe16pOsFCCYj68mUtAs2dNRDjTYCL0Rk9HrSjltPU0gosuGHVmz4EX6-yZW7OI_gnlTQfg3E2wYoPM9o4KvupY1EsGGVQqSTRcSES__PwYb37g3WMSkDw1MeCxes2b4CLZVGRjkrm9MamUKq7Y6EbD7669G.png)
Hit Launch to get started with your E2E Node.
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/6715e051ef1b646099644035_AD_4nXf5DdwrmR8cI_Oc2DQMAAiYu2KuRpQofEw386YvkkkLAGT2WvH1xFwQuVQXR2gA5YGpz0dZQfzGHYRXwtm8uQmb-Yndp_aV-HByQxY5Ul2aeOoTZR176L4qw5xB60gn_sKOred4HNZjpelTJUlVjgMkCq93.png)
When the Node is ready to be used, it’ll show the Jupyter Lab logo. Hit on the logo to activate your workspace.
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/6715e051c5a88b21007542d6_AD_4nXe9gADT5wOctU46F3qKRAN0PcHoZngSKdYhtjWoUC_7weyKJAEV76bX4kNnf5xm2P0u4j2Obijn0Y2TMkr-m_wfyAcmgoVErA8fzLMnmiQIgbTb4XLE68xzI7taTSm6VDsRE0bcKe6QALVeTqBJoeo1qlU.png)
Select the Python3 pykernel, then select the option to get your Jupyter Notebook ready. Now you are ready to start coding.
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/6715e0515c5e064b37d4bd43_AD_4nXdaQiHpms4moyKExdWxNDAFQNfKBLxQAOYbwd-tFKCII3BNQ3gsWF9HHWMJ-yELr0MbQLVf0OybCL46Zzd5xvw5b-SsH-9djSIl8eL40xeLJSroEcrPiZidi-k3j12EyJ7awnFJmkeyZstOm3hjuoZAtSo.png)
Installation of the Required Libraries
Before we start building the Apple Phone Assistant, we need to install the necessary Python libraries. These libraries provide various functionalities, such as working with LangGraph for AI workflows, PyPDF2 for PDF processing, and Gradio for creating a user-friendly interface.
To install them, use the following command:
!pip install -q langgraph PyPDF2 gradio transformers
- LangGraph: Helps in creating AI workflows and building stateful applications.
- PyPDF2: Allows extraction of content from PDFs (useful for extracting Apple phone reviews and specs).
- Gradio: Makes it easy to build interactive web-based UIs for machine learning models.
Once the installation is complete, we can move forward with creating the assistant.
Key Imports
This section covers the essential libraries and tools that enhance the functionality of the Apple Phone Assistant, enabling smooth user interactions and intelligent responses.
- PyPDF2
from PyPDF2 import PdfReader
PyPDF2 is used to extract text from PDF documents containing Apple phone specifications and customer reviews, which serve as the assistant’s knowledge base.
- LangGraph
from langgraph.graph
import StateGraph
from langgraph.graph.message import add_messages
from typing import Annotated
from typing_extensions import TypedDict
LangGraph manages the flow of conversation, using StateGraph to handle states and add_messages to maintain the message history. It helps route user queries to the appropriate workflows (specifications, reviews, or general queries).
- Gradio
import gradio as gr
Gradio provides the front-end interface for the assistant, allowing users to interact through a simple chat interface where queries and responses are exchanged in real time.
- Transformers
import transformers
from transformers import AutoTokenizer, AutoModelForCausalLM
Transformers Library offers pre-trained models for text generation and tokenization, enabling the assistant to handle complex queries and generate accurate, human-like responses.
These libraries collectively enable a responsive, intelligent assistant capable of processing user queries about Apple phone specs, reviews, and more.
Logging into Hugging Face CLI
To use Hugging Face models and datasets in your local environment, you need to authenticate with the Hugging Face CLI. Here’s how you can log in:
!huggingface-cli login
Loading the Llama 3.1 Model and Creating a Text Generation Pipeline
In this section, we’ll load the Llama 3.1 model and create a text generation pipeline. This allows you to generate text based on any given input.
Load the Model and Tokenizer
The model ID used is "meta-llama/Llama-3.1-8B".
You can access the model here - https://huggingface.co/meta-llama/Llama-3.1-8B
The tokenizer and model are loaded using the AutoTokenizer and AutoModelForCausalLM classes from the transformers library. We specify the torch_dtype as bfloat16 to optimize the model for performance, and device_map="auto" ensures the model is automatically placed on the appropriate hardware (GPU or CPU).
Create the Text Generation Pipeline
We create a text generation pipeline using the transformers.pipeline function. The max_length=200 argument ensures that the generated text does not exceed 200 tokens.
Here’s the code to do this:
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
import transformers
# Load Llama 3.1 model and tokenizer
model_id = "meta-llama/Llama-3.1-8B"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype=torch.bfloat16, device_map="auto")
# Create a text generation pipeline
pipeline = transformers.pipeline(
"text-generation",
model=model,
tokenizer=tokenizer,
max_length=1000
)
With this setup, you can now generate text by passing inputs to the pipeline. The model will generate coherent, contextually relevant responses based on the input provided.
Extracting Text from PDFs
In this section, we define a function to extract text from PDF documents. This can be particularly useful for processing customer reviews, product specifications, or other content stored in PDF format. We use the PyPDF2 library to read the PDF and extract its text.
Function Definition
- The function extract_text_from_pdf(pdf_path) accepts the path to a PDF file as its input.
- It initializes a PdfReader object from the PyPDF2 library to read the content of the PDF file.
- The function then iterates through each page in the PDF and appends the extracted text to a string.
- Finally, it returns the accumulated text.
Here's the code to extract text from PDFs:
from PyPDF2 import PdfReader
# Function to extract text from PDFs
def extract_text_from_pdf(pdf_path):
reader = PdfReader(pdf_path)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
You can now use this function to extract text from any PDF by providing its path. The text can then be processed further, such as for querying information or feeding it into a language model.
Loading Data from PDFs
After extracting the text from the PDF files, we load the data from the Apple phone specifications and customer reviews PDFs. This step will allow us to process and use the content within these documents in our AI-driven workflows.
Load PDF Data
- We use the extract_text_from_pdf function that we defined earlier to extract the content from the two PDF files: one containing the Apple phone specifications (apple-specs.pdf) and the other containing customer reviews (customer-review-apple.pdf).
- The extracted text is stored in two separate variables: specs_data and reviews_data.
Here's how the code works:
# Load data from PDFs
specs_pdf = "/content/apple-specs.pdf"
reviews_pdf = "/content/customer-review-apple.pdf"
specs_data = extract_text_from_pdf(specs_pdf)
reviews_data = extract_text_from_pdf(reviews_pdf)
After this step, you have the data from both PDFs ready to be used for query processing or integrated into your AI agent's workflow.
Specs Workflow Pipeline
In this implementation, we enhance the Specs Workflow by utilizing the Model pipeline for generating responses based on user queries about Apple phone specifications. The specs_workflow function works by taking the user's query and combining it with the technical specifications data extracted from PDFs. This combined input is then passed to a Llama3.1-8B text generation model (Llama 3.1 model) to produce a relevant response.
The function processes the input and generates a response detailing the specifications of the queried Apple phone model. The result is then returned to the user in a conversational format. If the pipeline fails to generate an answer, a fallback message is displayed. This approach offers a robust solution for querying specifications and makes use of a local model pipeline for efficient and scalable responses.
# Specs Workflow using pipeline
def specs_workflow(state: State):
user_query = tuple(state["messages"][0])[0][1] # Extract user query
# Define the prompt with the user's query and the specs data
prompt = f"You are an expert assistant who provides technical specifications about Apple phones. Use the following specs data to answer queries:\n\n{specs_data}\n\nUser Query: {user_query}\n\nProvide details about the query."
# Generate the response using the pipeline
response = pipeline(prompt, max_length=200, num_return_sequences=1)
# Extract the generated text from the pipeline output
generated_response = response[0]['generated_text'].strip()
# Return the generated response or a fallback message if no response was found
return {"messages": [("assistant", generated_response or "No specs found.")]}
Reviews Workflow
In this Reviews Workflow, we leverage the LLM pipeline to generate responses to user queries about customer reviews for Apple phones. By utilizing a pre-trained model, this function efficiently processes the input query and matches it with a dataset of reviews, returning relevant responses in real-time.
The reviews_workflow function now utilizes the pipeline to produce detailed responses. The user's query is used to construct a prompt along with the review data, and the pipeline generates the corresponding reviews. If no reviews are found or a response isn't generated, the function will return a fallback message.
# Reviews Workflow using pipeline
def reviews_workflow(state: State):
user_query = tuple(state["messages"][0])[0][1] # Extract user query
# Define the prompt with the user's query and the reviews data
prompt = f"You are an expert assistant who provides customer reviews about Apple phones. Use the following reviews data to answer queries:\n\n{reviews_data}\n\nUser Query: {user_query}\n\nProvide customer reviews about the query."
# Generate the response using the pipeline
response = pipeline(prompt, max_length=200, num_return_sequences=1)
# Extract the generated text from the pipeline output
generated_response = response[0]['generated_text'].strip()
# Return the generated response or a fallback message if no response was found
return {"messages": [("assistant", generated_response or "No reviews found.")]}
Fallback Workflow for Handling Unmatched Queries
The Fallback Workflow handles all user queries that do not fit into predefined categories such as product specs or reviews. When a user's query doesn't match a specific workflow, the fallback system steps in, utilizing a LLM to generate a response from the model. This ensures that the assistant can still provide answers even for general or unforeseen queries.
In this updated workflow, the user's query is passed through the LLM pipeline, which leverages the model to generate responses based on the input. If the model doesn't generate a suitable answer, the assistant will respond with a fallback message indicating the inability to process the query.
This structure ensures a smooth conversation flow even when the assistant is confronted with queries that are outside the predefined workflows.
# Fallback Workflow using pipeline
def fallback_workflow(state: State):
user_query = tuple(state["messages"][0])[0][1] # Extract user query
# Define the prompt for generating a response with the pipeline
prompt = f"You are a helpful assistant. Respond to the user's query in a clear and concise manner:\n\n{user_query}"
# Generate the response using the pipeline
response = pipeline(prompt, max_length=200, num_return_sequences=1)
# Extract the generated text from the pipeline output
generated_response = response[0]['generated_text'].strip()
# Return the generated response or a fallback message if no response was found
return {"messages": [("assistant", generated_response or "I'm sorry, I couldn't process your query.")]}
Classifying User Queries
The classify_query_with_pipeline function utilizes the Llama3.1-8B model pipeline to classify user queries into categories such as 'specs', 'reviews', or 'generic'. The function provides a text prompt that clearly instructs the assistant to classify the query into one of these categories, generating the result using the model pipeline. This approach leverages LLM robust capabilities to classify the user input and return the appropriate label.
def classify_query_with_pipeline(user_input: str):
# Define the prompt for classification
prompt = f"You are an intelligent assistant that classifies user queries into one of the following categories: 'specs', 'reviews', or 'generic'.\n\nClassify the following query: {user_input}"
# Generate response using Hugging Face pipeline
response = pipeline(prompt, max_length=50, num_return_sequences=1)
# Extract the classification result
classification = response[0]['generated_text'].strip()
return classification
Routing User Queries to Appropriate Workflows
The route_workflow function acts as the decision-making component of the system. Based on the user's query, it classifies the query into categories such as 'specs', 'reviews', or 'generic' using the classify_query_with_pipeline function. After classification, the function routes the query to the corresponding workflow (specs_workflow, reviews_workflow, or fallback_workflow). This allows the assistant to handle different types of requests effectively, ensuring that the appropriate response is provided based on the user's query type.
# Router Node
def route_workflow(state: State):
# Classify the user query into one of the categories: 'specs', 'reviews', or 'generic'
query_type = classify_query_with_pipeline(tuple(state["messages"][0])[0][1])
# Store the query type in the state for further processing
state["query_type"] = query_type
# Route to the appropriate workflow based on the query type
if query_type == "specs":
return specs_workflow(state)
elif query_type == "reviews":
return reviews_workflow(state)
else:
return fallback_workflow(state)
Creating the LangGraph Workflow
In this section, we create a LangGraph instance to manage the different workflows for the Apple Phone Assistant. We define a StateGraph and add the route_workflow as a node in the graph. The entry and finish points of the graph are set to the router node, ensuring that the system starts and ends with the query routing logic. Finally, the graph is compiled, ready to handle user queries through the specified workflows.
# Create LangGraph
graph_builder = StateGraph(State)
# Add workflows as nodes
graph_builder.add_node("router", route_workflow)
# Set entry and finish points
graph_builder.set_entry_point("router")
graph_builder.set_finish_point("router")
# Compile the graph
graph = graph_builder.compile()
Running the Apple Phone Assistant Agent
In this section, we define the run_agent function that simulates a conversation with the Apple Phone Assistant. The function continuously accepts user input and processes the query through the LangGraph workflow. If the user types "quit" or "exit", the conversation ends. The agent processes the query by routing it to the appropriate workflow and displays the assistant's response. This process occurs within a loop, making the interaction continuous until the user chooses to exit.
# Run the Agent
def run_agent():
print("Welcome to the Apple Phone Assistant! Type 'quit' to exit.")
while True:
user_input = input("User: ")
if user_input.lower() in ["quit", "exit"]:
print("Goodbye!")
break
# Process the query through LangGraph
for event in graph.stream({"messages": [("user", user_input)], "query_type": "generic"}):
for value in event.values():
print("Assistant:", value["messages"][-1][1])
# Start the agent
if __name__ == "__main__":
run_agent()
Building the Customer Assistant with a Gradio Interface
In this section, we create a user-friendly Gradio interface for the Apple Phone Assistant. The interface allows users to interact with the assistant through a simple web-based chatbot. Users can input queries, such as asking for specifications or reviews of Apple phones. The assistant processes the query using the LangGraph workflows, and the response is displayed on the interface. The application continues to run until the user types "quit" or "exit".
import gradio as gr
# Gradio interface function
def gradio_agent(user_input):
if user_input.lower() in ["quit", "exit"]:
return "Goodbye!"
# Process the query through LangGraph
responses = []
for event in graph.stream({"messages": [("user", user_input)], "query_type": "generic"}):
for value in event.values():
responses.append(value["messages"][-1][1]) # Collect all assistant responses
return "\n".join(responses)
# Create the Gradio interface
interface = gr.Interface(
fn=gradio_agent,
inputs=gr.Textbox(lines=2, placeholder="Enter your query here..."),
outputs=gr.Textbox(label="Assistant Response"),
title="Apple Phone Assistant",
description="Ask about Apple phone specs, reviews, or general queries.",
theme="compact"
)
# Launch the Gradio app
if __name__ == "__main__":
interface.launch()
Summary
In this blog, we explored how to build an AI agent for personalizing customer experiences. By integrating LangGraph workflows with powerful language models, we created an intelligent assistant capable of handling a variety of customer queries related to Apple phone specifications and reviews. The assistant was designed to classify queries, respond with relevant data, and handle fallback scenarios, all while ensuring a seamless user experience.
With a Gradio interface, we made the system accessible and easy to use, providing an interactive platform where users can engage with the assistant in real time. This project demonstrates how AI agents can be effectively utilized to enhance customer experiences by offering personalized and context-driven interactions.
As AI continues to evolve, such systems can be expanded to address a broader range of customer needs, helping businesses offer tailored support and improve overall satisfaction.
Supporting Context
Specs
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a98989b8f17935b1054839_AD_4nXeHTA1kx7tqmOgqLVjNs8ZFr3EGpkLCXivsW3J6N0FmsqFaCa0OWaE5r5Vn7WVSUFkkUYEcTlTUejs244Sq0r6N7UZv5bnoDtprw8r6yVscZqw-q55NtNJyGbqGuuje016VEo37.png)
Reviews
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a989898d33b7efb8585af3_AD_4nXc3YQ0pE2hUoaISJa5xomDahBJlWq3hMihTZXc5NW8Hq9k4N-bZdgWwgzr1ZVRVrRR6HtV6tRIqpTAYu_EewjCOVeKSE8mkOfaFnoJGOAbWfBLmGkuEqx5bg3b80WSWJcrXzTDI2A.png)
Results
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a98989b164e5805c5a4577_AD_4nXdoRpDtlgpDHOy1Axc32sM-Q9Uj5gWn8ymuQcKlCxCD2-EGJvJR8YbboWR9oJ6xQqdTJuaCc4klLdF1qk5SeIr5aDJz-DeQXWa5a_NK569B2kO-cDpbOveKCg_94Ppx-lqs7PvX2g.png)
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a98989bf8c1ddce5b73530_AD_4nXcyCk_-Cwt-2HjLv6xwZoZPH-uuBxIr_7LnGgwwgkCye8Rzvt4e0yx1ILibH3WRqK-PV3z_NsSVkbIkhRvoqxi4Oeq8l2TV3hPXgljYcWY8gfN_I3K_B_jKwTbv4qtkw8r0YH-R.png)
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a98989dae45ced7ebe672c_AD_4nXdi0bXJdYRUCkVnn_PvSdMhoKeq53SHmubSbvucyjS_t5HXBSpHXhyfTqhndntyvTZRgTFDwiQH2c8-Ba6u7Iua344Gq_IHOpM8fFMJwRrXxTA4TucZpGFjAl-zakO1c6IWCVDX5Q.png)
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a9898944bb668785309cba_AD_4nXcDorTiyFzU5yEaok6SjDeGSKRKYmmjWugTO1frsNPSNaWKLpvOyVWbpz1Iqe-MHVMljekcy_WZ1YZc6BdQYgS8lSW3EQJKvTMhGSfC_o7-BzvdozbfFNkPck8dkXHqGiQazsBhCA.png)
![](https://cdn.prod.website-files.com/6473d8d02a3cf26273f2787d/67a989893380730e89551911_AD_4nXdpTRTtAxCIOjv1-I4YIzJLmF6JzcjyIN2czEY054cubYVPjCOIKOx5jDbEIvxjuvblmLeMopTL_54BFhj2Q-Vgyb7i6FXKSZ042VYZafHgQTlKDvHfF8JtfhdCCEQBNOf5XvQl3w.png)
Why Choose E2E Cloud?
What E2E Cloud offers are the following:
- Unbeatable GPU Performance: Access top-tier GPUs like H200, H100, and A100—ideal for state-of-the-art AI and big data projects.
- India’s Best Price-to-Performance Cloud: Whether you’re a developer, data scientist, or AI enthusiast, E2E Cloud delivers affordable, high-performance solutions tailored to your needs.
Get Started Today
Ready to supercharge your projects with cutting-edge GPU technology?
E2E Cloud is your partner for bringing ambitious ideas to life, offering unmatched speed, efficiency, and scalability. Don’t wait—start your journey today and harness the power of GPUs to elevate your projects.