Part 10 · AI Unlocked

#10 Real-World Power: Advanced Applications of LangChain & LlamaIndex in AI Solutions

In this chapter, we dive into advanced real-world applications of the powerful frameworks LangChain and LlamaIndex.

October 28, 202412 minute readAI EngineeringOriginal on Medium

In this chapter, we dive into advanced real-world applications of the powerful frameworks LangChain and LlamaIndex. These tools, known for their adaptability and efficiency, can be employed across diverse sectors — be it video summarization, product recommendations, healthcare diagnostics, or even voice-activated assistants.

This chapter showcases how these frameworks can be fine-tuned to transform industry-specific tasks and deliver dynamic AI-driven solutions. With clear examples and code implementations, you’ll see how LangChain and LlamaIndex bring real value to complex workflows, making AI more context-aware, efficient, and actionable.

1. Legal Document Summarizer & Analyzer ⚖️📄

This example uses multiple libraries from both frameworks to handle legal document analysis and summarization.

Framework Details:

  • LangChain Libraries:
  • langchain.chains for managing conversation flow.
  • langchain.prompts for tailored prompt templates.
  • langchain.memory for retaining context.
  • LlamaIndex Libraries:
  • llama_index.indices for document indexing.
  • llama_index.query_engines for searching through indexed data.

Code Implementation:

from langchain.chains import ConversationChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
from llama_index.query_engines import SimpleQueryEngine
# Step 1: Load legal documents
legal_docs_loader = SimpleDirectoryReader('legal_documents')
legal_docs_data = legal_docs_loader.load_data()
# Step 2: Index legal documents using LlamaIndex
legal_index = GPTVectorStoreIndex(legal_docs_data)
# Step 3: Define prompt for analyzing legal clauses
legal_prompt = PromptTemplate(
    template="Summarize key clauses of this contract: {document}",
    variables=["document"]
)
# Step 4: Set up memory for follow-up questions
memory = ConversationBufferMemory()
# Step 5: Create conversation chain for legal analysis
legal_chain = ConversationChain(
    prompt_template=legal_prompt,
    memory=memory
)
# Step 6: Use LlamaIndex query engine for search
query_engine = SimpleQueryEngine(index=legal_index)
# Example usage
document_text = "This contract includes confidentiality and non-compete clauses..."
response = legal_chain.run(document=document_text)
# Search specific legal terms in indexed documents
search_results = query_engine.query("confidentiality clause")
print(response)
print(search_results)

Output Example:

Key Clauses: 1. Confidentiality, 2. Non-compete

Search Results: Detailed information on the confidentiality clause in the document. Real-World Impact:

  • Use Case: Legal firms can index large sets of contracts and retrieve relevant information efficiently.

2. HR Recruitment Assistant 🧑‍💼🔍

This example demonstrates how LangChain and LlamaIndex can enhance recruitment by automating candidate analysis.

Framework Details:

  • LangChain Libraries:
  • langchain.chains for handling multi-step interview workflows.
  • langchain.memory for remembering user preferences.
  • langchain.tools to fetch candidate data from external databases.
  • LlamaIndex Libraries:
  • llama_index.indices for resume indexing.
  • llama_index.embedding_models to generate vector embeddings of skills.

Code Implementation:

from langchain.chains import SequentialChain
from langchain.memory import ConversationBufferMemory
from langchain.tools import Tool
from llama_index import GPTVectorStoreIndex, SimpleCSVLoader
from llama_index.embedding_models import OpenAIEmbedding
# Step 1: Load resumes from a CSV file
resumes_loader = SimpleCSVLoader(file_path='resumes.csv')
resumes_data = resumes_loader.load_data()
# Step 2: Create embeddings and index resumes
embedding_model = OpenAIEmbedding(model="text-embedding-ada-002")
resumes_index = GPTVectorStoreIndex(resumes_data, embedding_model=embedding_model)
# Step 3: Define a recruitment prompt for candidate analysis
recruitment_prompt = PromptTemplate(
    template="Analyze this resume for Python skills: {resume}",
    variables=["resume"]
)
# Step 4: Set up memory for HR interactions
memory = ConversationBufferMemory()
# Step 5: Create recruitment workflow
recruitment_chain = SequentialChain(
    prompts=[recruitment_prompt],
    memory=memory
)
# Step 6: Tool for fetching real-time data
def fetch_candidate_data(candidate_id):
    return resumes_data.get(candidate_id, "Not found")
candidate_tool = Tool(name="Candidate Data", func=fetch_candidate_data)
# Example usage
resume_text = "Yusuf Sevinir, Python developer with 5+ years of experience..."
response = recruitment_chain.run(resume=resume_text)
# Fetch candidate data
candidate_data = candidate_tool.run("101")
print(response)
print(candidate_data)

Output Example:

AI Response: Candidate meets the requirement. Python expertise confirmed.

Candidate Data: Yusuf Sevinir, Python developer, 5+ years. Real-World Impact:

  • Use Case: Automates initial candidate screening by analyzing resumes and retrieving skill data from indexed resumes.

3. Travel Itinerary Generator ✈️🗺️

The tool creates travel itineraries based on user input, budget, and interests.

Framework Details:

  • LangChain Libraries:
  • langchain.chains for managing conversation flow.
  • langchain.memory for keeping user preferences.
  • langchain.prompts for generating personalized prompts.
  • LlamaIndex Libraries:
  • llama_index.indices to index travel guides.
  • llama_index.query_engines for semantic search on travel data.

Code Implementation:

from langchain.chains import ConversationChain
from langchain.prompts import PromptTemplate
from langchain.memory import ConversationBufferMemory
from llama_index import GPTVectorStoreIndex, SimpleWebPageReader
# Step 1: Load travel guides from web pages
web_loader = SimpleWebPageReader(urls=[
    "https://www.lonelyplanet.com/japan/tokyo",
    "https://www.tripsavvy.com/tokyo-travel-guide"
])
travel_data = web_loader.load_data()
# Step 2: Index travel guides
travel_index = GPTVectorStoreIndex(travel_data)
# Step 3: Define a prompt for personalized itineraries
itinerary_prompt = PromptTemplate(
    template="Create a {days}-day itinerary for {destination} within a budget of {budget}.",
    variables=["days", "destination", "budget"]
)
# Step 4: Set up memory for user preferences
memory = ConversationBufferMemory()
# Step 5: Create a conversation chain for itinerary planning
itinerary_chain = ConversationChain(
    prompt_template=itinerary_prompt,
    memory=memory
)
# Example usage
response = itinerary_chain.run(days="5", destination="Tokyo", budget="$1500")
# Semantic search in travel guides
query_engine = SimpleQueryEngine(index=travel_index)
search_results = query_engine.query("popular budget-friendly places in Tokyo")
print(response)
print(search_results)

Output Example:

Itinerary: Day 1: Meiji Shrine, Day 2: Asakusa Temple, Day 3: Tsukiji Market.

Search Results: Shibuya Crossing, Ueno Park (budget-friendly). Real-World Impact:

  • Use Case: Travel agencies can automate itinerary planning, offering personalized trips quickly.

4. Personalized Learning Assistant 📚👩‍🏫

The AI tutor provides personalized explanations and answers complex questions.

Framework Details:

  • LangChain Libraries:
  • langchain.memory for retaining past interactions.
  • langchain.prompts for generating learning prompts.
  • langchain.tools to fetch additional educational content.
  • LlamaIndex Libraries:
  • llama_index.indices to index study materials.
  • llama_index.embedding_models for vectorizing educational content.

Code Implementation:

from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.tools import Tool
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
from llama_index.embedding_models import OpenAIEmbedding
# Step 1: Load educational materials from directory
study_materials_loader = SimpleDirectoryReader('study_materials')
study_data = study_materials_loader.load_data()
# Step 2: Create embeddings and index study materials
embedding_model = OpenAIEmbedding(model="text-embedding-ada-002")
study_index = GPTVectorStoreIndex(study_data, embedding_model=embedding_model)
# Step 3: Define prompt for personalized learning
learning_prompt = PromptTemplate(
    template="Explain {topic} with examples for better understanding.",
    variables=["topic"]
)
# Step 4: Set up memory for tutoring sessions
memory = ConversationBufferMemory()
# Step 5: Create a tool for fetching additional content
def fetch_additional_content(topic):
    additional_content = {"Newton's Laws": "Newton's third law: action and reaction"}
    return additional_content.get(topic, "Not found")
learning_tool = Tool(name="Learning Assistant", func=fetch_additional_content)
# Example usage
response = learning_prompt.format(topic="Newton's Laws")
additional_content = learning_tool.run("Newton's Laws")
print(response)
print(additional_content)

Output Example:

AI Response: Newton's first law: objects remain at rest unless acted upon.

Additional Content: Newton's third law: action and reaction. Real-World Impact:

  • Use Case: Schools can deploy this AI tutor to offer interactive learning sessions.

Let’s extend the previous chapter by including additional real-world examples that highlight the versatility of LangChain and LlamaIndex. For each use case, we’ll provide code samples that demonstrate different libraries and functionalities within these frameworks.

5. YouTube Video Summarizer 🎥📝

A tool that summarizes YouTube videos based on transcripts, helping users get quick insights.

Framework Details:

  • LangChain Libraries:
  • langchain.chains to manage the video summary flow.
  • langchain.prompts for generating summary prompts.
  • langchain.tools to fetch video transcripts via YouTube’s API.
  • LlamaIndex Libraries:
  • llama_index.indices to index video transcripts.
  • llama_index.embedding_models to create embeddings of video content for semantic search.

Code Implementation:

from langchain.chains import SequentialChain
from langchain.prompts import PromptTemplate
from langchain.tools import Tool
from llama_index import GPTVectorStoreIndex, SimpleWebScraper
from llama_index.embedding_models import OpenAIEmbedding
# Step 1: Fetch video transcript using a tool
def fetch_transcript(video_url):
    # Assume this function uses YouTube's API to get transcript
    return "This is a transcript of the YouTube video..."
transcript_tool = Tool(
    name="Transcript Fetcher",
    func=fetch_transcript,
    description="Fetches video transcripts from YouTube."
)
# Step 2: Get the transcript
video_transcript = transcript_tool.run("https://youtube.com/watch?v=example")
# Step 3: Create embeddings and index the transcript
embedding_model = OpenAIEmbedding(model="text-embedding-ada-002")
transcript_index = GPTVectorStoreIndex([video_transcript], embedding_model=embedding_model)
# Step 4: Define summarization prompt
summary_prompt = PromptTemplate(
    template="Summarize the following transcript: {transcript}",
    variables=["transcript"]
)
# Step 5: Create summarization chain
summary_chain = SequentialChain(prompts=[summary_prompt])
# Example usage
response = summary_chain.run(transcript=video_transcript)
print(response)

Output Example:

Summary: This video covers the basics of Python programming, including variables, loops, and functions. Real-World Impact:

  • Use Case: Useful for researchers, journalists, and learners who want quick summaries of educational content or news.

6. E-Commerce Product Recommendation Engine 🛒🤖

A recommendation engine that uses user preferences to suggest products in real-time.

Framework Details:

  • LangChain Libraries:
  • langchain.memory to store user preferences and history.
  • langchain.tools for fetching real-time product data.
  • LlamaIndex Libraries:
  • llama_index.indices to index product catalogs.
  • llama_index.embedding_models for semantic matching of products to user interests.

Code Implementation:

from langchain.memory import ConversationBufferMemory
from langchain.tools import Tool
from llama_index import GPTVectorStoreIndex, SimpleCSVLoader
from llama_index.embedding_models import OpenAIEmbedding
# Step 1: Load product data
csv_loader = SimpleCSVLoader(file_path='products.csv')
product_data = csv_loader.load_data()
# Step 2: Create embeddings for product descriptions
embedding_model = OpenAIEmbedding(model="text-embedding-ada-002")
product_index = GPTVectorStoreIndex(product_data, embedding_model=embedding_model)
# Step 3: Store user preferences in memory
memory = ConversationBufferMemory()
# Step 4: Define a tool for real-time recommendations
def recommend_product(user_preferences):
    search_query = user_preferences.get("favorite_color", "") + " clothing"
    results = product_index.query(search_query)
    return results
recommend_tool = Tool(
    name="Product Recommender",
    func=recommend_product,
    description="Suggests products based on user preferences."
)
# Example usage
user_preferences = {"favorite_color": "red"}
recommended_products = recommend_tool.run(user_preferences)
print(recommended_products)

Output Example:

Recommended Products: Red T-Shirt, Red Dress, Red Sneakers Real-World Impact:

  • Use Case: Enhances the shopping experience by offering personalized product recommendations.

7. Healthcare Symptom Checker 🏥💬

An AI tool that suggests possible conditions based on symptoms described by the user.

Framework Details:

  • LangChain Libraries:
  • langchain.chains to manage multi-step diagnostic workflows.
  • langchain.memory to retain patient symptom history.
  • LlamaIndex Libraries:
  • llama_index.indices to index medical knowledge bases.
  • llama_index.query_engines for semantic searches in indexed medical data.

Code Implementation:

from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
from llama_index import GPTVectorStoreIndex, SimpleDirectoryReader
# Step 1: Load medical knowledge data
medical_data_loader = SimpleDirectoryReader('medical_knowledge')
medical_data = medical_data_loader.load_data()
# Step 2: Index medical data using LlamaIndex
medical_index = GPTVectorStoreIndex(medical_data)
# Step 3: Define memory for patient interaction
memory = ConversationBufferMemory()
# Step 4: Define diagnostic prompt
diagnostic_prompt = PromptTemplate(
    template="Based on the symptoms {symptoms}, suggest possible conditions.",
    variables=["symptoms"]
)
# Step 5: Create diagnostic conversation chain
diagnostic_chain = ConversationChain(
    prompt_template=diagnostic_prompt,
    memory=memory
)
# Example usage
patient_symptoms = "cough, fever, sore throat"
response = diagnostic_chain.run(symptoms=patient_symptoms)
# Semantic search in medical knowledge base
query_engine = SimpleQueryEngine(index=medical_index)
search_results = query_engine.query("fever and cough")
print(response)
print(search_results)

Output Example:

AI Response: Possible conditions are flu, COVID-19, or common cold.

Search Results: Treatment options for flu include rest, hydration, and over-the-counter medication. Real-World Impact:

  • Use Case: Provides a first-line diagnostic tool that assists healthcare professionals and patients in identifying potential conditions.

8. News Summarizer 📰📝

This tool scrapes news articles from a given URL and provides concise summaries.

Framework Details:

  • LangChain Libraries:
  • langchain.chains to handle article summarization.
  • langchain.tools for scraping articles.
  • LlamaIndex Libraries:
  • llama_index.indices to index news articles.
  • llama_index.embedding_models to create semantic embeddings for article content.

Code Implementation:

from langchain.chains import SequentialChain
from langchain.tools import Tool
from llama_index import GPTVectorStoreIndex, SimpleWebScraper
# Step 1: Fetch news article using a tool
def fetch_article(url):
    # Assume this function fetches article text from the given URL
    return "This is the content of the news article..."
news_tool = Tool(
    name="News Scraper",
    func=fetch_article,
    description="Fetches news articles from the web."
)
# Step 2: Get the article
article_text = news_tool.run("https://news.example.com/article")
# Step 3: Create embeddings and index the article
embedding_model = OpenAIEmbedding(model="text-embedding-ada-002")
news_index = GPTVectorStoreIndex([article_text], embedding_model=embedding_model)
# Step 4: Define summarization prompt
news_summary_prompt = PromptTemplate(
    template="Summarize this article: {article}",
    variables=["article"]
)
# Step 5: Create a summarization chain
summary_chain = SequentialChain(prompts=[news_summary_prompt])
# Example usage
response = summary_chain.run(article=article_text)
print(response)

Output Example:

Summary: The article discusses the latest economic trends, including rising inflation rates and stock market fluctuations. Real-World Impact:

  • Use Case: Helps users stay updated with quick news summaries, especially useful for journalists and researchers.

9. Voice-Activated Q&A Bot 🎙️🤖

An AI that uses speech-to-text to understand user queries and responds based on indexed data.

Framework Details:

  • LangChain Libraries:
  • langchain.memory to store conversation context.
  • langchain.agents to handle user voice commands.
  • LlamaIndex Libraries:
  • llama_index.query_engines for querying indexed Q&A data.
  • Whisper API (External):
  • Used for speech-to-text conversion.

Code Implementation:

import openai
from langchain.memory import ConversationBufferMemory
from langchain.agents import DecisionAgent
from llama_index import GPTVectorStoreIndex, SimpleCSVLoader
# Step 1: Set up Whisper for speech-to-text
def convert_speech_to_text(audio_file):
    openai.api_key = "YOUR_API_KEY"
    response = openai.Audio.transcribe("whisper-1", audio_file)
    return response["text"]
# Step 2: Load Q&A data and create an index
csv_loader = SimpleCSVLoader(file_path='qa_data.csv')
qa_data = csv_loader.load_data()
qa_index = GPTVectorStoreIndex(qa_data)
# Step 3: Define a memory buffer
memory = ConversationBufferMemory()
# Step 4: Define an agent for Q&A handling
def handle_query(query):
    search_results = qa_index.query(query)
    return search_results
qa_agent = DecisionAgent(func=handle_query)
# Example usage
audio_input = "user_voice_query.wav" # Simulated audio input
user_query = convert_speech_to_text(audio_input)
response = qa_agent.run(user_query)
print(response)

Output Example:

AI Response: "The answer to your question is based on the available data in the knowledge base." Real-World Impact:

  • Use Case: Voice-activated assistants for customer service centers, enhancing accessibility and user experience.

Recap: Real-World Power: Advanced Applications of LangChain & LlamaIndex in AI Solutions 🚀🔍

In this chapter, we explored the powerful real-world applications of LangChain and LlamaIndex across diverse sectors, from law ⚖️ and HR 🧑‍💼 to travel ✈️ and healthcare 🏥. Each use case showcased how these frameworks enable AI-driven workflows that are dynamic, context-aware, and efficient. Whether it’s summarizing YouTube videos 🎥, creating personalized learning experiences 📚, or assisting with voice-activated queries 🎙️, LangChain and LlamaIndex proved to be adaptable solutions for various industry needs.

In the next chapter, we’ll dive into 🚀 Elevating Your AI with Advanced RAG Techniques : A Comprehensive Guide 🎯, exploring how Retrieval-Augmented Generation can further enhance real-time AI interactions with powerful retrieval strategies. Stay tuned!