Project: Knowledge Base
The shop's documents.
The first file is the knowledge base. It turns the shop's policy files into a searchable store, saves it, so it's built only once, and offers a search tool the agent can call.

- File one: kb.py
- Build once, save
- A search tool
kb.py
If the saved store doesn't exist, we build it with our helper, and dump it to a file. Then we load it. Search policies is the tool the agent will call. It returns the text of the two closest chunks. The block at the bottom runs only when we run this file directly, as a quick test. Run it. A question about refunds returns the returns policy text.
from pathlib import Path
from langchain.tools import tool
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from shop_kb import build_store
PATH = "store.json"
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
if not Path(PATH).exists():
build_store().dump(PATH)
store = InMemoryVectorStore.load(PATH, embeddings)
@tool
def search_policies(query: str) -> str:
"""Search shop policies: shipping, returns, gift cards, loyalty, opening hours."""
return "\n\n".join(d.page_content for d in store.similarity_search(query, k=2))
if __name__ == "__main__":
print(search_policies.invoke("How do I get a refund?"))Refunds go back to the original payment method within 5 working days of us receiving the book. To start a return, reply to your order confirmation email. # Returns and refunds You can return any book within 30 days of delivery for a full refund. Books must be unread and in the condition you received them. Damaged or faulty books can be returned at any time, and we pay the return postage.
Also uses kb/shipping.md
# Shipping
Standard delivery in the UK takes 3 to 5 working days and costs £2.99.
Delivery is free on orders over £25.
Express delivery arrives the next working day if you order before 2pm, and costs £5.99.
We ship to Ireland and France in 5 to 8 working days for £7.50.
Every order gets a tracking link by email once it leaves our warehouse.Also uses kb/returns.md
# Returns and refunds
You can return any book within 30 days of delivery for a full refund.
Books must be unread and in the condition you received them.
Damaged or faulty books can be returned at any time, and we pay the return postage.
Refunds go back to the original payment method within 5 working days of us receiving the book.
To start a return, reply to your order confirmation email.Also uses kb/gift_cards.md
# Gift cards
Riverside Books gift cards come in values from £10 to £100.
Gift cards never expire and can be used online or in our shop.
A gift card can be used across several orders until the balance is spent.
Gift cards cannot be exchanged for cash.Also uses kb/loyalty.md
# Riverside Readers loyalty club
Joining the Riverside Readers club is free.
Members earn one point for every pound they spend.
100 points can be swapped for a £5 voucher.
Members get early access to signed editions and a birthday discount of 10 percent.Also uses kb/shop.md
# About the shop
Riverside Books has one physical shop, at 12 Mill Lane, Bath.
The shop is open from 9am to 6pm Monday to Saturday, and 11am to 4pm on Sunday.
We sell new and second-hand books, and run a children's story time every Saturday at 10am.
You can contact us at hello@riverside-books.example or on 01225 000 000.Also uses shop_kb.py
from pathlib import Path
from langchain_core.documents import Document
from langchain_core.vectorstores import InMemoryVectorStore
from langchain_google_genai import GoogleGenerativeAIEmbeddings
from langchain_text_splitters import RecursiveCharacterTextSplitter
def load_docs(folder="kb"):
return [Document(p.read_text(), metadata={"source": p.name})
for p in sorted(Path(folder).glob("*.md"))]
def build_store(folder="kb"):
splitter = RecursiveCharacterTextSplitter(chunk_size=300, chunk_overlap=40)
chunks = splitter.split_documents(load_docs(folder))
embeddings = GoogleGenerativeAIEmbeddings(model="gemini-embedding-001")
return InMemoryVectorStore.from_documents(chunks, embeddings)Also uses shop_tools.py
from langchain.tools import tool
BOOKS = {
"The Hobbit": {"stock": 7, "price": 8.99, "author": "J.R.R. Tolkien"},
"Dune": {"stock": 3, "price": 9.99, "author": "Frank Herbert"},
"Circe": {"stock": 0, "price": 10.99, "author": "Madeline Miller"},
}
@tool
def check_stock(title: str) -> int:
"""Copies of a book in stock at Riverside Books."""
return BOOKS.get(title, {}).get("stock", 0)
@tool
def get_price(title: str) -> float:
"""Price of a book in pounds."""
return BOOKS.get(title, {}).get("price", 0.0)
@tool
def get_author(title: str) -> str:
"""Author of a book."""
return BOOKS.get(title, {}).get("author", "unknown")Design choices
We return plain text, because that's what the model reads. Two chunks keep the context small. The tool's docstring lists the topics, so the model knows when to use it. And saving the store makes the bot start quickly.
| key | value |
|---|---|
| Returns | plain text |
| k=2 | small context |
| Docstring | lists topics |
| Saved store | fast start |
Summary
The knowledge base builds and saves the store, and exposes a search policies tool. Next, the tools for stock and orders.
Key takeaways- Build once, load fast
- @tool search_policies
- Test block