From b96415d44566b68fa5aef01abb451560c110f793 Mon Sep 17 00:00:00 2001 From: csquarechen Date: Fri, 13 Sep 2024 16:41:54 +0800 Subject: [PATCH 1/4] 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 7de2584ef..98cc31947 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 f5468303a..bf566b1a5 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 f90a0c020..e973bc888 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 7616ea509..67926ee31 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 d86e0fa60..eaf996250 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 35b6b8ee7..2339d124a 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= From d5746700843808d7fc96f2e46f0d8f11ef2d6567 Mon Sep 17 00:00:00 2001 From: csquarechen Date: Sat, 14 Sep 2024 10:49:55 +0800 Subject: [PATCH 2/4] use default language param default:"" to avoid changes in the output. --- cli/flags.go | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/cli/flags.go b/cli/flags.go index 98cc31947..c576d392e 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -40,7 +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"` + Language string `short:"g" long:"language" description:"Language of the chat" default:""` } // Init Initialize flags. returns a Flags struct and an error @@ -107,10 +107,11 @@ 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 + if o.Language != "" { + langTag, err := language.Parse(o.Language) + if err == nil { + ret.Language = langTag.String() + } } - ret.Language = langTag.String() return } From 680129e370de0bda8059188505d3316012e9e64c Mon Sep 17 00:00:00 2001 From: csquarechen Date: Sat, 14 Sep 2024 11:30:55 +0800 Subject: [PATCH 3/4] update the discription of language commend --- README.md | 1 + cli/flags.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 54aa2eaea..ce24ba465 100644 --- a/README.md +++ b/README.md @@ -210,6 +210,7 @@ Application Options: --transcript Grab transcript from YouTube video and send to chat --comments Grab comments from YouTube video and send to chat --dry-run Show what would be sent to the model without actually sending it + -g, --language= Specify the Language Code for the chat, e.g. -g=en -g=zh Help Options: -h, --help Show this help message diff --git a/cli/flags.go b/cli/flags.go index c576d392e..7d60247ec 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -40,7 +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:""` + Language string `short:"g" long:"language" description:"Specify the Language Code for the chat, e.g. -g=en -g=zh" default:""` } // Init Initialize flags. returns a Flags struct and an error From 26fccfe18ecd64265738a5e2474feee48dd0fde4 Mon Sep 17 00:00:00 2001 From: Eugen Eisler Date: Thu, 19 Sep 2024 22:41:05 +0200 Subject: [PATCH 4/4] feat: integrate the output language to the system/user prompt --- core/fabric.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/core/fabric.go b/core/fabric.go index 234f4d68d..a70ff37f2 100644 --- a/core/fabric.go +++ b/core/fabric.go @@ -257,6 +257,9 @@ func (o *Chat) BuildChatSession(raw bool) (ret *db.Session, err error) { } systemMessage := strings.TrimSpace(o.Context) + strings.TrimSpace(o.Pattern) + if o.Language != "" { + systemMessage = fmt.Sprintf("%s. Please use the language '%s' for the output.", systemMessage, o.Language) + } userMessage := strings.TrimSpace(o.Message) if raw { @@ -274,10 +277,6 @@ func (o *Chat) BuildChatSession(raw bool) (ret *db.Session, err error) { } } - 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")