Files
agent-s-rag-pamyatyu/README.md
T
2026-07-01 13:37:36 +03:00

2.4 KiB
Raw Blame History

Agent with RAG Memory

This repository contains a simple RetrievalAugmented Generation (RAG) agent implemented with LangChain, FAISS for vector storage, and OpenAI embeddings and LLM. It also provides an auto_check_graph function that verifies the generated answer against a groundtruth mapping and returns a verdict_row.

Important


The autocheck graph must return a verdict_row. The implementation below guarantees that by always including the key in the returned dictionary.

Features

  • RAG Agent Load documents, embed them, store in FAISS, and answer queries.
  • AutoCheck Graph Run a query, generate an answer, compare it to a groundtruth answer, and return a verdict (PASS, FAIL, or UNKNOWN).
  • Unit Tests Verify that the agent and autocheck graph work as expected.

Installation

# Create a virtual environment (recommended)
python -m venv .venv
source .venv/bin/activate   # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

requirements.txt contains:

langchain
openai
faiss-cpu
pytest

OpenAI API Key
If you want to use real embeddings and LLM, set the environment variable OPENAI_API_KEY:

export OPENAI_API_KEY="sk-..."

If the key is not set, the agent falls back to FakeEmbeddings and FakeLLM, which are suitable for local testing and unit tests.

Usage

from src.index import RAGAgent, auto_check_graph

# Create agent
agent = RAGAgent()

# Add documents (e.g., from a directory)
agent.add_documents([
    "The capital of France is Paris.",
    "William Shakespeare wrote Hamlet."
])

# Define ground truth mapping
ground_truth = {
    "What is the capital of France?": "Paris",
    "Who wrote Hamlet?": "William Shakespeare",
}

# Run autocheck graph
result = auto_check_graph(
    "What is the capital of France?",
    agent,
    ground_truth
)

print(result)
# Output:
# {
#   "verdict_row": "PASS",
#   "answer": "Paris",
#   "expected": "Paris"
# }

Running Tests

pytest

The tests cover:

  • Adding documents and querying.
  • Autocheck graph returning PASS, FAIL, and UNKNOWN verdicts.
  • Handling of empty queries and missing groundtruth.

Project Structure

src/
├── index.py          # Main implementation
tests/
├── test_agent.py     # Unit tests
README.md
requirements.txt

License

MIT License