Skip to content

Commit

Permalink
[syntax] moved examples into individual files
Browse files Browse the repository at this point in the history
`tsx src/new/run-examples.ts` runs them all in the shell
  • Loading branch information
seefeldb committed Nov 13, 2023
1 parent 76c286d commit 7d900b2
Show file tree
Hide file tree
Showing 16 changed files with 395 additions and 316 deletions.
15 changes: 15 additions & 0 deletions seeds/breadboard-web/docs/graphs/new/single-node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## single-node.ts

```mermaid
%%{init: 'themeVariables': { 'fontFamily': 'Fira Code, monospace' }}%%
graph TD;
classDef default stroke:#ffab40,fill:#fff2ccff,color:#000
classDef input stroke:#3c78d8,fill:#c9daf8ff,color:#000
classDef output stroke:#38761d,fill:#b6d7a8ff,color:#000
classDef passthrough stroke:#a64d79,fill:#ead1dcff,color:#000
classDef slot stroke:#a64d79,fill:#ead1dcff,color:#000
classDef config stroke:#a64d79,fill:#ead1dcff,color:#000
classDef secrets stroke:#db4437,fill:#f4cccc,color:#000
classDef slotted stroke:#a64d79
```
14 changes: 14 additions & 0 deletions seeds/breadboard-web/public/graphs/new/single-node.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"title": "Single Node",
"edges": [],
"nodes": [
{
"id": "passthrough-0",
"type": "passthrough",
"configuration": {
"foo": "bar"
}
}
],
"graphs": {}
}
4 changes: 4 additions & 0 deletions seeds/breadboard-web/public/local-boards.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
"title": "List Google Drive files",
"url": "/graphs/list-files.json"
},
{
"title": "Single Node",
"url": "/graphs/new/single-node.json"
},
{
"title": "Tour Guide Writer",
"url": "/graphs/tour-guide-writer.json"
Expand Down
19 changes: 19 additions & 0 deletions seeds/breadboard-web/src/boards/new/custom-inline-action.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow } from "../../new/lib.js";

export const graph = flow(
(inputs) => {
return flow(async (inputs) => {
const { a, b } = await inputs;
return { result: ((a as number) || 0) + ((b as number) || 0) };
}, inputs);
},
{ a: 1, b: 2 }
);

export default await graph.serialize({ title: "Custom inline action" });
55 changes: 55 additions & 0 deletions seeds/breadboard-web/src/boards/new/if-else-imperative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow, action } from "../../new/lib.js";
import { llm } from "../../new/kits.js";

const math = action((inputs) => {
return llm
.promptTemplate({
template:
"Write a Javascript function called `run` to compute the result for this question:\nQuestion: {{question}}\nCode: ",
question: inputs.question,
})
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
)
.completion.as("code")
.to(llm.runJavascript());
});

const search = action((inputs) => {
// TODO: Implement
return inputs;
});

export const graph = flow(
async (inputs) => {
const { completion } = await llm
.promptTemplate({
template:
"Is this question about math? Answer YES or NO.\nQuestion: {{question}}\nAnswer: ",
question: inputs.question,
})
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
);
if (completion && (completion as string).startsWith("YES")) {
return math({ question: inputs.question });
} else {
return search(inputs);
}
},
{ question: "1+1" }
);

export default await graph.serialize({ title: "IfElse, imperative execution" });
61 changes: 61 additions & 0 deletions seeds/breadboard-web/src/boards/new/if-else-serializable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow, action } from "../../new/lib.js";
import { llm } from "../../new/kits.js";

const math = action((inputs) => {
return llm
.promptTemplate({
template:
"Write a Javascript function called `run` to compute the result for this question:\nQuestion: {{question}}\nCode: ",
question: inputs.question,
})
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
)
.completion.as("code")
.to(llm.runJavascript());
});

const search = action((inputs) => {
// TODO: Implement
return inputs;
});

export const graph = flow(
async (inputs) => {
return llm
.promptTemplate({
template:
"Is this question about math? Answer YES or NO.\nQuestion: {{question}}\nAnswer: ",
question: inputs.question,
})
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
)
.to(
async (inputs) => {
const { completion, math, search } = await inputs;
if (completion?.startsWith("YES")) {
return math({ question: inputs.question });
} else {
return search(inputs);
}
},
{ math, search }
);
},
{ question: "1+1" }
);

export default await graph.serialize({ title: "IfElse, serializable" });
30 changes: 30 additions & 0 deletions seeds/breadboard-web/src/boards/new/math-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow } from "../../new/lib.js";
import { llm } from "../../new/kits.js";

export const graph = flow(
(inputs) => {
return llm
.promptTemplate({
template:
"Write a Javascript function called `run` to compute the result for this question:\nQuestion: {{question}}\nCode: ",
question: inputs.question,
})
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
)
.completion.as("code")
.to(llm.runJavascript());
},
{ question: "1+1" }
);

export default await graph.serialize({ title: "Math, chain style graph" });
27 changes: 27 additions & 0 deletions seeds/breadboard-web/src/boards/new/math-imperative.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow } from "../../new/lib.js";
import { llm } from "../../new/kits.js";

export const graph = flow(
(inputs) => {
const { prompt } = llm.promptTemplate({
template:
"Write a Javascript function called `run` to compute the result for this question:\nQuestion: {{question}}\nCode: ",
question: inputs.question,
});
const { completion } = llm.generateText({
text: prompt,
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
});
const result = llm.runJavascript({ code: completion });
return result;
},
{ question: "1+1" }
);

export default await graph.serialize({ title: "Math, imperative style graph" });
32 changes: 32 additions & 0 deletions seeds/breadboard-web/src/boards/new/math-inline-chain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { core, llm } from "../../new/kits.js";

const question = "1+1";

export const graph = core
.passthrough({ question })
.to(
llm.promptTemplate({
template:
"Write a Javascript function called `run` to compute the result for this question:\nQuestion: {{question}}\nCode: ",
})
)
.prompt.as("text")
.to(
llm.generateText({
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
})
)
.completion.as("code")
.to(llm.runJavascript());

// This would be typically used as "await graph", not as a (serialized) graph

export default await graph.serialize({
title: "Math, directly calling a chain",
});
19 changes: 19 additions & 0 deletions seeds/breadboard-web/src/boards/new/simple-graph.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow } from "../../new/lib.js";
import { core } from "../../new/kits.js";

export const graph = flow(
(inputs) => {
const p1 = core.passthrough(inputs);
const { foo } = p1; // Get an output, as a Promise!
return { foo };
},
{ foo: "bar", bar: "baz" }
);

export default await graph.serialize({ title: "Simple graph" });
18 changes: 18 additions & 0 deletions seeds/breadboard-web/src/boards/new/simple-imperative-function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { flow } from "../../new/lib.js";
import { core } from "../../new/kits.js";

export const graph = flow(
async (inputs) => {
const { foo } = await core.passthrough(inputs);
return { foo };
},
{ foo: "bar", baz: "bar" }
);

export default await graph.serialize({ title: "Simple imperative function" });
13 changes: 13 additions & 0 deletions seeds/breadboard-web/src/boards/new/single-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* @license
* Copyright 2023 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

import { core } from "../../new/kits.js";

export const graph = core.passthrough({ foo: "bar" });

// This would be typically used as "await graph", not as a (serialized) graph

export default await graph.serialize({ title: "Single node" });
Loading

0 comments on commit 7d900b2

Please sign in to comment.