A Go-based API server that provides a chat interface using Google's Gemini 2.0 API with conversation context management and session handling.
- Session Management: Maintains conversation context with a 60-second inactivity timeout
- Base Prompt Integration: Automatically prepends a base prompt to all conversations
- Gemini 2.0 API Integration: Uses Google's Gemini 2.0 Flash Experimental model
- Context Preservation: Keeps conversation history for contextual responses
- Automatic Cleanup: Removes expired sessions to manage memory usage
- Discord Integration: Sends logs and conversation events to Discord webhook
Send a prompt and receive a response with conversation context.
Request Body:
{
"session_id": "unique-session-id",
"prompt": "Your message here"
}Response:
{
"session_id": "unique-session-id",
"response": "AI response here",
"error": "error message if any"
}Health check endpoint.
Response:
{
"status": "healthy"
}# Set your Gemini API key
export GEMINI_API_KEY="your_api_key_here"
# Basic usage (defaults to localhost:8080)
go run main.go config.go
# Custom host and port
go run main.go config.go -h 192.168.1.100 -p 9000
# With custom base prompt file
go run main.go config.go -prompt /path/to/prompt.txt
# All options together
go run main.go config.go -h 0.0.0.0 -p 8080 -prompt custom_prompt.txt-h: Host to bind the server to (default: "localhost")-p: Port to bind the server to (default: "8080")-prompt: Path to file containing custom base prompt (optional, uses config.go default if not provided)
The server will start on the specified host and port.
Using curl:
# First message in a session
curl -X POST http://localhost:8080/prompt \
-H "Content-Type: application/json" \
-d '{"session_id": "user123", "prompt": "Hello! What can you help me with?"}'
# Follow-up message (maintains context)
curl -X POST http://localhost:8080/prompt \
-H "Content-Type: application/json" \
-d '{"session_id": "user123", "prompt": "Can you remember what I just asked?"}'Run the test client to see the API in action:
go run client_test.go- Host: Configurable via
-hflag (default: "localhost") - Port: Configurable via
-pflag (default: "8080") - Base Prompt: Configurable via
-promptflag or defined inconfig.goas fallback - Session Timeout: 60 seconds of inactivity
- Model: gemini-2.0-flash-exp (Google Gemini 2.0)
- Context Limit: Maximum 20 messages per session
- API Key: Loaded from
GEMINI_API_KEYenvironment variable - Logging: Timestamped logs with request/response tracking
- Discord Webhook: Integrated for real-time logging and conversation monitoring
- Sessions are created automatically when a new
session_idis provided - Each session maintains conversation history
- Sessions expire after 60 seconds of inactivity
- Expired sessions are automatically cleaned up every 30 seconds
- Context is limited to the last 20 messages to manage API costs
- The API key is now loaded from the
GEMINI_API_KEYenvironment variable - Set the environment variable before running the server:
export GEMINI_API_KEY="your_api_key_here" - To get a Gemini API key, visit: https://makersuite.google.com/app/apikey
- Consider adding authentication and rate limiting
- Validate and sanitize user inputs
- Standard Go libraries only (no external dependencies)
- Requires Go 1.16 or later
- Fork/Clone this repository
- Connect to Railway: Link your GitHub repository to Railway
- Set Environment Variables:
GEMINI_API_KEY: Your Gemini API key
- Deploy: Railway will automatically build and deploy using the Dockerfile
# Build the Docker image
docker build -t chatbot .
# Run with environment variable
docker run -p 8008:8008 -e GEMINI_API_KEY="your_api_key" chatbot
# Or run with custom settings
docker run -p 8008:8008 -e GEMINI_API_KEY="your_api_key" chatbot ./chatbot -h 0.0.0.0 -p 8008 -t 120# Set your Gemini API key
export GEMINI_API_KEY="your_api_key_here"
# Run locally
go run main.go config.go -h 0.0.0.0 -p 8008 -t 120