Skip to content
reaatech

Files · Google Gemini Lead Intake for Chili Piper SMB Sales Booking

70 (1 binary, 591.4 kB total)attempt 1

README.md·5546 B·markdown
markdown
# Google Gemini Lead Intake for Chili Piper SMB Sales Booking
 
> Qualify inbound leads through a conversational interface, then automatically book meetings via Chili Piper based on lead score and availability.
 
A tutorialized reference solution from [reaatech.com](https://reaatech.com), demonstrating how to build production-grade AI systems with the `@reaatech/*` package family.
 
## Running locally
 
```bash
pnpm install
pnpm test            # vitest run with coverage
pnpm dev             # next dev
```
 
## Project layout
 
```
app/                  Next.js App Router pages + API routes
src/                  services, lib, adapters
tests/                vitest suite (mirrors src/)
packages/             API references for every dependency (read these first)
DEV_PLAN.md           build plan for this recipe
```
 
## Architecture
 
The lead intake pipeline processes inbound messages through a series of stages:
 
```
User message → GET /api/health

POST /api/chat

Session continuity (Redis) — load / create conversation session

Intent classification — KeywordClassifier (fast, local) → Gemini fallback (deep understanding)

ConfidenceRouter — route, clarify, or fallback based on threshold config

[ROUTE → schedule_demo] → Lead scoring → Chili Piper booking API → Gemini-generated confirmation
[CLARIFY]              → Gemini generates clarifying question
[FALLBACK]             → Gemini FAQ-style response

Cost telemetry (per-request budget tracking)
```
 
Key components:
- **Vertex AI Gemini** (`src/lib/llm.ts`) — LLM provider for intent classification, FAQ responses, and confirmation messages
- **KeywordClassifier** (`@reaatech/confidence-router-classifiers`) — fast local intent matching with substring keyword patterns; falls back to Gemini when confidence < 0.6
- **ConfidenceRouter** (`@reaatech/confidence-router`) — routes high-confidence intents to booking, requests clarification for ambiguous input, or falls through to FAQ
- **Chili Piper** (`src/lib/chilipiper.ts`) — books SMB demo meetings via the Chili Piper API
- **Redis** (`src/lib/session.ts`) — persists conversation history across turns for multi-turn session continuity
- **Cost telemetry** (`src/lib/budget.ts`) — tracks per-request token usage and enforces daily spending caps
 
## Setup guide
 
### Environment variables
 
Create a `.env.local` file at the project root:
 
```bash
# GCP Vertex AI
GOOGLE_CLOUD_PROJECT=your-gcp-project-id
GOOGLE_CLOUD_LOCATION=us-central1
GOOGLE_GENAI_USE_ENTERPRISE=true
GOOGLE_APPLICATION_CREDENTIALS=/path/to/service-account.json
 
# Redis
REDIS_URL=redis://localhost:6379
 
# Chili Piper OAuth
CHILIPIPER_CLIENT_ID=your-client-id
CHILIPIPER_CLIENT_SECRET=your-client-secret
CHILIPIPER_BASE_URL=https://api.chilipiper.com
 
# Langfuse (optional — tracing)
LANGFUSE_PUBLIC_KEY=
LANGFUSE_SECRET_KEY=
 
# Tunables
ROUTE_THRESHOLD=0.8
FALLBACK_THRESHOLD=0.3
MAX_SESSION_TOKENS=4096
DAILY_BUDGET_USD=5.0
```
 
### GCP Vertex AI
 
1. Create a GCP project and enable the Vertex AI API
2. Create a service account with the **Vertex AI User** role
3. Download the service account key JSON
4. Set `GOOGLE_APPLICATION_CREDENTIALS` to the key path, or configure Workload Identity Federation
 
### Redis
 
The app connects to any Redis-compatible instance (local, Upstash, Redis Cloud, ElastiCache).
- **Local dev**: `redis://localhost:6379`
- **Production**: Set `REDIS_URL` to your Redis connection string
 
### Chili Piper OAuth
 
1. Register a Chili Piper OAuth app at https://app.chilipiper.com/integrations
2. Note the **Client ID** and **Client Secret**
3. Set the OAuth redirect URL to your application domain
4. Assign the required scopes for meeting booking
 
## API endpoints
 
### `POST /api/chat`
 
Process an inbound lead message and return a response.
 
**Request body**
 
| Field | Type | Required | Description |
|---|---|---|---|
| `message` | `string` | yes | The user's message |
| `sessionId` | `string` | no | Existing session ID for multi-turn continuity |
| `userEmail` | `string` | no | Lead's email address (used for Chili Piper booking) |
 
```json
{
  "message": "I'd like to book a demo",
  "sessionId": "abc-123",
  "userEmail": "lead@example.com"
}
```
 
**Response body**
 
| Field | Type | Description |
|---|---|---|
| `reply` | `string` | AI-generated reply text |
| `sessionId` | `string` | Session ID for subsequent requests |
| `decisionType` | `"ROUTE" \| "CLARIFY" \| "FALLBACK"` | Routing decision made by ConfidenceRouter |
| `bookingId` | `string` | Present when a Chili Piper booking was created |
| `leadScore` | `number` | Lead score from `lead-scorer` (0–1) |
 
```json
{
  "reply": "Great news! I've booked your demo for today at 2:00 PM UTC. Here's your meeting link: https://meet.chilipiper.com/...",
  "sessionId": "abc-123",
  "decisionType": "ROUTE",
  "bookingId": "cp_booking_456",
  "leadScore": 0.92
}
```
 
**curl example**
 
```bash
curl -X POST http://localhost:3000/api/chat \
  -H "Content-Type: application/json" \
  -d '{"message":"I want to book a demo","sessionId":"abc-123","userEmail":"lead@example.com"}'
```
 
### `GET /api/health`
 
Check service health including Redis connectivity.
 
**Response body** (healthy)
 
```json
{ "status": "ok", "redis": "connected" }
```
 
**Response body** (degraded — Redis unreachable)
 
```json
{ "status": "degraded", "redis": "Error: connect ECONNREFUSED ..." }
```
 
**curl example**
 
```bash
curl http://localhost:3000/api/health
```
 
## License
 
MIT — see [LICENSE](./LICENSE).