Skip to content
This repository was archived by the owner on Aug 9, 2025. It is now read-only.

Commit 4201623

Browse files
authored
feat: add prompt iteration (#29)
1 parent 007a9ba commit 4201623

2 files changed

Lines changed: 129 additions & 59 deletions

File tree

‎main.go‎

Lines changed: 99 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"os"
1414
"path/filepath"
15+
"reflect"
1516
"strconv"
1617
"strings"
1718

@@ -29,20 +30,24 @@ import (
2930
)
3031

3132
type AppConfig struct {
32-
Name string
33-
Model string
34-
ChooseAIModel bool
35-
ChoosePlugin bool
36-
ChooseWorkflow bool
37-
Temperature string
38-
Role string
39-
Raw bool
40-
Version bool
41-
WorkflowPath string
33+
Name string
34+
Model string
35+
ChooseAIModel bool
36+
ChoosePlugin bool
37+
ChooseWorkflow bool
38+
Temperature string
39+
Role string
40+
Raw bool
41+
Version bool
42+
WorkflowPath string
43+
IteratorPrompt bool
44+
CurrentIterationValue interface{}
4245
}
4346

4447
type Tasks struct {
45-
Tasks []Task `yaml:"tasks"`
48+
IterationValuesIn string `yaml:"iterator_script"`
49+
IterationValues []interface{}
50+
Tasks []Task `yaml:"tasks"`
4651
}
4752

4853
type Task struct {
@@ -58,7 +63,7 @@ type Task struct {
5863

5964
const (
6065
configFileName = "config.yaml"
61-
version = "0.3.1"
66+
version = "0.4.0"
6267
)
6368

6469
var (
@@ -196,6 +201,7 @@ func initializeFlags() {
196201
flags.BoolVarP(&appCfg.Version, "version", "v", false, "Print the version")
197202
flags.StringVarP(&appCfg.WorkflowPath, "workflow", "w", "", "The path to a workflow file")
198203
flags.BoolVarP(&appCfg.ChooseWorkflow, "choose-workflow", "W", false, "Choose a workflow to run")
204+
flags.BoolVarP(&appCfg.IteratorPrompt, "iterator", "i", false, "String array of prompts ['prompt1', 'prompt2']")
199205
flags.SortFlags = false
200206
}
201207

@@ -391,6 +397,7 @@ func runExpr(input string, expression string) (string, error) {
391397
"ReadFile": readfile,
392398
"Extism": callExtismPlugin,
393399
"Resend": resend,
400+
"iterValue": appCfg.CurrentIterationValue,
394401
}
395402

396403
program, err := expr.Compile(expression, expr.Env(env))
@@ -464,32 +471,57 @@ func handleTasks(prompt string) error {
464471
return err
465472
}
466473

467-
if len(tasks.Tasks) > 0 {
468-
if prompt != "" {
469-
tasks.Tasks[0].Prompt = prompt + " " + tasks.Tasks[0].Prompt
474+
if tasks.IterationValuesIn == "" {
475+
tasks.IterationValues = []interface{}{nil}
476+
} else {
477+
env := map[string]interface{}{
478+
"input": prompt,
479+
"Get": httpGet,
480+
"AppendFile": appendFile,
481+
"ReadFile": readfile,
482+
"Extism": callExtismPlugin,
483+
"Resend": resend,
470484
}
471-
}
472485

473-
ctx, cancel := context.WithCancel(context.Background())
474-
var res string
475-
action := func() {
476-
res, err = generateResponseForTasks(tasks)
486+
program, err := expr.Compile(tasks.IterationValuesIn, expr.Env(env), expr.AsKind(reflect.Slice))
477487
if err != nil {
478-
cancel()
479-
fmt.Println(err)
480-
os.Exit(1)
488+
return err
481489
}
482-
cancel()
483-
}
484490

485-
go action()
486-
_ = spinner.New().Title("Generating...").TitleStyle(lipgloss.NewStyle().Faint(true)).Context(ctx).Run()
491+
output, err := expr.Run(program, env)
492+
if err != nil {
493+
return err
494+
}
487495

488-
if err != nil {
489-
return err
496+
tasks.IterationValues = output.([]interface{})
490497
}
491498

492-
fmt.Print(res)
499+
for i := range tasks.IterationValues {
500+
appCfg.CurrentIterationValue = tasks.IterationValues[i]
501+
502+
if len(tasks.Tasks) > 0 {
503+
if prompt != "" {
504+
tasks.Tasks[0].Prompt = prompt + " " + tasks.Tasks[0].Prompt
505+
}
506+
}
507+
508+
var res string
509+
action := func(tasks Tasks) {
510+
res, err = generateResponseForTasks(tasks)
511+
if err != nil {
512+
fmt.Println(err)
513+
os.Exit(1)
514+
}
515+
}
516+
517+
_ = spinner.New().
518+
Title("Generating...").
519+
TitleStyle(lipgloss.NewStyle().Faint(true)).
520+
Action(func() { action(tasks) }).
521+
Run()
522+
523+
fmt.Print(res)
524+
}
493525
return nil
494526
}
495527

@@ -571,24 +603,52 @@ func runCommand(cmd *cobra.Command, args []string) error {
571603
return err
572604
}
573605
}
574-
575-
prompt := generatePrompt(args, appCfg.Raw)
576-
577-
ctx, cancel := context.WithCancel(context.Background())
578606
var res string
579-
action := func() {
607+
action := func(prompt string) {
580608
res, err = pluginCfg.generateResponse(prompt, appCfg.Raw)
581-
cancel()
582609
}
583610

584-
go action()
585-
_ = spinner.New().Title("Generating...").TitleStyle(lipgloss.NewStyle().Faint(true)).Context(ctx).Run()
611+
var prompt string
612+
if appCfg.IteratorPrompt {
613+
if len(args) > 0 {
614+
prompt = args[0]
615+
} else {
616+
huh.NewInput().
617+
Title("Enter the prompts in brackets separated by commas:").
618+
Value(&prompt).
619+
Placeholder("[prompt1, prompt2]").
620+
WithTheme(huh.ThemeCharm()).
621+
Run()
622+
}
623+
var prompts []string
624+
ps := strings.Trim(prompt, "[]")
625+
prompts = strings.Split(ps, ",")
626+
for _, p := range prompts {
627+
_ = spinner.New().
628+
Title("Generating...").
629+
TitleStyle(lipgloss.NewStyle().Faint(true)).
630+
Action(func() { action(p) }).
631+
Run()
632+
633+
fmt.Println(res)
634+
}
635+
} else {
636+
prompt = generatePrompt(args, appCfg.Raw)
637+
638+
_ = spinner.New().
639+
Title("Generating...").
640+
TitleStyle(lipgloss.NewStyle().Faint(true)).
641+
Action(func() { action(prompt) }).
642+
Run()
643+
}
586644

587645
if err != nil {
588646
return err
589647
}
590648

591-
fmt.Print(res)
649+
if !appCfg.IteratorPrompt {
650+
fmt.Print(res)
651+
}
592652
return nil
593653
}
594654

‎workflows/rss.yaml‎

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,49 @@
11
name: Hacker News Top Story Workflow
22
description: |
33
This workflow calls the RSS feed for hacker news, obtains the title and url of
4-
the first story in the feed and sends it to perplexity to provide a summary
5-
and insight.
4+
the first 5 stories in the feed, and sends them on by one to Perplexity's online
5+
model which has up to date information. The model provides a summary and
6+
insight for each.
7+
8+
Requires version 0.4.0 or later of the assembllm plugin.
9+
iterator_script: >
10+
let rss = Get('https://news.ycombinator.com/rss');
11+
split(rss, "<item>")[1:]
12+
| map(
13+
join(
14+
split(#, "<title>")
15+
| last()
16+
| split("</title>")
17+
| map(
18+
split(#, "<link>")
19+
| last() + " --- "
20+
| split("</link>")
21+
| first()
22+
)
23+
)
24+
)
25+
| take(5)
626
727
tasks:
828
- name: topic
9-
pre_script: >
10-
let rss = Get('https://news.ycombinator.com/rss');
11-
split(rss, "<item>")[1:]
12-
| map(
13-
join(
14-
split(#, "<title>")
15-
| last()
16-
| split("</title>")
17-
| map(
18-
split(#, "<link>")
19-
| last() + " --- "
20-
| split("</link>")
21-
| first()
22-
)
23-
)
24-
)
25-
| first()
2629
plugin: perplexity
2730
prompt: |
2831
# Task Objective
2932
3033
Give a detailed analysis of the topic supported by a URL source provided.
3134
Provide references, if available. Include the URL provided at the end of
32-
the summary.
35+
the summary. Response must be 250 words or less.
3336
3437
## Task Details
3538
3639
Be insightful. Think about the author's claims deeply and express the strengths
3740
and weaknesses of their argument. Where practical, think about how the topic
3841
can impact a person's daily life, or an industry long-term, or is it just
3942
passing information with little impact.
43+
post_script: |
44+
//
45+
// Get the title from the rss feed input and add it to the top of the output
46+
// as a markdown header.
47+
//
48+
let title = split(iterValue, "---") | first();
49+
"# " + title + "\n" + input

0 commit comments

Comments
 (0)
⌂ 换网址