Skip to content

Commit

Permalink
Merge pull request #957 from quarechen/specify_language_return
Browse files Browse the repository at this point in the history
Add cmd -g to select the language would reply
  • Loading branch information
eugeis committed Sep 19, 2024
2 parents 64da74e + 26fccfe commit a61590e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 5 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,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
-u, --scrape_url= Scrape website URL to markdown using Jina AI
-q, --scrape_question= Search question using Jina AI

Expand Down
8 changes: 8 additions & 0 deletions cli/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +41,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:"Specify the Language Code for the chat, e.g. -g=en -g=zh" default:""`
ScrapeURL string `short:"u" long:"scrape_url" description:"Scrape website URL to markdown using Jina AI"`
ScrapeQuestion string `short:"q" long:"scrape_question" description:"Search question using Jina AI"`
}
Expand Down Expand Up @@ -109,6 +111,12 @@ func (o *Flags) BuildChatRequest() (ret *common.ChatRequest) {
PatternVariables: o.PatternVariables,
Message: o.Message,
}
if o.Language != "" {
langTag, err := language.Parse(o.Language)
if err == nil {
ret.Language = langTag.String()
}
}
return
}

Expand Down
1 change: 1 addition & 0 deletions common/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type ChatRequest struct {
PatternName string
PatternVariables map[string]string
Message string
Language string
}

type ChatOptions struct {
Expand Down
13 changes: 8 additions & 5 deletions core/chatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions core/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a61590e

Please sign in to comment.