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-api
npm install
Create 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 -d
POST /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/tools
PUT /ai/chat-with-memory/tools/duckduckgo_search
Content-Type: application/json
{
"enabled": true
}
DELETE /ai/chat-with-memory/history/user123
src/
โโโ 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
IToolService
interface - 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! ๐