feat: add reasoning command
This commit is contained in:
		
							parent
							
								
									4ab359053a
								
							
						
					
					
						commit
						6328ff0f18
					
				
							
								
								
									
										1
									
								
								bot.go
								
								
								
								
							
							
						
						
									
										1
									
								
								bot.go
								
								
								
								
							| 
						 | 
				
			
			@ -38,6 +38,7 @@ func initBot() (*tele.Bot, error) {
 | 
			
		|||
	b.Handle("/year_progress", handleYearProgressCmd)
 | 
			
		||||
	b.Handle("/xr", handleExchangeRateCmd)
 | 
			
		||||
 | 
			
		||||
	b.Handle("/reason", handleReasonCmd)
 | 
			
		||||
	b.Handle("/tr", handleTranslateCmd)
 | 
			
		||||
	for _, tbtn := range translateBtns {
 | 
			
		||||
		b.Handle(tbtn, handleTranslateBtn)
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,65 @@
 | 
			
		|||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"regexp"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	tele "gopkg.in/telebot.v3"
 | 
			
		||||
 | 
			
		||||
	"git.gensokyo.cafe/kkyy/tgbot_misaka_5882f7/openai"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
var (
 | 
			
		||||
	reasonCmdRe               = regexp.MustCompile(`^\s*\/reason(@\S*)?\s*`)
 | 
			
		||||
	reasoningIndicatorMessage = "🤔💭 Thinking..."
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
func handleReasonCmd(c tele.Context) error {
 | 
			
		||||
	msg := c.Message()
 | 
			
		||||
	if msg == nil {
 | 
			
		||||
		return nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	payload := strings.TrimSpace(reasonCmdRe.ReplaceAllString(msg.Text, ""))
 | 
			
		||||
	if payload == "" {
 | 
			
		||||
		return c.Reply("Usage: `/reason <text>`",
 | 
			
		||||
			&tele.SendOptions{ParseMode: tele.ModeMarkdown},
 | 
			
		||||
			tele.Silent,
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	req := openai.ChatRequest{
 | 
			
		||||
		Model: openai.ModelO1Mini,
 | 
			
		||||
		Messages: []openai.ChatMessage{
 | 
			
		||||
			{
 | 
			
		||||
				Role:    openai.ChatRoleUser,
 | 
			
		||||
				Content: payload,
 | 
			
		||||
			},
 | 
			
		||||
		},
 | 
			
		||||
		// reasoning_effort is only available to `o1` and `o3-mini`, which is not yet accessible.
 | 
			
		||||
		// ReasoningEffort: openai.ReasoningEffortHigh,
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	replyMsg, err := c.Bot().Reply(msg, reasoningIndicatorMessage, tele.Silent)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Errorw("assistant: failed to complete reasoning request", "error", err)
 | 
			
		||||
		return c.Reply("Sorry, there's a technical issue. 😵💫 Please try again later.", tele.Silent)
 | 
			
		||||
	}
 | 
			
		||||
	err = assistantStreamedResponse(req, func(text string, finished bool) error {
 | 
			
		||||
		var err error
 | 
			
		||||
		replyMsg, err = c.Bot().Edit(replyMsg, text)
 | 
			
		||||
		if finished && err == nil {
 | 
			
		||||
			replyMsg.ReplyTo = msg // nasty bug
 | 
			
		||||
			if err := cacheMessage(replyMsg); err != nil {
 | 
			
		||||
				logger.Warnw("failed to cache message", "error", err)
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return err
 | 
			
		||||
	})
 | 
			
		||||
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		logger.Errorw("assistant: failed to complete reasoning request", "error", err)
 | 
			
		||||
		return c.Reply("Sorry, there's a technical issue. 😵💫 Please try again later.", tele.Silent)
 | 
			
		||||
	}
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										1
									
								
								main.go
								
								
								
								
							
							
						
						
									
										1
									
								
								main.go
								
								
								
								
							| 
						 | 
				
			
			@ -41,6 +41,7 @@ func runBot() {
 | 
			
		|||
		logger.Info("Announcing commands...")
 | 
			
		||||
 | 
			
		||||
		if err = bot.SetCommands([]tele.Command{
 | 
			
		||||
			{Text: "reason", Description: "Think."},
 | 
			
		||||
			{Text: "tr", Description: "Translate text"},
 | 
			
		||||
			{Text: "kanji", Description: "Help with pronunciation of Kanji"},
 | 
			
		||||
			{Text: "xr", Description: "Currency exchange rates"},
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue