-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.go
More file actions
110 lines (91 loc) · 2.92 KB
/
web.go
File metadata and controls
110 lines (91 loc) · 2.92 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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"os/exec"
"strconv"
"time"
"github.com/fatih/color"
"github.com/gorilla/mux"
"github.com/spf13/cobra"
)
var WebCmd = &cobra.Command{
Use: "web",
Short: "Start a web server with chat interface and API",
Long: `Starts a web server that provides both a web interface and REST API for AI interactions.
The server runs on port 8080 by default (configurable via PORT environment variable).
Available endpoints:
- Web UI: http://localhost:8080/
- API: POST to http://localhost:8080/answer with JSON body {"message": "your question", "history": {}}`,
Run: executeWebCommand,
}
func executeWebCommand(cmd *cobra.Command, args []string) {
port := getPort()
r := mux.NewRouter()
// Open browser after a short delay to ensure server is running
go func() {
time.Sleep(500 * time.Millisecond)
url := fmt.Sprintf("http://localhost:%d", port)
log.Printf("Opening %s in your browser", url)
if err := exec.Command("open", url).Run(); err != nil {
// Try with xdg-open for Linux
if err := exec.Command("xdg-open", url).Run(); err != nil {
// Try with start for Windows
if err := exec.Command("cmd", "/c", "start", url).Run(); err != nil {
log.Printf("Could not open browser: %v", err)
}
}
}
}()
r.HandleFunc("/answer", answerHandler).Methods("POST")
webFS := getEmbeddedWebFS()
r.PathPrefix("/").Handler(http.FileServer(http.FS(webFS)))
log.Printf("Starting web server on port %d\n", port)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%d", port), r))
}
func answerHandler(w http.ResponseWriter, r *http.Request) {
var requestBody struct {
Message string `json:"message"`
History map[string]string `json:"history"`
}
err := json.NewDecoder(r.Body).Decode(&requestBody)
if err != nil {
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "application/json")
var formattedHistory string
if len(requestBody.History) > 0 {
formattedHistory = "Previous conversation:\n"
for question, answer := range requestBody.History {
formattedHistory += "Question: " + question + "\nAnswer: " + answer + "\n\n"
}
}
query := fmt.Sprintf("You are an AI assistant. %sNew question: %s",
formattedHistory,
requestBody.Message)
os.Setenv("SKIP_SYS_INFO", "true")
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
os.Setenv("SKIP_SYS_INFO", "true")
ai := AskQuery(query, nil)
os.Setenv("SKIP_SYS_INFO", "")
response := `{"message": " ` + ai.Response + `"}`
w.Write([]byte(response))
}
func getPort() int {
portStr := os.Getenv("GENAI_PORT")
if portStr == "" {
color.New(color.FgYellow).Println("PORT environment variable not set, defaulting to 8080")
return 8080
}
port, err := strconv.Atoi(portStr)
if err != nil {
color.New(color.FgRed).Printf("Invalid PORT environment variable: %v, defaulting to 8080\n", err)
return 8080
}
return port
}