Skip to content

Commit

Permalink
Added example for core kit invoke
Browse files Browse the repository at this point in the history
  • Loading branch information
jig925 committed Jun 21, 2024
1 parent 0327278 commit ea3c3a6
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/examples/core-kit-invoke/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### Core Kit Invoke

The core kit `invoke` function allows us to call boards within another board. This is a powerful tool as it allows us to chain multiple boards together to create entire pipelines of logic.

## Example

In this example we are calling the `hello-world` board inside our wrapper board. The wrapper board will then concatenate the output from the `hello-world` board with an input from the wrapper board and return the concacatenated string.

## Use Cases

Chaining boards together is a powerful tool as we could use multiple LMM pipelines together. For example we could have a board which calls an API to get some data, in this case let's say this API fetches articles from a website. A wrapper board could then take these articles and perform summarization, another wrapper board could then take this summarization and perform sentiment analysis. The possibilities are endless. This allows us to build boards independently of each other and build large pipelines of logic.

for more complicated examples see the following boards:

[Hacker News Algolia Simplified Search](https://github.com/ExaDev/breadboard/blob/develop/packages/breadboard-web/src/boards/hacker-news-simplified-algolia-search.ts)
[Hacker News Algolia Simplified Story Search](https://github.com/ExaDev/breadboard/blob/develop/packages/breadboard-web/src/boards/hacker-news-simplified-algolia-story-search.ts)
[Hacker News Algolia Simplified Comment Search](https://github.com/ExaDev/breadboard/blob/develop/packages/breadboard-web/src/boards/hacker-news-simplified-algolia-search.ts)
10 changes: 10 additions & 0 deletions src/examples/core-kit-invoke/hello-world.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#! /usr/bin/env npx -y tsx

import { board } from "@google-labs/breadboard";

const graph = await board<{ message: string; }>(({ message }, { output }) => {
const renamedOutput = message.as("output").to(output());
return renamedOutput;
}).serialize()

export default graph
37 changes: 37 additions & 0 deletions src/examples/core-kit-invoke/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#! /usr/bin/env npx -y tsx

import Core, { core } from "@google-labs/core-kit";
import graph from "./hello-world.js"
import { BoardRunner, GraphDescriptor, asRuntimeKit, base, code } from "@google-labs/breadboard";

const concat = code<{string1: string, string2: string}>((inputs) => {
const {string1, string2} = inputs

const concatResult = string1.concat(string2)
return {result: concatResult}
});

// Invokes our Hello World board and returns the "Hello World" as the output
const invocation = core.invoke({
$metadata: { title: "Invoke Hello World Board" },
$board: graph,
message: "Hello World"
});

const output = base.output();
// Takes the output from the Hello World Board and concatenates it with a string
const {result} = concat({string1: invocation.output.isString(), string2: " from the wrapper!"})

// Returns concat result as board output
result.to(output)

const serialised = await output.serialize({
title: "Core Kit Invoke Example",
});

const runner = await BoardRunner.fromGraphDescriptor(serialised as GraphDescriptor);
for await (const stop of runner.run({kits: [asRuntimeKit(Core)] })) {
if (stop.type === "output") {
console.log(JSON.stringify(stop.outputs, null, 2));
}
}

0 comments on commit ea3c3a6

Please sign in to comment.