liveBook · Manning

LangChain

As explained in Just Use Postgres!

LangChain is a software framework for building applications that compose language models with prompts, tools, data sources, and processing steps.

LangChain is a framework for connecting language models with prompts and application context. A Python function can use its OllamaLLM interface to send a structured prompt and retrieved Postgres data to TinyLlama. section 8.6.2

Listing 8.15 Answering user questions using the LLM and provided context
def answer_question(question, context):
    # Connecting to the tinyllama model with the LangChain Ollama interface
    llm = OllamaLLM(model="tinyllama", temperature=0.6)

    # Generating a final prompt for the LLM based on considering
    # the provided context
    prompt = f"""
    You're a movie expert and your task is to answer questions about movies
    based on the provided context.

    This is the user's question: {question}
    Consider the following context to provide a detailed and
    accurate answer: {context}

    The context includes the following details for each movie:
    - "Title" of the movie
    - "Vote Average" - the average rating of the movie
    - "Budget" - the budget allocated for the movie in US dollars
    - "Revenue" - the total revenue generated by the movie in US dollars
    - "Release Date" - the date the movie was released

    Respond in an engaging style that inspires the user to watch the movies.
    """

    # Invoke the LLM passing the prompt. The LLM will generate a response.
    response = llm.invoke(prompt)
    return response

Use the runnable pattern to define answer_question(question, context), configure TinyLlama, insert question and context into a prompt, and call llm.invoke(prompt). section 8.6.2

Retrieval-augmented movie answers

A RAG workflow retrieves movie records from Postgres for the question “I'd like to watch the best movies about pirates,” then passes that context to LangChain and prints the generated answer. Retrieved results include Pirates of the Caribbean films with ratings, budgets, revenue, and release dates. section 8.6.4

How Just Use Postgres! covers it

How deep it goes
Demonstrates prompt construction, model invocation, RAG flow, and stateless-history handling without deeper framework internals.
Examples
Uses TinyLlama, an OllamaLLM interface, a movie prompt, and retrieved Pirates of the Caribbean records.
What it assumes
Assumes Python, Postgres retrieval, prompts, and language-model basics while demonstrating context insertion and stateless history handling.
What you can do after
Apply it via Python to build a RAG question-answering function backed by Postgres and an Ollama-served language model.
How it connects
Ties LangChain to LLMs, prompts, RAG, Postgres, pgvector, Ollama, and conversation history.

Also explained in

Compare how 19 books explain LangChain →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Just Use Postgres!. The section links point to the parts of the book it draws on.