Skip to content

Commit

Permalink
Fix parsing in filter afforder
Browse files Browse the repository at this point in the history
  • Loading branch information
noahshinn committed Dec 8, 2023
1 parent 2690870 commit 8b5bd0b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 29 deletions.
44 changes: 22 additions & 22 deletions afforder/afforderstrategy/filterafforder/filter_afforder.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,24 +43,6 @@ func (fa *FilterAfforder) filterBrowserDisplay(ctx context.Context, br *browser.
rawBrowserDisplay := br.GetDisplay().MD
numberedBrowserDisplay := displayBrowserContentWithLineno(rawBrowserDisplay)
trajDisplay := traj.GetAbbreviatedText()
messages := []*llm.Message{
{
Role: llm.MessageRoleSystem,
Content: systemPromptToFilterAffordances,
},
{
Role: llm.MessageRoleUser,
Content: fmt.Sprintf(`----- START BROWSER -----
%s
----- END BROWSER -----
----- START TRAJECTORY -----
%s
----- END TRAJECTORY -----
First, list a description of the next action that should be taken. Then, list a sequence of the irrelevant lines. These lines will be deleted and the remaining lines will be displayed as the web browser for your next action.`, numberedBrowserDisplay, trajDisplay),
},
}
functionDef := &llm.FunctionDef{
Name: "filter_irrelevant_lines",
Description: "Filter out the irrelevant lines from the browser display. The remaining lines will be displayed as the web browser for your next action.",
Expand All @@ -82,17 +64,35 @@ First, list a description of the next action that should be taken. Then, list a
Required: []string{"next_action_description", "irrelevant_lines"},
},
}
messages := []*llm.Message{
{
Role: llm.MessageRoleSystem,
Content: systemPromptToFilterAffordances,
},
{
Role: llm.MessageRoleUser,
Content: fmt.Sprintf(`----- START BROWSER -----
%s
----- END BROWSER -----
----- START TRAJECTORY -----
%s
----- END TRAJECTORY -----
First, list a description of the next action that should be taken. Then, list a sequence of the irrelevant lines. These lines will be deleted and the remaining lines will be displayed as the web browser for your next action. Use the %s function call.`, numberedBrowserDisplay, trajDisplay, functionDef.Name),
},
}
var args map[string]interface{}
if res, err := fa.models.ChatModels[llm.ChatModelGPT4].Message(ctx, messages, &llm.MessageOptions{
Temperature: 0.0,
Functions: []*llm.FunctionDef{functionDef},
FunctionCall: "filter_affordances",
FunctionCall: functionDef.Name,
}); err != nil {
return "", fmt.Errorf("failed to get response from chat model: %w", err)
} else if res.FunctionCall == nil {
return "", fmt.Errorf("response from chat model did not include a function call")
} else if res.FunctionCall.Name != "filter_affordances" {
return "", fmt.Errorf("response from chat model did not include a function call to filter_affordances")
} else if res.FunctionCall.Name != functionDef.Name {
return "", fmt.Errorf("response from chat model did not include a function call to %s", functionDef.Name)
} else if err := json.Unmarshal([]byte(res.FunctionCall.Arguments), &args); err != nil {
return "", fmt.Errorf("failed to unmarshal arguments from chat model response: %w", err)
} else if irrelevantLinesRes, ok := args["irrelevant_lines"].([]interface{}); !ok {
Expand Down Expand Up @@ -124,7 +124,7 @@ func displayBrowserContentWithLineno(s string) string {

func parseIrrelevantLinesResult(res []string) (map[int]struct{}, error) {
irrelevantLines := make(map[int]struct{})
re := regexp.MustCompile(`^(\d+(:|-)\d+|:\d+|\d+:|<\d+>) description="(.+)"$`)
re := regexp.MustCompile(`^(\d+(:|-)\d+|:\d+|\d+:|<\d+>|\d+) description=['"]?(.+)['"]?$`)
for _, line := range res {
matches := re.FindStringSubmatch(line)
if matches == nil {
Expand Down
4 changes: 2 additions & 2 deletions afforder/strategy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type AfforderStrategyID string

const (
AfforderStrategyIDFunctionAfforder AfforderStrategyID = "function_afforder"
AfforderStrategyIDFilterAfforder AfforderStrategyID = "filter_afforder"
AfforderStrategyIDFunctionAfforder AfforderStrategyID = "function"
AfforderStrategyIDFilterAfforder AfforderStrategyID = "filter"
)

const DefaultAfforderStrategyID = AfforderStrategyIDFunctionAfforder
Expand Down
6 changes: 3 additions & 3 deletions browser/browser.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ func (b *Browser) updateDisplay() error {
return fmt.Errorf("error getting location: %w", err)
} else if html, err := b.getHTML(); err != nil {
return fmt.Errorf("error getting html for location %s: %w", location, err)
} else if md, err := b.translators[language.LanguageMD].Translate(html); err != nil {
return fmt.Errorf("error translating html to %s for location %s: %w", language.LanguageMD, location, err)
} else {
b.display.HTML = html
b.display.Location = location
if b.display.MD == "" {
b.display.MD = "No MD display available yet."
}
b.display.MD = md
return nil
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/shell/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ func main() {
runHeadful := flag.Bool("headful", false, "run the browser in non-headless mode")
logPath := flag.String("log-path", "out", "the path to write the trajectory and browser display to")
initialURL := flag.String("url", "https://www.google.com", "the initial url to visit")
actorStrategy := flag.String("actor-strategy", "base_llm", "the actor strategy to use; one of [base_llm, reflexion]")
afforderStrategy := flag.String("afforder-strategy", "function_afforder", "the afforder strategy to use; one of [function_afforder, filter_afforder]")
actorStrategy := flag.String("actor-strategy", "base", "the actor strategy to use; one of [\"base\", \"reflexion\"]")
afforderStrategy := flag.String("afforder-strategy", "function", "the afforder strategy to use; one of [\"function\", filter\"]")
verbose := flag.Bool("verbose", false, "whether to print verbose debug logs")
flag.Parse()

Expand Down

0 comments on commit 8b5bd0b

Please sign in to comment.