Server FastAPI yang menyediakan berbagai tools dan integrasi dengan Google Gemini AI.
- Auto Tool Router: Routing otomatis ke tool yang tepat berdasarkan prompt
- Google Gemini Integration: Integrasi dengan Google AI Studio (Gemini 2.0 Flash Exp) menggunakan LangChain
- Multiple Tools:
- Weather (Cuaca)
- News (Berita)
- Web Search (Pencarian Web)
- Dictionary (Kamus)
- Quotes (Kutipan Bijak)
- Python 3.8+
- pip (Python package manager)
-
Clone atau download repository ini
-
Install dependencies:
pip install -r requirements.txtAtau install manual:
pip install fastapi uvicorn requests pydantic python-dotenv streamlit langchain-google-genai-
PENTING: Konfigurasi API Key Google
a. Dapatkan API key dari Google AI Studio
b. Buat/edit file
.envdi root folder project:GOOGLE_API_KEY=your_actual_api_key_herec. Pastikan API key valid dan aktif (bukan expired atau restricted)
Catatan: Ganti
your_actual_api_key_heredengan API key Anda sendiri
Buka terminal pertama dan jalankan:
uvicorn server:app --reloadServer akan berjalan di http://localhost:8000
Buka terminal kedua dan jalankan:
streamlit run app.pyAplikasi Streamlit akan terbuka otomatis di browser (http://localhost:8501)
Setelah menjalankan kedua server (FastAPI dan Streamlit), buka browser di http://localhost:8501
Fitur Streamlit App:
- Auto Tool Router: Masukkan pertanyaan dan sistem otomatis memilih tool yang tepat
- Google Gemini AI: Chat interaktif dengan AI Google Gemini
- Manual Tools: Pilih dan gunakan tool secara manual:
- Weather Tool: Cek cuaca kota
- News Tool: Cari berita berdasarkan topik
- Dictionary Tool: Cari definisi kata
- Quotes Tool: Dapatkan kutipan bijak
- Web Search Tool: Pencarian web
Endpoint ini secara otomatis memilih tool yang tepat berdasarkan kata kunci dalam prompt.
Endpoint: POST /auto
Request Body:
{
"prompt": "cuaca di Jakarta"
}Contoh Penggunaan:
curl -X POST http://localhost:8000/auto \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"cuaca di Bandung\"}"curl -X POST http://localhost:8000/auto \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"berita teknologi\"}"curl -X POST http://localhost:8000/auto \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"definisi technology\"}"curl -X POST http://localhost:8000/auto \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"quote bijak\"}"curl -X POST http://localhost:8000/auto \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"python programming\"}"Endpoint untuk berkomunikasi langsung dengan Google Gemini AI.
Endpoint: POST /llm
Request Body:
{
"prompt": "Jelaskan apa itu artificial intelligence"
}Contoh Penggunaan:
curl -X POST http://localhost:8000/llm \
-H "Content-Type: application/json" \
-d "{\"prompt\": \"Apa itu FastAPI?\"}"Cek status server.
Endpoint: GET /
curl http://localhost:8000/Response:
{
"status": "MCP Server with Google AI Studio - OK"
}Setelah server berjalan, akses dokumentasi API interaktif di:
- Swagger UI:
http://localhost:8000/docs - ReDoc:
http://localhost:8000/redoc
.
├── .env # Environment variables (API keys)
├── server.py # Main FastAPI application
├── app.py # Streamlit frontend application
├── providers/
│ ├── google_llm.py # Google Gemini integration
│ └── ollama_llm.py # Ollama integration (optional)
└── tools/
├── weather_tool.py # Weather API
├── news_tool.py # News API
├── search_tool.py # DuckDuckGo search
├── dictionary_tool.py # Dictionary API
└── quotes_tool.py # Quotes API
Auto router mendeteksi kata kunci berikut:
| Kata Kunci | Tool | Contoh |
|---|---|---|
cuaca, weather |
Weather Tool | "cuaca di Jakarta" |
berita, news |
News Tool | "berita teknologi" |
definisi, arti |
Dictionary Tool | "definisi python" |
quote, bijak |
Quotes Tool | "quote bijak" |
| Lainnya | Web Search | "python tutorial" |
Aplikasi Streamlit menyediakan 3 mode:
- Auto Tool Router - Routing otomatis berdasarkan kata kunci
- Google Gemini AI - Chat interface dengan history
- Manual Tools - Pilih tool secara manual dengan UI yang user-friendly
Pastikan semua dependencies sudah terinstall:
pip install fastapi uvicorn requests pydantic python-dotenv streamlit langchain-google-genaiPastikan FastAPI server sudah berjalan di http://localhost:8000 sebelum menjalankan Streamlit
Penyebab umum:
- API key tidak ada di file
.env - API key expired atau tidak valid
- API key memiliki restrictions yang tidak sesuai
Solusi:
- Pastikan file
.envada di root folder project - Dapatkan API key baru dari Google AI Studio
- Copy API key dan paste ke file
.env:GOOGLE_API_KEY=AIzaSy...your_actual_key - Restart server FastAPI setelah mengubah
.env - Pastikan tidak ada spasi atau karakter tambahan di API key
Pastikan server sudah berjalan dengan perintah:
uvicorn server:app --reloadLihat panduan lengkap di TROUBLESHOOTING.md
Quick Fix:
-
Test components:
python test_simple.py
-
Try server without LLM:
uvicorn server_simple:app --reload --port 8001
-
Check API key dan ganti jika perlu di file
.env -
Ganti model di
providers/google_llm.py:model="models/gemini-1.5-flash" # More stable