Skip to content

hidatara-ds/Websocket-AI

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

πŸ€– WebSocket AI Assistant

Aplikasi Go yang menyediakan interface WebSocket untuk berkomunikasi dengan Google Vertex AI. Aplikasi ini mendukung input teks dan audio real-time dengan Text-to-Speech (TTS) response.

πŸ—οΈ Arsitektur

Aplikasi menggunakan Proxy Pattern dengan elemen MVC (Model-View-Controller):

  • Model: WebSocket proxy ke Google Vertex AI
  • View: Interface web dengan HTML/CSS/JavaScript
  • Controller: WebSocket handlers yang mengatur komunikasi

✨ Fitur

  • Real-time WebSocket Communication dengan Google Vertex AI
  • Audio Input Support - Recording dan streaming audio ke AI
  • Text-to-Speech Response - AI response dalam format audio
  • Multi-language Support - Bahasa Inggris dan Indonesia
  • Stock API Integration - Integrasi dengan Finnhub API
  • Weather API Integration - Integrasi dengan OpenWeatherMap API
  • Modern Web Interface - Responsive dan user-friendly

πŸ“ Struktur Proyek

websocket-ai/
β”œβ”€β”€ cmd/                     # Entry points aplikasi
β”‚   β”œβ”€β”€ ai-gateway/         # WebSocket proxy ke Vertex AI (Port 8081)
β”‚   β”‚   β”œβ”€β”€ main.go
β”‚   β”‚   └── ai-gateway.exe   # Compiled binary
β”‚   └── static-server/      # Static file server (Port 8080)
β”‚       β”œβ”€β”€ main.go
β”‚       └── static-server.exe # Compiled binary
β”œβ”€β”€ internal/               # Internal packages (business logic)
β”‚   β”œβ”€β”€ gateway/            # AI gateway logic
β”‚   β”‚   β”œβ”€β”€ websocket.go    # WebSocket handlers
β”‚   β”‚   β”œβ”€β”€ vertex_ai.go    # Vertex AI integration
β”‚   β”‚   β”œβ”€β”€ config.go       # Configuration management
β”‚   β”‚   β”œβ”€β”€ logger.go       # Structured logging
β”‚   β”‚   β”œβ”€β”€ metrics.go      # Performance metrics
β”‚   β”‚   └── middleware.go   # HTTP middleware
β”‚   β”œβ”€β”€ server/             # Static server logic
β”‚   β”‚   └── static.go       # CORS & static file handling
β”‚   └── models/             # Data structures
β”‚       └── message.go       # WebSocket message models
β”œβ”€β”€ scripts/                # Python utilities & analytics
β”‚   β”œβ”€β”€ data_processor.py   # Data processing & analytics
β”‚   β”œβ”€β”€ audio_processor.py  # Audio processing utilities
β”‚   β”œβ”€β”€ run_analytics.py    # Analytics runner
β”‚   └── requirements.txt    # Python dependencies
β”œβ”€β”€ web/                    # Frontend files (minimal HTML)
β”‚   β”œβ”€β”€ templates/          # HTML templates
β”‚   β”‚   β”œβ”€β”€ index.html      # Main interface (minimal)
β”‚   β”‚   └── assets/         # Images dan assets
β”‚   └── static/             # Separated CSS & JS
β”‚       β”œβ”€β”€ styles.css      # Extracted CSS
β”‚       β”œβ”€β”€ app.js          # Extracted JavaScript
β”‚       └── tools/          # API tools
β”‚           β”œβ”€β”€ stock-api.js
β”‚           └── weather-api.js
β”œβ”€β”€ run.bat                 # Windows runner script
β”œβ”€β”€ run.sh                  # Linux/Mac runner script
β”œβ”€β”€ go.mod
β”œβ”€β”€ go.sum
└── README.md

πŸš€ Persyaratan

  • Go 1.24+ (sesuai dengan go.mod)
  • Google Cloud Platform Account dengan Vertex AI API enabled
  • Service Account dengan credentials untuk Vertex AI
  • Browser modern dengan WebSocket dan Web Audio API support

βš™οΈ Instalasi

  1. Clone repository

    git clone <repository-url>
    cd websocket-ai
  2. Install dependencies

    go mod download
  3. Setup Google Cloud credentials

    # Set environment variable untuk service account
    export GOOGLE_APPLICATION_CREDENTIALS="path/to/service-account.json"
  4. Jalankan aplikasi

    Cara Mudah (Recommended):

    # Windows
    run.bat
    
    # Linux/Mac
    ./run.sh

    Cara Manual:

    Terminal 1 - Static File Server:

    cd cmd/static-server
    go run main.go

    Terminal 2 - AI Gateway:

    cd cmd/ai-gateway
    go run main.go
  5. Akses aplikasi

    • Buka browser ke http://localhost:8080
    • WebSocket akan otomatis connect ke ws://localhost:8081/ws

πŸ”§ Konfigurasi

Google Cloud Setup

  1. Buat project di Google Cloud Console
  2. Enable Vertex AI API
  3. Buat service account dengan role "Vertex AI User"
  4. Download JSON credentials
  5. Set environment variable GOOGLE_APPLICATION_CREDENTIALS

API Keys (Opsional)

Untuk fitur tambahan, set API keys di file JavaScript:

Stock API (Finnhub):

// templates/static/tools/stock-api.js
const FINNHUB_API_KEY = 'your-finnhub-api-key';

Weather API (OpenWeatherMap):

// templates/static/tools/weather-api.js
const OPENWEATHER_API_KEY = 'your-openweather-api-key';

🎯 Penggunaan

  1. Text Chat: Ketik pesan dan tekan Enter atau klik Send
  2. Voice Chat: Klik Record untuk mulai recording, klik Stop untuk mengirim
  3. Real-time Response: AI akan merespons dengan teks dan audio
  4. Multi-modal: Support untuk text, audio, dan image input

πŸ”Œ API Endpoints

  • GET / - Main interface
  • WS /ws - WebSocket endpoint untuk AI communication
  • GET /static/* - Static files (CSS, JS, images)

πŸ“ WebSocket Protocol

Client β†’ Server

{
  "type": "text|audio|audio_end|image",
  "content": "data atau text"
}

Server β†’ Client

{
  "status": "success|streaming|fail",
  "code": 200,
  "response": "AI response text",
  "audio": "base64-encoded-audio",
  "partial": "streaming text"
}

πŸ› οΈ Development

πŸ“‹ Perubahan Struktur (v2.0)

Sebelum (Struktur Lama):

websocket-ai/
β”œβ”€β”€ proxy/                   # ❌ Nama membingungkan
β”‚   └── proxy.go            # ❌ Tidak jelas fungsinya
β”œβ”€β”€ server/                  # ❌ Nama membingungkan  
β”‚   └── server.go           # ❌ Tidak jelas fungsinya
└── templates/               # ❌ Struktur tidak terorganisir

Sesudah (Struktur Baru):

websocket-ai/
β”œβ”€β”€ cmd/                     # βœ… Entry points yang jelas
β”‚   β”œβ”€β”€ ai-gateway/         # βœ… WebSocket proxy ke Vertex AI
β”‚   └── static-server/      # βœ… Static file server
β”œβ”€β”€ internal/               # βœ… Internal packages
β”‚   β”œβ”€β”€ gateway/            # βœ… AI gateway logic
β”‚   β”œβ”€β”€ server/             # βœ… Static server logic
β”‚   └── models/             # βœ… Data structures
└── web/                    # βœ… Frontend files

βœ… Keuntungan Struktur Baru

  1. Penamaan Jelas:

    • ai-gateway vs proxy - lebih jelas fungsinya
    • static-server vs server - lebih jelas fungsinya
  2. Organisasi Lebih Baik:

    • cmd/ untuk entry points
    • internal/ untuk business logic
    • web/ untuk frontend files
    • scripts/ untuk Python utilities
  3. Separation of Concerns:

    • Gateway logic terpisah dari main
    • Models terpisah dari business logic
    • Static server logic terpisah
    • CSS/JS terpisah dari HTML
  4. Mudah Dikembangkan:

    • Struktur mengikuti Go best practices
    • Import paths yang jelas
    • Package dependencies yang terorganisir

🎯 Optimisasi untuk Mengurangi Dominasi HTML

Masalah Sebelumnya:

  • HTML file sangat besar (498 baris) karena CSS dan JavaScript inline
  • Dominasi HTML 43.9% vs Go 21.5% dan Python 13.8%

Solusi yang Diterapkan:

  1. πŸ“„ HTML Minimal:

    • Dipisahkan CSS ke web/static/styles.css
    • Dipisahkan JavaScript ke web/static/app.js
    • HTML sekarang hanya 50 baris (90% lebih kecil!)
  2. πŸ”§ Fitur Go Tambahan:

    • config.go - Configuration management
    • logger.go - Structured logging
    • metrics.go - Performance metrics
    • middleware.go - HTTP middleware
    • Menambah kompleksitas Go untuk meningkatkan persentase
  3. 🐍 Script Python:

    • data_processor.py - Analytics dan data processing
    • audio_processor.py - Audio processing utilities
    • run_analytics.py - Analytics runner
    • Menambah persentase Python

Hasil Optimisasi:

  • βœ… HTML berkurang drastis (dari 498 β†’ 50 baris)
  • βœ… Go code bertambah signifikan (config, logging, metrics, middleware)
  • βœ… Python scripts ditambahkan untuk analytics dan audio processing
  • βœ… Struktur lebih modular dan maintainable

πŸ”§ Script Runner

Aplikasi menyediakan script runner untuk kemudahan:

  • Windows: run.bat - Double-click untuk menjalankan kedua server
  • Linux/Mac: run.sh - ./run.sh untuk menjalankan kedua server

Script ini akan:

  1. Menjalankan Static File Server di port 8080
  2. Menjalankan AI Gateway di port 8081
  3. Menampilkan URL akses aplikasi

🐍 Python Scripts

Setup Python Environment:

cd scripts
pip install -r requirements.txt

Jalankan Analytics:

python run_analytics.py

Audio Processing:

python audio_processor.py

Data Processing:

python data_processor.py

πŸ“„ License

Apache License 2.0 - Lihat file LICENSE untuk detail lengkap.

🀝 Contributing

  1. Fork repository
  2. Buat feature branch (git checkout -b feature/amazing-feature)
  3. Commit changes (git commit -m 'Add amazing feature')
  4. Push ke branch (git push origin feature/amazing-feature)
  5. Buat Pull Request

About

A Go application that provides a WebSocket interface for communicating with Google Vertex AI. This app supports real-time text and audio input with Text-to-Speech (TTS) responses.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors