A modular, production-ready AI assistant backend built with Python, LangChain, OpenAI API, and RAG (Retrieval-Augmented Generation) pipeline.
- π RAG Pipeline: Embed documents and perform similarity search for context retrieval
- π€ LangChain Agent: Chain of tools including Calculator and Google Search
- π¬ Conversational Memory: Maintains conversation history using LangChain's ConversationBufferMemory
- π§Ύ Document Ingestion: Support for PDFs, TXT, Markdown, and DOCX files
- π οΈ Embeddings: OpenAI embeddings for document vectorization
- ποΈ Vector Store: ChromaDB for fast document retrieval
- π Environment-based Configuration: Secure API key management
- π Swagger Documentation: Auto-generated API docs
- π FastAPI Backend: High-performance async API
ContextAgent/ βββ app/ β βββ main.py # FastAPI app entrypoint β βββ routes/ β β βββ chat.py # Chat endpoints β β βββ ingest.py # Document upload endpoints β βββ chains/ β β βββ qa_chain.py # RAG + LLM chain β β βββ agent_chain.py # LangChain agent setup β βββ tools/ β β βββ calculator.py # Custom LangChain tools β β βββ google_search.py # Web search tool β βββ memory/ β β βββ session_memory.py # Conversational memory β βββ ingest/ β β βββ embedder.py # Embedding function β β βββ vector_store.py # ChromaDB setup β βββ utils/ β β βββ config.py # Environment + settings β β βββ document_loader.py # Document processing β βββ schemas/ β βββ request_model.py # Pydantic schemas βββ .env.example # Environment variables template βββ requirements.txt # Python dependencies βββ README.md # This file git clone git https://github.com:webcodelabb/ContextAgent.git cd ContextAgentpip install -r requirements.txtCopy the example environment file and add your API keys:
cp env.example .envEdit .env and add your OpenAI API key:
OPENAI_API_KEY=your_openai_api_key_here OPENAI_MODEL=gpt-4python -m app.mainThe server will start at http://localhost:8000
Ask questions to the AI assistant.
Request Body:
{ "question": "What does this PDF say about climate change?", "history": [ {"role": "user", "content": "Summarize the document"}, {"role": "agent", "content": "Sure, here's the summary..."} ], "use_rag": true, "use_agent": false }Response:
{ "answer": "The PDF discusses the recent changes in global temperatures and the effects of greenhouse gases...", "sources": ["climate_report_2024.pdf"], "reasoning": null, "metadata": { "model": "gpt-4", "session_id": "default", "documents_retrieved": 3 } }Get conversation history for a session.
Clear conversation memory for a session.
Get information about available tools.
Get statistics about the chat system.
Upload a document for processing.
Supported formats: PDF, TXT, MD, DOCX
Ingest all supported documents from a directory.
Get statistics about ingested documents.
Clear all ingested documents.
Check system health and configuration.
| Variable | Description | Default |
|---|---|---|
OPENAI_API_KEY | OpenAI API key (required) | - |
OPENAI_MODEL | OpenAI model to use | gpt-4 |
VECTOR_STORE_TYPE | Vector store type | chroma |
CHROMA_PERSIST_DIRECTORY | ChromaDB storage path | ./chroma_db |
HOST | Server host | 0.0.0.0 |
PORT | Server port | 8000 |
SERP_API_KEY | SerpAPI key for web search | - |
LANGCHAIN_TRACING_V2 | Enable LangSmith tracing | false |
LANGCHAIN_API_KEY | LangSmith API key | - |
import requests # Chat with RAG response = requests.post("http://localhost:8000/chat/", json={ "question": "What are the main points in the uploaded documents?", "use_rag": True }) print(response.json()["answer"]) # Chat with Agent response = requests.post("http://localhost:8000/chat/", json={ "question": "What's 15 * 23?", "use_agent": True }) print(response.json()["answer"])# Simple chat curl -X POST "http://localhost:8000/chat/" \ -H "Content-Type: application/json" \ -d '{"question": "Hello, how are you?"}' # Upload document curl -X POST "http://localhost:8000/ingest/upload" \ -F "file=@document.pdf" # Get system stats curl "http://localhost:8000/chat/stats"- Start the server:
python -m app.main - Open Swagger docs:
http://localhost:8000/docs - Test endpoints through the interactive interface
# Health check curl http://localhost:8000/health # Upload a test document curl -X POST "http://localhost:8000/ingest/upload" \ -F "file=@test_document.pdf" # Ask a question curl -X POST "http://localhost:8000/chat/" \ -H "Content-Type: application/json" \ -d '{"question": "What is this document about?"}'- Document Ingestion: Upload PDFs, TXTs, MDs, DOCXs
- Text Processing: Split documents into chunks
- Embedding: Convert text to vectors using OpenAI
- Storage: Store in ChromaDB vector database
- Retrieval: Find relevant documents for queries
- Generation: Generate answers using LLM with context
- Calculator Tool: Perform mathematical calculations
- Google Search Tool: Search the web for current information
- Conversational Memory: Maintains chat history
- Multi-step Reasoning: Chain multiple tools together
- PDF: PyPDF2 for text extraction
- TXT: UTF-8 text files
- MD: Markdown files
- DOCX: Microsoft Word documents
- Chunking: Intelligent text splitting with overlap
- Metadata: Preserves source information
FROM python:3.11-slim WORKDIR /app COPY requirements.txt . RUN pip install -r requirements.txt COPY . . EXPOSE 8000 CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000"]# Production environment export OPENAI_API_KEY=your_production_key export OPENAI_MODEL=gpt-4 export HOST=0.0.0.0 export PORT=8000- Set up proper CORS configuration
- Use environment variables for secrets
- Implement authentication if needed
- Monitor API usage and costs
- Set up logging and monitoring
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests if applicable
- Submit a pull request
This project is licensed under the MIT License.
- LangChain for the amazing LLM framework
- OpenAI for the GPT models
- ChromaDB for vector storage
- FastAPI for the web framework
Built with β€οΈ for the AI community Testing coauthor achievement Fixing coauthor format