Skip to content

Commit

Permalink
[syntax] added support for memoized (nee constant) wires
Browse files Browse the repository at this point in the history
  • Loading branch information
seefeldb committed Nov 15, 2023
1 parent 44bfe92 commit cfe89f0
Show file tree
Hide file tree
Showing 8 changed files with 298 additions and 73 deletions.
24 changes: 24 additions & 0 deletions seeds/breadboard-web/docs/graphs/new/accumulating-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## accumulating-context.ts

```mermaid
%%{init: 'themeVariables': { 'fontFamily': 'Fira Code, monospace' }}%%
graph TD;
userRequest[/"input <br> id='userRequest'"/]:::input -- "text->question" --> assistant["promptTemplate <br> id='assistant'"]
userRequest[/"input <br> id='userRequest'"/]:::input -- "text->user" --> append0["append <br> id='append-0'"]
start(("passthrough <br> id='start'")):::passthrough --> userRequest[/"input <br> id='userRequest'"/]:::input
output3{{"output <br> id='output-3'"}}:::output --> userRequest[/"input <br> id='userRequest'"/]:::input
assistant["promptTemplate <br> id='assistant'"] -- "prompt->text" --> generateText2["generateText <br> id='generateText-2'"]
append0["append <br> id='append-0'"] -- "accumulator->accumulator" --> append0["append <br> id='append-0'"]
append0["append <br> id='append-0'"] -- "accumulator->context" --> assistant["promptTemplate <br> id='assistant'"]
generateText2["generateText <br> id='generateText-2'"] -- "completion->accumulator" --> append0["append <br> id='append-0'"]
generateText2["generateText <br> id='generateText-2'"] -- "completion->text" --> output3{{"output <br> id='output-3'"}}:::output
secrets1("secrets <br> id='secrets-1'"):::secrets -- "PALM_KEY->PALM_KEY" --o generateText2["generateText <br> id='generateText-2'"]
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
```
4 changes: 2 additions & 2 deletions seeds/breadboard-web/docs/graphs/new/custom-inline-action.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
```mermaid
%%{init: 'themeVariables': { 'fontFamily': 'Fira Code, monospace' }}%%
graph TD;
fn7["runJavascript <br> id='fn-7'"] -- all --> output6{{"output <br> id='output-6'"}}:::output
input5[/"input <br> id='input-5'"/]:::input -- all --> fn7["runJavascript <br> id='fn-7'"]
fn89["fn-8 <br> id='fn-8-9'"] -- all --> output7{{"output <br> id='output-7'"}}:::output
input6[/"input <br> id='input-6'"/]:::input -- all --> fn89["fn-8 <br> id='fn-8-9'"]
classDef default stroke:#ffab40,fill:#fff2ccff,color:#000
classDef input stroke:#3c78d8,fill:#c9daf8ff,color:#000
classDef output stroke:#38761d,fill:#b6d7a8ff,color:#000
Expand Down
141 changes: 141 additions & 0 deletions seeds/breadboard-web/public/graphs/new/accumulating-context.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
{
"title": "New: Accumulating context",
"edges": [
{
"from": "userRequest",
"to": "assistant",
"out": "text",
"in": "question"
},
{
"from": "userRequest",
"to": "append-0",
"out": "text",
"in": "user"
},
{
"from": "start",
"to": "userRequest",
"out": "",
"in": ""
},
{
"from": "output-3",
"to": "userRequest",
"out": "",
"in": ""
},
{
"from": "assistant",
"to": "generateText-2",
"out": "prompt",
"in": "text"
},
{
"from": "append-0",
"to": "append-0",
"out": "accumulator",
"in": "accumulator"
},
{
"from": "append-0",
"to": "assistant",
"out": "accumulator",
"in": "context"
},
{
"from": "generateText-2",
"to": "append-0",
"out": "completion",
"in": "accumulator"
},
{
"from": "generateText-2",
"to": "output-3",
"out": "completion",
"in": "text"
},
{
"from": "secrets-1",
"to": "generateText-2",
"out": "PALM_KEY",
"in": "PALM_KEY",
"constant": true
}
],
"nodes": [
{
"id": "userRequest",
"type": "input",
"configuration": {
"schema": {
"type": "object",
"properties": {
"text": {
"type": "string",
"title": "User",
"description": "Type here to chat with the assistant"
}
},
"required": [
"text"
]
}
}
},
{
"id": "start",
"type": "passthrough",
"configuration": {}
},
{
"id": "output-3",
"type": "output",
"configuration": {
"schema": {
"type": "object",
"properties": {
"text": {
"type": "string",
"title": "Assistant",
"description": "Assistant's response in the conversation with the user"
}
},
"required": [
"text"
]
}
}
},
{
"id": "assistant",
"type": "promptTemplate",
"configuration": {
"template": "This is a conversation between a friendly assistant and their user. You are the assistant and your job is to try to be helpful, empathetic, and fun.\n{{context}}\n\n== Current Conversation\nuser: {{question}}\nassistant:",
"context": ""
}
},
{
"id": "append-0",
"type": "append",
"configuration": {
"accumulator": "\n== Conversation History"
}
},
{
"id": "generateText-2",
"type": "generateText",
"configuration": {}
},
{
"id": "secrets-1",
"type": "secrets",
"configuration": {
"keys": [
"PALM_KEY"
]
}
}
],
"graphs": {}
}
21 changes: 9 additions & 12 deletions seeds/breadboard-web/public/graphs/new/custom-inline-action.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,31 @@
"title": "New: Custom inline action",
"edges": [
{
"from": "fn-7",
"to": "output-6",
"from": "fn-8-9",
"to": "output-7",
"out": "*",
"in": "*"
},
{
"from": "input-5",
"to": "fn-7",
"from": "input-6",
"to": "fn-8-9",
"out": "*",
"in": "*"
}
],
"nodes": [
{
"id": "output-6",
"id": "output-7",
"type": "output",
"configuration": {}
},
{
"id": "fn-7",
"type": "runJavascript",
"configuration": {
"code": "async function fn_7(inputs2) {const{a,b}=await inputs2;return{result:(a||0)+(b||0)}}",
"name": "fn_7"
}
"id": "fn-8-9",
"type": "fn-8",
"configuration": {}
},
{
"id": "input-5",
"id": "input-6",
"type": "input",
"configuration": {}
}
Expand Down
2 changes: 1 addition & 1 deletion seeds/breadboard-web/public/local-boards.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
},
{
"title": "New: Accumulating context",
"url": "/graphs/new/accumulator.json"
"url": "/graphs/new/accumulating-context.json"
},
{
"title": "New: Custom inline action",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const input = base.input({
},
});

core.passthrough({ $id: "start" }).to(input);
core.passthrough({ $id: "start" }).as({}).to(input);

const prompt = llm.promptTemplate({
template:
Expand All @@ -33,24 +33,37 @@ const prompt = llm.promptTemplate({

const conversationMemory = llm.append({
accumulator: "\n== Conversation History",
$id: "conversationMemory",
user: input.text,
});
conversationMemory.accumulator.to(conversationMemory);
conversationMemory.in(conversationMemory.accumulator);
prompt.in({ context: conversationMemory.accumulator });
// conversationMemory.accumulator.to(prompt.context); ???
// conversationMemory.accumulator.as("context").to(prompt);

const response = llm.generateText({
text: prompt.prompt,
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY,
PALM_KEY: llm.secrets({ keys: ["PALM_KEY"] }).PALM_KEY.memoize(),
});

conversationMemory.in({ accumulator: response.completion });
// response.completion.to(conversationMemory.accumulator);

prompt.in({ context: conversationMemory.accumulator });
const output = base.output({
text: response.completion,

const output = base.output({ text: response.completion });
schema: {
type: "object",
properties: {
text: {
type: "string",
title: "Assistant",
description: "Assistant's response in the conversation with the user",
},
},
required: ["text"],
},
});

// TODO: Don't send data, just "->"
output.to(input);
output.as({}).to(input);

export const graph = input; // Any node would work here.

Expand Down
12 changes: 7 additions & 5 deletions seeds/breadboard-web/src/boards/new/custom-inline-action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
* SPDX-License-Identifier: Apache-2.0
*/

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

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

export const example = { a: 1, b: 2 };
Expand Down
Loading

0 comments on commit cfe89f0

Please sign in to comment.