Lesson 18 LangChain & LlamaIndex
Theme:

Homework

Complete these tasks before Lesson 19. Work in a notebook or script. The goal is to deepen your understanding of when and how to use orchestration frameworks effectively.

Task 1: Add Conversation Memory to LangChain

Goal: Extend your LangChain chain from the lab to support multi-turn conversations with memory.

Instructions

Build a conversational chain that remembers previous exchanges:

  1. Create a ChatPromptTemplate that includes a placeholder for conversation history:
    from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
    
    prompt = ChatPromptTemplate.from_messages([
        ("system", "You are a helpful technical assistant. Be concise."),
        MessagesPlaceholder(variable_name="history"),
        ("human", "{input}")
    ])
  2. Implement a conversation loop that maintains a list of messages and passes them as the history variable on each turn.
  3. Have at least a 5-turn conversation where later questions depend on context from earlier turns. For example:
    • Turn 1: "What is a vector database?"
    • Turn 2: "Give me three examples of popular ones."
    • Turn 3: "Which of those would you recommend for a small project?"
    • Turn 4: "How would I set it up with Python?"
    • Turn 5: "Can you summarise what we discussed?"
  4. Verify that the model correctly references information from earlier turns in its later responses.

Self-check

Task 2: Swap LLM Providers

Goal: Experience the provider-agnostic benefit of frameworks by switching from Claude to another provider.

Instructions

Take your working LangChain chain (from the lab or Task 1) and your LlamaIndex RAG pipeline:

  1. Swap the LLM from Claude to OpenAI (or vice versa, or to a local model via Ollama if you have one running):
    # LangChain: change one import and one line
    from langchain_openai import ChatOpenAI
    model = ChatOpenAI(model="gpt-4o", temperature=0)
    
    # LlamaIndex: change one import and one line
    from llama_index.llms.openai import OpenAI
    Settings.llm = OpenAI(model="gpt-4o")
  2. Run the exact same queries on both providers.
  3. Compare the outputs:
    • Are the answers substantively different?
    • Is one more verbose or more concise?
    • For the RAG pipeline: does one cite sources more accurately?
  4. Record your observations in a short comparison (~150 words).

Self-check

Task 3: Framework Decision Memo

Goal: Practice the real-world skill of choosing the right tool for a project.

Instructions

Choose one of the following project scenarios (or invent your own):

Write a decision memo (300–500 words) that recommends one of these approaches:

  1. Direct API calls only
  2. LangChain
  3. LlamaIndex
  4. A combination

Your memo should address:

Self-check

Submission

There is no formal submission for this course. These tasks are for your own development. However:

Next lesson: Lesson 19 — Comparing LLM Providers