agent.pybusinessLangchainv1.0.0

LangChain: Customer Support Agent

Support chatbot with RAG over your help docs. Answers questions with source citations.

Setup time: ~15 min
Model: GPT-4o
Cost: ~$0.20/day
Last updated: Mar 16, 2026
byRunbooks Communitycontributor

Template

agent.py
# Install: pip install langchain langchain-openai langchain-community chromadb

from langchain_community.document_loaders import DirectoryLoader, TextLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from langchain_community.vectorstores import Chroma
from langchain.chains import ConversationalRetrievalChain
from langchain.memory import ConversationBufferWindowMemory
from langchain.prompts import PromptTemplate
import os

# --- Config ---
DOCS_DIR = "./help-docs"
CHROMA_DIR = "./chroma_support"
OPENAI_API_KEY = os.environ["OPENAI_API_KEY"]

# --- Load help docs ---
loader = DirectoryLoader(DOCS_DIR, glob="**/*.md", loader_cls=TextLoader)
docs = loader.load()

splitter = RecursiveCharacterTextSplitter(chunk_size=800, chunk_overlap=150)
chunks = splitter.split_documents(docs)

# --- Build vector store ---
embeddings = OpenAIEmbeddings()
vectorstore = Chroma.from_documents(chunks, embeddings, persist_directory=CHROMA_DIR)
retriever = vectorstore.as_retriever(search_kwargs={"k": 3})

# --- Memory for conversation context ---
memory = ConversationBufferWindowMemory(
    memory_key="chat_history",
    return_messages=True,
    k=5,
)

# --- Support-specific prompt ---
system_prompt = """You are a helpful customer support agent. Answer questions using the provided context from our help documentation. If you don't know the answer, say so honestly and suggest contacting support@example.com. Always cite which help article your answer comes from. Be friendly and concise."""

# --- Build chain ---
llm = ChatOpenAI(model="gpt-4o", temperature=0.1)
qa_chain = ConversationalRetrievalChain.from_llm(
    llm=llm,
    retriever=retriever,
    memory=memory,
    return_source_documents=True,
)

# --- Run ---
if __name__ == "__main__":
    print("Customer Support Agent ready. Type 'quit' to exit.")
    while True:
        question = input("\nCustomer: ")
        if question.lower() == "quit":
            break
        result = qa_chain.invoke({"question": question})
        print(f"\nAgent: {result['answer']}")
        sources = set(d.metadata.get("source", "unknown") for d in result["source_documents"])
        if sources:
            print(f"Sources: {', '.join(sources)}")

Setup

  1. 1

    Copy the agent.py content above.

  2. 2

    Create a Python virtual environment and install dependencies.

  3. 3

    Set your OPENAI_API_KEY environment variable.

  4. 4

    Run: python agent.py

Run with LangChain

This is a Python script using LangChain. Set up a virtual environment and install dependencies.

# 1. Create a virtual environment
python -m venv venv && source venv/bin/activate

# 2. Install dependencies
pip install langchain langchain-openai langchain-community chromadb

# 3. Set your API key
export OPENAI_API_KEY="sk-..."

# 4. Save the agent.py from above and run it
python agent.py

Version History

v1.0.0Initial releaseMar 16, 2026

Framework

Langchain

Requirements

Python 3.10+
OpenAI API key

Estimated cost

~$0.20/day

on GPT-4o model

File type

agent.py

Version

v1.0.0

Updated Mar 16, 2026

Contributor

Runbooks Community

Community submission

You might also like