Skip to content

Commit

Permalink
ai_generate
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed May 10, 2024
1 parent 6b71ac1 commit fb6a8d6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
27 changes: 27 additions & 0 deletions .grit/patterns/universal/ai/_ai_generate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
tags: [ai, sample, util, hidden, example, docs]
---

# AI generate

GritQL can generate new code based on some instruction using the `ai_generate` function.

Just call ai_generate with your instructions to assign a value to a variable. Metavariables can be referenced in the instructions

```grit
language js
`console.log($msg)` where {
$level = ai_generate(`Based on this log message: $msg, generate a new log level of either "info", "warn", or "error" with only one word`),
} => `logger.level("$level", $msg)`
```

## Rewrites a basic console.log

```js
console.log('Hello, world!');
```

```js
logger.level('info', 'Hello, world!');
```
4 changes: 2 additions & 2 deletions .grit/patterns/universal/ai/ai.grit
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ $match
}

// Generate new code using the provied instructions.
function ai_transform($instruct, $pattern) {
function ai_generate($instruct, $pattern) {
$messages = [
{
role: "system",
Expand All @@ -139,6 +139,6 @@ $instruct


$answer = llm_chat($messages, $pattern, model="claude-3-haiku-20240307", stop_sequences=["</code>"]),
$answer <: includes r"<code>(?:\s+)([\s\S]+)</code>"($final),
$answer <: includes r"<code>(?:\s+)([\s\S]+)(?:\n)</code>"($final),
return `$final`
}

0 comments on commit fb6a8d6

Please sign in to comment.