File size: 1,922 Bytes
c8ab7aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Python AI service contract

## Endpoint

```http
POST /ai/analyze-post
```

## Request

```json
{
  "postId": 101,
  "content": "Hom nay hoc Spring Boot Kafka kha hay"
}
```

## Response

```json
{
  "postId": 101,
  "modelName": "sentence-transformers/all-MiniLM-L6-v2",
  "dimension": 384,
  "embedding": [0.012, -0.034, 0.221],
  "moderation": {
    "status": "SAFE",
    "spamScore": 0.05,
    "toxicityScore": 0.01,
    "scamScore": 0.02
  }
}
```

## Responsibilities

Python should:

1. Receive `postId` and `content`.
2. Clean text lightly.
3. Generate embedding with `sentence-transformers/all-MiniLM-L6-v2`.
4. Calculate spam, toxicity, and scam scores.
5. Return the response above.

Python should not return or calculate:

- topic
- hashtag
- keyword
- summary

## Moderation Status

Allowed values:

- `SAFE`
- `LIMITED`
- `BLOCKED`

Scores should be normalized from `0.0` to `1.0`.

Spring Boot derives `qualityScore` from moderation scores:

```text
qualityScore = 1 - (spamScore * 0.4 + toxicityScore * 0.4 + scamScore * 0.2)
```

The value is clamped to `0.0..1.0`.

## Minimal FastAPI Shape

```python
from fastapi import FastAPI
from pydantic import BaseModel
from sentence_transformers import SentenceTransformer

app = FastAPI()
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")

class AnalyzePostRequest(BaseModel):
    postId: int
    content: str

@app.post("/ai/analyze-post")
def analyze_post(request: AnalyzePostRequest):
    content = " ".join((request.content or "").split())
    embedding = model.encode(content).tolist()
    return {
        "postId": request.postId,
        "modelName": "sentence-transformers/all-MiniLM-L6-v2",
        "dimension": len(embedding),
        "embedding": embedding,
        "moderation": {
            "status": "SAFE",
            "spamScore": 0.0,
            "toxicityScore": 0.0,
            "scamScore": 0.0,
        },
    }
```