Skip to content

Commit

Permalink
feat: ai function in user space
Browse files Browse the repository at this point in the history
  • Loading branch information
morgante committed Dec 13, 2023
1 parent aca97ad commit 09eb643
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 1 deletion.
121 changes: 121 additions & 0 deletions .grit/patterns/universal/ai/_ai_transform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
# AI transform

GritQL can use AI to transform a target variable based on some instruction using the `ai_transform` function.

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

```grit
language yaml
pattern pair($key, $value) {
block_mapping_pair(key=contains $key, $value)
}
or {
`- $task` where {
$task <: block_mapping(items=some pair(key=`across`)),
$task => ai_transform(match=$task, instruct="Replace this `across` task with `in_parallel` tasks, distributing the values across each child like `in_parallel:
- task: task-1
- task: task-2`")
}
}
```

## Handles a basic transform - currently

```yaml
jobs:
- name: build-and-use-image
plan:
- across:
- var: name
values:
- file1
- file2
- file3
task: create-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/((.:name))
outputs:
- name: manifests
file: input.yaml
- task: list-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
inputs:
- name: manifests
run:
path: ls
args:
- manifests
```
```yaml
jobs:
- name: build-and-use-image
plan:
- in_parallel:
- task: task-1
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file1
outputs:
- name: manifests

- task: task-2
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file2
outputs:
- name: manifests

- task: task-3
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
run:
path: touch
args:
- manifests/file3
outputs:
- name: manifests

file: input.yaml
- task: list-file
config:
platform: linux
image_resource:
type: registry-image
source: { repository: busybox }
inputs:
- name: manifests
run:
path: ls
args:
- manifests
```
25 changes: 25 additions & 0 deletions .grit/patterns/universal/ai/_js_transform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# AI transform - JS

GritQL can use AI to transform a target variable based on some instruction using the `ai_transform` function.

tags: #ai, #hidden, #test

```grit
language js
`console.log($_)` as $log where {
$log => ai_transform(match=$log, instruct="Use console.error instead")
}
```

## Proof of sanity

```js
const { grit } = require('grit');
console.log('Hello world!');
```

```js
const { grit } = require('grit');
console.error('Hello world!');
```
20 changes: 19 additions & 1 deletion .grit/patterns/universal/ai/ai.grit
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
language js
language universal

// Given a question and a list of choices,
function ai_ask($question, $choices) {
Expand Down Expand Up @@ -66,3 +66,21 @@ pattern ai_is($condition, $examples, $counter_examples) {
$answer <: "yes"
}
}


// Transform the provided code to match the given instructions
function ai_transform($match, $instruct) {
$messages = [
{
role: "system",
content: `Transform the provided code using this instruction: $instruct. Respond with a code snippet, do not provide any other text.`
},
{
role: "user",
content: $match
}
],

$answer = llm_chat($messages),
return `$answer`
}

0 comments on commit 09eb643

Please sign in to comment.