-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathme_notebook.go
More file actions
70 lines (66 loc) · 2.18 KB
/
me_notebook.go
File metadata and controls
70 lines (66 loc) · 2.18 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
package httpserver
import (
"encoding/json"
"net/http"
"github.com/lextures/lextures/server/internal/apierr"
"github.com/lextures/lextures/server/internal/service/notebookrag"
)
type notebookRagJSON struct {
Question string `json:"question"`
Notebooks []struct {
CourseCode string `json:"courseCode"`
CourseTitle string `json:"courseTitle"`
Markdown string `json:"markdown"`
} `json:"notebooks"`
}
func (d Deps) handleNotebookQuery() http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
w.Header().Set("Allow", http.MethodPost)
http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
return
}
if d.openRouterClient() == nil {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
apierr.WriteJSON(w, http.StatusServiceUnavailable, apierr.CodeAiNotConfigured, "AI features are not configured on this server.")
return
}
var body notebookRagJSON
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
apierr.WriteJSON(w, http.StatusBadRequest, apierr.CodeInvalidInput, "Invalid JSON body.")
return
}
userID, ok := d.meUserID(w, r)
if !ok {
return
}
docs := make([]notebookrag.DocInput, 0, len(body.Notebooks))
for _, n := range body.Notebooks {
docs = append(docs, notebookrag.DocInput{
CourseCode: n.CourseCode,
CourseTitle: n.CourseTitle,
Markdown: n.Markdown,
})
}
docs = notebookrag.FilterDocs(docs)
resp, err := notebookrag.Answer(r.Context(), d.Pool, d.openRouterClient(), userID, body.Question, docs)
if err != nil {
if notebookrag.IsValidationError(err) {
apierr.WriteJSON(w, http.StatusBadRequest, apierr.CodeInvalidInput, err.Error())
return
}
if notebookrag.IsGenerationError(err) {
msg := err.Error()
if len(msg) > 800 {
msg = msg[:800]
}
apierr.WriteJSON(w, http.StatusBadGateway, apierr.CodeAiGenerationFailed, msg)
return
}
apierr.WriteJSON(w, http.StatusInternalServerError, apierr.CodeInternal, "Could not complete notebook query.")
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
_ = json.NewEncoder(w).Encode(resp)
}
}