Route monitoring TG classification through AI service
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 5s
Some checks failed
Build and Deploy / build-and-deploy (push) Failing after 5s
This commit is contained in:
@@ -1,14 +1,11 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
@@ -17,6 +14,8 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"monitoring-tg/internal/aiservice"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
)
|
||||
|
||||
@@ -33,14 +32,14 @@ type config struct {
|
||||
PostgresPort int
|
||||
|
||||
LLMEnabled bool
|
||||
LLMBaseURL string
|
||||
LLMAPIKey string
|
||||
LLMModel string
|
||||
LLMTimeout time.Duration
|
||||
LLMMaxTokens int
|
||||
LLMMinTextLength int
|
||||
ClassifyInterval time.Duration
|
||||
ClassifyBatchSize int
|
||||
AIServiceURL string
|
||||
AIServiceToken string
|
||||
}
|
||||
|
||||
type pendingMessage struct {
|
||||
@@ -52,29 +51,10 @@ type pendingMessage struct {
|
||||
Extracted map[string]any
|
||||
}
|
||||
|
||||
type chatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []chatMessage `json:"messages"`
|
||||
Temperature float64 `json:"temperature"`
|
||||
MaxTokens int `json:"max_tokens"`
|
||||
ResponseFormat responseFmt `json:"response_format"`
|
||||
}
|
||||
|
||||
type chatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type responseFmt struct {
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type chatResponse struct {
|
||||
Choices []struct {
|
||||
Message chatMessage `json:"message"`
|
||||
} `json:"choices"`
|
||||
}
|
||||
|
||||
func main() {
|
||||
cfg := loadConfig()
|
||||
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
|
||||
@@ -95,7 +75,11 @@ func main() {
|
||||
}
|
||||
defer pool.Close()
|
||||
|
||||
worker := &classifier{cfg: cfg, db: pool, http: &http.Client{Timeout: cfg.LLMTimeout}}
|
||||
worker := &classifier{
|
||||
cfg: cfg,
|
||||
db: pool,
|
||||
ai: aiservice.New(cfg.AIServiceURL, cfg.AIServiceToken, cfg.LLMTimeout),
|
||||
}
|
||||
slog.Info(
|
||||
"classifier_started",
|
||||
"interval", cfg.ClassifyInterval.String(),
|
||||
@@ -124,9 +108,9 @@ func main() {
|
||||
}
|
||||
|
||||
type classifier struct {
|
||||
cfg config
|
||||
db *pgxpool.Pool
|
||||
http *http.Client
|
||||
cfg config
|
||||
db *pgxpool.Pool
|
||||
ai *aiservice.Client
|
||||
}
|
||||
|
||||
func (c *classifier) runOnce(ctx context.Context) (int, error) {
|
||||
@@ -218,50 +202,53 @@ func (c *classifier) classify(ctx context.Context, msg pendingMessage) (json.Raw
|
||||
return nil, err
|
||||
}
|
||||
|
||||
payload := chatRequest{
|
||||
Model: c.cfg.LLMModel,
|
||||
Messages: []chatMessage{
|
||||
responseFormat, _ := json.Marshal(responseFmt{Type: "json_object"})
|
||||
payload := aiservice.ChatInput{
|
||||
Messages: []aiservice.Message{
|
||||
{Role: "system", Content: systemPrompt},
|
||||
{Role: "user", Content: buildUserPrompt(msg.Text)},
|
||||
},
|
||||
Temperature: 0.1,
|
||||
MaxTokens: c.cfg.LLMMaxTokens,
|
||||
ResponseFormat: responseFmt{Type: "json_object"},
|
||||
ResponseFormat: responseFormat,
|
||||
}
|
||||
body, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, strings.TrimRight(c.cfg.LLMBaseURL, "/")+"/v1/chat/completions", bytes.NewReader(body))
|
||||
job, err := c.ai.CreateJob(ctx, aiservice.CreateJobRequest{
|
||||
OwnerService: "monitoring-tg",
|
||||
OwnerRef: fmt.Sprintf("%d", msg.ID),
|
||||
TaskType: "telegram_classification",
|
||||
ModelProfile: c.cfg.LLMModel,
|
||||
Priority: 5,
|
||||
MaxAttempts: 2,
|
||||
Input: body,
|
||||
IdempotencyKey: fmt.Sprintf("monitoring-tg:telegram_classification:%d", msg.ID),
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
if c.cfg.LLMAPIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.cfg.LLMAPIKey)
|
||||
}
|
||||
|
||||
resp, err := c.http.Do(req)
|
||||
waitCtx, cancel := context.WithTimeout(ctx, c.cfg.LLMTimeout)
|
||||
defer cancel()
|
||||
job, err = c.ai.WaitJob(waitCtx, job.ID, 2*time.Second)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
||||
b, _ := io.ReadAll(io.LimitReader(resp.Body, 512))
|
||||
return nil, fmt.Errorf("llm http %d: %s", resp.StatusCode, strings.TrimSpace(string(b)))
|
||||
if job.Status != "done" {
|
||||
msg := "ai-service job " + job.Status
|
||||
if job.ErrorMessage != nil && *job.ErrorMessage != "" {
|
||||
msg += ": " + *job.ErrorMessage
|
||||
}
|
||||
return nil, errors.New(msg)
|
||||
}
|
||||
|
||||
var parsed chatResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&parsed); err != nil {
|
||||
var parsed aiservice.ChatResult
|
||||
if err := json.Unmarshal(job.Result, &parsed); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(parsed.Choices) == 0 {
|
||||
return nil, errors.New("llm returned no choices")
|
||||
}
|
||||
|
||||
raw := strings.TrimSpace(parsed.Choices[0].Message.Content)
|
||||
raw := strings.TrimSpace(parsed.Content)
|
||||
if raw == "" {
|
||||
return nil, errors.New("llm returned empty content")
|
||||
}
|
||||
@@ -425,14 +412,14 @@ func loadConfig() config {
|
||||
PostgresHost: env("POSTGRES_HOST", "db"),
|
||||
PostgresPort: envInt("POSTGRES_PORT", 5432),
|
||||
LLMEnabled: envBool("LLM_ENABLED", true),
|
||||
LLMBaseURL: env("LLM_BASE_URL", "http://10.2.3.5:8002"),
|
||||
LLMAPIKey: env("LLM_API_KEY", ""),
|
||||
LLMModel: env("LLM_MODEL", "qwen2.5-14b"),
|
||||
LLMTimeout: time.Duration(envInt("LLM_TIMEOUT_SECONDS", 120)) * time.Second,
|
||||
LLMMaxTokens: envInt("LLM_MAX_TOKENS", 600),
|
||||
LLMMinTextLength: envInt("LLM_MIN_TEXT_LENGTH", 20),
|
||||
ClassifyInterval: time.Duration(envInt("LLM_CLASSIFY_INTERVAL_SECONDS", 20)) * time.Second,
|
||||
ClassifyBatchSize: envInt("LLM_CLASSIFY_BATCH_SIZE", 5),
|
||||
AIServiceURL: env("AI_SERVICE_URL", ""),
|
||||
AIServiceToken: env("AI_SERVICE_TOKEN", ""),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -20,6 +20,8 @@ import (
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
"monitoring-tg/internal/aiservice"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/minio/minio-go/v7"
|
||||
@@ -43,10 +45,10 @@ type config struct {
|
||||
PostgresPort int
|
||||
PollIntervalSeconds int
|
||||
LLMEnabled bool
|
||||
LLMBaseURL string
|
||||
LLMAPIKey string
|
||||
LLMModel string
|
||||
LLMTimeout time.Duration
|
||||
AIServiceURL string
|
||||
AIServiceToken string
|
||||
MinioEndpoint string
|
||||
MinioAccessKey string
|
||||
MinioSecretKey string
|
||||
@@ -62,6 +64,7 @@ type app struct {
|
||||
http *http.Client
|
||||
python *http.Client
|
||||
minio *minio.Client
|
||||
ai *aiservice.Client
|
||||
}
|
||||
|
||||
type accessScope struct {
|
||||
@@ -149,6 +152,7 @@ func main() {
|
||||
http: &http.Client{Timeout: cfg.LLMTimeout},
|
||||
python: &http.Client{Timeout: 15 * time.Minute},
|
||||
minio: minioClient,
|
||||
ai: aiservice.New(cfg.AIServiceURL, cfg.AIServiceToken, cfg.LLMTimeout),
|
||||
}
|
||||
|
||||
server := &http.Server{
|
||||
@@ -1118,24 +1122,32 @@ func (a *app) handleStats(ctx context.Context, w http.ResponseWriter, r *http.Re
|
||||
|
||||
func (a *app) handleLLMStatus(ctx context.Context, w http.ResponseWriter) {
|
||||
ready := false
|
||||
var providerError string
|
||||
model := a.cfg.LLMModel
|
||||
if a.cfg.LLMEnabled {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, strings.TrimRight(a.cfg.LLMBaseURL, "/")+"/v1/models", nil)
|
||||
if err == nil {
|
||||
if a.cfg.LLMAPIKey != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+a.cfg.LLMAPIKey)
|
||||
}
|
||||
resp, err := a.http.Do(req)
|
||||
if err == nil {
|
||||
ready = resp.StatusCode >= 200 && resp.StatusCode < 300
|
||||
_ = resp.Body.Close()
|
||||
status, err := a.ai.ProvidersStatus(ctx)
|
||||
if err != nil {
|
||||
providerError = err.Error()
|
||||
} else {
|
||||
for _, provider := range status.Providers {
|
||||
if provider.Name == "llm" {
|
||||
ready = provider.Configured && provider.OK
|
||||
providerError = provider.Error
|
||||
if provider.Model != "" {
|
||||
model = provider.Model
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
writeJSON(w, http.StatusOK, map[string]any{
|
||||
"enabled": a.cfg.LLMEnabled,
|
||||
"ready": ready,
|
||||
"base_url": a.cfg.LLMBaseURL,
|
||||
"model": a.cfg.LLMModel,
|
||||
"enabled": a.cfg.LLMEnabled,
|
||||
"ready": ready,
|
||||
"base_url": a.cfg.AIServiceURL,
|
||||
"model": model,
|
||||
"provider": "ai-service",
|
||||
"provider_error": providerError,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1777,10 +1789,10 @@ func loadConfig() config {
|
||||
PostgresPort: envInt("POSTGRES_PORT", 5432),
|
||||
PollIntervalSeconds: envInt("POLL_INTERVAL_SECONDS", 60),
|
||||
LLMEnabled: envBool("LLM_ENABLED", true),
|
||||
LLMBaseURL: env("LLM_BASE_URL", "http://10.2.3.5:8002"),
|
||||
LLMAPIKey: env("LLM_API_KEY", ""),
|
||||
LLMModel: env("LLM_MODEL", "qwen2.5-14b"),
|
||||
LLMTimeout: time.Duration(envInt("LLM_TIMEOUT_SECONDS", 120)) * time.Second,
|
||||
AIServiceURL: env("AI_SERVICE_URL", ""),
|
||||
AIServiceToken: env("AI_SERVICE_TOKEN", ""),
|
||||
MinioEndpoint: env("MINIO_ENDPOINT", ""),
|
||||
MinioAccessKey: env("MINIO_ACCESS_KEY", ""),
|
||||
MinioSecretKey: env("MINIO_SECRET_KEY", ""),
|
||||
|
||||
Reference in New Issue
Block a user