feat: add cmd -g to select the language would reply

This commit is contained in:
csquarechen 2024-09-13 16:41:54 +08:00
parent d25ea0e88b
commit b96415d445
6 changed files with 21 additions and 8 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/common"
"github.com/jessevdk/go-flags" "github.com/jessevdk/go-flags"
"golang.org/x/text/language"
) )
// Flags create flags struct. the users flags go into this, this will be passed to the chat struct in cli // Flags create flags struct. the users flags go into this, this will be passed to the chat struct in cli
@ -39,6 +40,7 @@ type Flags struct {
YouTubeTranscript bool `long:"transcript" description:"Grab transcript from YouTube video and send to chat"` YouTubeTranscript bool `long:"transcript" description:"Grab transcript from YouTube video and send to chat"`
YouTubeComments bool `long:"comments" description:"Grab comments from YouTube video and send to chat"` YouTubeComments bool `long:"comments" description:"Grab comments from YouTube video and send to chat"`
DryRun bool `long:"dry-run" description:"Show what would be sent to the model without actually sending it"` DryRun bool `long:"dry-run" description:"Show what would be sent to the model without actually sending it"`
Language string `short:"g" long:"language" description:"Language of the chat" default:"en"`
} }
// Init Initialize flags. returns a Flags struct and an error // Init Initialize flags. returns a Flags struct and an error
@ -105,5 +107,10 @@ func (o *Flags) BuildChatRequest() (ret *common.ChatRequest) {
PatternVariables: o.PatternVariables, PatternVariables: o.PatternVariables,
Message: o.Message, Message: o.Message,
} }
langTag, err := language.Parse(o.Language)
if err != nil {
langTag = language.English
}
ret.Language = langTag.String()
return return
} }

View File

@ -11,6 +11,7 @@ type ChatRequest struct {
PatternName string PatternName string
PatternVariables map[string]string PatternVariables map[string]string
Message string Message string
Language string
} }
type ChatOptions struct { type ChatOptions struct {

View File

@ -60,7 +60,9 @@ func (o *Chatter) Send(request *common.ChatRequest, opts *common.ChatOptions) (m
} }
func (o *Chatter) NewChat(request *common.ChatRequest) (ret *Chat, err error) { func (o *Chatter) NewChat(request *common.ChatRequest) (ret *Chat, err error) {
ret = &Chat{} ret = &Chat{
Language: request.Language,
}
if request.ContextName != "" { if request.ContextName != "" {
var ctx *db.Context var ctx *db.Context
@ -97,8 +99,9 @@ func (o *Chatter) NewChat(request *common.ChatRequest) (ret *Chat, err error) {
} }
type Chat struct { type Chat struct {
Context string Context string
Pattern string Pattern string
Message string Message string
Session *db.Session Session *db.Session
Language string
} }

View File

@ -253,6 +253,10 @@ func (o *Chat) BuildChatSession() (ret *db.Session, err error) {
ret.Append(&common.Message{Role: "user", Content: userMessage}) ret.Append(&common.Message{Role: "user", Content: userMessage})
} }
if o.Language != "" {
ret.Append(&common.Message{Role: "system", Content: "please use " + o.Language + " language"})
}
if ret.IsEmpty() { if ret.IsEmpty() {
ret = nil ret = nil
err = fmt.Errorf("no session, pattern or user messages provided") err = fmt.Errorf("no session, pattern or user messages provided")

2
go.mod
View File

@ -5,6 +5,7 @@ go 1.22.5
toolchain go1.22.6 toolchain go1.22.6
require ( require (
github.com/anaskhan96/soup v1.2.5
github.com/atotto/clipboard v0.1.4 github.com/atotto/clipboard v0.1.4
github.com/go-git/go-git/v5 v5.12.0 github.com/go-git/go-git/v5 v5.12.0
github.com/google/generative-ai-go v0.17.0 github.com/google/generative-ai-go v0.17.0
@ -30,7 +31,6 @@ require (
dario.cat/mergo v1.0.0 // indirect dario.cat/mergo v1.0.0 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/ProtonMail/go-crypto v1.0.0 // indirect github.com/ProtonMail/go-crypto v1.0.0 // indirect
github.com/anaskhan96/soup v1.2.5 // indirect
github.com/cloudflare/circl v1.3.7 // indirect github.com/cloudflare/circl v1.3.7 // indirect
github.com/cyphar/filepath-securejoin v0.2.4 // indirect github.com/cyphar/filepath-securejoin v0.2.4 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect

2
go.sum
View File

@ -291,8 +291,6 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/gookit/color.v1 v1.1.6 h1:5fB10p6AUFjhd2ayq9JgmJWr9WlTrguFdw3qlYtKNHk=
gopkg.in/gookit/color.v1 v1.1.6/go.mod h1:IcEkFGaveVShJ+j8ew+jwe9epHyGpJ9IrptHmW3laVY=
gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME= gopkg.in/warnings.v0 v0.1.2 h1:wFXVbFY8DY5/xOe1ECiWdKCzZlxgshcYVNkBHstARME=
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=