-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandlers_ai.go
More file actions
125 lines (107 loc) · 2.71 KB
/
handlers_ai.go
File metadata and controls
125 lines (107 loc) · 2.71 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"github.com/gin-gonic/gin"
)
func handleAI(c *gin.Context) {
apiURL := "https://api.cerebras.ai/v1/chat/completions"
apiKey := os.Getenv("CEREBRAS_API_KEY")
if apiKey == "" {
c.JSON(500, gin.H{"error": "Missing CEREBRAS_API_KEY in .env"})
return
}
content := c.Query("content")
user := c.DefaultQuery("user", "Guest")
returnHistory := c.Query("history") == "1"
historyData := c.Query("history_data")
model := c.DefaultQuery("model", "llama3.3-70b")
var history []map[string]any
if historyData != "" {
if err := json.Unmarshal([]byte(historyData), &history); err != nil {
c.JSON(400, gin.H{"error": "Invalid JSON in 'history_data'"})
return
}
}
if content == "" && len(history) == 0 {
c.JSON(400, gin.H{"error": "Missing 'content' or 'history_data'"})
return
}
if content != "" {
history = append(history, map[string]any{
"role": "user",
"content": content,
})
}
payload := map[string]any{
"model": model,
"messages": history,
}
data, err := json.Marshal(payload)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to marshal request payload"})
return
}
req, err := http.NewRequest("POST", apiURL, bytes.NewBuffer(data))
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
req.Header.Set("Authorization", "Bearer "+apiKey)
req.Header.Set("Content-Type", "application/json")
resp, err := http.DefaultClient.Do(req)
if err != nil {
c.JSON(500, gin.H{"error": err.Error()})
return
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
c.JSON(500, gin.H{"error": "Failed to read response body"})
return
}
if resp.StatusCode != 200 {
c.JSON(resp.StatusCode, gin.H{"error": string(body)})
return
}
var parsed map[string]any
if err := json.Unmarshal(body, &parsed); err != nil {
c.JSON(500, gin.H{"error": "Invalid JSON response from Cerebras"})
return
}
choices, ok := parsed["choices"].([]any)
if !ok || len(choices) == 0 {
c.JSON(500, gin.H{"error": "No choices returned"})
return
}
choice, ok := choices[0].(map[string]any)
if !ok {
c.JSON(500, gin.H{"error": "Invalid choice format"})
return
}
message, ok := choice["message"].(map[string]any)
if !ok {
c.JSON(500, gin.H{"error": "Invalid message format"})
return
}
msgContent, ok := message["content"].(string)
if !ok {
c.JSON(500, gin.H{"error": "Invalid content format"})
return
}
aiResponse := fmt.Sprint(msgContent)
history = append(history, map[string]any{
"role": "assistant",
"content": aiResponse,
})
fmt.Printf("APPS - USER: %s, AI: %s\n", user, aiResponse)
if returnHistory {
c.JSON(200, history)
} else {
c.String(200, aiResponse)
}
}