Skip to content

LangChain & RAG

Use AIGuard alongside LangChain by replacing the chat model call with aiguard.chat() and passing the retrieved context as metadata for grounding checks.

Install

$pip install aiguard-safety langchain langchain-community

RAG example

python
import aiguard
from langchain_community.vectorstores import FAISS
from langchain_openai import OpenAIEmbeddings

store = FAISS.load_local("index", OpenAIEmbeddings())

def answer(question: str) -> str:
    docs = store.similarity_search(question, k=4)
    context = "\n\n".join(d.page_content for d in docs)

    resp = aiguard.chat(
        model="gpt-4o",
        messages=[
            {"role": "system", "content": "Answer only from the provided context."},
            {"role": "user",   "content": f"Context:\n{context}\n\nQ: {question}"},
        ],
        metadata={"feature": "rag", "context": context},
    )
    return resp.choices[0].message.content

Set hallucination.mode: context in aiguard.yaml so the contextual grounding scorer runs on these traces.