breadboard-ai / breadboard

A library for prototyping generative AI applications.
Apache License 2.0
160 stars 22 forks source link

Create example showing code node created inside and outside board, and anonymously #1912

Closed TinaNikou closed 1 week ago

TinaNikou commented 3 months ago

Add example showing the code node being used that is created inside and outside a board, and anonymously.

The created sample will need to be added to the breadboard mono-repo, and then referenced and linked in the Breadboard README doc (in the "Creating code nodes" section of packages/breadboard/README.md).

A sample has been created:

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

// Created outside board
const split = code(({ toSplit }) => {
    if (typeof toSplit !== "string") return {};
    return { split: toSplit.split("") };
});

const reverserBoard = board(() => {
    const input = base.input();
    const output = base.output();

    const splitNode = split();
    input.message.as("toSplit").to(splitNode);

    // Created inside board
    const reverse = code(({ toReverse }) => {
        if (!Array.isArray(toReverse)) return {};
        return { reversed: toReverse.reverse() };
    });

    const reverseNode = reverse();
    splitNode.split.as("toReverse").to(reverseNode);

    // Anonymous
    const concat = reverseNode.reversed.as("arr").to(
        code(({ arr }) => {
            if (!Array.isArray(arr)) return {};

            return { concatenated: arr.join("") };
        })()
    );

    concat.concatenated.to(output);

    return output;
});

console.log(await reverserBoard({ message: "Hello World!" })); // { concat: '!dlroW olleH' }
TinaNikou commented 3 months ago

Discussed with Joe and going to write this up into a fuller doc

TinaNikou commented 1 week ago

This applies to the old syntax.