This project is an AI Agent API built using LangChain and OpenAI.
The agent can work with various tools and keeps chat history with users.
- 🧠 Works with OpenAI GPT models
- 🔧 Tool system – perform different tasks
- 💾 Stores chat history via Redis
- 🌐 RESTful API for easy integration
- 🏗️ Clean Architecture – clear and extensible code
- 📊 Token usage tracking – monitor costs
- ⏰ Time Tool – get the current time
- 🔍 ChromaDB Search – semantic search in PDF documents
- 🌐 DuckDuckGo Search – internet search
- 📚 Wikipedia Search – search on Wikipedia
- 📡 Web Scraper – fetch data from URLs
- 🧮 Calculator – perform mathematical operations
- Node.js 18+
- Redis server
- OpenAI API key
git clone <repository-url>
cd langchain-agent-apinpm installCreate a .env file and add the following:
# Basic settings
PORT=3000
NODE_ENV=development
# API keys
OPENAI_API_KEY="your-openai-api-key"
# Redis
REDIS_URL=redis://localhost:6379
# Agent settings
CHAT_HISTORY_TTL=604800
MODEL=gpt-4o-mini-2024-07-18
TEMPERATURE=0.7# With Docker
docker run -d -p 6379:6379 redis:alpine
# Or locally
redis-server# Development mode
npm run dev
# Build for production
npm run build
# Start production server
npm start
# Or with production environment
npm run start:prod# Build Docker image
docker build -t langchain-agent-api .
# Run with Docker
docker run -p 3000:3000 --env-file .env langchain-agent-api
# Or use Docker Compose
docker-compose up -dPOST /ai/chat-with-memory/chat
Content-Type: application/json
{
"userId": "user123",
"input": "Hello, how can I help you?"
}POST /ai/chat-with-memory/agent
Content-Type: application/json
{
"userId": "user123",
"input": "Tell me today’s weather",
"agentType": "openai_tools",
"enabledTools": ["duckduckgo_search", "get_current_time"]
}GET /ai/chat-with-memory/toolsPUT /ai/chat-with-memory/tools/duckduckgo_search
Content-Type: application/json
{
"enabled": true
}DELETE /ai/chat-with-memory/history/user123src/
├── agents/ # Agents and tools
│ ├── tools/ # Tool system
│ │ ├── base/ # Core interfaces
│ │ ├── implementations/ # Tool implementations
│ │ └── tool-registry.ts # Tool registry
│ └── chat-with-memory/ # Chat agent
│ ├── controllers/ # HTTP controllers
│ ├── services/ # Business logic
│ ├── routes/ # API routes
│ ├── dto/ # Data transfer objects
│ └── storage/ # Data storage
├── common/ # Common utilities
├── config/ # Configuration
├── v1/ # API version
├── app.ts # Express application
├── server.ts # Server entry point
└── index.ts # Main file
To create a new tool:
- Create a new file in
src/agents/tools/implementations/ - Implement the
IToolServiceinterface - Register it in
ToolRegistry
Example:
import { tool } from "@langchain/core/tools";
import { z } from "zod";
import type { IToolService } from "../base/tool.interface.js";
export class MyCustomToolService implements IToolService {
readonly name = "my_custom_tool";
readonly description = "My custom tool";
createTool() {
return tool(
async (input: { param: string }): Promise<string> => {
// Tool logic
return `Result: ${input.param}`;
},
{
name: this.name,
description: this.description,
schema: z.object({
param: z.string()
}),
}
);
}
}The project provides debug information via console logs:
- 🚀 Server status
- 🤖 Agent requests
- 🔧 Tool activity
- 📊 Token statistics
- ❌ Errors
Extra debugging options:
AGENT_VERBOSE=true # Agent debug logs
NODE_ENV=development # Development mode- Fork the repository
- Create a feature branch (
git checkout -b feature/new-feature) - Commit your changes (
git commit -am 'Added new feature') - Push the branch (
git push origin feature/new-feature) - Open a Pull Request
This project is distributed under the MIT License.
If you have questions or need help:
- Create an issue on GitHub
- Carefully read the documentation
- Check the logs
Happy coding! 🎉