|
| 1 | +# Serverless AI Chat with RAG using LangChain.js |
| 2 | + |
| 3 | +Serverless TypeScript Retrieval-Augmented Generation (RAG) chat sample: Lit + Vite frontend (Azure Static Web Apps), Azure Functions backend with LangChain.js, Cosmos DB vector store, Blob Storage for source documents, optional Azure OpenAI or local Ollama models. Provisioned by Bicep & Azure Developer CLI (azd) with CI/CD. Focus: reliability, citations, low cost, clear extension points. |
| 4 | + |
| 5 | +> **MISSION**: Provide a maintained Azure reference implementation of a serverless LangChain.js RAG chat that showcases best practices (citations, reliability, tooling) while staying lean and easy to extend. |
| 6 | +
|
| 7 | +## Overview |
| 8 | + |
| 9 | +- End-user asks questions in a web UI; backend performs RAG: embed/query vector store (Cosmos DB or in‑memory/faiss fallback), assemble context, invoke LLM (Azure OpenAI or local Ollama), stream answer + citations to client. |
| 10 | +- Documents (PDF/others) uploaded -> chunked & embedded -> stored for retrieval; blob storage keeps originals. |
| 11 | +- Architecture (high level): |
| 12 | + - Frontend: `packages/webapp` (Lit components, served locally by Vite, deployed via Static Web Apps) |
| 13 | + - Backend: `packages/api` (Azure Functions isolated worker w/ LangChain.js chains) |
| 14 | + - Data: Cosmos DB (vector and chat history), Blob Storage (docs) |
| 15 | + - Infra: `infra/` Bicep templates composed by `infra/main.bicep`, parameters in `infra/main.parameters.json` |
| 16 | + - Scripts: ingestion helper in `scripts/upload-documents.js` |
| 17 | + |
| 18 | +## Key Technologies and Frameworks |
| 19 | + |
| 20 | +- TypeScript (monorepo via npm workspaces) |
| 21 | +- Azure Functions (Node.js runtime v4) + LangChain.js core/community providers |
| 22 | +- Lit + Vite for frontend UI |
| 23 | +- Azure Cosmos DB (vector store via @langchain/azure-cosmosdb) / faiss-node (local alt) |
| 24 | +- Azure Blob Storage (document source persistence) |
| 25 | +- Azure OpenAI / Ollama (LLM + embeddings) |
| 26 | +- Infrastructure as Code: Bicep + Azure Developer CLI (azd) |
| 27 | +- CI/CD: GitHub Actions |
| 28 | + |
| 29 | +## Constraints and Requirements |
| 30 | + |
| 31 | +- Maintain simplicity; avoid premature abstractions or heavy frameworks |
| 32 | +- No proprietary dependencies beyond Azure services (prefer OSS + Azure) |
| 33 | + |
| 34 | +## Development Workflow |
| 35 | + |
| 36 | +Root scripts (run from repository root): |
| 37 | + |
| 38 | +- `npm run start` – Launch webapp (`:8000`) and API Functions host (`:7071`) concurrently |
| 39 | +- `npm run build` – Build all workspaces |
| 40 | +- `npm run clean` – Clean build outputs |
| 41 | +- `npm run upload:docs` – Invoke ingestion script against local Functions host |
| 42 | + |
| 43 | +Backend (`packages/api`): |
| 44 | + |
| 45 | +- `npm run start` – Clean, build, start Functions host with TS watch |
| 46 | +- `npm run build` – TypeScript compile to `dist` |
| 47 | + |
| 48 | +Frontend (`packages/webapp`): |
| 49 | + |
| 50 | +- `npm run dev` – Vite dev server (port 8000) |
| 51 | +- `npm run build` – Production build |
| 52 | + |
| 53 | +## Coding Guidelines |
| 54 | + |
| 55 | +- TypeScript strict-ish (reduced lint rules via XO config) balancing clarity for newcomers |
| 56 | +- Prettier enforced via lint-staged pre-commit hook |
| 57 | +- Favor explicit imports; keep functions small & composable |
| 58 | + |
| 59 | +## Security Considerations |
| 60 | + |
| 61 | +- Secrets managed via Azure (Function App / Static Web App settings) – Avoid committing secrets |
| 62 | +- Test artifacts (traces, screenshots) must not include secrets → scrub logs & env variable exposure |
| 63 | +- Principle of least privilege in Bicep role assignments |
| 64 | + |
| 65 | +## Extension Points |
| 66 | + |
| 67 | +- Swappable embeddings & LLM providers (Azure OpenAI ↔ Ollama) with minimal config changes |
| 68 | + |
| 69 | +## Environment Variables (High-Level) |
| 70 | + |
| 71 | +- Azure OpenAI endpoints |
| 72 | +- Cosmos DB connection / database name |
| 73 | +- Blob storage account & container |
0 commit comments