From b96415d44566b68fa5aef01abb451560c110f793 Mon Sep 17 00:00:00 2001 From: csquarechen Date: Fri, 13 Sep 2024 16:41:54 +0800 Subject: [PATCH] feat: add cmd -g to select the language would reply --- cli/flags.go | 7 +++++++ common/domain.go | 1 + core/chatter.go | 13 ++++++++----- core/fabric.go | 4 ++++ go.mod | 2 +- go.sum | 2 -- 6 files changed, 21 insertions(+), 8 deletions(-) diff --git a/cli/flags.go b/cli/flags.go index 7de2584..98cc319 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -9,6 +9,7 @@ import ( "github.com/danielmiessler/fabric/common" "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 @@ -39,6 +40,7 @@ type Flags struct { 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"` 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 @@ -105,5 +107,10 @@ func (o *Flags) BuildChatRequest() (ret *common.ChatRequest) { PatternVariables: o.PatternVariables, Message: o.Message, } + langTag, err := language.Parse(o.Language) + if err != nil { + langTag = language.English + } + ret.Language = langTag.String() return } diff --git a/common/domain.go b/common/domain.go index f546830..bf566b1 100644 --- a/common/domain.go +++ b/common/domain.go @@ -11,6 +11,7 @@ type ChatRequest struct { PatternName string PatternVariables map[string]string Message string + Language string } type ChatOptions struct { diff --git a/core/chatter.go b/core/chatter.go index f90a0c0..e973bc8 100644 --- a/core/chatter.go +++ b/core/chatter.go @@ -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) { - ret = &Chat{} + ret = &Chat{ + Language: request.Language, + } if request.ContextName != "" { var ctx *db.Context @@ -97,8 +99,9 @@ func (o *Chatter) NewChat(request *common.ChatRequest) (ret *Chat, err error) { } type Chat struct { - Context string - Pattern string - Message string - Session *db.Session + Context string + Pattern string + Message string + Session *db.Session + Language string } diff --git a/core/fabric.go b/core/fabric.go index 7616ea5..67926ee 100644 --- a/core/fabric.go +++ b/core/fabric.go @@ -253,6 +253,10 @@ func (o *Chat) BuildChatSession() (ret *db.Session, err error) { 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() { ret = nil err = fmt.Errorf("no session, pattern or user messages provided") diff --git a/go.mod b/go.mod index d86e0fa..eaf9962 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.22.5 toolchain go1.22.6 require ( + github.com/anaskhan96/soup v1.2.5 github.com/atotto/clipboard v0.1.4 github.com/go-git/go-git/v5 v5.12.0 github.com/google/generative-ai-go v0.17.0 @@ -30,7 +31,6 @@ require ( dario.cat/mergo v1.0.0 // indirect github.com/Microsoft/go-winio v0.6.1 // 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/cyphar/filepath-securejoin v0.2.4 // indirect github.com/davecgh/go-spew v1.1.1 // indirect diff --git a/go.sum b/go.sum index 35b6b8e..2339d12 100644 --- a/go.sum +++ b/go.sum @@ -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-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= 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/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=