feat: solution for 'Агент с RAG-памятью'
CI / build (push) Has been cancelled

This commit is contained in:
2026-06-25 12:41:22 +03:00
parent 14ab96d098
commit a8a8111eca
4 changed files with 117 additions and 112 deletions
+31 -67
View File
@@ -1,93 +1,57 @@
# RAG Memory Agent
# Agent with RAG Memory
A simple Retrieval-Augmented Generation (RAG) memory system built with Node.js, TypeScript, and Express.
It stores user data in an inmemory virtual file system and uses a language model (OpenAI or a mock) to answer queries based on stored memory.
This project demonstrates a simple LangChain agent that uses Retrieval-Augmented Generation (RAG) to answer questions based on a small set of documents. The implementation is written in TypeScript and follows the latest LangChain initialization patterns.
## Features
- **Virtual File System** CRUD operations for memory entries.
- **LLM abstraction** Uses OpenAI GPT3.5Turbo if `OPENAI_API_KEY` is set, otherwise falls back to a mock echo.
- **RAG Agent** Retrieves relevant memory, builds a prompt, and generates an answer.
- **RESTful API** Endpoints for managing memory and querying the agent.
- **Unit tests** Jest tests for VFS and Agent logic.
- **Updated Agent Initialization**: Uses `initializeAgentExecutorWithOptions` from LangChain.
- **Custom Text Splitter**: Configured with a chunk size of 1000 characters and an overlap of 200 characters.
- **RAG Memory**: Embeddings are stored in a FAISS vector store and queried via a RetrievalQA chain.
- **Simple Test Harness**: Runs a sample query and prints the agent's response.
## Installation
## Prerequisites
- Node.js v18 or newer
- npm
## Setup
```bash
git clone https://git.brojs.ru/kuzakhmetovartur/prakticheskoe-zadanie-agent-s-rag-pamyat.git
cd prakticheskoe-zadanie-agent-s-rag-pamyat
# Clone the repository
git clone https://github.com/your-username/agent-rag-memory.git
cd agent-rag-memory
# Install dependencies
npm install
# Create a .env file with your OpenAI API key
echo "OPENAI_API_KEY=your_api_key_here" > .env
```
## Environment Variables
Create a `.env` file based on `.env.example`:
## Running the Agent
```bash
cp .env.example .env
```
- `OPENAI_API_KEY` (optional) Your OpenAI API key. If omitted, the agent will use a mock LLM.
- `PORT` Port number for the server (default: 3000).
## Running the Server
```bash
npm run dev # Development with ts-node
# or
npm run build
npm start
```
The server will start on `http://localhost:<PORT>`.
You should see output similar to:
## API Endpoints
| Method | Path | Description | Body (JSON) |
|--------|-----------|---------------------------------------------|---------------------------------|
| GET | `/memory` | List all memory entries (id, snippet). | |
| POST | `/memory` | Create a new memory entry. | `{ "content": "string" }` |
| DELETE | `/memory/:id` | Delete a memory entry by ID. | |
| POST | `/query` | Query the agent. | `{ "query": "string" }` |
### Example Requests
```bash
# Add memory
curl -X POST http://localhost:3000/memory \
-H "Content-Type: application/json" \
-d '{"content":"I love programming in TypeScript."}'
# Query
curl -X POST http://localhost:3000/query \
-H "Content-Type: application/json" \
-d '{"query":"What do I like?"}'
```
## Testing
Run unit tests with coverage:
```bash
npm test
=== Agent Response ===
Paris
```
## Project Structure
```
src/
index.ts # Server entry point
agent.ts # RAG agent logic
llm.ts # LLM abstraction
vfs.ts # Virtual file system
utils.ts # Helpers
routes.ts # Express routes
middleware.ts # Error handling & validation
tests/
vfs.test.ts
agent.test.ts
agent-rag-memory/
├── src/
└── index.ts # Main implementation
├── package.json
├── tsconfig.json
└── README.md
```
## License
MIT © Your Name
MIT License