https://github.com/lgrammel/zx-ai
zx + vercel/ai
https://github.com/lgrammel/zx-ai
Last synced: 6 months ago
JSON representation
zx + vercel/ai
- Host: GitHub
- URL: https://github.com/lgrammel/zx-ai
- Owner: lgrammel
- License: mit
- Created: 2024-06-23T07:59:40.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-06-23T08:19:36.000Z (over 1 year ago)
- Last Synced: 2024-10-04T18:41:49.120Z (about 1 year ago)
- Language: JavaScript
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# zx-ai
Quick guide how to use the [Vercel AI SDK](https://sdk.vercel.ai) with [ZX](https://github.com/google/zx) to create executable Node.js scripts that use AI.
1. [Install zx (e.g. with brew)](https://google.github.io/zx/getting-started#install)
1. Install
```sh
npm i ai @ai-sdk/openai dotenv
```
1. Add OpenAI API key to `.env` (`OPENAI_API_KEY`)
1. Write a `.mjs` script (e.g. `hello-world.mjs`)
```ts
#!/usr/bin/env zximport { openai } from "@ai-sdk/openai";
import { streamText } from "ai";
import { configDotenv } from "dotenv";configDotenv();
const { textStream } = await streamText({
model: openai("gpt-4o"),
prompt: "How can I list files in bash?",
});for await (const textPart of textStream) {
process.stdout.write(textPart);
}
```
1. Make executable: `chmod +x hello-world.mjs`
1. Run: `./hello-world.mjs`