Skip to content

Commit

Permalink
support for module .mjs files (#561)
Browse files Browse the repository at this point in the history
* use tsx to run .mjs files

* mark esbuild as external

* add tsx dep

* added module test

* refine assertion

* packaging tsx

* alwasy importPrompt?

* add tidy examples

* updated globs

* various docs

* typo

* more docs tweak

* updatd samples

* notes about .mjs

* move a few tets to mjs

* updated test

* docs

* docs

* fix tests

* more mjs file

* added more docs

* package more deps

* add *.mjs to generated jsconfig

* calm down docs reviewer

* module voodoo

* updated jsconfig.json

* more logging of imports

* absolute path fix

* execution in server

* streaming of trace back to vscode

* handle cancellation

* add start stop script

* more tweaking of format

* more plumbing

* removing virtual file

* more fixes

* removing batch cli command, removing gpspecs

* more fixing

* typo

* updating fragment

* default files on phi3

* shorter summary

* tweak summary

* more plumbing

* better handling of building files

* catch exception in commands

* piping more messages to vscode

* wire cli info

* more logging on run

* fixing removal of spec

* more script fixes

* nire script fixes

* build fixes

* upgrading dependencies

* removing evalprompt

* test logging

* more logging

* Revert "removing evalprompt"

This reverts commit 6635661.

* revert eval import

* updated command.md

* use eval for .js files

* add genai-test command

* removing batch commands

* logging of csv output

* emoji for imports

* add ini test
  • Loading branch information
pelikhan authored Jun 30, 2024
1 parent 4825831 commit 10982d9
Show file tree
Hide file tree
Showing 154 changed files with 2,876 additions and 3,237 deletions.
5 changes: 1 addition & 4 deletions .github/workflows/build-genai.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ jobs:
- name: download ollama docker
run: docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
- name: run test within scripts
run: yarn test:scripts --out-summary ./summary.txt --test-delay 10
- name: Append summary.txt to $GITHUB_STEP_SUMMARY
run: |
cat packages/sample/summary.txt >> $GITHUB_STEP_SUMMARY
run: yarn test:scripts --out-summary $GITHUB_STEP_SUMMARY --test-delay 10
- name: Add comment to PR
uses: actions/github-script@v5
if: github.event_name == 'pull_request'
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/genai-commander.yml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ jobs:
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
OPENAI_API_TYPE: ${{ secrets.OPENAI_API_TYPE }}
OPENAI_API_BASE: ${{ secrets.OPENAI_API_BASE }}
- name: start ollama
if: contains(github.event.comment.body, '/genai-test')
run: docker run -d -v ollama:/root/.ollama -p 11434:11434 --name ollama ollama/ollama
- name: run test within scripts
if: contains(github.event.comment.body, '/genai-test')
run: yarn test:scripts --out-summary $GITHUB_STEP_SUMMARY --test-delay 10
- name: Archive genai results
if: always()
uses: actions/upload-artifact@v4
Expand Down
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ To compile and package the Visual Studio Code extension, run the `package` scrip
yarn package
```

You will find the built package files, `genaiscript.vsix` and `genaiscript.insiders.vsix`,
You will find the built package files, `genaiscript.vsix`,
in the `packages/vscode` folder.

## Release
Expand Down
1,078 changes: 655 additions & 423 deletions THIRD_PARTY_LICENSES.md

Large diffs are not rendered by default.

11 changes: 8 additions & 3 deletions demo/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions demo/genaisrc/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"include": [
"*.js",
"*.mjs",
"./genaiscript.d.ts"
]
}
2 changes: 1 addition & 1 deletion demo/web-app-basic-linux/summary_and_diagrams.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ stateDiagram
AppServicePlan --> WebApp
```

```js title="foo.genai.js"
```js title="foo.genai.mjs"
$`Write a one sentence poem.`
```

Expand Down
11 changes: 8 additions & 3 deletions docs/genaisrc/genaiscript.d.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions docs/genaisrc/jsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
},
"include": [
"*.js",
"*.mjs",
"./genaiscript.d.ts"
]
}
12 changes: 6 additions & 6 deletions docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
"preview": "astro preview",
"astro": "astro",
"genai:test": "node ../packages/cli/built/genaiscript.cjs test src/**/*.md",
"genai:frontmatter": "node ../packages/cli/built/genaiscript.cjs batch frontmatter src/**/*.md --apply-edits",
"genai:technical": "node ../packages/cli/built/genaiscript.cjs batch technical src/**/*.md --apply-edits",
"genai:frontmatter": "for file in \"src/**/*.md\"; do\nnode ../packages/cli/built/genaiscript.cjs run frontmatter \"$file\" --apply-edits\ndone",
"genai:technical": "for file in \"src/**/*.md\"; do\nnode ../packages/cli/built/genaiscript.cjs run technical \"$file\" --apply-edits\ndone",
"genai:alt-text": "node scripts/image-alt-text.mjs"
},
"dependencies": {
"@astrojs/check": "^0.7.0",
"@astrojs/starlight": "^0.24.2",
"astro": "^4.10.3",
"typescript": "5.4.5"
"@astrojs/starlight": "^0.24.5",
"astro": "^4.11.3",
"typescript": "5.5.2"
},
"devDependencies": {
"zx": "^8.1.2"
"zx": "^8.1.3"
}
}
28 changes: 14 additions & 14 deletions docs/src/content/docs/case-studies/image-alt-text.mdx
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
---
title: Image Alt Text
sidebar:
order: 10
order: 10
description: Learn how to automatically generate descriptive alt text for images using OpenAI Vision model to enhance accessibility and SEO.
keywords: alt text automation, image accessibility, SEO optimization, OpenAI Vision, Astro integration
---
import { Image } from 'astro:assets';
import { Code } from '@astrojs/starlight/components';

import { Image } from "astro:assets"
import { Code } from "@astrojs/starlight/components"
import scriptSrc from "../../../../genaisrc/image-alt-text.genai.js?raw"
import src from '../../../assets/debugger.png';
import src from "../../../assets/debugger.png"
import alt from "../../../assets/debugger.png.txt?raw"

It is a best practice to provide an `alt` attribute for images.
This attribute is used to describe the image to users who are unable to see it.
It is a best practice to provide an `alt` attribute for images.
This attribute is used to describe the image to users who are unable to see it.
It is also used by search engines to understand the content of the image.

```html "alt"
Expand All @@ -30,7 +31,7 @@ However, this task can be tedious and developer are often tempted to skip it, or
To solve this issue, we created a script that uses the OpenAI Vision model to analyze the documentation
images and generate a description alt text.

To start, we assume that the script is run on a single image file
To start, we assume that the script is run on a single image file
and we use [defImage](/genaiscript/reference/scripts/images) to add it to the prompt context.

```js title="image-alt-text.genai.js"
Expand All @@ -55,18 +56,16 @@ The GenAIScript documentation uses Astro, which allows to author pages in [MDX](
The code below shows how the generate alt text, stored in a separate text file, is injected in the final HTML.

```mdx

import { Image } from 'astro:assets';
import src from '../../../assets/debugger.png';
import { Image } from "astro:assets"
import src from "../../../assets/debugger.png"
import alt from "../../../assets/debugger.png.txt?raw"

<Image src={src} alt={alt}/>

<Image src={src} alt={alt} />
```

The `debugger.png` image shows the screenshot of a debugging session and the generate alt text file contents.

<Image src={src} alt={alt}/>
<Image src={src} alt={alt} />

<Code code={alt} wrap={true} lang="txt" title="debugger.png.txt" />

Expand All @@ -76,7 +75,8 @@ Using the [batch](/genaiscript/reference/cli/batch) command, we can apply the sc
in a single command.

```sh
npx --yes genaiscript batch image-alt-text assets/**.png --apply-edits
for file in assets/**.png; do
npx --yes genaiscript run image-alt-text "$file" --apply-edits
```
To avoid regenerating the alt text, we also detect if a file exists in the script and cancel accordingly.
Expand Down
10 changes: 5 additions & 5 deletions docs/src/content/docs/case-studies/seo-frontmatter.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ to merge the new front matter with the existing front matter.
## Batching over all files

Once the script has been tuned on a few files, you can automate using
the [CLI](/genaiscript/reference/cli). The CLI has a **batch** mode that applies a LLM generation
on each file and a **--apply-edits** flag to apply the changes to the file.
the [CLI](/genaiscript/reference/cli). The CLI has a **--apply-edits** flag to apply the changes to the file.

```bash
genaiscript batch frontmatter src/**/*.md --apply-edits
```sh
for file in src/**/*.md; do
npx --yes genaiscript run frontmatter "$file" --apply-edits
```
You can run this command in your CI/CD pipeline to keep your SEO front matter up to date.
Expand All @@ -38,7 +38,7 @@ Add this command to your `package.json` to make it easier to run again.
{
...
"scripts": {
"genai:frontmatter": "genaiscript batch frontmatter src/**/*.md --apply-edits",
"genai:frontmatter": "for file in \"src/**/*.md\"; do\ngenaiscript run frontmatter \"$file\" --apply-edits\ndone",
}
}
```
Expand Down
6 changes: 0 additions & 6 deletions docs/src/content/docs/faq.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ keywords: AI, script generation, performance, best practices, limitations
- **Can I use GenAIScript in IDEs other than VS Code?**
Currently, GenAIScript is integrated with VS Code, but it can be written in any IDE. The VS Code extension, however, provides additional support for creating and debugging scripts.

- **What is a GPSpec and how do I create one?**
A GPSpec is an optional markdown file that provides context for a GenAIScript. You can create one by writing a markdown document that lists arguments or includes more complex documentation.

- **What are foundation models and LLMs in the context of GenAIScript?**
Foundation models and LLMs (Large Language Models) are AI models that GenAIScript can interact with to perform tasks like generating text or processing information.

Expand Down Expand Up @@ -60,9 +57,6 @@ keywords: AI, script generation, performance, best practices, limitations
- **How can I use GenAIScript to automate document translation?**
One of our case studies illustrates the use of GenAIScript for translating document fragments between languages: [Translation Case Study](/genaiscript/case-studies/documentation-translations)

- **Is it possible to generate executable code from natural language specifications using GenAIScript?**
Yes, you can use a GPSpec to define the task and a GenAIScript to translate the spec into code in a particular programming language.

- **Can I use GenAIScript to summarize documents or create dialogues from monologues?**
Yes, LLMs are good at summarizing and can be used within GenAIScript to summarize documents or convert monologues into dialogues.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const { stdout: changes } = await host.exec("git", [
"main",
"--",
":!**/genaiscript.d.ts",
":!**/jsconfig.json",
":!genaisrc/*",
":!.github/*",
":!.vscode/*",
Expand Down
92 changes: 0 additions & 92 deletions docs/src/content/docs/getting-started/configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -392,98 +392,6 @@ model running locally, while keeping the default `openai` model connection infor
OLLAMA_PHI3_API_BASE=http://localhost:11434/v1
```

## VSCode Copilot Insiders

Visual Studio Code **Insiders** provides an
**experimental support** to use your Copilot subscription
to access a LLM inside of Visual Studio Code.

:::caution[Limitations]

- **This features requires a GitHub Copilot subscription.**
- **This feature is still a [proposed api](https://github.com/microsoft/vscode/blob/main/src/vscode-dts/vscode.proposed.languageModels.d.ts) and requires the Insiders editor.**
- The API changes daily and may not be stable.
- Functions and Images are not supported.
- The model configuration, temperature, max tokens, may not be available depending on the language model provider.
- Some scripts may fail or behave differently due to internal alignment prompts from Copilot.

:::

<Steps>

<ol>

<li>

Install [Visual Studio Code **Insiders**](https://code.visualstudio.com/insiders/).
It can be installed side-by-side with the stable version of Visual Studio Code.

</li>

<li>

Open your project in Visual Studio Code Insiders (green icon).

<Image src={insidersSrc} alt={insidersAlt} />

</li>

<li>

Download **genaiscript.insiders.vsix** from the [releases page](https://github.com/microsoft/genaiscript/releases/latest)
to your project folder.

<FileTree>

- ...
- **genaiscript.insiders.vsix**

</FileTree>

</li>

<li>

Right click on **genaiscript.insiders.vsix** and select **Install from VSIX**.

</li>

<li>

Add a `.vscode-insiders/argv.json` file to enables loading the proposed APIs for the GenAIScript extension.

```json title=".vscode-insiders/argv.json"
{
"enable-proposed-api": ["genaiscript.genaiscript-vscode"]
}
```

</li>

<li>

Restart Visual Studio Code Insiders to enable the extension.

</li>

<li>

When you try to run a script, you will be prompted to authorize the Copilot subscription.
After authorizing access, you will see a quick pick dialog to select one of the installed Language Model.

<Image src={selectLLMSrc} alt={selectLLMAlt} />

Note that this dialog is not used once you have a populated `.env` file.

</li>

</ol>

</Steps>

If you are using the Insiders build, make sure to update the **genaiscript.insiders.vsix** regularly
by repeating the steps above. The daily builds of Insiders may contain breaking chagnes that require new builds
of our extension.

## Next steps

Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/getting-started/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ The following script generates a prompt that
takes files (.txt, .pdf, .docx) as input and
saves the summaries in another files.

```js wrap title="summarize.genai.js" system=false assistant=true user=true
```js wrap title="summarize.genai.mjs" system=false assistant=true user=true
// context: define a "FILE" variable
const file = def("FILE", env.files)
// task: appends text to the prompt (file is the variable name)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/content/docs/getting-started/testing-scripts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ to validate the output of the script.

The tests are added as an array of objects in the `tests` key of the `script` function.

```js title="proofreader.genai.js" wrap
```js title="proofreader.genai.mjs" wrap
scripts({
...,
tests: {
Expand Down
Loading

0 comments on commit 10982d9

Please sign in to comment.