Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rainchen/cc486f9fe2b169cbcf20a10f490b6f1d to your computer and use it in GitHub Desktop.
Save rainchen/cc486f9fe2b169cbcf20a10f490b6f1d to your computer and use it in GitHub Desktop.
iteration node demo break-while-loop poc
app:
description: ''
icon: "\U0001F916"
icon_background: '#FFEAD5'
mode: advanced-chat
name: 'iteration node demo: break-while-loop-poc'
workflow:
features:
file_upload:
image:
enabled: false
number_limits: 3
transfer_methods:
- local_file
- remote_url
opening_statement: click go to start
retriever_resource:
enabled: false
sensitive_word_avoidance:
enabled: false
speech_to_text:
enabled: false
suggested_questions:
- go
suggested_questions_after_answer:
enabled: false
text_to_speech:
enabled: false
language: ''
voice: ''
graph:
edges:
- data:
isInIteration: false
sourceType: iteration
targetType: code
id: 1721200808708-source-1721202208578-target
source: '1721200808708'
sourceHandle: source
target: '1721202208578'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: code
targetType: answer
id: 1721202208578-source-answer-target
source: '1721202208578'
sourceHandle: source
target: answer
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: true
iteration_id: '1721200808708'
sourceType: code
targetType: if-else
id: 1721204234263-source-1721204196722-target
source: '1721204234263'
sourceHandle: source
target: '1721204196722'
targetHandle: target
type: custom
zIndex: 1002
- data:
isInIteration: true
iteration_id: '1721200808708'
sourceType: code
targetType: code
id: 1721213193234-source-1721204234263-target
source: '1721213193234'
sourceHandle: source
target: '1721204234263'
targetHandle: target
type: custom
zIndex: 1002
- data:
isInIteration: true
iteration_id: '1721200808708'
sourceType: code
targetType: code
id: 1721214225930-source-1721201702365-target
source: '1721214225930'
sourceHandle: source
target: '1721201702365'
targetHandle: target
type: custom
zIndex: 1002
- data:
isInIteration: true
iteration_id: '1721200808708'
sourceType: if-else
targetType: code
id: 1721204196722-true-1721215004550-target
source: '1721204196722'
sourceHandle: 'true'
target: '1721215004550'
targetHandle: target
type: custom
zIndex: 1002
- data:
isInIteration: true
iteration_id: '1721200808708'
sourceType: code
targetType: code
id: 1721215004550-source-1721214225930-target
source: '1721215004550'
sourceHandle: source
target: '1721214225930'
targetHandle: target
type: custom
zIndex: 1002
- data:
isInIteration: false
sourceType: code
targetType: iteration
id: 1721201056657-source-1721200808708-target
source: '1721201056657'
sourceHandle: source
target: '1721200808708'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: code
targetType: code
id: 1721213003983-source-1721201056657-target
source: '1721213003983'
sourceHandle: source
target: '1721201056657'
targetHandle: target
type: custom
zIndex: 0
- data:
isInIteration: false
sourceType: start
targetType: code
id: 1721200069100-source-1721213003983-target
source: '1721200069100'
sourceHandle: source
target: '1721213003983'
targetHandle: target
type: custom
zIndex: 0
nodes:
- data:
desc: ''
selected: false
title: Start
type: start
variables:
- label: max_iteration
max_length: 256
options: []
required: true
type: number
variable: max_iteration
- label: stop_flag
max_length: 48
options: []
required: true
type: number
variable: stop_flag
height: 116
id: '1721200069100'
position:
x: 30
y: 396
positionAbsolute:
x: 30
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
answer: '{{#1721202208578.result#}}'
desc: ''
selected: false
title: Answer
type: answer
variables: []
height: 107
id: answer
position:
x: 3091
y: 396
positionAbsolute:
x: 3091
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
desc: ''
height: 392
iterator_selector:
- '1721201056657'
- result
output_selector:
- '1721201702365'
- result
output_type: array[string]
selected: false
startNodeType: code
start_node_id: '1721213193234'
title: Iteration
type: iteration
width: 1785.1428571428573
height: 392
id: '1721200808708'
position:
x: 942
y: 396
positionAbsolute:
x: 942
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 1785
zIndex: 1
- data:
code: "\ndef main(index: int, item: int) -> dict:\n result = f\"index:{index},\
\ item:{item}\"\n return {\n \"result\": result,\n }\n"
code_language: python3
desc: return an list of string like "index:{index}, item:{item}"
isInIteration: true
isIterationStart: false
iteration_id: '1721200808708'
outputs:
result:
children: null
type: string
selected: false
title: Code:return_result
type: code
variables:
- value_selector:
- '1721200808708'
- item
variable: item
- value_selector:
- '1721200808708'
- index
variable: index
extent: parent
height: 102
id: '1721201702365'
parentId: '1721200808708'
position:
x: 1525.1428571428573
y: 116.42857142857144
positionAbsolute:
x: 2467.1428571428573
y: 512.4285714285714
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1001
- data:
code: "\ndef main(max_iteration: int) -> dict:\n arr = list(range(1, max_iteration\
\ + 1))\n return {\n \"result\": arr,\n }\n"
code_language: python3
desc: creates an array based on the given max_iteration value
outputs:
result:
children: null
type: array[number]
selected: false
title: Code:create_array
type: code
variables:
- value_selector:
- '1721200069100'
- max_iteration
variable: max_iteration
height: 102
id: '1721201056657'
position:
x: 638
y: 396
positionAbsolute:
x: 638
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
code: "\ndef main(iteration_result: list) -> dict:\n return {\n \
\ \"result\": \"\\n\".join(iteration_result),\n }\n"
code_language: python3
desc: join the list into a string with "\n"
outputs:
result:
children: null
type: string
selected: false
title: Code:join_iteration_result
type: code
variables:
- value_selector:
- '1721200808708'
- output
variable: iteration_result
height: 84
id: '1721202208578'
position:
x: 2787
y: 396
positionAbsolute:
x: 2787
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
author: admin
desc: ''
height: 91
selected: false
showAuthor: false
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"setup
a Code node to pass a \"session\" dict object since dict object is mutable","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ''
type: ''
width: 259
height: 91
id: '1721203889515'
position:
x: 325.2536823280344
y: 535.8401952112322
positionAbsolute:
x: 325.2536823280344
y: 535.8401952112322
selected: false
sourcePosition: right
targetPosition: left
type: custom-note
width: 259
- data:
conditions:
- comparison_operator: is
id: '1721204199550'
value: '0'
variable_selector:
- '1721204234263'
- result
desc: if result is 0 then continue following flow
isInIteration: true
isIterationStart: false
iteration_id: '1721200808708'
logical_operator: and
selected: false
title: IF:compare_stop_flag
type: if-else
extent: parent
height: 174
id: '1721204196722'
parentId: '1721200808708'
position:
x: 684.560100721648
y: 85
positionAbsolute:
x: 1626.560100721648
y: 481
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1002
- data:
code: "def main(stop_flag: str, session: dict) -> dict:\n result = 1 if\
\ stop_flag == session[\"stop_flag\"] else 0\n\n return {\n \"\
result\": str(result),\n }"
code_language: python3
dependencies: []
desc: result = 1 if session["stop_flag"] == stop_flag else 0
isInIteration: true
isIterationStart: false
iteration_id: '1721200808708'
outputs:
result:
children: null
type: string
selected: false
title: Code:compare_stop_flag
type: code
variables:
- value_selector:
- '1721200069100'
- stop_flag
variable: stop_flag
- value_selector:
- '1721213003983'
- session
variable: session
extent: parent
height: 102
id: '1721204234263'
parentId: '1721200808708'
position:
x: 409.85714285714266
y: 85
positionAbsolute:
x: 1351.8571428571427
y: 481
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1002
- data:
code: "import random\ndef main() -> dict:\n output = random.randint(1,\
\ 3)\n output = 2 # assume random result is 2\n return {\n \
\ \"text\": str(output),\n }\n"
code_language: python3
desc: generate a random number in range of 1~3
isInIteration: true
isIterationStart: false
iteration_id: '1721200808708'
outputs:
text:
children: null
type: string
selected: false
title: Code:simulate_llm
type: code
variables: []
extent: parent
height: 102
id: '1721215004550'
parentId: '1721200808708'
position:
x: 958.5505379228609
y: 112.16374779142473
positionAbsolute:
x: 1900.550537922861
y: 508.1637477914247
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1002
- data:
code: "\ndef main() -> dict:\n session = {\"key1\": \"item1\", \"key2\"\
: \"item2\", \"key3\": \"item3\", \"stop_flag\": \"\"}\n return {\n \
\ \"session\": session,\n }\n"
code_language: python3
desc: define an session dict object
outputs:
session:
children: null
type: object
selected: false
title: session
type: code
variables: []
height: 84
id: '1721213003983'
position:
x: 334
y: 396
positionAbsolute:
x: 334
y: 396
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
- data:
code: "def main(session: dict) -> dict:\n return {\n \"result\"\
: session,\n }\n"
code_language: python3
desc: observe session value
isInIteration: true
isIterationStart: true
iteration_id: '1721200808708'
outputs:
result:
children: null
type: object
selected: false
title: Code:observe_session_store
type: code
variables:
- value_selector:
- '1721213003983'
- session
variable: session
extent: parent
height: 84
id: '1721213193234'
parentId: '1721200808708'
position:
x: 117
y: 85
positionAbsolute:
x: 1059
y: 481
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1001
- data:
code: "\ndef main(llm_text: str, session: dict) -> dict:\n session[\"stop_flag\"\
] = llm_text\n return {\n \"session\": session,\n }\n"
code_language: python3
desc: store LLM text in session
isInIteration: true
iteration_id: '1721200808708'
outputs:
session:
children: null
type: object
selected: false
title: Code:update_session
type: code
variables:
- value_selector:
- '1721213003983'
- session
variable: session
- value_selector:
- '1721215004550'
- text
variable: llm_text
extent: parent
height: 84
id: '1721214225930'
parentId: '1721200808708'
position:
x: 1225.1428571428573
y: 116.42857142857144
positionAbsolute:
x: 2167.1428571428573
y: 512.4285714285714
selected: false
sourcePosition: right
targetPosition: left
type: custom
width: 244
zIndex: 1002
- data:
author: admin
desc: ''
height: 91
selected: false
showAuthor: true
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"update
the \"session\" dict object","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ' (1)'
type: ''
width: 259
height: 91
id: '17212177126490'
position:
x: 2165.3386607135726
y: 651.0737396149178
positionAbsolute:
x: 2165.3386607135726
y: 651.0737396149178
selected: false
sourcePosition: right
targetPosition: left
type: custom-note
width: 259
- data:
author: admin
desc: ''
height: 88
selected: false
showAuthor: true
text: ''
theme: blue
title: ''
type: ''
width: 240
height: 88
id: '1721217775430'
position:
x: 2193.6971319996137
y: 652.4386034178486
positionAbsolute:
x: 2193.6971319996137
y: 652.4386034178486
sourcePosition: right
targetPosition: left
type: custom-note
width: 240
- data:
author: admin
desc: ''
height: 88
selected: false
showAuthor: false
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"try
to update the \"session\" dict object","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ''
type: ''
width: 260
height: 88
id: '1721217790594'
position:
x: 2185.7122085748774
y: 806.031161643069
positionAbsolute:
x: 2185.7122085748774
y: 806.031161643069
selected: false
sourcePosition: right
targetPosition: left
type: custom-note
width: 260
- data:
author: admin
desc: ''
height: 110
selected: false
showAuthor: false
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"for
example:","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"max_iteration=5","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"stop_flag=2","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ''
type: ''
width: 252
height: 110
id: '1721217896852'
position:
x: 30
y: 549.1136162098757
positionAbsolute:
x: 30
y: 549.1136162098757
selected: false
sourcePosition: right
targetPosition: left
type: custom-note
width: 252
- data:
author: admin
desc: ''
height: 101
selected: false
showAuthor: false
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"skip
if stop_flag == session[\"stop_flag\"](by Code:simulate_llm)","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ''
type: ''
width: 549
height: 101
id: '1721217990823'
position:
x: 1330.8718945980838
y: 806.031161643069
positionAbsolute:
x: 1330.8718945980838
y: 806.031161643069
selected: false
sourcePosition: right
targetPosition: left
type: custom-note
width: 549
- data:
author: admin
desc: ''
height: 271
selected: false
showAuthor: false
text: '{"root":{"children":[{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"actual
output:","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:0,
item:1","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:1,
item:2","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:2,
item:3","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:3,
item:4","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:4,
item:5","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[],"direction":null,"format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"expected
output:","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[],"direction":null,"format":"","indent":0,"type":"paragraph","version":1,"textFormat":0},{"children":[{"detail":0,"format":0,"mode":"normal","style":"","text":"index:0,
item:1","type":"text","version":1},{"type":"linebreak","version":1},{"detail":0,"format":0,"mode":"normal","style":"","text":"index:1,
item:2","type":"text","version":1}],"direction":"ltr","format":"","indent":0,"type":"paragraph","version":1,"textFormat":0}],"direction":"ltr","format":"","indent":0,"type":"root","version":1}}'
theme: blue
title: ''
type: ''
width: 311
height: 271
id: '1721218245910'
position:
x: 3104.405041423591
y: 566.0798820011455
positionAbsolute:
x: 3104.405041423591
y: 566.0798820011455
selected: true
sourcePosition: right
targetPosition: left
type: custom-note
width: 311
viewport:
x: -1384.9166015084425
y: 131.53205341486313
zoom: 0.6261800813907383
@rainchen
Copy link
Author

I'm attempting to implement the "break" behavior within an iteration node.

We are aware that dictionary objects are mutable in Python. I attempted to use a Code node to pass a "session" dictionary object through the iteration node and then update this "session" dictionary object within the iteration node.

For example:

def main(session: dict) -> dict:
    for key, value in session.items():
        if key == "key2":
            session[key] = "updated-item2"
        print(f"{key}: {value}")
    return {}  # Return an empty dictionary or modify as needed

# Example usage
session = {"key1": "item1", "key2": "item2", "key3": "item3"}
result = main(session)
print(session)  # Output will be the modified dictionary {'key1': 'item1', 'key2': 'updated-item2', 'key3': 'item3'}

However, this approach did not succeed.

Screenshot 2024-07-17 at 20 16 16
Screenshot 2024-07-17 at 20 16 39

It appears that the output of the Code node is an "immutable" object(maybe a clone value).

We can observe that in the first iteration, session["stop_flag"] was set to "2", but in the second iteration, session["stop_flag"] remained empty.

Here is my Proof of Concept chatbot YAML:
gist.github.com/rainchen/cc486f9fe2b169cbcf20a10f490b6f1d

I hope someone can find a solution to this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment