This guide will help you set up and run the AI Knowledge Graph Learning System on your local machine.
Before you begin, ensure you have the following installed:
- Node.js 18+ - Download
- npm or pnpm (comes with Node.js)
- Git (for cloning the repository)
The fastest way to get everything running:
# Navigate to backend directory
cd backend
# Install dependencies
uv sync
# Create environment file
cat > .env << 'EOF'
OPENAI_API_KEY=your_openai_api_key_here
LLM_MODEL=gpt-4
DEBUG=true
CORS_ORIGINS=["http://localhost:3000"]
EOF
# Start backend server
uv run uvicorn app.main:app --reload --port 8000 --host 0.0.0.0The backend should now be running at http://localhost:8000
# Navigate to frontend directory (in a new terminal)
cd frontend
# Install dependencies
pnpm install
# Create environment file
cat > .env << 'EOF'
NEXT_PUBLIC_API_URL=http://localhost:8000
EOF
# Start frontend development server
pnpm devThe frontend should now be running at http://localhost:3000
Open your browser and navigate to:
- Frontend: http://localhost:3000
- Backend API Docs: http://localhost:8000/docs
You can verify your LLM provider configuration by running:
cd /root/AIFeyman/backend
# Test the API connection
uv run python -m app.core.llmThis will test:
- ✅ Base URL is reachable
- ✅ API key is valid
- ✅ Model name is recognized
Expected output:
============================================================
Testing LLM API Connection
============================================================
Provider: https://api.openai.com/v1
Model: gpt-4
Status: ✅ Connection successful! API is responding correctly.
============================================================
If you see errors, check:
- OPENAI_API_KEY is set correctly
- OPENAI_BASE_URL matches your provider
- LLM_MODEL is valid for your provider
cd backend
# Install dependencies using uv
uv syncCreate a .env file in the backend directory:
cat > .env << 'EOF'
# OpenAI API Configuration (Required)
OPENAI_API_KEY=sk-your-openai-api-key-here
# LLM Provider Configuration (Optional)
# Default: https://api.openai.com/v1 (Official OpenAI)
# You can use other OpenAI-compatible providers like OpenRouter, Together AI, DeepSeek, etc.
# Examples:
# - Official OpenAI: https://api.openai.com/v1
# - OpenRouter: https://openrouter.ai/api/v1
# - Together AI: https://api.together.xyz/v1
# - DeepSeek: https://api.deepseek.com/v1
OPENAI_BASE_URL=https://api.openai.com/v1
# LLM Settings (Optional)
LLM_MODEL=gpt-4
LLM_TEMPERATURE=0.3
LLM_MAX_TOKENS=2000
# Server Configuration (Optional)
API_HOST=0.0.0.0
API_PORT=8000
DEBUG=true
LOG_LEVEL=INFO
# Session Configuration (Optional)
SESSION_TIMEOUT_MINUTES=60
MAX_CONCEPTS_PER_SESSION=5
# CORS Configuration (Optional)
CORS_ORIGINS=["http://localhost:3000", "http://localhost:5173"]
EOFyour-openai-api-key-here with your actual OpenAI API key.
# From the backend directory
uv run uvicorn app.main:app --reload --port 8000 --host 0.0.0.0
# Or with more options:
uv run uvicorn app.main:app --host 0.0.0.0 --port 8000 --reload --log-level infoExpected Output:
INFO: Started server process [12345]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://0.0.0.0:8000 (Press CTRL+C to quit)
API Documentation:
- Swagger UI: http://localhost:8000/docs
- ReDoc: http://localhost:8000/redoc
cd frontend
# Using pnpm (recommended)
pnpm installCreate a .env file in the frontend directory:
cat > .env << 'EOF'
# Backend API URL
NEXT_PUBLIC_API_URL=http://localhost:8000
EOF# Using pnpm
pnpm devExpected Output:
Local: http://localhost:3000
Network: use --host to expose
press h + enter to show help
Note: The app will automatically reload when you make changes to the code.
Solution:
- Check that you've created the
.envfile in thebackenddirectory - Verify the API key is set correctly
- Restart the backend server after adding the key
Solution:
# Kill process using port 8000
lsof -ti:8000 | xargs kill -9
# Or use a different port
uv run uvicorn app.main:app --reload --port 8001 --host 0.0.0.0Solution:
- Reinstall dependencies:
uv sync - Check Python version:
python --version(should be 3.9+)
Solution:
# Clear node_modules and reinstall
rm -rf node_modules
pnpm installSolution:
# Use a different port
pnpm dev -- --port 3001Solution:
- Check backend is running on http://localhost:8000
- Verify
.envhas correctNEXT_PUBLIC_API_URL - Check browser console for CORS errors
- Restart frontend after changing environment variables
# Create a test session
curl -X POST http://localhost:8000/api/session/start \
-H "Content-Type: application/json" \
-d '{"source_text": "什么是闭包?"}'
# Expected response:
# {
# "session_id": "some-uuid",
# "initial_graph": { "nodes": [...], "edges": [...] }
# }-
Open http://localhost:3000 in your browser
-
You should see the main page with:
- Title: "AI 知识图谱学习系统"
- Text input area
- "开始学习" button
-
Test the full flow:
- Enter some text (e.g., "什么是闭包?")
- Click "开始学习"
- Wait for session initialization
- See the knowledge graph and chat interface
# Navigate to backend
cd backend
# Run with auto-reload
uv run uvicorn app.main:app --reload --port 8000 --host 0.0.0.0
# In another terminal, run tests (if available)
uv run pytest# Navigate to frontend
cd frontend
# Install dependencies (if not already installed)
pnpm install
# Start development server with auto-reload
pnpm dev
# Build for production
pnpm build
# Preview production build
pnpm start/root/AIFeyman
├── backend/
│ ├── app/
│ │ ├── api/ # API route handlers
│ │ │ ├── session.py
│ │ │ ├── chat.py
│ │ │ └── concept.py
│ │ ├── core/ # Core utilities
│ │ │ ├── config.py
│ │ │ ├── session.py
│ │ │ ├── llm.py
│ │ │ └── prompts.py
│ │ ├── models/ # Pydantic schemas
│ │ │ └── schema.py
│ │ └── services/ # Business logic
│ │ ├── graph_engine.py
│ │ └── tutor_engine.py
│ └── main.py # FastAPI app entry point
│
├── frontend/
│ ├── components/ # React components
│ │ ├── ChatInterface.tsx
│ │ ├── ConceptDrawer.tsx
│ │ ├── KnowledgeGraph.tsx
│ │ └── MainPage.tsx
│ ├── hooks/ # Custom React hooks
│ │ └── useLearningSession.ts
│ ├── types/ # TypeScript types
│ │ └── api.ts
│ ├── utils/ # Utilities
│ │ ├── api-client.ts
│ │ └── network.tsx
│ └── package.json
│
├── CLAUDE.md # Claude Code guidance
├── API.md # API documentation
└── GETTING_STARTED.md # This file
Once you have the application running:
-
Explore the API
- Visit http://localhost:8000/docs for interactive API documentation
- Try out different endpoints using the Swagger UI
-
Understand the Code
- Read CLAUDE.md for high-level architecture
- Check API.md for detailed API reference
- Explore the codebase structure above
-
Customize
- Modify prompts in
backend/app/core/prompts.py - Adjust UI in
frontend/components/ - Add new features following the existing patterns
- Modify prompts in
3a. Use Different LLM Providers
- The application supports any OpenAI-compatible API provider
- Edit
backend/.envand set:# For OpenRouter (access to 100+ models) OPENAI_BASE_URL=https://openrouter.ai/api/v1 OPENAI_API_KEY=sk-or-v1-your-key-here LLM_MODEL=meta-llama/llama-2-70b-chat # For Together AI OPENAI_BASE_URL=https://api.together.xyz/v1 OPENAI_API_KEY=your-together-ai-key LLM_MODEL=mistralai/Mixtral-8x7B-Instruct-v0.1 # For DeepSeek (cost-effective, good for coding) OPENAI_BASE_URL=https://api.deepseek.com/v1 OPENAI_API_KEY=your-deepseek-key LLM_MODEL=deepseek-chat # For local deployment (Ollama) OPENAI_BASE_URL=http://localhost:11434/v1 OPENAI_API_KEY=ollama LLM_MODEL=llama2
- Restart the backend server after changing configuration
- Production Deployment
- Set up a real database (PostgreSQL, MongoDB, or Redis)
- Configure proper CORS origins
- Set up SSL/TLS certificates
- Deploy backend to a cloud provider (AWS, GCP, Azure)
- Deploy frontend to Vercel, Netlify, or similar
If you encounter issues:
- Check the Common Setup Issues section above
- Review error messages in the terminal/console
- Check the browser's developer console (F12)
- Consult the API documentation at http://localhost:8000/docs
- Review the code comments and documentation
- Use
uvicornwith--reloadonly for development - Consider using
gunicornwith multiple workers for production - Set appropriate
SESSION_TIMEOUT_MINUTESfor your use case - Monitor memory usage with in-memory session storage
- Use production build (
pnpm build) for testing performance - Enable code splitting for large applications
- Use React DevTools Profiler to identify performance issues
- Implement proper error boundaries
- API Keys: Never commit API keys to version control
- CORS: Restrict CORS_ORIGINS in production
- Authentication: Add authentication before deploying to production
- Rate Limiting: Implement rate limiting for API endpoints
- Input Validation: All inputs are validated with Pydantic
- HTTPS: Use HTTPS in production environments
For production deployment, consult your cloud provider's security best practices.
# Check Python version
python --version # Should be 3.9+
# Check virtual environment is activated
which python # Should show .venv path
# Verify dependencies
pip list
# Check for syntax errors
python -m py_compile main.py# Check Node.js version
node --version # Should be 18+
# Clear cache and reinstall
rm -rf node_modules .next
pnpm install
# Check for TypeScript errors
npx tsc --noEmit# Check backend is running
curl http://localhost:8000/health
# Check CORS configuration
# Verify frontend URL is in CORS_ORIGINS
# Check network tab in browser devtools
# Look for 404, 500, or CORS errorsYou should now have:
- ✅ Backend running on http://localhost:8000
- ✅ Frontend running on http://localhost:3000
- ✅ API documentation at http://localhost:8000/docs
- ✅ Working development environment
Happy coding! 🚀