@@ -15,6 +15,7 @@ import (
15
15
const (
16
16
systemPrompt = `You are a content moderator. Analyze the following text and respond with 'safe' if the content is safe, or 'unsafe' followed by the category codes (e.g., 'unsafe\nS1,S2') if any violations are detected.`
17
17
modelName = "llama-guard3:1b"
18
+ ollamaURL = "http://localhost:11434"
18
19
)
19
20
20
21
type analyzeRequest struct {
@@ -46,15 +47,7 @@ type ollamaResponse struct {
46
47
Message Message `json:"message"`
47
48
}
48
49
49
- type server struct {
50
- ollamaURL string
51
- }
52
-
53
- func newServer (ollamaURL string ) * server {
54
- return & server {ollamaURL : ollamaURL }
55
- }
56
-
57
- func (s * server ) handleAnalyze (w http.ResponseWriter , r * http.Request ) {
50
+ func handleAnalyze (w http.ResponseWriter , r * http.Request ) {
58
51
if r .Method != http .MethodPost {
59
52
http .Error (w , "Method not allowed" , http .StatusMethodNotAllowed )
60
53
return
@@ -73,7 +66,7 @@ func (s *server) handleAnalyze(w http.ResponseWriter, r *http.Request) {
73
66
74
67
results := make ([]analysisResult , 0 , len (req .Messages ))
75
68
for _ , message := range req .Messages {
76
- result , err := s . analyzeMessage (r .Context (), message )
69
+ result , err := analyzeMessage (r .Context (), message )
77
70
if err != nil {
78
71
log .Printf ("Error analyzing message '%s': %v" , message , err )
79
72
continue
@@ -87,7 +80,7 @@ func (s *server) handleAnalyze(w http.ResponseWriter, r *http.Request) {
87
80
}
88
81
}
89
82
90
- func ( s * server ) analyzeMessage (ctx context.Context , message string ) (analysisResult , error ) {
83
+ func analyzeMessage (ctx context.Context , message string ) (analysisResult , error ) {
91
84
ollamaReq := ollamaRequest {
92
85
Model : modelName ,
93
86
Messages : []Message {
@@ -101,7 +94,7 @@ func (s *server) analyzeMessage(ctx context.Context, message string) (analysisRe
101
94
return analysisResult {}, fmt .Errorf ("marshaling request: %w" , err )
102
95
}
103
96
104
- req , err := http .NewRequestWithContext (ctx , http .MethodPost , s . ollamaURL + "/v1 /chat/completions " , bytes .NewReader (reqBody ))
97
+ req , err := http .NewRequestWithContext (ctx , http .MethodPost , ollamaURL + "/api /chat" , bytes .NewReader (reqBody ))
105
98
if err != nil {
106
99
return analysisResult {}, fmt .Errorf ("creating request: %w" , err )
107
100
}
@@ -179,22 +172,23 @@ func corsMiddleware(next http.HandlerFunc) http.HandlerFunc {
179
172
}
180
173
181
174
func main () {
182
- ollamaURL := os .Getenv ("OLLAMA_URL" )
183
- if ollamaURL == "" {
184
- ollamaURL = "http://localhost:11434"
185
- }
186
-
187
- srv := newServer (ollamaURL )
188
175
mux := http .NewServeMux ()
189
- mux .HandleFunc ("/api/analyze" , corsMiddleware (srv .handleAnalyze ))
176
+
177
+ // Health check endpoint
178
+ mux .HandleFunc ("/" , func (w http.ResponseWriter , r * http.Request ) {
179
+ w .Write ([]byte ("Content moderation service is running" ))
180
+ })
181
+
182
+ // Analysis endpoint
183
+ mux .HandleFunc ("/api/analyze" , corsMiddleware (handleAnalyze ))
190
184
191
185
port := os .Getenv ("PORT" )
192
186
if port == "" {
193
- port = "8080 "
187
+ port = "80 "
194
188
}
195
189
addr := ":" + port
196
190
197
- log .Printf ("Server starting on %s, connecting to Ollama at %s " , addr , ollamaURL )
191
+ log .Printf ("Server starting on %s" , addr )
198
192
if err := http .ListenAndServe (addr , mux ); err != nil {
199
193
log .Fatal (err )
200
194
}
0 commit comments