Skip to content
This repository has been archived by the owner on Dec 8, 2023. It is now read-only.

feat: allow ai_is to have examples and counter-examples #268

Merged
merged 17 commits into from
Dec 7, 2023
35 changes: 35 additions & 0 deletions .grit/patterns/_ai_is_bare.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# AI conditions.

Test `ai_is` with no counter-examples.

tags: #ai, #sample, #util, #hidden

```grit
`console.log($msg)` => `// REDACTED: $msg` where {
$msg <: ai_is("references personally identifiable information")
}
```

## Solve some basic cases

```js
console.log('This is the system. It is fine.');
console.log('We are now processing the user. Their name is:', user.name);
```

```ts
console.log('This is the system. It is fine.');
// REDACTED: 'We are now processing the user. Their name is:', user.name;
```

## With double quotes

```js
console.log('This is the system. It is fine.');
console.log('We are now processing "the user". Their name is:', user.name);
```

```ts
console.log('This is the system. It is fine.');
// REDACTED: 'We are now processing "the user". Their name is:', user.name;
```
2 changes: 1 addition & 1 deletion .grit/patterns/ai_match.md → .grit/patterns/_ai_match.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ tags: #ai, #sample, #util, #hidden

```grit
`console.log($msg)` => `// REDACTED: $msg` where {
$msg <: ai_is("references personally identifiable information")
$msg <: ai_is("it references personally identifiable information")
}
```

Expand Down
44 changes: 38 additions & 6 deletions .grit/patterns/ai.grit
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,51 @@ function ai_ask($question, $choices) {
return $answer
}

// Given a question and a list of choices,
pattern ai_is($condition) {
// Match the given condition, optionally with examples and counter-examples.
pattern ai_is($condition, $examples, $counter_examples) {
$item_to_check where {
$messages = [
{
role: "system",
content: `Classify whether the input code should match this condition: $condition. Answer with yes or no.`
},
{
role: "user",
content: $item_to_check
}
],
$examplar_messages = [],
if (!$examples <: undefined) {
$examples <: some bubble($examplar_messages) $example where {
$examplar_messages += {
answer: `yes`,
content: $example,
}
},
},
if (!$counter_examples <: undefined) {
$counter_examples <: some bubble($examplar_messages) $counter_example where {
$examplar_messages += {
answer: `no`,
content: $counter_example,
}
},
},
// Workaround for https://github.com/getgrit/rewriter/issues/8137
$total_examples = length($examplar_messages),
if (!$total_examples <: 0) {
$shuffled_exemplars = shuffle($examplar_messages),
$shuffled_exemplars <: some bubble($messages) $exemplar where {
$messages += {
role: "user",
content: $exemplar.content
},
$messages += {
role: "assistant",
content: $exemplar.answer
},
},
},
$messages += {
role: "user",
content: $item_to_check
},
$answer = llm_choice($messages, choices=["yes", "no"]),
$answer <: "yes"
}
Expand Down
9 changes: 9 additions & 0 deletions .grit/patterns/lists.grit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
language js

// Concat two lists together
function concat($list_a, $list_b) {
$new_list = [],
$list_a <: some bubble($new_list) $item where $new_list += $item,
$list_b <: some bubble($new_list) $item where $new_list += $item,
return $new_list
}
23 changes: 22 additions & 1 deletion .grit/patterns/no_commented_out_code.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,28 @@ file($body) where {
},
$blocks = group_blocks(target=$comments),
$blocks <: some bubble $block where {
$block <: ai_is("commented out code that is valid JavaScript, not a descriptive comment"),
$joined = join($block, `\n`),
$joined <: ai_is(
"commented out code that is valid JavaScript, not a descriptive comment",
examples=[
"// console.log(name);",
"// for (const name of names) { console.log(name); }",
`// foo();
// bar();`,
`/**
* for (const item of items) {
* console.log(item);
* }
*/`
],
counter_examples=[
"// Read the user's name from the database",
`/**
* This is a comment that describes the code below.
* It is not commented out code.
*/`
]
),
// Remove the block
$block <: some bubble $comment => .
}
Expand Down
Loading