feat: add kanji command

main
Yiyang Kang 2023-04-21 15:43:05 +08:00
parent f6f8563aba
commit 2a8605631b
5 changed files with 96 additions and 0 deletions

View File

@ -91,6 +91,8 @@ func matchAssistantConversation(botUsr *tele.User, msg *tele.Message) []*tele.Me
type assistantStreamedResponseCb func(text string, finished bool) error
// TODO interrupt response with context.
func assistantStreamedResponse(request openai.ChatRequest, cb assistantStreamedResponseCb) error {
logger.Debugw("Openai chat request", "req", request)
ai := openai.NewClient(config.OpenAIApiKey)

1
bot.go
View File

@ -42,6 +42,7 @@ func initBot() (*tele.Bot, error) {
for _, tbtn := range translateBtns {
b.Handle(tbtn, handleTranslateBtn)
}
b.Handle("/kanji", handleKanjiCmd)
b.Handle(tele.OnText, handleGeneralMessage)
b.Handle(tele.OnSticker, handleGeneralMessage)

77
botcmd_kanji.go Normal file
View File

@ -0,0 +1,77 @@
package main
import (
"regexp"
"strings"
"github.com/samber/lo"
tele "gopkg.in/telebot.v3"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai"
"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai/prompts"
)
var kanjiRe = regexp.MustCompile(`\p{Han}`)
func handleKanjiCmd(c tele.Context) error {
msg := c.Message()
if msg == nil {
return nil
}
payload := strings.TrimSpace(msg.Payload)
if payload == "" {
return c.Reply("Usage: `/kanji <text>`", &tele.SendOptions{ParseMode: tele.ModeMarkdown}, tele.Silent)
}
if !kanjiRe.MatchString(payload) {
return c.Reply("// Your text does not contain any Kanji.")
}
req := openai.ChatRequest{
Model: openai.ModelGpt0305Turbo,
Messages: []openai.ChatMessage{
{
Role: openai.ChatRoleSystem,
Content: prompts.KanjiPronunciation(),
},
{
Role: openai.ChatRoleUser,
Content: payload,
},
{
Role: openai.ChatRoleSystem,
Content: `Reminder: Assistant should stick to the task of adding pronunciations for Kanji in the text sent by the user. If user's message seem irrelevant, just reply "IRRELEVANT"`,
},
},
Temperature: lo.ToPtr(0.2),
}
actionCh := setTyping(c)
var irrelevant = false
var replyMsg *tele.Message
err := assistantStreamedResponse(req, func(text string, finished bool) error {
<-actionCh
if strings.HasPrefix(text, `IRRELEVANT`) {
irrelevant = true
return nil // TODO interrupt response
}
var err error
if replyMsg == nil {
replyMsg, err = c.Bot().Reply(msg, text, tele.Silent)
} else {
replyMsg, err = c.Bot().Edit(replyMsg, text)
}
return err
})
if err != nil {
logger.Errorw("assistant: failed to complete conversation", "error", err)
_ = c.Reply("Sorry, there's a technical issue. 😵💫 Please try again later.", tele.Silent)
return err
}
if irrelevant {
return c.Reply("Sorry, I don't know what to do with your input.")
}
return nil
}

View File

@ -42,6 +42,7 @@ func runBot() {
if err = bot.SetCommands([]tele.Command{
{Text: "tr", Description: "Translate text"},
{Text: "kanji", Description: "Help with pronunciation of Kanji"},
{Text: "xr", Description: "Currency exchange rates"},
{Text: "year_progress", Description: "Time doesn't wait."},
{Text: "traffic", Description: "Show traffic usage."},

View File

@ -31,3 +31,18 @@ func Translate(targetLang string) string {
targetLang,
)
}
func KanjiPronunciation() string {
return strings.Join([]string{
`You are a helpful assistant. Your task is to help with pronunciation of Kanji in Japanese.`,
``,
`The user must send text in Japanese, otherwise it would be irrelevant to your task, and you must ignore them.`,
``,
`You must then reply with the exact same text as sent by the user, with pronunciations (hiragana) added for every occurrence of Kanji in the text.`,
``,
`Example`,
``,
`User: 藤原書記は、漫画『かぐや様は告らせたい~天才たちの恋愛頭脳戦~』に`,
`Assistant: 藤原(ふじわら)書記(しょき)は、漫画(まんが)『かぐや様(さま)は告(こく)らせたい~天才(てんさい)たちの恋愛(れんあい)頭脳戦(ずのうせん)~』に`,
}, "\n")
}