Artificial Intelligence

Chapter 10
Natural Language Processing and Generative AI

Post Graduate Program in IT Engineering
Artificial Intelligence   •   Post Graduate

Natural Language Processing and Generative AI

Introduction and Context

Natural Language Processing (NLP) is the study and engineering of computational systems that represent, analyse, retrieve, generate and interact with human language. It combines artificial intelligence, linguistics, statistics, machine learning, information retrieval, speech technology and human–computer interaction. Generative AI extends this field by producing new text, speech, images, code and other media. In this chapter, the focus is language-centred generative AI, especially foundation models and large language models (LLMs).

Language is difficult for machines because it is contextual, ambiguous, variable across communities and continuously evolving. The word “bank,” for example, may denote a financial institution or a river bank. Meaning can depend on discourse, world knowledge, social relationships and the speaker’s intention. Modern NLP systems address these difficulties using distributed representations, neural networks, attention mechanisms, large-scale pre-training and task-specific adaptation.

Contemporary systems support search, translation, summarisation, document classification, information extraction, question answering, coding assistance, speech interfaces and autonomous workflows. Their capabilities, however, must be assessed critically. A fluent output is not necessarily true, grounded, fair, secure or appropriate for a particular decision.

Learning Objectives

After completing this chapter, a student should be able to:

1. Explain major NLP representations, architectures and tasks.

2. Distinguish information extraction, information retrieval, speech processing and conversational AI.

3. Describe foundation models, LLM training, prompting, fine-tuning and retrieval-augmented generation.

4. Design an evaluation plan using task, safety, robustness and operational metrics.

5. Analyse hallucination, bias, privacy, security, copyright and governance risks.

6. Propose an appropriate architecture for a language-based enterprise application.

1. Language Representation and NLP Tasks

1.1 From symbols to distributed representations

Early NLP systems represented language using hand-written grammars, dictionaries and symbolic rules. These methods remain useful where rules are explicit and explainability is essential, but they are expensive to maintain and weak at handling variation. Statistical systems estimate patterns from corpora. Neural systems learn vector representations in which words, subwords, sentences or documents are encoded as points or directions in a high-dimensional space.

A one-hot representation assigns a separate coordinate to every vocabulary item. It is sparse and does not encode similarity. An embedding maps an item to a dense vector. Similar meanings may be represented by nearby vectors, although proximity is learned from usage patterns rather than guaranteed semantic truth. Contextual encoders improve on static word embeddings by producing different representations for a word in different contexts.

Tokenisation converts text into manageable units. A tokenizer may use words, characters or subword units such as byte-pair encoding or unigram-based segmentation. Subwords reduce the problem of unknown words and allow a model to represent names, inflections and technical terms. Tokenisation affects cost, sequence length and multilingual performance; it is therefore an engineering design decision rather than an invisible preprocessing step.

1.2 Sequence models and attention

Recurrent neural networks process a sequence incrementally and maintain a hidden state. Long short-term memory and gated recurrent units reduce, but do not eliminate, difficulties in learning long-range dependencies. Convolutional models can capture local patterns efficiently. Transformer models use attention to relate positions in a sequence directly.

For a query matrix Q, key matrix K and value matrix V, scaled dot-product attention is:

Attention(Q,K,V) = softmax(QKᵀ / √d_k)V.

Here, d_k is the dimensionality of each key. The scaling reduces excessively large dot products, which otherwise can make the softmax gradients very small. The equation describes one attention operation; practical transformers use multiple heads, learned projections, positional information and feed-forward layers. Self-attention allows each token to assign data-dependent weights to other tokens. Encoder-only models are commonly suited to representation and classification; decoder-only models generate tokens autoregressively; encoder–decoder models are useful for conditional generation such as translation.

An autoregressive language model estimates a sequence by the chain rule:

P(x₁,…,x_T) = ∏ from t=1 to T P(x_t | x₁,…,x_{t−1}).

The model predicts the next token given preceding tokens. During training, the usual objective is to minimise the negative log-likelihood (cross-entropy) over a corpus. This objective measures predictive fit, not truth, reasoning, fairness or usefulness. A lower loss can correlate with capability while still failing to guarantee reliable deployment.

1.3 Core NLP tasks

Natural language understanding tasks include sentiment analysis, topic classification, intent detection, natural language inference, semantic similarity and named-entity recognition. Generation tasks include translation, summarisation, dialogue response, report drafting and code generation. Structured prediction tasks assign labels to sequences, as in part-of-speech tagging or entity spans.

NLP pipelines often combine tasks. A customer-support system may detect language, identify intent, extract an account number, retrieve policy passages, formulate an answer and route uncertain cases to a human. Pipeline modularity can improve observability and control, whereas a single end-to-end model may be simpler but harder to diagnose.

2. Information Extraction and Information Retrieval

2.1 Information extraction

Information extraction (IE) converts unstructured language into structured information. Named-entity recognition identifies entities such as people, organisations, products, locations and dates. Relation extraction identifies links, for example, “Company A acquired Company B.” Event extraction records an event type, participants, time and location. Coreference resolution links expressions such as “the company” to a previously mentioned organisation.

A practical IE architecture may include document ingestion, layout and optical-character recognition, sentence segmentation, entity detection, relation or event extraction, validation and storage in a relational database or knowledge graph. Modern systems may combine supervised models, prompts, dictionaries, rules and human review. Evaluation should distinguish exact-match and partial-match performance. Precision is TP/(TP+FP), recall is TP/(TP+FN), and F1 is the harmonic mean of precision and recall: 2PR/(P+R), provided P+R is non-zero. These measures assume clearly defined positive instances and a representative labelled test set.

2.2 Information retrieval

Information retrieval (IR) finds documents or passages relevant to a query. Lexical methods such as inverted indexes and BM25 exploit term statistics and are strong for exact names, identifiers and rare terms. Dense retrieval represents queries and documents as vectors and ranks them using a similarity measure, often cosine similarity:

cos(u,v) = (u·v)/(||u|| ||v||),

where u and v are non-zero vectors. Cosine similarity compares orientation and is unaffected by common scaling of either vector. Hybrid retrieval combines lexical and dense evidence, often improving robustness.

A retrieval system has at least two stages: candidate generation and ranking. Evaluation commonly uses recall@k, precision@k, mean reciprocal rank and normalised discounted cumulative gain. The choice of metric should reflect the use case. A legal research system may prioritise high recall, while a question-answering interface may require highly precise top-ranked passages.

Retrieval quality depends on chunk size, overlap, metadata, document freshness, access control and query formulation. Very small chunks lose context; very large chunks dilute relevance and consume context capacity. Chunking should respect headings, tables, clauses and semantic boundaries rather than relying only on character counts.

3. Speech and Conversational AI

Speech recognition, or automatic speech recognition (ASR), maps an acoustic signal to text. A typical pipeline includes audio capture, noise handling, acoustic or speech representation, decoding and language modelling. Important measures include word error rate:

WER = (S + D + I)/N,

where S, D and I are substitutions, deletions and insertions relative to N reference words. WER can exceed 100 percent in unusual cases and should be reported by accent, environment, speaker group and task context; an aggregate score can hide serious disparities.

Text-to-speech (TTS) converts text into speech. Quality involves intelligibility, naturalness, pronunciation, prosody, latency and speaker identity. Voice systems require explicit consent and safeguards against impersonation. Speaker diarisation determines who spoke when, which is useful in meetings but imperfect when voices overlap.

Conversational AI includes chatbots, voice assistants and task-oriented dialogue systems. A robust architecture separates dialogue management from language generation. It maintains state, identifies user goals, invokes approved tools, confirms consequential actions and provides escalation paths. Open-ended conversation and task-oriented dialogue have different evaluation criteria: engaging prose is not sufficient for a system that must accurately change a reservation or process a payment.

4. Foundation Models and Large Language Models

A foundation model is trained on broad data at scale and adapted to many downstream tasks. An LLM is a language foundation model with a large parameter count and substantial pre-training data. Scale can produce emergent or improved capabilities, but capability is not uniform: models may be strong at common patterns and weak at rare facts, calibrated uncertainty, long-horizon planning or domain-specific procedures.

4.1 Training stages

Pre-training typically uses self-supervised prediction over large text collections. Data engineering is central: sources must be collected lawfully, filtered for duplication and harmful content, documented and balanced where possible. The model then may undergo supervised fine-tuning on instruction–response examples. Preference optimisation, including human or model-based preference signals, attempts to make responses more helpful, harmless and aligned with desired behaviour. These stages do not remove the possibility of factual error or undesired bias.

Inference generates tokens according to a probability distribution. Greedy decoding selects the highest-probability token at each step. Temperature modifies logits before sampling; lower temperature generally makes output more concentrated, while higher temperature increases diversity. Top-k and nucleus (top-p) sampling restrict the candidate distribution. These controls alter style and variability, not the model’s underlying knowledge or reliability.

4.2 Prompting

A prompt specifies a task, context, constraints and output format. Zero-shot prompting provides instructions without examples. Few-shot prompting includes examples that demonstrate the desired mapping. Role descriptions can establish perspective, but they do not create permissions or factual knowledge. Structured prompts should state the objective, input delimiters, evidence requirements, refusal conditions and schema.

Good prompt engineering is an empirical design activity. Prompts should be tested on representative, adversarial and out-of-distribution inputs. Exact wording is less important than clear task definition, relevant context, unambiguous constraints and appropriate output validation. Asking a model to “think step by step” may sometimes improve performance, but any hidden or exposed reasoning should not be treated as proof; the answer must still be checked against evidence and task criteria.

4.3 Fine-tuning and parameter-efficient adaptation

Fine-tuning updates model parameters using domain or task examples. It can improve terminology, style and consistent behaviour but may cause overfitting, catastrophic forgetting, data leakage or memorisation of sensitive examples. Parameter-efficient methods such as adapters and low-rank updates reduce storage and training cost by changing a smaller set of parameters. Fine-tuning is appropriate when the desired behaviour is stable and exemplars are available. It is not usually the best method for frequently changing factual knowledge; retrieval is better suited to that requirement.

5. Retrieval-Augmented Generation

Retrieval-augmented generation (RAG) combines search with generation. During indexing, documents are cleaned, segmented, embedded and stored with metadata and permissions. At query time, the system retrieves candidate passages, optionally reranks them, constructs a grounded prompt and asks a generator to answer using the supplied evidence. Citations, evidence spans and abstention rules can make the result more auditable.

A conceptual RAG pipeline is:

user query → query interpretation → authorised retrieval → reranking → context assembly → generation → citation and validation → response or escalation.

RAG can reduce unsupported claims, expose current organisational knowledge and avoid retraining for every document update. It does not guarantee correctness. Retrieval may return irrelevant, outdated or malicious text; the generator may misread evidence, combine incompatible passages or cite a source that does not support its claim. Prompt injection can occur when retrieved content contains instructions intended to manipulate the model.

Best practices include access-controlled retrieval, source timestamps, provenance, separate treatment of instructions and evidence, context limits, contradiction checks, answerability detection and human review for high-impact decisions. RAG evaluation should measure retrieval recall and answer faithfulness separately. A correct answer with poor retrieval may not generalise; a good retrieved passage with an incorrect answer identifies a generation or orchestration problem.

6. Agents and Tool-Using Systems

An AI agent is a system that observes a task, plans or selects actions, invokes tools and uses results to continue toward a goal. Tools may include search, databases, calculators, code execution, ticketing systems and APIs. An agent architecture should define available actions, input and output schemas, permissions, budgets, timeouts, memory policy and termination conditions.

Agents are useful where tasks require multiple controlled steps. They are risky when granted broad permissions, unrestricted browsing or the ability to execute irreversible actions. The principle of least privilege should apply: an agent receives only the access needed for its role. High-impact actions should require confirmation, dual control or human approval. Tool outputs must be validated because an external API can fail, return malformed data or be manipulated.

Agent evaluation should include task success, tool-call accuracy, cost, latency, recovery from failure, unsafe-action rate and reproducibility. A demonstration that succeeds once is not evidence of dependable autonomy.

7. Evaluation, Hallucination and Reliability

Hallucination refers to generated content that is unsupported, fabricated or inconsistent with available evidence. It can arise because language modelling rewards plausible continuation rather than truth, because prompts omit necessary context, because retrieval fails or because the model overgeneralises. Hallucination is not solved by a single confidence score: token probabilities are not calibrated probabilities that a claim is true.

A mature evaluation framework has several layers:

  • Capability: accuracy, F1, exact match, translation quality or task success.
  • Grounding: entailment by cited evidence, citation precision and citation completeness.
  • Calibration: whether stated confidence corresponds to observed correctness.
  • Robustness: paraphrases, noisy input, long contexts, adversarial prompts and distribution shift.
  • Safety: toxicity, privacy leakage, discrimination, unsafe advice and prompt-injection resistance.
  • Operations: latency, throughput, cost, availability and energy use.
  • Human factors: usefulness, workload, trust calibration and accessibility.

Test sets should be separated from development data, documented with provenance and reviewed for leakage. Human evaluation requires clear rubrics and inter-rater procedures. Automated judges can scale assessment but may share the same biases or weaknesses as the evaluated model. Statistical significance and confidence intervals are useful when comparing systems, especially on small or heterogeneous samples.

Controls include retrieval and citation, constrained decoding, schema validation, fact checking, calculators for numerical tasks, abstention, uncertainty communication, output filters, red-teaming and human escalation. Controls should be layered because each has failure modes. For example, a citation may be present but irrelevant, and a toxicity filter may miss subtle harassment.

8. Responsible, Secure and Governed NLP

Language systems process personal, confidential and sometimes legally protected information. Organisations should establish a lawful purpose, data minimisation, retention limits, access controls, encryption, audit logging and deletion procedures. Sensitive data should not be sent to an external model without an appropriate contractual and technical assessment.

Bias can enter through data, labels, sampling, model objectives, prompts and deployment context. Fairness assessment should examine relevant groups and error types, while recognising that fairness criteria can conflict. A model that performs well on average may fail for minority languages, dialects, disabilities or domain-specific terminology. Impact assessment and participatory review are preferable to relying solely on aggregate benchmarks.

Security threats include prompt injection, indirect injection through retrieved documents, jailbreaks, data exfiltration, model extraction, poisoning, insecure tool use and excessive agency. Defences include isolation of untrusted content, allow-listed tools, secret segregation, input and output monitoring, sandboxing, rate limits and incident response. Copyright and provenance questions arise in both training and output generation; organisational policy should address licensing, attribution, confidential material and responsibility for published content.

In regulated or high-impact settings, governance should assign accountability, document intended use and limitations, maintain model and data versions, monitor post-deployment performance and provide appeal or human review. Compliance is not identical to ethics: a legally permissible system may still be misleading, unfair or socially harmful.

9. Applied Case: Illustrative Enterprise Policy Assistant

Background

A fictional financial-services organisation receives thousands of employee questions about travel, procurement, security and leave policies. Its existing search portal contains current documents but requires users to know exact terminology.

Problem

A conversational assistant is proposed. Incorrect advice could cause financial loss, policy violations or unfair treatment. Policies are updated frequently, access rights differ by department and some documents contain confidential information.

Analysis

Fine-tuning alone would quickly become outdated and could memorise confidential examples. A general chatbot without retrieval might produce fluent but unsupported answers. The primary requirements are current evidence, access control, citations, concise explanations and escalation when policies conflict or the question is ambiguous.

Approach

The organisation implements an access-controlled hybrid RAG system. Documents are versioned, classified and segmented by policy clause. Lexical retrieval handles policy identifiers, while embeddings support paraphrased questions. A reranker selects passages within the user’s permissions. The generation prompt requires an answer based only on retrieved evidence, cites document title and effective date, identifies uncertainty and asks a clarification question when necessary. The system cannot approve expenses or change records; it can prepare a link or draft request for a human workflow.

Evaluation uses a held-out set of real and synthetic questions, including outdated-policy queries, conflicting documents, prompt injection in a document and unauthorised access attempts. Measures include retrieval recall, citation support, answer accuracy, abstention quality, latency and group-specific performance. Human policy experts review high-risk categories.

Outcome

In this illustrative case, the system improves discovery and reduces routine support workload, but it does not eliminate human policy ownership. Monitoring reveals that short queries and tables remain difficult, leading to improved metadata, table-aware parsing and a mandatory escalation route.

Learning Points

The case demonstrates that successful generative AI is an engineered socio-technical system, not merely a large model. Current knowledge favours retrieval, permissions and provenance; consequential actions require controls; and evaluation must test failure conditions rather than only fluent examples.

10. Contemporary Directions and Best Practices

Current research explores multimodal language models, long-context processing, smaller specialised models, multilingual and low-resource NLP, synthetic data, model routing, on-device inference and efficient adaptation. Long context does not guarantee that a model will attend correctly to every passage. Smaller models may offer lower cost, better privacy and easier deployment, sometimes outperforming larger models on narrow tasks.

A practical lifecycle is:

1. Define the decision or user task, risk level and success criteria.

2. Establish data rights, quality, provenance and access policy.

3. Select the simplest adequate architecture: rules, retrieval, classifier, LLM or agent.

4. Build a representative evaluation set before optimisation.

5. Add grounding, validation, monitoring and human escalation.

6. Pilot with restricted permissions and record incidents.

7. Monitor drift, cost, bias, security and user behaviour after deployment.

8. Review the system periodically and retire it when controls no longer suffice.

The central design principle is to match autonomy to risk. A model may draft a low-risk summary automatically, while a medical, legal, financial or employment decision should receive stronger evidence requirements and qualified human oversight.

Chapter Summary

NLP represents and processes language at levels ranging from tokens and embeddings to documents, dialogues and speech. Transformers and attention support powerful contextual representations, while autoregressive objectives enable fluent generation. Information extraction converts text into entities, relations and events; information retrieval locates relevant evidence; speech and conversational systems connect language models with audio, dialogue state and tools.

Foundation models and LLMs are broadly pretrained and adapted through prompting, supervised fine-tuning or parameter-efficient methods. RAG is effective for changing, organisation-specific knowledge but requires high-quality retrieval, permissions, provenance and validation. Agents extend models with tools and actions, demanding least privilege, confirmation and careful evaluation.

Fluency is not reliability. Hallucination, bias, privacy leakage, prompt injection, copyright uncertainty and unequal performance require layered technical controls and governance. A complete evaluation considers capability, grounding, calibration, robustness, safety, operations and human impact. Responsible deployment treats generative AI as a socio-technical system whose data, interfaces, workflows and accountability are as important as its model.

Key Terms

Tokenisation: Conversion of text into computational units such as words, characters or subwords.

Embedding: A learned dense vector representation of an item.

Contextual representation: A representation whose value depends on surrounding text.

Attention: A mechanism that weights information from other sequence positions.

Transformer: A neural architecture based primarily on attention and feed-forward layers.

Language model: A model that assigns probabilities to language sequences or tokens.

Information extraction: Conversion of unstructured text into structured entities, relations or events.

Named-entity recognition: Identification and classification of entity mentions in text.

Information retrieval: Finding and ranking documents or passages relevant to a query.

Dense retrieval: Retrieval using vector representations and similarity calculations.

Foundation model: A broadly trained model adaptable to many tasks.

Large language model: A large neural language foundation model trained on extensive text data.

Prompting: Specifying a task, context and constraints through an input instruction.

Fine-tuning: Updating model parameters using task- or domain-specific examples.

Retrieval-augmented generation: Generation conditioned on passages retrieved at query time.

Agent: A system that selects actions, invokes tools and pursues a task goal.

Hallucination: Unsupported, fabricated or inconsistent generated content.

Calibration: Agreement between stated confidence and observed correctness.

Prompt injection: Malicious or unintended instructions that manipulate a model through input or retrieved content.

Grounding: Linking an answer to reliable, relevant supporting evidence.

Review Questions

Short Answer Questions

1. Why are subword tokenisers useful in multilingual and technical NLP?

2. Distinguish information extraction from information retrieval.

3. What is the purpose of scaling by √d_k in scaled dot-product attention?

4. Why is fine-tuning generally unsuitable for frequently changing factual knowledge?

5. Define hallucination and name two controls that can reduce its impact.

Long Answer Questions

1. Explain the architecture and operating principles of a transformer-based language model.

2. Compare lexical, dense and hybrid retrieval, including appropriate evaluation metrics.

3. Describe the stages of training and adaptation for a foundation model and discuss their limitations.

4. Design a secure RAG architecture for an organisation with confidential, frequently changing documents.

5. Discuss the technical, ethical and governance requirements for deploying a conversational AI system in a high-impact domain.

Analytical / Application Questions

1. A policy assistant retrieves three passages, but one is obsolete and another belongs to a department the user cannot access. Design controls for indexing, retrieval, prompting and response validation.

2. An ASR system reports a WER of 8% overall but 18% for one accent group. Analyse why the aggregate score is insufficient and propose an evaluation and remediation plan.

3. An autonomous procurement agent correctly drafts purchase orders but occasionally submits them to the wrong supplier. Recommend changes to permissions, tool validation, confirmation and monitoring before production deployment.

Multiple Choice Questions with Answers

Question 1

Which NLP task converts a statement such as “Company A acquired Company B” into a structured link between entities?

A. Relation extraction

B. Tokenisation

C. Speech synthesis

D. Language identification

Correct Answer: A. Relation extraction

Question 2

In scaled dot-product attention, why is QKᵀ divided by √d_k?

A. To remove positional information

B. To reduce excessively large dot products before softmax

C. To make all tokens identical

D. To convert vectors into one-hot representations

Correct Answer: B. To reduce excessively large dot products before softmax

Question 3

Which approach is generally most suitable for frequently changing organisational facts?

A. Permanent prompt wording

B. Static word embeddings

C. Retrieval-augmented generation

D. Greedy decoding alone

Correct Answer: C. Retrieval-augmented generation

Question 4

If TP = 80 and FP = 20, what is precision?

A. 0.20

B. 0.50

C. 0.80

D. 0. reviewed

Correct Answer: C. 0.80

Question 5

Which metric is specifically associated with automatic speech-recognition errors?

A. WER

B. MRR

C. BLEU only

D. Cosine distance only

Correct Answer: A. WER

Question 6

What is the principal purpose of a reranker in a retrieval pipeline?

A. To convert speech to text

B. To reorder retrieved candidates using a more detailed relevance model

C. To fine-tune every model parameter

D. To encrypt the document database

Correct Answer: B. To reorder retrieved candidates using a more detailed relevance model

Question 7

Which statement about an LLM’s token probability is most accurate?

A. It is automatically a calibrated probability that the claim is true

B. It guarantees citation correctness

C. It reflects the model distribution over possible continuations, not necessarily factual truth

D. It measures fairness across demographic groups

Correct Answer: C. It reflects the model distribution over possible continuations, not necessarily factual truth

Question 8

Which control best reflects the principle of least privilege for an AI agent?

A. Giving the agent unrestricted administrator access

B. Allowing only the tools and permissions required for its task

C. Removing all audit logs

D. Allowing irreversible actions without confirmation

Correct Answer: B. Allowing only the tools and permissions required for its task

Question 9

What does WER count in its standard formula?

A. Substitutions, deletions and insertions relative to reference words

B. Only punctuation errors

C. Only incorrectly detected speakers

D. Retrieval results divided by query length

Correct Answer: A. Substitutions, deletions and insertions relative to reference words

Question 10

Which statement best describes hallucination in generative AI?

A. Any response longer than one paragraph

B. A response generated with low temperature

C. Unsupported, fabricated or evidence-inconsistent content

D. A correctly cited answer from a retrieved passage

Correct Answer: C. Unsupported, fabricated or evidence-inconsistent content

Artificial Intelligence
Chapter 10 of 15