0% found this document useful (0 votes)
3 views38 pages

Generative AI - Lab Programs

Uploaded by

somethingsomeof
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views38 pages

Generative AI - Lab Programs

Uploaded by

somethingsomeof
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Generative AI

Course Code: BAIL657C


Program 1
Explore pre-trained word vectors. Explore word relationships using
vector arithmetic. Perform arithmetic operations and analyze results.

from [Link] import load


# Load the pre-trained GloVe model (50 dimensions)
print("Loading pre-trained GloVe model (50 dimensions)...")
model = load("glove-wiki-gigaword-50")
# Function to perform vector arithmetic and analyze relationships
def ewr():
result = model.most_similar(positive=['king', 'woman'], negative=['man'],
topn=1)
print("\nking - man + woman = ?", result[0][0])
print("similarity:", result[0][1])
result = model.most_similar(positive=['paris', 'italy'], negative=['france'],
topn=1)
print("\nparis - france + italy = ?", result[0][0])
print("similarity:", result[0][1])
# Example 3: Find analogies for programming
result = model.most_similar(positive=['programming'], topn=5)
print("\nTop 5 words similar to 'programming':")
for word, similarity in result:
print(word, similarity)
ewr()
Program 2
Use dimensionality reduction (e.g., PCA or t-SNE) to visualize word
embeddings for Q 1. Select 10 words from a specific domain (e.g.,
sports, technology) and visualize their embeddings. Analyze clusters and
relationships. Generate contextually rich outputs using embeddings.
Write a program to generate 5 semantically similar words for a given
input.

import [Link] as plt


from [Link] import PCA
from [Link] import load
# Dimensionality reduction using PCA
def rd(ems):
pca = PCA(n_components=2)
r = pca.fit_transform(ems)
return r
# Visualize word embeddings
def visualize(words, ems):
[Link](figsize=(10, 6))
for i, word in enumerate(words):
x, y = ems[i]
[Link](x, y, marker='o', color='blue')
[Link](x + 0.02, y + 0.02, word, fontsize=12)
[Link]()
# Generate semantically similar words
def gsm(word):
sw = model.most_similar(word, topn=5)
for word, s in sw:
print(word, s)
# Load pre-trained GloVe model from Gensim API
print("Loading pre-trained GloVe model (50 dimensions)...")
model = load("glove-wiki-gigaword-50")
words = ['football', 'basketball', 'soccer', 'tennis', 'cricket']
ems = [model[word] for word in words]
e = rd(ems)
visualize(words, e)
gsm("programming")
Program 3

Train a custom Word2Vec model on a small dataset. Train embeddings


on a domain-specific corpus (e.g., legal, medical) and analyze how
embeddings capture domain-specific semantics.

from [Link] import Word2Vec


from [Link] import PCA
import [Link] as plt

corpus = [
"The patient was diagnosed with diabetes and hypertension.",
"MRI scans reveal abnormalities in the brain tissue.",
"The treatment involves antibiotics and regular monitoring.",
"Symptoms include fever, fatigue, and muscle pain.",
"The vaccine is effective against several viral infections.",
"Doctors recommend physical therapy for recovery.",
"The clinical trial results were published in the journal.",
"The surgeon performed a minimally invasive procedure.",
"The prescription includes pain relievers and anti-inflammatory drugs.",
"The diagnosis confirmed a rare genetic disorder."
]
token_corp =[[Link]().split() for sentence in corpus]
model=Word2Vec(sentences=token_corp,vector_size=5,window=2,
min_count=1,epochs=1000)
w=input("enter a word:").lower()
if w in [Link]:
similar=[Link].most_similar(w,topn=5)
print(f" word similar to {w}")
for i ,(wo,score) in enumerate(similar,1):
print(f"{i}.{wo} similarity:{score } ")
else:
print(" word not found in the vocabulary")
words=list([Link].index_to_key)
word_vectors=[Link][words]
pca=PCA(n_components=2)
result=pca.fit_transform(word_vectors)
[Link](figsize=(10,8))
[Link](result[:,0],result[:,1])
for i,word in enumerate(words):
[Link](word,xy=(result[i,0],result[i,1]))
[Link]("word embeddings visualization")
[Link]("PCA 1")
[Link]("PCA 2")
[Link](True)
[Link]()
Program 4
Use word embeddings to improve prompts for Generative AI model.
Retrieve similar words using word embeddings. Use the similar words
to enrich a GenAI prompt. Use the AI model to generate responses for
the original and enriched prompts. Compare the outputs in terms of
detail and relevance.

pip install gensim


pip install nltk
pip install transformers
from [Link] import load
from transformers import pipeline
import nltk
import string
from [Link] import word_tokenize
[Link]('punkt_tab')
print("loading pre trained word vectors")
word_vectors=load("glove-wiki-gigaword-100")
def replace_keyword_in_prompt(prompt,keyword,word_vectors,topn=1):
words=word_tokenize(prompt)
enriched_words=[]
for word in words:
cleaned_word=[Link]().strip([Link])
if cleaned_word==[Link]():
try:
similar_words=word_vectors.most_similar(cleane
d_word ,topn=topn)
if similar_words:
replacement_word=similar_words[0][0]
print(f"Replacing {word}-> {replacement_word}")
enriched_words.append(replacement_word)
continue
except KeyError:
print(f"{keyword} not found in vocabulary using
original word")
enriched_words.append(word)
enriched_prompt=" ".join(enriched_words)
print(f"\n Enriched Prompt:{enriched_prompt}")
return enriched_prompt
print("\n Loading GPT-4 model")
generator=pipeline("text-generation",model="gpt2")
def generate_response(prompt,max_length=100):
try:
response=generator(prompt,max_length=max_length,num_return_seq
uen ces=1)
return response[0]['generated_text']
except Exception as e:
print(f"error generating response {e}")
return None
original_prompt="write an essay on natural disaster"
print(f"Original prompt: {original_prompt}")
k_term="disaster"
enriched_prompt=replace_keyword_in_prompt(original_prompt,k_term,word_v
ectors)
print("\n generating response for original prompt")
original_response=generate_response(original_prompt)
print(original_response)
print("\n generating response for enriched prompt")
enriched_response=generate_response(enriched_prompt)
print(enriched_response)
print("\n comparison of responses")
print("original prompt response length",len(original_response))
print("enriched prompt response length",len(enriched_response))
print("original prompt response detail",original_response.count("."))
print("enriched prompt response detail",enriched_response.count("."))
Program 5
Use word embeddings to create meaningful sentences for creative tasks.
Retrieve similar words for a seed word. Create a sentence or story using
these words as a starting point. Write a program that: Takes a seed
word. Generates similar words. Constructs a short paragraph using
these words.

import random
import [Link] as api
# Load a pre-trained word embedding model
model = [Link]("glove-wiki-gigaword-50") # 50D GloVe embeddings
def get_similar_words(seed_word, top_n=5):
try:
similar_words = [word for word, _ in
model.most_similar(seed_word,topn=top_n)]
return similar_words
except KeyError:
return []
def create_paragraph(seed_word):
similar_words = get_similar_words(seed_word)
if not similar_words:
return f"Could not find similar words for '{seed_word}'. Try another
word!"
# Create a simple paragraph
paragraph = (
f"Once upon a time, a {seed_word} embarked on a journey. Along the way,
it encountered "
f"a {[Link](similar_words)}, which led it to a hidden
{[Link](similar_words)}. "
f"Despite the challenges, it found {[Link](similar_words)} and
embraced the "
f"adventure with {[Link](similar_words)}. In the end, the journey
was a tale of "
f"{[Link](similar_words)} and discovery."
)
return paragraph
# Example usage
seed_word = input("Enter a seed word: ").strip().lower()
print("\nGenerated Story:\n")
print(create_paragraph(seed_word))
Program 6
Use a pre-trained Hugging Face model to analyze sentiment in text.
Assume a real-world application, Load the sentiment analysis pipeline.
Analyze the sentiment by giving sentences to input.

from transformers import pipeline


# Load the sentiment analysis pipeline
sentiment_analyzer = pipeline("sentiment-analysis")
def analyze_sentiment(text):
result = sentiment_analyzer(text)
label = result[0]['label']
score=result[0]['score']
return f"Sentiment: {label} (Confidence: {score:.2f})"
while True:
user_input = input("Enter a sentence for sentiment analysis (or 'exit'
to quit):").strip()
if user_input.lower() == 'exit':
break
print(analyze_sentiment(user_input))
Program 7
Summarize long texts using a pre-trained summarization model using
Hugging face model. Load the summarization pipeline. Take a passage
as input and obtain the summarized text.

!pip install transformers torch


from transformers import AutoModelForSeq2SeqLM, AutoTokenizer
model_name ="facebook/bart-large-cnn"
tokenizer= AutoTokenizer.from_pretrained(model_name)

model =AutoModelForSeq2SeqLM.from_pretrained(model_name)

text="""Air pollution is the presence of substances in the air that are


harmful to humans, other living beings or the environment. Pollutants can be
gases, like ozone or nitrogen oxides, or small particles like soot and dust.
Both outdoor and indoor air can be polluted. Outdoor air pollution comes
from burning fossil fuels for electricity and transport, wildfires, some
industrial processes, waste management, demolition and agriculture. Indoor
air pollution is often from burning firewood or agricultural waste for
cooking and heating.
Other sources of air pollution include dust storms and volcanic eruptions.
Many sources of local air pollution, especially burning fossil fuels, also
release greenhouse gases that cause global warming. However, air pollution
may limit warming locally. Air pollution kills 7 or 8 million people each
year. It is a significant risk factor for a number of diseases, including stroke,
heart disease, chronic obstructive pulmonary disease (COPD), asthma,
coronavirus and lung cancer. Particulate matter is the most deadly, both for
indoor and outdoor pollution. Ozone affects crops, and forests are damaged
by the pollution that causes. acid rain. Overall, the World Bank has
estimated that welfare losses (premature deaths) and productivity losses (lost
labor caused by air pollution cost the world economy over $8 trillion per
year. Various technologies and str strategies reduce air pollution. Key
approaches include clean cookers, fire protection, improved waste
management, dust control, industrial scrubbers, electric vehicles and
renewable energy. National air quality laws have often been effective,
notably the 1956 Clean Air Act in Britain and the 1963 US Clean Air Act.
International efforts have had mixed results: the Montreal Protocol almost
eliminated harmful azone-depleting chemicals, while international action
clinate change has been less successful. """

#Tokenize the input text

Inputs= [Link](text, return_tensors="pt", max_length=512,


truncation-True)

#Generate the summary

summary_ids= [Link] (inputs, max_length=50, min_length=25,


length_penalty=2.0, num_beams=4, early_stopping=True)
summary=[Link](summary_ids[0], skip_special_tokens=True)
print(summary)
Program 8

Install langchain, cohere (for key), langchain-community. Get the api


key( By logging into Cohere and obtaining the cohere key). Load a text
document from your google drive . Create a prompt template to display
the output in a particular manner.

!pip install langchain cohere langchain-community google-colab


!pip install langchain-cohere
import cohere
import getpass
from langchain import PromptTemplate
from langchain_cohere import ChatCohere
from langchain_core.messages import HumanMessage
from [Link] import auth
from [Link] import drive
auth.authenticate_user()
[Link]('/content/drive')
file_path = "/content/drive/MyDrive/[Link]"
try:
with open(file_path, "r", encoding="utf-8") as file:
text_content = [Link]()
print(" File loaded successfully!")
except Exception as e:
print(" Error loading file:", str(e))
COHERE_API_KEY = [Link]("Enter your Cohere API Key: ")
# cZXJLZqoeIt9h40IWLLQgF4xKLR99XGDr1Gx7Ax4
cohere_llm = ChatCohere(cohere_api_key=COHERE_API_KEY,
model="command-a-03-2025")
template = """
You are an AI assistant helping to summarize and analyze a text document.
Here is the document content:
{text}
* Summary:
- Provide a concise summary of the document.
* Key Takeaways:
- List 3 important points from the text.
* Sentiment Analysis:
- Determine if the sentiment of the document is Positive, Negative, or
Neutral. """
prompt_template=PromptTemplate(input_variables=["text"],template=templ
ate)
formatted_prompt = prompt_template.format(text=text_content)
print("formatted_prompt := ",formatted_prompt)
response =
cohere_llm.invoke([HumanMessage(content=formatted_prompt)]).content
print("\n **Formatted Output** ")
print(response)
Program 9

Take the Institution name as input. Use Pydantic to define the schema
for the desired output and create a custom output parser. Invoke the
Chain and Fetch Results. Extract the below Institution related details
from Wikipedia: The founder of the Institution. When it was founded.
The current branches in the institution . How many employees are
working in it. A brief 4-line summary of the institution.

!pip install wikipedia-api pydantic


import re
import wikipediaapi
from pydantic import BaseModel, Field
from typing import List, Optional
class InstitutionDetails(BaseModel):
name: str
founder: Optional[str] = None
founded: Optional[str] = None
branches: List[str] = Field(default_factory=list)
number_of_employees: Optional[int] = None
summary: Optional[str] = None
def fetch_institution_details(institution_name: str) -> InstitutionDetails:
user_agent = "InstitutionScraper/1.0 (contact:
myemail@[Link])"
wiki = [Link](user_agent=user_agent, language='en')
page = [Link](institution_name)
if not [Link]():
raise ValueError(f"The page for '{institution_name}' does not
exist.")
full_text = [Link]
def extract_pattern(pattern, text, is_list=False):
match = [Link](pattern, text, [Link])
if match:
content = [Link](1).strip()
if is_list:
return [[Link]() for item in [Link](',')]
return content
return [] if is_list else
founder_pattern = r"(?:founded|established|started)\s+by\s+([^.\n,]+)"
founder_match = [Link](founder_pattern, full_text, [Link])
founder = founder_match.group(1).strip() if founder_match else "Unknown"
year_pattern =
r"(?:founded|established|started|incorporated)(?:\s+in)?(?:\s+the\s+year)?\s+(
\d{4})"
year_match = [Link](year_pattern, full_text, [Link])
founded = year_match.group(1) if year_match else "Unknown"
branches = extract_pattern(r"Branches\s*[:\-]?\s*(.*)", full_text,
is_list=True)
raw_employees = extract_pattern(r"Number of employees\s*[:\-
]?\s*([\d,]+)", full_text)
emp_count = None
if raw_employees:
try:
emp_count = int(raw_employees.replace(',', ''))
except ValueError:
emp_count = None
return InstitutionDetails(
name=[Link],
founder=founder,
founded=founded,
branches=branches,
number_of_employees=emp_count,
summary=[Link][:500] + "..." )
try:
val=input("Enter the Institution name:")
data = fetch_institution_details(val)
print(data.model_dump_json(indent=2))
except Exception as e:
print(f"Error: {e}")
Program 10

Build a chatbot for the Indian Penal Code. We’ll start by


downloading the official Indian Penal Code document, and then
we’ll create a chatbot that can interact with it. Users will be able
to ask questions about the Indian Penal Code and have a
conversation with it.

!pip install pymupdf

import fitz

def extract(file):

text = ""

with [Link](file) as pdf:

for page in pdf:

text += page.get_text()

return text

def search (query,ipc):


query=[Link]()

lines=[Link]("\n")

results=[]

for line in lines:

if query in [Link]():

[Link](line)

results = [line for line in lines if query in [Link]()]


if results:

return results [:15]

else:

return ["No relevant section found."]


def chatbot():

print("Loading IPC document...")

ipc = extract(r"/Users/ananthas/Desktop/[Link]")

while True:

query= input("Ask a question about the IPC: ")

if [Link]() == "exit":

print("Goodbye!")

break

results=search (query, ipc)

print("\n".join(results))

print("-" * 50)

chatbot()

OUTPUT:
VIVA QUESTIONS

1. What is GloVe?

GloVe (Global Vectors for Word Representation) is a pre-trained word embedding


model that converts words into numerical vectors.

2. What is word embedding?

Word embedding is a technique to represent words as numerical vectors so machines


can understand their meanings.

3. Why do we use pre-trained models?

To save time and use already trained knowledge from large datasets.

4. What does most_similar() function do?

It finds words whose vectors are closest to the given word vector.

5. What is vector arithmetic in NLP?

Performing mathematical operations on word vectors to find relationships between


words.

6. What is the dimension of glove-wiki-gigaword-50?

50 dimensions.

7. What is cosine similarity?

It measures similarity between two vectors A and B


Formula:
8. Why is similarity value between 0 and 1?

Because cosine similarity measures closeness between vectors.

9. What is positive and negative in most_similar()?


• positive → words to add
• negative → words to subtract

10. What dataset is used in glove-wiki-gigaword-50?

Wikipedia and Gigaword dataset.

11. Why word embeddings are useful?

They represent words as vectors capturing semantic meaning, allowing computers


to understand similarity.

14. What happens if word is not in vocabulary?

Model gives error or cannot find similarity.

15. Why vector arithmetic works?

Because embeddings capture semantic relationships in vector space.


16. Why reduce dimensions using PCA?
To visualize high-dimensional data in 2D while preserving variance

17. Why choose 2D for visualization?


Easy to plot and interpret clusters of similar words.

18. What is cosine similarity?


Measures angle between vectors;
smaller angle → more semantically similar.

19. Difference between PCA and t-SNE?

PCA is linear, preserves global structure. t-SNE is non-linear, preserves local


neighborhoods and is better for clusters.

20. How are similar words generated in code?

Using most_similar function which calculates cosine similarity between the target
word and all others.

21. Why football and soccer cluster together?

They are synonyms in sports domain, and embeddings capture usage context.

22. What is Word2Vec?

Word2Vec is a neural network-based model that learns word embeddings by


predicting words from context or vice versa.
23. Why use a domain-specific corpus?

To capture domain-specific semantics, e.g., medical terms like "patient" and


"diagnosis" are closer in vector space.

24. Difference between CBOW and Skip-gram?


CBOW predicts a word from its context; Skip-gram predicts context words from
a word. Skip-gram performs better on small datasets.

25. Why use vector_size=50?

It is a standard configuration that balances computational efficiency with the


quality of semantic representation, particularly when working with smaller datasets
or when fast training is required.

26. What does most_similar do?


Finds words closest in semantic space to the given word using cosine similarity.

27. How do embeddings capture meaning?

Words appearing in similar contexts get vectors close to each other; similar vectors
→ semantically related words.

28. What happens if the corpus is very small?

Embeddings may be noisy; words might not have enough context to learn
meaningful relationships.

29. Why do we enrich prompts using word embeddings?

Embeddings help identify semantically similar words, making prompts richer and
responses more detailed.

30. How are similar words retrieved?


Using most_similar from GloVe embeddings, which calculates
cosine similarity between word vectors.
31. Why does GPT-2 respond better to enriched prompts?

More contextually relevant words help the model understand topic scope and
nuances, producing richer responses.

32. What is no_repeat_ngram_size in text generation?


Prevents the model from repeating the same n-gram, improving
fluency and readability.

33. What are the risks of over-enrichment?

Adding too many unrelated similar words may confuse the AI and produce off-topic
or incoherent text.

34. Could this method be applied to other AI models?

Yes, any text generation model (GPT-3, LLaMA, BLOOM, etc.) benefits from
enriched prompts.

35. Difference between embedding-based enrichment and keyword insertion?

Embeddings capture semantic similarity, not just literal synonyms, making


enrichment more context-aware.

36. Why do we use word embeddings for creative writing?

They provide contextually related words, helping generate more meaningful and
coherent content.

37. Why do we import nltk?


We import the Natural Language Toolkit (NLTK) library in Python to gain access
to a comprehensive suite of pre-built tools, algorithms, and linguistic resources for
processing and analyzing human language data. This allows developers and
researchers to perform complex Natural Language Processing (NLP) tasks without
writing the underlying logic from scratch

38. why do we import transformers?


transformer library in Python to easily access, download,
We import the Hugging Face
s
train, and deploy state-of-the-art machine learning models across various domains
(text, vision, audio, and multimodal). It simplifies the use of complex models like
BERT, GPT, and T5 by providing a unified and user-friendly

39. what does pipeline of transformers module do?

The pipeline module in Hugging Face Transformers provides a high-level, easy-to-


use API
for running inference on pretrained models. It abstracts away the complex
steps of pre- processing, model inference, and post-processing into a single
call, supporting various modalities like text, audio, and images

40. What is genism?

Gensim = Generate Similar is a popular open source natural language processing


(NLP) library used for unsupervised topic modeling. It uses top academic models
and modern statistical machine learning to perform various complex tasks such as −
• Building document or word vectors
• Corpora
• Performing topic identification, performing document comparison
(retrieving semantically similar documents)
• Analysing plain-text documents for semantic structure

41. How does the number of top similar words (topn) affect results?

More words → richer paragraph but may be less focused; fewer words → concise but

may miss variety.

[Link] is the role of the pipeline function from transformers?

The pipeline function provides a high-level API to use pre-trained models easily.
In this case, it loads a sentiment analysis model without needing manual setup.

[Link] model is used by default in pipeline("sentiment-analysis")?

By default, it typically uses a pre-trained model like DistilBERT fine-tuned on


sentiment datasets such as SST-2.

44. What does the analyze_sentiment function do?

It takes a text input, passes it to the sentiment analyzer, extracts the label and
confidence score, and returns a formatted string.

45. What is contained in the result variable?


The result is a list of dictionaries. Each dictionary contains:

a. 'label': sentiment (e.g., POSITIVE or NEGATIVE)


b. 'score': confidence value between 0 and 1

46. Why do we use result[0]?


Because the pipeline returns a list (even for a single input), so we access the first
element to get the actual prediction.

47. What does the score represent?


The score represents the model’s confidence in its prediction ranging from 0 to 1.

48. Why is .strip() used on user input?


It removes leading and trailing whitespace, ensuring clean input for processing.

49. Can this program handle multiple sentences at once?


Currently, it processes one input at a time. However, the pipeline can accept a list
of sentences if modified.

50. How does the transformer model (used in the pipeline) understand
sentiment in text?

Transformer models like BERT or DistilBERT use self-attention mechanisms to


understand the context of each word relative to others in a sentence. Instead of
processing words sequentially, they analyze the entire sentence at once, capturing
relationships between words. This helps in identifying sentiment even when it
depends on context (e.g., negations like "not good").

51. What are the limitations of using a pre-trained sentiment analysis pipeline
without fine- tuning?
Pre-trained models may not perform well on domain-specific data (e.g., medical,
legal, or technical text). They may misinterpret slang, sarcasm, or context-specific
meanings. Fine-tuning on a custom dataset improves accuracy for specific use
cases.

52. How would you modify the program to return more detailed sentiment
categories (e.g., neutral, very positive)?
Need to use or fine-tune a model trained on multi-class sentiment datasets (e.g., 3-
class or 5-class classification). Then update the pipeline to use that model. The
output labels would expand beyond just POSITIVE/NEGATIVE, and your
function would handle additional categories accordingly.

53. Why is Meta Platforms's BART model suitable for summarization tasks?

BART (Bidirectional and Auto-Regressive Transformers) combines encoder-


decoder architecture, allowing it to understand context (like BERT) and generate
coherent text (like GPT). This makes it highly effective for abstractive
summarization.

54. What is the difference between extractive and abstractive summarization in


this context?

Extractive summarization selects key sentences directly from the input, whereas
abstractive summarization (used by BART) generates new sentences that may not
appear in the original text but preserve the meaning.
55. Why do we use AutoTokenizer and AutoModelForSeq2SeqLM instead of
specific classes?

These are generic classes that automatically select the correct tokenizer and model
architecture for the specified model name, improving flexibility and reducing code
complexity.

56. What is the significance of max_length=512 in tokenization?

It limits input size to 512 tokens, which is the maximum input length BART can
handle. Longer texts are truncated, which may lead to loss of information.

57. Explain the purpose of num_beams=4 in the generate() function.

Beam search explores multiple possible output sequences. num_beams=4 means


the model keeps track of 4 candidate sequences, improving summary quality at the
cost of computation.

58. What does length_penalty=1.2 do?

It penalizes shorter sequences slightly, encouraging the model to generate longer,


more informative summaries.

59. Why is no_repeat_ngram_size=3 used?

It prevents repetition of any 3-word sequence, improving readability and reducing


redundancy in the generated summary.

60. What is the role of repetition_penalty=1.2?

It discourages the model from repeating the same words or phrases, ensuring more
diverse and natural output.
61. How does early_stopping=True affect generation?

It stops the beam search once all candidate sequences reach an end condition,
reducing unnecessary computation and speeding up inference.

[Link] is the role of Cohere in the 8th program?

Cohere provides the large language model (command-a-03-2025) used for


generating summaries, extracting key takeaways, and performing sentiment
analysis.

63. Why is LangChain used in this code?

LangChain helps structure prompts, manage LLM interactions, and simplify


chaining tasks like summarization and sentiment analysis in a modular way.

64. What is the purpose of PromptTemplate?

PromptTemplate standardizes and dynamically inserts input text into a predefined


prompt structure, ensuring consistent instructions for the LLM.

65. How does ChatCohere differ from a standard API call?

ChatCohere is an abstraction that allows conversational interaction with Cohere


models, handling message formatting and response parsing internally.

66. Why is [Link]() used for the API key?

It securely accepts the API key without displaying it on the screen, preventing
accidental exposure in logs or notebooks.
67. What is the function of Google Colab authentication and drive mounting?

Authentication allows access to Google services, while mounting Google Drive


enables reading external files like [Link].

68. Explain the structure of the prompt used in this program.

The prompt includes:

a. Context (document content)


b. Instructions (summary, key takeaways, sentiment)
This structured format guides the LLM to produce organized output.

69. Why is HumanMessage used in invoke()?

It represents user input in a chat-based format, making the interaction compatible


with chat-oriented LLMs.

70. What is the role of Pydantic in this program?

Answer:
Pydantic is used to define a structured schema (InstitutionDetails) for the output. It
ensures type validation, provides default values, and converts the extracted data
into a clean JSON format.

71. Why is a schema (InstitutionDetails) created using BaseModel?

The schema enforces a fixed structure for the extracted data, making it easier to
validate, serialize, and maintain consistency instead of using unstructured
dictionaries.
72. What is the purpose of the wikipedia-api library?
wikipedia-api allows the program to fetch Wikipedia page content
programmatically, including full text and summary of an institution.

73. Why is a User-Agent required in the Wikipedia API?

Answer:
A User-Agent identifies the client making the request. Wikipedia requires it to
prevent misuse and to track responsible usage of their API.

74. How does the program extract the founder of an institution?

Answer:
It uses a regular expression pattern:
(?:founded|established|started)\s+by\s+([^.\n,]+)
This searches for phrases like “founded by” and extracts the name that follows.

75. What are the limitations of using regex for data extraction?

Answer:
Regex may fail if the text format changes or is complex. It cannot fully understand
context, leading to incorrect or incomplete extraction (as seen in the founder output).

76. Why are some fields marked as Optional in the schema?

Answer:
Fields like founder and number_of_employees are marked as Optional because this
information may not always be available on Wikipedia pages.
77. How does the program handle missing or invalid employee data?

Answer:
If employee data is not found or cannot be converted to an integer, the program
assigns None to number_of_employees using exception handling.

78. What is the purpose of using Field(default_factory=list) for the branches


attribute?

Answer:
Field(default_factory=list) ensures that each instance of the schema gets its own
empty list by default. This avoids issues with mutable default arguments and ensures
safe handling of branch data.

79. Why is [Link][:500] + "..." used in the program?

Answer:
This limits the summary length to 500 characters to keep the output concise and
readable, while still providing a brief overview of the institution. The "..." indicates
that the summary has been truncated.
80. What is the role of PyMuPDF in this(10th) program?

PyMuPDF (imported as fitz) is used to open and read the PDF file. It extracts text
content from each page, enabling further processing and searching.

81. How does the extract() function work internally?

It opens the PDF using [Link](), iterates through each page, extracts text using
page.get_text(), and concatenates it into a single string.

82. What are the limitations of page.get_text() for legal documents like IPC?

It may not preserve formatting, tables, or section hierarchy. Complex layouts


(columns, footnotes) can lead to disordered or incomplete text extraction.

You might also like