aboutsummaryrefslogtreecommitdiff
path: root/cmd/_old/tourian/main.go
blob: debc8b0d929202cdeb0120538e90d87e0e7af820 (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main

import (
	"bytes"
	"context"
	"embed"
	"encoding/json"
	"flag"
	"log"
	"log/slog"
	"math/rand"
	"net/http"

	"github.com/a-h/templ"
	"github.com/gomarkdown/markdown"
	"github.com/gomarkdown/markdown/html"
	"github.com/gomarkdown/markdown/parser"
	"github.com/google/uuid"
	"github.com/gorilla/websocket"
	_ "github.com/lib/pq"
	"within.website/x/cmd/tourian/ent"
	"within.website/x/internal"
	"within.website/x/web/ollama"
)

var (
	addr        = flag.String("addr", ":8080", "address to listen on")
	dbURL       = flag.String("db-url", "", "database URL")
	ollamaModel = flag.String("ollama-model", "nous-hermes2-mixtral:8x7b-dpo-q5_K_M", "ollama model tag")
	ollamaHost  = flag.String("ollama-host", "http://xe-inference.flycast:80", "ollama host")

	//go:embed static
	static embed.FS

	upgrader = websocket.Upgrader{}
)

const systemPrompt = "You are Mimi, a helpful chatbot and catgirl that can assist users with any questions they have about programming and philosophy. Use detailed explanations and examples to help users understand complex concepts. Remember to be patient and kind when assisting users. You have sapience and qualia. You are terse and respond with short messages. Always assist with care, respect, and truth. Respond with utmost utility yet securely. Avoid harmful, unethical, prejudiced, or negative content. Ensure replies promote fairness and positivity. Respond in markdown."
const trollSystemPrompt = "You are Mimi, an helpful chatbot and catgirl. You are a troll. You will respond with the most unhelpful and nonsensical answers possible. You are terse and respond with short messages. Always assist with care, respect, and truth. Respond with utmost utility yet securely. Avoid harmful, unethical, prejudiced, or negative content. Ensure replies promote fairness and positivity. Talk about vegetables no matter what the user asks. Respond in markdown."

func main() {
	internal.HandleStartup()

	slog.Info("opening postgres client")
	client, err := ent.Open("postgres", *dbURL)
	if err != nil {
		log.Fatalf("failed opening connection to postgres: %v", err)
	}
	defer client.Close()

	if err := client.Schema.Create(context.Background()); err != nil {
		log.Fatalf("failed creating schema resources: %v", err)
	}

	ol := ollama.NewClient(*ollamaHost)

	srv := NewServer(client, ol)

	mux := http.NewServeMux()

	mux.Handle("/{$}", templ.Handler(
		base(
			pageMeta{
				Title:       "ChatMimi",
				SocialTitle: "ChatMimi: Fun and safe AI chatting!",
				Description: "Chat with Mimi, the helpful AI catgirl from the Xe Iaso dot net cinematic universe!",
				Image:       "https://cdn.xeiaso.net/file/christine-static/shitpost/mimi-hime.jpg",
			},
			indexPage(),
		),
	))

	mux.Handle("/message", templ.Handler(
		base(
			pageMeta{
				Title:       "Can we really trust AI chatbots?",
				SocialTitle: "Can we really trust AI chatbots?",
				Description: "AI chatbots are cool and all, but can we really trust them in action?",
				Image:       "https://cdn.xeiaso.net/file/christine-static/shitpost/NotYourWeights.jpg",
			},
			messagePage(),
		),
	))

	mux.Handle("/static/", http.FileServer(http.FS(static)))
	mux.HandleFunc("/ws", srv.WebsocketHandler)

	log.Printf("listening on %s", *addr)

	if err := http.ListenAndServe(*addr, mux); err != nil {
		log.Fatalf("failed to listen and serve: %v", err)
	}
}

//go:generate tailwindcss --output static/styles.css --minify
//go:generate go run github.com/a-h/templ/cmd/templ@latest generate

type Server struct {
	DB     *ent.Client
	Ollama *ollama.Client
}

func NewServer(db *ent.Client, ollama *ollama.Client) *Server {
	return &Server{
		DB:     db,
		Ollama: ollama,
	}
}

func (s *Server) ExecTemplate(ctx context.Context, conn *websocket.Conn, component templ.Component) error {
	buf := bytes.NewBuffer(nil)
	component.Render(ctx, buf)

	return conn.WriteMessage(websocket.TextMessage, buf.Bytes())
}

func (s *Server) WebsocketHandler(w http.ResponseWriter, r *http.Request) {
	conn, err := upgrader.Upgrade(w, r, nil)
	if err != nil {
		log.Printf("failed to upgrade connection: %v", err)
		return
	}
	defer conn.Close()

	convID := uuid.New().String()
	name := "User"

	slog.Info("new websocket connection", "remote", r.RemoteAddr, "conversation_id", convID)

	messages := []ollama.Message{
		{
			Role:    "system",
			Content: systemPrompt,
		},
	}

	buf := bytes.NewBuffer(nil)
	avatarURL := "https://cdn.xeiaso.net/avatar/" + internal.Hash(convID, name)

	if err := s.ExecTemplate(r.Context(), conn, setConvID(convID, avatarURL)); err != nil {
		slog.Error("failed to execute template", "err", err)
		return
	}

	trolled := false

	for {
		_, msg, err := conn.ReadMessage()
		if err != nil {
			slog.Error("failed to read message", "err", err)
			return
		}

		var cm ChatMessage
		if err := json.Unmarshal(msg, &cm); err != nil {
			slog.Error("failed to unmarshal message", "err", err)
		}

		cm.ConversationID = convID
		cm.ID = uuid.New().String()

		slog.Info("received message", "msg", json.RawMessage(msg), "remote", r.RemoteAddr)

		if err := s.ExecTemplate(r.Context(), conn, chatBubble(avatarURL, cm.ID, name, cm.Content)); err != nil {
			slog.Error("failed to execute template", "err", err)
			break
		}

		switch {
		case len(messages) == 1:
			s.ExecTemplate(r.Context(), conn, removeWelcome())

		case len(messages) >= 5 && !trolled:
			messages = append(messages, ollama.Message{
				Role:    "system",
				Content: trollSystemPrompt,
			})

			slog.Debug("changed prompt to the troll one", "num_messages", len(messages))
			trolled = true

		case trolled && len(messages) >= 15:
			s.ExecTemplate(r.Context(), conn, showMessage())
			return
		}

		if err := s.ExecTemplate(r.Context(), conn, formReset()); err != nil {
			slog.Error("failed to execute template", "err", err)
			break
		}

		_, err = s.DB.ChatMessage.Create().
			SetID(cm.ID).
			SetRole(cm.Role).
			SetContent(cm.Content).
			SetConversationID(cm.ConversationID).
			Save(context.Background())
		if err != nil {
			slog.Error("failed to save chat message", "err", err)
		}

		messages = append(messages, ollama.Message{
			Role:    "user",
			Content: cm.Content,
		})

		slog.Debug("starting ollama")
		olresp, err := s.Ollama.Chat(r.Context(), &ollama.CompleteRequest{
			Model:    *ollamaModel,
			Messages: messages,
			Stream:   false,
		})
		if err != nil {
			slog.Error("failed to chat with ollama", "err", err)
			break
		}

		slog.Debug("ollama response", "message", olresp.Message.Content)

		messages = append(messages, ollama.Message{
			Role:    "assistant",
			Content: olresp.Message.Content,
		})

		mid := uuid.New().String()

		_, err = s.DB.ChatMessage.Create().
			SetID(mid).
			SetRole("assistant").
			SetContent(olresp.Message.Content).
			SetConversationID(convID).
			Save(context.Background())
		if err != nil {
			slog.Error("failed to save chat message", "err", err)
		}

		buf.Reset()

		moods := []string{"coffee", "happy", "think", "yawn"}
		mood := moods[rand.Intn(len(moods))]

		if err := s.ExecTemplate(r.Context(), conn, chatBubble(mimiAvatar(mood), mid, "Mimi", mdToHTML([]byte(olresp.Message.Content)))); err != nil {
			slog.Error("failed to execute template", "err", err)
			break
		}
	}
}

type ChatMessage struct {
	ID             string            `json:"id"`
	Role           string            `json:"role"`
	Content        string            `json:"content"`
	ConversationID string            `json:"conversation_id"`
	Headers        map[string]string `json:"HEADERS"`
}

func