From 4006f3f417d25ee686aa99f300020137f47d00bf Mon Sep 17 00:00:00 2001 From: Azwar Tamim Date: Wed, 28 Aug 2024 21:50:06 +0700 Subject: [PATCH 1/4] Add dry run --- README.md | 1 + cli/cli.go | 7 +++++++ cli/flags.go | 1 + 3 files changed, 9 insertions(+) diff --git a/README.md b/README.md index eddcf16..1be0c35 100644 --- a/README.md +++ b/README.md @@ -179,6 +179,7 @@ Application Options: -u, --url= Choose ollama url (default: http://127.0.0.1:11434) -o, --output= Output to file -n, --latest= Number of latest patterns to list (default: 0) + --dry-run Show what would be sent to the model without actually sending it Help Options: -h, --help Show this help message diff --git a/cli/cli.go b/cli/cli.go index f8178de..5fa856d 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -142,6 +142,13 @@ func Cli() (message string, err error) { } } + if currentFlags.DryRun { + fmt.Println("Dry run: Would send the following request:") + fmt.Printf("Chat Request: %+v\n", currentFlags.BuildChatRequest()) + fmt.Printf("Chat Options: %+v\n", currentFlags.BuildChatOptions()) + return + } + var chatter *core.Chatter if chatter, err = fabric.GetChatter(currentFlags.Model, currentFlags.Stream); err != nil { return diff --git a/cli/flags.go b/cli/flags.go index ee8bdd4..1980a56 100644 --- a/cli/flags.go +++ b/cli/flags.go @@ -37,6 +37,7 @@ type Flags struct { YouTube string `short:"y" long:"youtube" description:"YouTube video url to grab transcript, comments from it 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"` + DryRun bool `long:"dry-run" description:"Show what would be sent to the model without actually sending it"` } // Init Initialize flags. returns a Flags struct and an error From 7d3bf8c3a24a839e878b91eb3535c217d7360124 Mon Sep 17 00:00:00 2001 From: Azwar Tamim Date: Thu, 29 Aug 2024 15:46:48 +0700 Subject: [PATCH 2/4] Fix dry run --- cli/cli.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 5fa856d..12b32b9 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -143,10 +143,38 @@ func Cli() (message string, err error) { } if currentFlags.DryRun { - fmt.Println("Dry run: Would send the following request:") - fmt.Printf("Chat Request: %+v\n", currentFlags.BuildChatRequest()) - fmt.Printf("Chat Options: %+v\n", currentFlags.BuildChatOptions()) - return + var patternContent string + var contextContent string + + if currentFlags.Pattern != "" { + pattern, patternErr := fabric.Db.Patterns.GetPattern(currentFlags.Pattern) + if patternErr != nil { + fmt.Printf("Error getting pattern content: %v\n", patternErr) + return "", patternErr + } + patternContent = pattern.Pattern // Assuming the content is stored in the 'Pattern' field + } + + if currentFlags.Context != "" { + context, contextErr := fabric.Db.Contexts.GetContext(currentFlags.Context) + if contextErr != nil { + fmt.Printf("Error getting context content: %v\n", contextErr) + return "", contextErr + } + contextContent = context.Content + } + + systemMessage := strings.TrimSpace(contextContent) + strings.TrimSpace(patternContent) + userMessage := strings.TrimSpace(currentFlags.Message) + + fmt.Println("Dry run: Would send the following request:\n") + if systemMessage != "" { + fmt.Printf("System:\n%s\n\n", systemMessage) + } + if userMessage != "" { + fmt.Printf("User:\n%s\n", userMessage) + } + return "", nil } var chatter *core.Chatter From feabd565dcb3c6a747121fb8ab69d38aba560135 Mon Sep 17 00:00:00 2001 From: Azwar Tamim Date: Sun, 1 Sep 2024 13:44:56 +0700 Subject: [PATCH 3/4] Refactor dry run to DryRun Vendor --- cli/cli.go | 37 +---------------- core/chatter.go | 2 + core/fabric.go | 21 +++++++--- core/models.go | 6 ++- vendors/dryrun/dryrun.go | 88 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 110 insertions(+), 44 deletions(-) create mode 100644 vendors/dryrun/dryrun.go diff --git a/cli/cli.go b/cli/cli.go index 12b32b9..1a5d4f4 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -142,43 +142,8 @@ func Cli() (message string, err error) { } } - if currentFlags.DryRun { - var patternContent string - var contextContent string - - if currentFlags.Pattern != "" { - pattern, patternErr := fabric.Db.Patterns.GetPattern(currentFlags.Pattern) - if patternErr != nil { - fmt.Printf("Error getting pattern content: %v\n", patternErr) - return "", patternErr - } - patternContent = pattern.Pattern // Assuming the content is stored in the 'Pattern' field - } - - if currentFlags.Context != "" { - context, contextErr := fabric.Db.Contexts.GetContext(currentFlags.Context) - if contextErr != nil { - fmt.Printf("Error getting context content: %v\n", contextErr) - return "", contextErr - } - contextContent = context.Content - } - - systemMessage := strings.TrimSpace(contextContent) + strings.TrimSpace(patternContent) - userMessage := strings.TrimSpace(currentFlags.Message) - - fmt.Println("Dry run: Would send the following request:\n") - if systemMessage != "" { - fmt.Printf("System:\n%s\n\n", systemMessage) - } - if userMessage != "" { - fmt.Printf("User:\n%s\n", userMessage) - } - return "", nil - } - var chatter *core.Chatter - if chatter, err = fabric.GetChatter(currentFlags.Model, currentFlags.Stream); err != nil { + if chatter, err = fabric.GetChatter(currentFlags.Model, currentFlags.Stream, currentFlags.DryRun); err != nil { return } diff --git a/core/chatter.go b/core/chatter.go index 70123f3..12dbfd1 100644 --- a/core/chatter.go +++ b/core/chatter.go @@ -2,6 +2,7 @@ package core import ( "fmt" + "github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/vendors" @@ -11,6 +12,7 @@ type Chatter struct { db *db.Db Stream bool + DryRun bool model string vendor vendors.Vendor diff --git a/core/fabric.go b/core/fabric.go index 7e295d2..a93edeb 100644 --- a/core/fabric.go +++ b/core/fabric.go @@ -3,20 +3,22 @@ package core import ( "bytes" "fmt" + "os" + "strconv" + "strings" + "github.com/atotto/clipboard" "github.com/danielmiessler/fabric/common" "github.com/danielmiessler/fabric/db" "github.com/danielmiessler/fabric/vendors/anthropic" "github.com/danielmiessler/fabric/vendors/azure" + "github.com/danielmiessler/fabric/vendors/dryrun" "github.com/danielmiessler/fabric/vendors/gemini" "github.com/danielmiessler/fabric/vendors/groc" "github.com/danielmiessler/fabric/vendors/ollama" "github.com/danielmiessler/fabric/vendors/openai" "github.com/danielmiessler/fabric/youtube" "github.com/pkg/errors" - "os" - "strconv" - "strings" ) const DefaultPatternsGitRepoUrl = "https://github.com/danielmiessler/fabric.git" @@ -57,7 +59,7 @@ func NewFabricBase(db *db.Db) (ret *Fabric) { "Enter the index the name of your default model") ret.VendorsAll.AddVendors(openai.NewClient(), azure.NewClient(), ollama.NewClient(), groc.NewClient(), - gemini.NewClient(), anthropic.NewClient()) + gemini.NewClient(), anthropic.NewClient(), dryrun.NewClient()) return } @@ -182,13 +184,20 @@ func (o *Fabric) configure() (err error) { return } -func (o *Fabric) GetChatter(model string, stream bool) (ret *Chatter, err error) { +func (o *Fabric) GetChatter(model string, stream bool, dryRun bool) (ret *Chatter, err error) { ret = &Chatter{ db: o.Db, Stream: stream, + DryRun: dryRun, } - if model == "" { + if dryRun { + ret.vendor = dryrun.NewClient() + ret.model = model + if ret.model == "" { + ret.model = o.DefaultModel.Value + } + } else if model == "" { ret.vendor = o.FindByName(o.DefaultVendor.Value) ret.model = o.DefaultModel.Value } else { diff --git a/core/models.go b/core/models.go index 980508e..2eaf775 100644 --- a/core/models.go +++ b/core/models.go @@ -16,8 +16,10 @@ type VendorsModels struct { } func (o *VendorsModels) AddVendorModels(vendor string, models []string) { - o.Vendors = append(o.Vendors, vendor) - o.VendorsModels[vendor] = models + if vendor != "DryRun" { + o.Vendors = append(o.Vendors, vendor) + o.VendorsModels[vendor] = models + } } func (o *VendorsModels) GetVendorAndModelByModelIndex(modelIndex int) (vendor string, model string) { diff --git a/vendors/dryrun/dryrun.go b/vendors/dryrun/dryrun.go new file mode 100644 index 0000000..0d2e246 --- /dev/null +++ b/vendors/dryrun/dryrun.go @@ -0,0 +1,88 @@ +package dryrun + +import ( + "bytes" + "fmt" + + "github.com/danielmiessler/fabric/common" +) + +type Client struct{} + +func NewClient() *Client { + return &Client{} +} + +func (c *Client) GetName() string { + return "DryRun" +} + +func (c *Client) IsConfigured() bool { + return true +} + +func (c *Client) Configure() error { + return nil +} + +func (c *Client) ListModels() ([]string, error) { + return []string{"dry-run-model"}, nil +} + +func (c *Client) SendStream(messages []*common.Message, options *common.ChatOptions, channel chan string) error { + output := "Dry run: Would send the following request:\n\n" + + for _, msg := range messages { + switch msg.Role { + case "system": + output += fmt.Sprintf("System:\n%s\n\n", msg.Content) + case "user": + output += fmt.Sprintf("User:\n%s\n\n", msg.Content) + default: + output += fmt.Sprintf("%s:\n%s\n\n", msg.Role, msg.Content) + } + } + + output += "Options:\n" + output += fmt.Sprintf("Model: %s\n", options.Model) + output += fmt.Sprintf("Temperature: %f\n", options.Temperature) + output += fmt.Sprintf("TopP: %f\n", options.TopP) + output += fmt.Sprintf("PresencePenalty: %f\n", options.PresencePenalty) + output += fmt.Sprintf("FrequencyPenalty: %f\n", options.FrequencyPenalty) + + channel <- output + close(channel) + return nil +} + +func (c *Client) Send(messages []*common.Message, options *common.ChatOptions) (string, error) { + fmt.Println("Dry run: Would send the following request:") + + for _, msg := range messages { + switch msg.Role { + case "system": + fmt.Printf("System:\n%s\n\n", msg.Content) + case "user": + fmt.Printf("User:\n%s\n\n", msg.Content) + default: + fmt.Printf("%s:\n%s\n\n", msg.Role, msg.Content) + } + } + + fmt.Println("Options:") + fmt.Printf("Model: %s\n", options.Model) + fmt.Printf("Temperature: %f\n", options.Temperature) + fmt.Printf("TopP: %f\n", options.TopP) + fmt.Printf("PresencePenalty: %f\n", options.PresencePenalty) + fmt.Printf("FrequencyPenalty: %f\n", options.FrequencyPenalty) + + return "", nil +} + +func (c *Client) Setup() error { + return nil +} + +func (c *Client) SetupFillEnvFileContent(buffer *bytes.Buffer) { + // No environment variables needed for dry run +} From 33632030f6647e4c1877bb7e7f23115fec57d871 Mon Sep 17 00:00:00 2001 From: Azwar Tamim Date: Mon, 2 Sep 2024 14:41:39 +0700 Subject: [PATCH 4/4] Revert unneeded DryRun Vendor registration --- core/fabric.go | 2 +- core/models.go | 6 ++---- vendors/dryrun/dryrun.go | 29 +++++++++++++++-------------- 3 files changed, 18 insertions(+), 19 deletions(-) diff --git a/core/fabric.go b/core/fabric.go index a93edeb..7616ea5 100644 --- a/core/fabric.go +++ b/core/fabric.go @@ -59,7 +59,7 @@ func NewFabricBase(db *db.Db) (ret *Fabric) { "Enter the index the name of your default model") ret.VendorsAll.AddVendors(openai.NewClient(), azure.NewClient(), ollama.NewClient(), groc.NewClient(), - gemini.NewClient(), anthropic.NewClient(), dryrun.NewClient()) + gemini.NewClient(), anthropic.NewClient()) return } diff --git a/core/models.go b/core/models.go index 2eaf775..980508e 100644 --- a/core/models.go +++ b/core/models.go @@ -16,10 +16,8 @@ type VendorsModels struct { } func (o *VendorsModels) AddVendorModels(vendor string, models []string) { - if vendor != "DryRun" { - o.Vendors = append(o.Vendors, vendor) - o.VendorsModels[vendor] = models - } + o.Vendors = append(o.Vendors, vendor) + o.VendorsModels[vendor] = models } func (o *VendorsModels) GetVendorAndModelByModelIndex(modelIndex int) (vendor string, model string) { diff --git a/vendors/dryrun/dryrun.go b/vendors/dryrun/dryrun.go index 0d2e246..c13350c 100644 --- a/vendors/dryrun/dryrun.go +++ b/vendors/dryrun/dryrun.go @@ -2,6 +2,7 @@ package dryrun import ( "bytes" + "context" "fmt" "github.com/danielmiessler/fabric/common" @@ -29,10 +30,10 @@ func (c *Client) ListModels() ([]string, error) { return []string{"dry-run-model"}, nil } -func (c *Client) SendStream(messages []*common.Message, options *common.ChatOptions, channel chan string) error { +func (c *Client) SendStream(msgs []*common.Message, opts *common.ChatOptions, channel chan string) error { output := "Dry run: Would send the following request:\n\n" - for _, msg := range messages { + for _, msg := range msgs { switch msg.Role { case "system": output += fmt.Sprintf("System:\n%s\n\n", msg.Content) @@ -44,21 +45,21 @@ func (c *Client) SendStream(messages []*common.Message, options *common.ChatOpti } output += "Options:\n" - output += fmt.Sprintf("Model: %s\n", options.Model) - output += fmt.Sprintf("Temperature: %f\n", options.Temperature) - output += fmt.Sprintf("TopP: %f\n", options.TopP) - output += fmt.Sprintf("PresencePenalty: %f\n", options.PresencePenalty) - output += fmt.Sprintf("FrequencyPenalty: %f\n", options.FrequencyPenalty) + output += fmt.Sprintf("Model: %s\n", opts.Model) + output += fmt.Sprintf("Temperature: %f\n", opts.Temperature) + output += fmt.Sprintf("TopP: %f\n", opts.TopP) + output += fmt.Sprintf("PresencePenalty: %f\n", opts.PresencePenalty) + output += fmt.Sprintf("FrequencyPenalty: %f\n", opts.FrequencyPenalty) channel <- output close(channel) return nil } -func (c *Client) Send(messages []*common.Message, options *common.ChatOptions) (string, error) { +func (c *Client) Send(ctx context.Context, msgs []*common.Message, opts *common.ChatOptions) (string, error) { fmt.Println("Dry run: Would send the following request:") - for _, msg := range messages { + for _, msg := range msgs { switch msg.Role { case "system": fmt.Printf("System:\n%s\n\n", msg.Content) @@ -70,11 +71,11 @@ func (c *Client) Send(messages []*common.Message, options *common.ChatOptions) ( } fmt.Println("Options:") - fmt.Printf("Model: %s\n", options.Model) - fmt.Printf("Temperature: %f\n", options.Temperature) - fmt.Printf("TopP: %f\n", options.TopP) - fmt.Printf("PresencePenalty: %f\n", options.PresencePenalty) - fmt.Printf("FrequencyPenalty: %f\n", options.FrequencyPenalty) + fmt.Printf("Model: %s\n", opts.Model) + fmt.Printf("Temperature: %f\n", opts.Temperature) + fmt.Printf("TopP: %f\n", opts.TopP) + fmt.Printf("PresencePenalty: %f\n", opts.PresencePenalty) + fmt.Printf("FrequencyPenalty: %f\n", opts.FrequencyPenalty) return "", nil }