Fast AI chat backend with conversation memory using Redis caching.
- JWT authentication
- Conversation history (30 min sessions)
- Multi-user support
- Groq AI integration
- Go 1.21+
- Redis 7.0+
- MongoDB 6.0+
- Auth0 account
- Groq API key
- Create account at auth0.com
- Create new application (Regular Web Application)
- Note your Domain and configure in environment variables
- Add
http://localhost:8080to Allowed Callback URLs
- Clone repository:
git clone https://github.com/yourusername/backend-optimizer
cd backend-optimizer- Install dependencies:
go mod download- Set up environment variables (create
.envfile or export):
export REDIS_URL="localhost:6379"
export MONGO_URL="mongodb://localhost:27017"
export AUTH0_DOMAIN="your-domain.auth0.com" # From Auth0 dashboard
export AUTH0_AUDIENCE="your-audience" # From Auth0 dashboard
export GROQ_API_KEY="gsk_..." # From console.groq.com- Run the server:
go run cmd/server/main.go- Server runs at
http://localhost:8080
POST /Register or /Login with user credentials
Body:
{
"email": "user@example.com",
"password": "SecurePass123!"
}Returns JWT token for authenticated requests.
POST /askAI
Headers:
{
"Authorization": "Bearer YOUR_JWT_TOKEN",
"Content-Type": "application/json"
}Body:
{
"role": "user",
"content": "Your message here"
}After starting the server:
- Register a user:
curl -X POST http://localhost:8080/Register \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"Test123!"}'- Login to get JWT token:
curl -X POST http://localhost:8080/Login \
-H "Content-Type: application/json" \
-d '{"email":"test@example.com","password":"Test123!"}'- Chat with AI (use token from step 2):
curl -X POST http://localhost:8080/askAI \
-H "Authorization: Bearer YOUR_TOKEN_HERE" \
-H "Content-Type: application/json" \
-d '{"role":"user","content":"Hello!"}'.
├── README.md
├── cmd
│ └── server
│ └── main.go
├── go.mod
├── go.sum
└── internal
├── ai
│ ├── groq_dto.go
│ └── model.go
├── api
│ ├── client_auth.go
│ ├── handlers.go
│ ├── handlers_ai.go
│ ├── handlers_todo.go
│ ├── handlers_user.go
│ └── routes.go
├── cache
│ └── cache.go
├── db
│ └── db.go
├── models
│ ├── cachedb.go
│ ├── todos.go
│ └── user.go
├── service
│ └── risk_service.go
├── tests
│ └── benchmark_test.go
└── utils
├── config.go
└── logger.go
12 directories, 21 files
- Cosine similarity for smart context selection
- Two-tier caching (hot/cold)
- Embedding-based message retrieval
MIT