Skip to content

Commit

Permalink
Add If and Box nodes (#89)
Browse files Browse the repository at this point in the history
* Integrate `Box` and `If` nodes from OpenAPI spec (updated internal field to avoid collision)

* Add `Box` and `If` examples

* Bump version

* Fix formatting
  • Loading branch information
liamgriffiths authored Jun 21, 2024
1 parent 34b80f1 commit 71df9b8
Show file tree
Hide file tree
Showing 15 changed files with 1,048 additions and 426 deletions.
2 changes: 1 addition & 1 deletion bin/update-version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { execSync } from "node:child_process";

// NOTE: Merged with API version to produce the full SDK version string
// https://docs.substrate.run/versioning
const SDK_VERSION = "1.0.1";
const SDK_VERSION = "1.0.2";

const ok = (message: string) => console.log("\x1b[32m✓\x1b[0m", message);

Expand Down
38 changes: 38 additions & 0 deletions examples/box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env -S npx ts-node --transpileOnly

import { Substrate, ComputeText, Box, sb } from "substrate";

async function main() {
const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"];

const substrate = new Substrate({ apiKey: SUBSTRATE_API_KEY });

const languages = ["swedish", "ukranian", "thai", "turkish"] as const;

const texts: Record<string, ComputeText> = languages.reduce(
(nodes, language) => {
return {
...nodes,
[language]: new ComputeText({
prompt: sb.interpolate`count to 10 in ${language}`,
max_tokens: 50,
}),
};
},
{},
);

const box = new Box({
value: languages.reduce((obj, language) => {
return {
...obj,
[language]: texts[language]!.future.text,
};
}, {}),
});

const res = await substrate.run(box);

console.log({ box: res.get(box) });
}
main();
63 changes: 63 additions & 0 deletions examples/if.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env -S npx ts-node --transpileOnly

import { Substrate, ComputeJSON, Box, If, sb } from "substrate";

async function main() {
const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"];

const substrate = new Substrate({ apiKey: SUBSTRATE_API_KEY });

const planets = ["Jupiter", "Mars"];

const sizes = planets.map((planet) => {
return new ComputeJSON({
prompt: sb.interpolate`How big is ${planet}?`,
json_schema: {
type: "object",
properties: {
planetName: {
type: "string",
description: "The name of the planet",
enum: planets,
},
radius: {
type: "string",
description: "The radius of the planet in kilometers",
},
},
},
});
});

const [jupiter, mars] = sizes as [ComputeJSON, ComputeJSON];
const radius = (p: ComputeJSON) =>
p.future.json_object.get("radius") as unknown as number;

const comparison = new ComputeJSON({
prompt: sb.interpolate`Is ${radius(jupiter)} > ${radius(mars)}?`,
json_schema: {
type: "object",
properties: {
isGreaterThan: {
type: "boolean",
},
},
},
});

const result = new If({
condition: comparison.future.json_object.get("isGreaterThan") as any,
value_if_true: jupiter.future.json_object,
value_if_false: mars.future.json_object,
});

const output = new Box({
value: sb.interpolate`The bigger planet is ${result.future.result.get(
"planetName",
)}!`,
});

const res = await substrate.run(output);
console.log(res.get(output));
}
main();
18 changes: 18 additions & 0 deletions examples/kitchen-sink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
Mixtral8x7BInstruct,
Llama3Instruct8B,
Llama3Instruct70B,
If,
Box,
} from "substrate";

const STAGING = "https://api-staging.substrate.run";
Expand All @@ -50,6 +52,22 @@ const ALL_ENVS = [STAGING, PRODUCTION];
const VECTOR_STORE = "kitchen-sink";

const examples = [
new Box({
value: {
a: 1,
b: [2, 3, { c: 1 }],
d: { e: [4] },
},
}),
new If({
condition: true,
value_if_true: "yes",
value_if_false: "no",
}),
new FindOrCreateVectorStore({
collection_name: VECTOR_STORE,
model: "jina-v2",
}),
new FindOrCreateVectorStore({
collection_name: VECTOR_STORE,
model: "jina-v2",
Expand Down
22 changes: 22 additions & 0 deletions examples/qa.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/env -S npx ts-node --transpileOnly

import { Substrate, Box, sb } from "substrate";

async function main() {
const SUBSTRATE_API_KEY = process.env["SUBSTRATE_API_KEY"];

const substrate = new Substrate({ apiKey: SUBSTRATE_API_KEY });

const node = new Box({
value: {
a: "b",
c: {
d: [1, 2, 3],
},
},
});
const res = await substrate.run(node);
console.log(res.apiResponse.status);
console.log(JSON.stringify(res.json));
}
main();
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "substrate",
"version": "120240617.0.1",
"version": "120240617.0.2",
"description": "The official SDK for the Substrate API",
"repository": {
"type": "git",
Expand Down
24 changes: 12 additions & 12 deletions src/Future.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export class Trace extends Directive {
for (let item of this.items) {
if (item instanceof Future) {
// @ts-expect-error (protected result())
item = await item.result();
item = await item._result();
}
result = result[item as string | number];
}
Expand Down Expand Up @@ -127,7 +127,7 @@ export class JQ extends Directive {
override async result(): Promise<JQCompatible> {
return this.target instanceof Future
? // @ts-expect-error (accessing protected prop: id)
await this.target.result()
await this.target._result()
: this.target;
}

Expand Down Expand Up @@ -166,7 +166,7 @@ export class StringConcat extends Directive {
for (let item of this.items) {
if (item instanceof Future) {
// @ts-expect-error (protected result())
item = await item.result();
item = await item._result();
}
result = result.concat(item);
}
Expand Down Expand Up @@ -204,7 +204,7 @@ export abstract class Future<T> {
return { __$$SB_GRAPH_OP_ID$$__: this._id };
}

protected async result(): Promise<T> {
protected async _result(): Promise<T> {
return this._directive.result();
}

Expand Down Expand Up @@ -263,8 +263,8 @@ export class FutureString extends Future<string> {
return FutureString.concat(...[this, ...items]);
}

protected override async result(): Promise<string> {
return super.result();
protected override async _result(): Promise<string> {
return super._result();
}
}

Expand All @@ -273,8 +273,8 @@ export class FutureNumber extends Future<number> {}
export abstract class FutureArray extends Future<any[] | FutureArray> {
abstract at(index: number): Future<any>;

protected override async result(): Promise<any[] | FutureArray> {
return super.result();
protected override async _result(): Promise<any[] | FutureArray> {
return super._result();
}
}

Expand All @@ -293,8 +293,8 @@ export abstract class FutureObject extends Future<Object> {
}, this) as Future<any>;
}

protected override async result(): Promise<Object> {
return super.result();
protected override async _result(): Promise<Object> {
return super._result();
}
}

Expand All @@ -311,7 +311,7 @@ export class FutureAnyObject extends Future<Object> {
return new FutureAnyObject(this._directive.next(index));
}

protected override async result(): Promise<Object> {
return super.result();
protected override async _result(): Promise<Object> {
return super._result();
}
}
2 changes: 1 addition & 1 deletion src/GEN_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
20240617.20240620
20240617.20240621
Loading

0 comments on commit 71df9b8

Please sign in to comment.