Spaces:
Sleeping
Sleeping
File size: 7,820 Bytes
345a8d5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# Интеграция Embedding Service с Go Backend
## Адрес сервиса
```
https://calcifer0323-matching.hf.space
```
## Endpoints
| Метод | Путь | Описание |
|-------|------|----------|
| GET | `/` | Информация о сервисе |
| GET | `/health` | Проверка здоровья |
| GET | `/model-info` | Информация о модели (размерность для pgvector) |
| POST | `/embed` | Эмбеддинг из готового текста |
| POST | `/prepare-and-embed` | ⭐ **ОСНОВНОЙ** - подготовка полей + эмбеддинг |
| POST | `/batch` | Пакетная обработка |
## Архитектура
```
Frontend → Go Backend → PostgreSQL + pgvector
↓
Embedding Service (STATELESS)
(только генерирует эмбеддинги, не хранит)
```
---
## Шаг 1: Настройка PostgreSQL + pgvector
```sql
-- Установить расширение
CREATE EXTENSION IF NOT EXISTS vector;
-- Добавить колонку в leads (384 измерения)
ALTER TABLE leads ADD COLUMN IF NOT EXISTS embedding vector(384);
-- Добавить колонку в properties
ALTER TABLE properties ADD COLUMN IF NOT EXISTS embedding vector(384);
-- Создать индексы для быстрого поиска
CREATE INDEX IF NOT EXISTS leads_embedding_idx
ON leads USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
CREATE INDEX IF NOT EXISTS properties_embedding_idx
ON properties USING ivfflat (embedding vector_cosine_ops) WITH (lists = 100);
```
---
## Шаг 2: Интеграция в Go Backend
### 2.1 HTTP клиент
```go
package embedding
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"
)
const ServiceURL = "https://calcifer0323-matching.hf.space"
type Client struct {
http *http.Client
}
func NewClient() *Client {
return &Client{
http: &http.Client{Timeout: 30 * time.Second},
}
}
// Request для /prepare-and-embed
type PrepareAndEmbedRequest struct {
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Requirement map[string]interface{} `json:"requirement,omitempty"`
Price *float64 `json:"price,omitempty"`
District *string `json:"district,omitempty"`
Rooms *int `json:"rooms,omitempty"`
Area *float64 `json:"area,omitempty"`
Address *string `json:"address,omitempty"`
}
// Response от /prepare-and-embed
type PrepareAndEmbedResponse struct {
Embedding []float32 `json:"embedding"`
Dimensions int `json:"dimensions"`
PreparedText string `json:"prepared_text"`
}
// GetEmbedding - получить эмбеддинг для лида или объекта
func (c *Client) GetEmbedding(req PrepareAndEmbedRequest) ([]float32, error) {
body, _ := json.Marshal(req)
resp, err := c.http.Post(
ServiceURL+"/prepare-and-embed",
"application/json",
bytes.NewBuffer(body),
)
if err != nil {
return nil, fmt.Errorf("request failed: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("service returned %d", resp.StatusCode)
}
var result PrepareAndEmbedResponse
json.NewDecoder(resp.Body).Decode(&result)
return result.Embedding, nil
}
```
### 2.2 Работа с pgvector
```go
import "github.com/pgvector/pgvector-go"
// Сохранение эмбеддинга
func (r *LeadRepo) SaveEmbedding(ctx context.Context, leadID string, embedding []float32) error {
vec := pgvector.NewVector(embedding)
_, err := r.db.Exec(ctx,
`UPDATE leads SET embedding = $1 WHERE lead_id = $2`,
vec, leadID,
)
return err
}
// Поиск похожих объектов
func (r *PropertyRepo) FindSimilar(ctx context.Context, leadEmbedding []float32, limit int) ([]Match, error) {
vec := pgvector.NewVector(leadEmbedding)
rows, err := r.db.Query(ctx, `
SELECT property_id, title, price, district, rooms, area,
1 - (embedding <=> $1) as similarity
FROM properties
WHERE embedding IS NOT NULL
ORDER BY embedding <=> $1
LIMIT $2
`, vec, limit)
// ... обработка результатов
}
```
---
## Шаг 3: Флоу создания лида
```go
func (s *LeadService) CreateLead(ctx context.Context, req CreateLeadRequest) (*Lead, error) {
// 1. Сохранить лид в БД
lead, err := s.repo.Create(ctx, req)
if err != nil {
return nil, err
}
// 2. Получить эмбеддинг (можно асинхронно)
go func() {
embedding, err := s.embeddingClient.GetEmbedding(PrepareAndEmbedRequest{
Title: lead.Title,
Description: lead.Description,
Price: extractPrice(lead.Requirement),
District: extractDistrict(lead.Requirement),
Rooms: extractRooms(lead.Requirement),
})
if err != nil {
log.Printf("embedding failed for %s: %v", lead.ID, err)
return
}
s.repo.SaveEmbedding(context.Background(), lead.ID, embedding)
}()
return lead, nil
}
```
---
## Шаг 4: Эндпоинт матчинга
```go
// GET /leads/{id}/matches?limit=10
func (h *Handler) GetMatches(w http.ResponseWriter, r *http.Request) {
leadID := chi.URLParam(r, "id")
limit := parseIntParam(r, "limit", 10)
// Получить эмбеддинг лида
leadEmbedding, err := h.leadRepo.GetEmbedding(r.Context(), leadID)
if err != nil {
respondError(w, "Lead has no embedding", 400)
return
}
// Найти похожие объекты
matches, err := h.propertyRepo.FindSimilar(r.Context(), leadEmbedding, limit)
if err != nil {
respondError(w, err.Error(), 500)
return
}
respondJSON(w, MatchesResponse{
LeadID: leadID,
Matches: matches,
})
}
```
---
## API Response для Frontend
```json
GET /api/leads/{leadId}/matches
{
"leadId": "550e8400-e29b-41d4-a716-446655440000",
"matches": [
{
"propertyId": "7c9e6679-7425-40de-944b-e07fc1f90ae7",
"title": "3-комнатная квартира в центре",
"price": 9500000,
"district": "Центральный",
"rooms": 3,
"area": 78.5,
"similarity": 0.92
}
]
}
```
---
## Зависимости Go
```bash
go get github.com/pgvector/pgvector-go
```
---
## Проверка работоспособности
```bash
# Health check
curl https://calcifer0323-matching.hf.space/health
# Тест эмбеддинга
curl -X POST https://calcifer0323-matching.hf.space/prepare-and-embed \
-H "Content-Type: application/json" \
-d '{"title": "Ищу квартиру", "price": 10000000, "rooms": 3}'
# Информация о модели
curl https://calcifer0323-matching.hf.space/model-info
```
---
## FAQ
**Q: Что если Embedding Service недоступен?**
A: Лид сохранится без эмбеддинга. Добавьте retry-логику или фоновую задачу.
**Q: Как переиндексировать все записи?**
A: Используйте `/batch` endpoint для массовой обработки.
**Q: Нужно ли хранить prepared_text?**
A: Нет, только для отладки. Храните только `embedding`.
|