Compare commits
No commits in common. "6328ff0f188a33bde9fd4b167a426a77e94bf604" and "fa5f399dfb8668d3cbe37573eecf5a6fd260a9d0" have entirely different histories.
6328ff0f18
...
fa5f399dfb
|
@ -193,8 +193,8 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
|||
logger.Warnw("failed to cache message", "error", err)
|
||||
}
|
||||
|
||||
nBytes := 0 // Used to estimated number of tokens. For now we treat 3 bytes as 1 token.
|
||||
nBytesMax := 16384 * 3 // Leave some space for the response
|
||||
nBytes := 0 // Used to estimated number of tokens. For now we treat 3 bytes as 1 token.
|
||||
nBytesMax := (4096 - 512) * 3 // Leave some space for the response
|
||||
|
||||
sysMsg := prompts.Assistant()
|
||||
chatReqMsgs := []openai.ChatMessage{
|
||||
|
@ -237,6 +237,7 @@ func handleAssistantConversation(c tele.Context, thread []*tele.Message) error {
|
|||
Model: openai.ModelGpt4O,
|
||||
Messages: chatReqMsgs,
|
||||
Temperature: lo.ToPtr(0.42),
|
||||
MaxTokens: 2048,
|
||||
User: assistantHashUserId(lastMsg.Sender.ID),
|
||||
}
|
||||
|
||||
|
|
1
bot.go
1
bot.go
|
@ -38,7 +38,6 @@ 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)
|
||||
|
|
|
@ -1,65 +0,0 @@
|
|||
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
|
||||
}
|
12
go.mod
12
go.mod
|
@ -11,10 +11,10 @@ require (
|
|||
github.com/eko/gocache/lib/v4 v4.2.0
|
||||
github.com/eko/gocache/store/ristretto/v4 v4.2.2
|
||||
github.com/go-errors/errors v1.5.1
|
||||
github.com/go-resty/resty/v2 v2.16.5
|
||||
github.com/goccy/go-json v0.10.5
|
||||
github.com/go-resty/resty/v2 v2.16.3
|
||||
github.com/goccy/go-json v0.10.4
|
||||
github.com/ilyakaznacheev/cleanenv v1.5.0
|
||||
github.com/samber/lo v1.49.1
|
||||
github.com/samber/lo v1.47.0
|
||||
go.uber.org/zap v1.27.0
|
||||
golang.org/x/net v0.34.0
|
||||
gopkg.in/telebot.v3 v3.3.8
|
||||
|
@ -32,15 +32,15 @@ require (
|
|||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/prometheus/client_golang v1.20.5 // indirect
|
||||
github.com/prometheus/client_model v0.6.1 // indirect
|
||||
github.com/prometheus/common v0.62.0 // indirect
|
||||
github.com/prometheus/common v0.61.0 // indirect
|
||||
github.com/prometheus/procfs v0.15.1 // indirect
|
||||
go.uber.org/mock v0.5.0 // indirect
|
||||
go.uber.org/multierr v1.11.0 // indirect
|
||||
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c // indirect
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 // indirect
|
||||
golang.org/x/sync v0.10.0 // indirect
|
||||
golang.org/x/sys v0.29.0 // indirect
|
||||
golang.org/x/text v0.21.0 // indirect
|
||||
google.golang.org/protobuf v1.36.4 // indirect
|
||||
google.golang.org/protobuf v1.36.2 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
olympos.io/encoding/edn v0.0.0-20201019073823-d3554ca0b0a3 // indirect
|
||||
)
|
||||
|
|
12
go.sum
12
go.sum
|
@ -157,13 +157,9 @@ github.com/go-playground/universal-translator v0.17.0/go.mod h1:UkSxE5sNxxRwHyU+
|
|||
github.com/go-playground/validator/v10 v10.4.1/go.mod h1:nlOn6nFhuKACm19sB/8EGNn9GlaMV7XkbRSipzJ0Ii4=
|
||||
github.com/go-resty/resty/v2 v2.16.3 h1:zacNT7lt4b8M/io2Ahj6yPypL7bqx9n1iprfQuodV+E=
|
||||
github.com/go-resty/resty/v2 v2.16.3/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
||||
github.com/go-resty/resty/v2 v2.16.5 h1:hBKqmWrr7uRc3euHVqmh1HTHcKn99Smr7o5spptdhTM=
|
||||
github.com/go-resty/resty/v2 v2.16.5/go.mod h1:hkJtXbA2iKHzJheXYvQ8snQES5ZLGKMwQ07xAwp/fiA=
|
||||
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
|
||||
github.com/goccy/go-json v0.10.4 h1:JSwxQzIqKfmFX1swYPpUThQZp/Ka4wzJdK0LWVytLPM=
|
||||
github.com/goccy/go-json v0.10.4/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/goccy/go-yaml v1.9.5/go.mod h1:U/jl18uSupI5rdI2jmuCswEA2htH9eXfferR3KfscvA=
|
||||
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
|
||||
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
|
||||
|
@ -379,8 +375,6 @@ github.com/prometheus/common v0.10.0/go.mod h1:Tlit/dnDKsSWFlCLTWaA1cyBgKHSMdTB8
|
|||
github.com/prometheus/common v0.26.0/go.mod h1:M7rCNAaPfAosfx8veZJCuw84e35h3Cfd9VFqTh1DIvc=
|
||||
github.com/prometheus/common v0.61.0 h1:3gv/GThfX0cV2lpO7gkTUwZru38mxevy90Bj8YFSRQQ=
|
||||
github.com/prometheus/common v0.61.0/go.mod h1:zr29OCN/2BsJRaFwG8QOBr41D6kkchKbpeNH7pAjb/s=
|
||||
github.com/prometheus/common v0.62.0 h1:xasJaQlnWAeyHdUBeGjXmutelfJHWMRr+Fg4QszZ2Io=
|
||||
github.com/prometheus/common v0.62.0/go.mod h1:vyBcEuLSvWos9B1+CyL7JZ2up+uFzXhkqml0W5zIY1I=
|
||||
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
|
||||
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
|
||||
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
|
||||
|
@ -397,8 +391,6 @@ github.com/ryanuber/columnize v0.0.0-20160712163229-9b3edd62028f/go.mod h1:sm1tb
|
|||
github.com/sagikazarmark/crypt v0.6.0/go.mod h1:U8+INwJo3nBv1m6A/8OBXAq7Jnpspk5AxSgDyEQcea8=
|
||||
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
|
||||
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
|
||||
github.com/samber/lo v1.49.1 h1:4BIFyVfuQSEpluc7Fua+j1NolZHiEHEpaSEKdsH0tew=
|
||||
github.com/samber/lo v1.49.1/go.mod h1:dO6KHFzUKXgP8LDhU0oI8d2hekjXnGOu0DB8Jecxd6o=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
|
||||
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
|
||||
|
@ -481,8 +473,6 @@ golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EH
|
|||
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8 h1:yqrTHse8TCMW1M1ZCP+VAR/l0kKxwaAIqN/il7x4voA=
|
||||
golang.org/x/exp v0.0.0-20250106191152-7588d65b2ba8/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
|
||||
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c h1:KL/ZBHXgKGVmuZBZ01Lt57yE5ws8ZPSkkihmEyq7FXc=
|
||||
golang.org/x/exp v0.0.0-20250128182459-e0ece0dbea4c/go.mod h1:tujkw807nyEEAamNbDrEGzRav+ilXA7PCRAd6xsmwiU=
|
||||
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
|
||||
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
|
||||
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
|
||||
|
@ -961,8 +951,6 @@ google.golang.org/protobuf v1.27.1/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQ
|
|||
google.golang.org/protobuf v1.28.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
|
||||
google.golang.org/protobuf v1.36.2 h1:R8FeyR1/eLmkutZOM5CWghmo5itiG9z0ktFlTVLuTmU=
|
||||
google.golang.org/protobuf v1.36.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
google.golang.org/protobuf v1.36.4 h1:6A3ZDJHn/eNqc1i+IdefRzy/9PokBTPvcqMySR7NNIM=
|
||||
google.golang.org/protobuf v1.36.4/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
|
||||
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
|
|
1
main.go
1
main.go
|
@ -41,7 +41,6 @@ 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"},
|
||||
|
|
|
@ -6,40 +6,28 @@ type ChatRole string
|
|||
|
||||
const (
|
||||
ChatRoleSystem ChatRole = "system"
|
||||
ChatRoleDeveloper ChatRole = "developer" // replaces `system` role for o1 and newer models
|
||||
ChatRoleTool ChatRole = "tool"
|
||||
ChatRoleAssistant ChatRole = "assistant"
|
||||
ChatRoleUser ChatRole = "user"
|
||||
)
|
||||
|
||||
type ReasoningEffort string
|
||||
|
||||
const (
|
||||
ReasoningEffortLow ReasoningEffort = "low"
|
||||
ReasoningEffortMedium ReasoningEffort = "medium"
|
||||
ReasoningEffortHigh ReasoningEffort = "high"
|
||||
)
|
||||
|
||||
type ChatMessage struct {
|
||||
Role ChatRole `json:"role"`
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
type ChatRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []ChatMessage `json:"messages"`
|
||||
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"` // Deprecated: in favor of `max_completion_tokens`
|
||||
MaxCompletionTokens int `json:"max_completion_tokens,omitempty"` // Including visible output tokens and reasoning tokens.
|
||||
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.
|
||||
ReasoningEffort ReasoningEffort `json:"reasoning_effort,omitempty"` // Constrains effort on reasoning for reasoning models.
|
||||
Model string `json:"model"`
|
||||
Messages []ChatMessage `json:"messages"`
|
||||
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"` // 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 {
|
||||
|
|
|
@ -20,7 +20,7 @@ func NewClient(apiKey string) *Client {
|
|||
cli := resty.New().
|
||||
SetTransport(&http.Transport{
|
||||
Proxy: http.ProxyFromEnvironment,
|
||||
ResponseHeaderTimeout: 90 * time.Second,
|
||||
ResponseHeaderTimeout: 10 * time.Second,
|
||||
}).
|
||||
SetBaseURL("https://api.openai.com").
|
||||
SetHeader("Authorization", "Bearer "+apiKey).
|
||||
|
|
|
@ -2,7 +2,6 @@ package openai
|
|||
|
||||
const (
|
||||
ModelGpt4O = "gpt-4o" // Safe default
|
||||
ModelO1Preview = "o1-preview" // Expensive reasoning model
|
||||
ModelO1Mini = "o1-mini" // Cheaper reasoning model
|
||||
ModelO3Mini = "o3-mini" // Cheaper yet powerful reasoning model
|
||||
ModelO1Preview = "o1-preview" // Expensive
|
||||
ModelO1Mini = "o1-mini"
|
||||
)
|
||||
|
|
Loading…
Reference in New Issue