From 39868f4f07924663a8ea3659b1765a006650c437 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 21 Mar 2023 12:39:49 +0800 Subject: [PATCH 1/3] feat(AI): add done channel for faster response --- assistant.go | 7 +++++-- openai/chat.go | 1 + openai/client.go | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/assistant.go b/assistant.go index 6fd09c3..0b218e0 100644 --- a/assistant.go +++ b/assistant.go @@ -134,7 +134,10 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR if minWaitSatisfied { break Drain } - <-minWait + select { + case <-minWait: + case <-resp.Done: + } minWaitSatisfied = true } } @@ -155,7 +158,7 @@ func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedR minWaitDurSecs := lo.Min([]int{nUpdates, 4}) + nErrs*3 minWait = time.After(time.Duration(minWaitDurSecs) * time.Second) - // send the partial message + // Send the partial message respText := respBuilder.String() + assistantWritingSign if err := cb(respText, false); err != nil { logger.Warnw("failed to send partial update", "error", err) diff --git a/openai/chat.go b/openai/chat.go index fc213a8..ac525df 100644 --- a/openai/chat.go +++ b/openai/chat.go @@ -51,6 +51,7 @@ type ChatResponseStream struct { Created int Model string Stream chan string + Done chan struct{} Err error } diff --git a/openai/client.go b/openai/client.go index e7abc7f..1800cf9 100644 --- a/openai/client.go +++ b/openai/client.go @@ -77,11 +77,15 @@ func (c *Client) ChatCompletionStream(request ChatRequest) (*ChatResponseStream, return nil, errors.Errorf("status code: %d, body: %q", resp.StatusCode(), respBodyStr) } - ret := &ChatResponseStream{Stream: make(chan string, 1024)} + ret := &ChatResponseStream{ + Stream: make(chan string, 1024), + Done: make(chan struct{}), + } go func() { defer func() { rbody.Close() close(ret.Stream) + close(ret.Done) }() var contentBegan bool From 816ae68aeb4ea1c14e5765c20c0c4f555599c175 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 21 Mar 2023 12:52:28 +0800 Subject: [PATCH 2/3] chore: update readme and makefile --- Makefile | 4 +++- README.md | 8 +++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index a5ee112..7073cef 100644 --- a/Makefile +++ b/Makefile @@ -1,3 +1,5 @@ +all: build + build: lint CGO_ENABLED=0 go build -v -trimpath -ldflags='-s -w' @@ -13,4 +15,4 @@ send: pack clean: rm -f tgbot_misaka_5882f7 tgbot_misaka_5882f7.* -.PHONY: build pack send clean +.PHONY: all build pack send clean test diff --git a/README.md b/README.md index 55a39da..e93a96d 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,9 @@ -`MISAKA 5882F7` +# `MISAKA 5882F7` Just another random telegram bot. + +Code is messy. Expect a lot of hiccups. + +## Known problems + +- Message caching needs improvement From b60be9ae7e5818ff01a95aad87e06ac5f57bfb03 Mon Sep 17 00:00:00 2001 From: Yiyang Kang Date: Tue, 21 Mar 2023 13:35:25 +0800 Subject: [PATCH 3/3] feat(AI): add salted user id --- assistant.go | 9 +++++++++ openai/chat.go | 18 +++++++++--------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/assistant.go b/assistant.go index 0b218e0..4b4ef18 100644 --- a/assistant.go +++ b/assistant.go @@ -1,6 +1,8 @@ package main import ( + "crypto/md5" + "encoding/base64" "strconv" "strings" "time" @@ -234,6 +236,7 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error { Messages: chatReqMsgs, Temperature: lo.ToPtr(0.42), MaxTokens: 2048, + User: assistantHashUserId(lastMsg.Sender.ID), } typingNotifyCh := setTyping(c) @@ -272,3 +275,9 @@ func assistantRemoveMention(msg, name string) string { } return orig } + +func assistantHashUserId(uid int64) string { + seasoned := []byte("RdnuRPqp66vtbc28QRO0ecKSLKXifz7G9UbXLoyCMpw" + strconv.FormatInt(uid, 10)) + hashed := md5.Sum(seasoned) // Don't judge me + return base64.URLEncoding.EncodeToString(hashed[:])[:22] +} diff --git a/openai/chat.go b/openai/chat.go index ac525df..3564ed6 100644 --- a/openai/chat.go +++ b/openai/chat.go @@ -18,16 +18,16 @@ type ChatMessage struct { type ChatRequest struct { Model string `json:"model"` Messages []ChatMessage `json:"messages"` - Temperature *float64 `json:"temperature,omitempty"` - TopP *float64 `json:"top_p,omitempty"` - N int `json:"n,omitempty"` - Stream bool `json:"stream,omitempty"` - Stop []string `json:"stop,omitempty"` + Temperature *float64 `json:"temperature,omitempty"` // What sampling temperature to use, between 0 and 2. + TopP *float64 `json:"top_p,omitempty"` // Nucleus sampling. Specify this or temperature but not both. + N int `json:"n,omitempty"` // How many chat completion choices to generate for each input message. + Stream bool `json:"stream,omitempty"` // If set, partial message deltas will be sent as data-only server-sent events as they become available. + Stop []string `json:"stop,omitempty"` // Up to 4 sequences where the API will stop generating further tokens. MaxTokens int `json:"max_tokens,omitempty"` - PresencePenalty *float64 `json:"presence_penalty,omitempty"` - FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` - LogitBias map[string]float64 `json:"logit_bias,omitempty"` - User string `json:"user,omitempty"` + PresencePenalty *float64 `json:"presence_penalty,omitempty"` // Number between -2.0 and 2.0. + FrequencyPenalty *float64 `json:"frequency_penalty,omitempty"` // Number between -2.0 and 2.0. + LogitBias map[string]float64 `json:"logit_bias,omitempty"` // Modify the likelihood of specified tokens appearing in the completion. + User string `json:"user,omitempty"` // A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse. } type ChatResponseChoice struct {