Skip to content

Commit

Permalink
Universal compiler (#12)
Browse files Browse the repository at this point in the history
* Universal compiler

* wip

* fix style issues
  • Loading branch information
neoxelox authored Jan 21, 2025
1 parent 535dc7b commit 2fe91d5
Show file tree
Hide file tree
Showing 18 changed files with 717 additions and 49 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,8 @@ This is just a small example of what PromptL can do. It is a powerful tool that
## Links

[Website](https://promptl.ai/) | [Documentation](https://docs.latitude.so/promptl/getting-started/introduction)

## Development

- To build the JavaScript library, run `pnpm build:lib`
- To build the universal WASM module with RPC, first install [`javy`](https://github.com/bytecodealliance/javy/releases) and then run `pnpm build:rpc`
1 change: 1 addition & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export default [
globals: {
...globals.node,
...globals.browser,
Javy: 'readonly',
},

parser: tsParser,
Expand Down
31 changes: 31 additions & 0 deletions examples/lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Run `pnpm build:lib` before running this example

import assert from 'node:assert'
import { inspect } from 'node:util'
import { Chain } from '../dist/index.js'

const prompt = `
<step>
<system>
You are a helpful assistant.
</system>
<user>
Say hello.
</user>
</step>
<step>
<user>
Now say goodbye.
</user>
</step>
`

const chain = new Chain({ prompt })
let conversation = await chain.step()
conversation = await chain.step('Hello!')
conversation = await chain.step('Goodbye!')

assert(chain.completed)
assert(conversation.completed)

console.log(inspect(conversation.messages, { depth: null }))
148 changes: 148 additions & 0 deletions examples/rpc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Run `pnpm build:rpc` before running this example

import assert from 'node:assert'
import { FileHandle, mkdir, open, readFile, writeFile } from 'node:fs/promises'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { inspect } from 'node:util'
import { WASI } from 'node:wasi'

const PROMPTL_WASM_PATH = './dist/promptl.wasm'

const prompt = `
<step>
<system>
You are a helpful assistant.
</system>
<user>
Say hello.
</user>
</step>
<step>
<user>
Now say goodbye.
</user>
</step>
`
let chain, conversation
chain = await createChain(prompt);
({ chain, ...conversation } = await stepChain(chain));
({ chain, ...conversation } = await stepChain(chain, 'Hello!'));
({ chain, ...conversation } = await stepChain(chain, 'Goodbye!'));

assert(chain.completed)
assert(conversation.completed)

console.log(inspect(conversation.messages, { depth: null }))

// Utility functions

async function createChain(prompt: string): Promise<any> {
return await execute([
{
id: 1,
procedure: 'createChain',
parameters: {
prompt: prompt,
},
},
]).then((result) => result[0]!.value)
}

async function stepChain(chain: any, response?: any): Promise<any> {
return await execute([
{
id: 1,
procedure: 'stepChain',
parameters: {
chain: chain,
response: response,
},
},
]).then((result) => result[0]!.value)
}

async function execute(data: any): Promise<any> {
const dir = join(tmpdir(), 'promptl')
const stdin_path = join(dir, 'stdin')
const stdout_path = join(dir, 'stdout')
const stderr_path = join(dir, 'stderr')

await mkdir(dir, { recursive: true })
await writeFile(stdin_path, '')
await writeFile(stdout_path, '')
await writeFile(stderr_path, '')

let stdin: FileHandle | undefined
let stdout: FileHandle | undefined
let stderr: FileHandle | undefined

let wasmStdin: FileHandle | undefined
let wasmStdout: FileHandle | undefined
let wasmStderr: FileHandle | undefined

try {
stdin = await open(stdin_path, 'w')
stdout = await open(stdout_path, 'r')
stderr = await open(stderr_path, 'r')

wasmStdin = await open(stdin_path, 'r')
wasmStdout = await open(stdout_path, 'w')
wasmStderr = await open(stderr_path, 'w')

const wasi = new WASI({
version: 'preview1',
args: [],
env: {},
stdin: wasmStdin.fd,
stdout: wasmStdout.fd,
stderr: wasmStderr.fd,
returnOnExit: true,
})

const bytes = await readFile(PROMPTL_WASM_PATH)
WebAssembly.validate(bytes)
const module = await WebAssembly.compile(bytes)
const instance = await WebAssembly.instantiate(
module,
wasi.getImportObject(),
)

await send(stdin, data)

wasi.start(instance)

const [out, err] = await Promise.all([receive(stdout), receive(stderr)])
if (err) throw new Error(err)

return out
} finally {
await Promise.all([
stdin?.close(),
stdout?.close(),
stderr?.close(),
wasmStdin?.close(),
wasmStdout?.close(),
wasmStderr?.close(),
])
}
}

async function send(file: FileHandle, data: any) {
await writeFile(file, JSON.stringify(data) + '\n', {
encoding: 'utf8',
flush: true,
})
}

async function receive(file: FileHandle): Promise<any> {
const data = await readFile(file, {
encoding: 'utf8',
}).then((data) => data.trim())

try {
return JSON.parse(data)
} catch {
return data
}
}
13 changes: 0 additions & 13 deletions examples/start.ts

This file was deleted.

9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
],
"scripts": {
"dev": "rollup -c -w",
"build": "rollup -c",
"build:lib": "rollup -c",
"build:rpc": "rollup -c rollup.config.rpc.mjs",
"test": "vitest run",
"test:watch": "vitest",
"prettier": "prettier --write src/**/*.ts",
Expand All @@ -35,24 +36,28 @@
"dependencies": {
"acorn": "^8.9.0",
"code-red": "^1.0.3",
"fast-sha256": "^1.3.0",
"locate-character": "^3.0.0",
"yaml": "^2.4.5",
"zod": "^3.23.8"
},
"devDependencies": {
"eslint": "^9.17.0",
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.17.0",
"@rollup/plugin-alias": "^5.1.0",
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.6",
"@types/estree": "^1.0.1",
"@types/node": "^20.12.12",
"@typescript-eslint/eslint-plugin": "^8.19.0",
"eslint": "^9.17.0",
"eslint-plugin-prettier": "^5.2.1",
"globals": "^15.14.0",
"prettier": "^3.4.2",
"rollup": "^4.10.0",
"rollup-plugin-dts": "^6.1.1",
"rollup-plugin-execute": "^1.1.1",
"tslib": "^2.8.1",
"tsx": "^4.19.2",
"typescript": "^5.6.3",
Expand Down
Loading

0 comments on commit 2fe91d5

Please sign in to comment.