Skip to content

Commit 442a9c8

Browse files
committed
update
1 parent fa74c42 commit 442a9c8

File tree

17 files changed

+382
-403
lines changed

17 files changed

+382
-403
lines changed

cookbook/agent/agent

8.36 MB
Binary file not shown.

cookbook/agent/flow.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ func CreateAgentFlow(llm *LLM, searcher Searcher) *flyt.Flow {
1010
search := NewSearchWebNode(searcher)
1111
answer := NewAnswerQuestionNode(llm)
1212

13-
flow := flyt.NewFlow(decide)
14-
15-
flow.Connect(decide, "search", search)
16-
flow.Connect(decide, "answer", answer)
17-
flow.Connect(search, "decide", decide)
18-
19-
return flow
13+
return flyt.NewFlow(decide).
14+
Connect(decide, "search", search).
15+
Connect(decide, "answer", answer).
16+
Connect(search, "decide", decide)
2017
}
2118
func NewSharedState() *flyt.SharedStore {
2219
return flyt.NewSharedStore()

cookbook/agent/nodes.go

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
// NewDecideActionNode creates a node that decides whether to search for more information or provide an answer.
1313
// It uses an LLM to analyze the question and context to make this decision.
1414
func NewDecideActionNode(llm *LLM) flyt.Node {
15-
return flyt.NewNode(
16-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
15+
return flyt.NewNode().
16+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
1717
question := shared.GetString("question")
1818
context := shared.GetStringOr("context", "No previous search")
1919
searchCount := shared.GetInt("search_count")
@@ -23,8 +23,8 @@ func NewDecideActionNode(llm *LLM) flyt.Node {
2323
"context": context,
2424
"search_count": searchCount,
2525
}, nil
26-
}),
27-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
26+
}).
27+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
2828
data := prepResult.(map[string]any)
2929
question := data["question"].(string)
3030
contextStr := data["context"].(string)
@@ -71,8 +71,8 @@ Example responses:
7171
"contextStr": contextStr,
7272
"searchCount": searchCount,
7373
}, nil
74-
}),
75-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
74+
}).
75+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
7676
data := execResult.(map[string]any)
7777
response := data["response"].(string)
7878

@@ -97,21 +97,21 @@ Example responses:
9797
}
9898

9999
return flyt.Action(action), nil
100-
}),
101-
)
100+
}).
101+
Build()
102102
}
103103

104104
// NewSearchWebNode creates a node that searches the web for information
105105
func NewSearchWebNode(searcher Searcher) flyt.Node {
106-
return flyt.NewNode(
107-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
106+
return flyt.NewNode().
107+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
108108
query := shared.GetString("search_query")
109109

110110
return map[string]any{
111111
"query": query,
112112
}, nil
113-
}),
114-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
113+
}).
114+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
115115
data := prepResult.(map[string]any)
116116
query := data["query"].(string)
117117

@@ -127,8 +127,8 @@ func NewSearchWebNode(searcher Searcher) flyt.Node {
127127
"results": results,
128128
"query": query,
129129
}, nil
130-
}),
131-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
130+
}).
131+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
132132
data := execResult.(map[string]any)
133133
results := data["results"].(string)
134134
query := data["query"].(string)
@@ -141,23 +141,23 @@ func NewSearchWebNode(searcher Searcher) flyt.Node {
141141
fmt.Println("📚 Found information, analyzing results...")
142142

143143
return "decide", nil
144-
}),
145-
)
144+
}).
145+
Build()
146146
}
147147

148148
// NewAnswerQuestionNode creates a node that generates the final answer
149149
func NewAnswerQuestionNode(llm *LLM) flyt.Node {
150-
return flyt.NewNode(
151-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
150+
return flyt.NewNode().
151+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
152152
question := shared.GetString("question")
153153
context := shared.GetString("context")
154154

155155
return map[string]any{
156156
"question": question,
157157
"context": context,
158158
}, nil
159-
}),
160-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
159+
}).
160+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
161161
data := prepResult.(map[string]any)
162162
question := data["question"].(string)
163163
contextStr := ""
@@ -180,15 +180,15 @@ Provide a short concise answer using the research results.`, question, contextSt
180180
}
181181

182182
return answer, nil
183-
}),
184-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
183+
}).
184+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
185185
answer := execResult.(string)
186186

187187
shared.Set("answer", answer)
188188
fmt.Println("✅ Answer generated successfully")
189189

190190
// End the flow
191191
return "done", nil
192-
}),
193-
)
192+
}).
193+
Build()
194194
}

cookbook/chat/chat

8.28 MB
Binary file not shown.

cookbook/chat/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
// CreateChatNode creates an interactive chat node using the NewNode helper
1515
func CreateChatNode(llm *LLM) flyt.Node {
16-
return flyt.NewNode(
17-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
16+
return flyt.NewNode().
17+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
1818
// Initialize messages if this is the first run
1919
messages, ok := shared.Get("messages")
2020
if !ok {
@@ -46,8 +46,8 @@ func CreateChatNode(llm *LLM) flyt.Node {
4646
shared.Set("messages", messageList)
4747

4848
return messageList, nil
49-
}),
50-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
49+
}).
50+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
5151
if prepResult == nil {
5252
return nil, nil
5353
}
@@ -61,8 +61,8 @@ func CreateChatNode(llm *LLM) flyt.Node {
6161
}
6262

6363
return response, nil
64-
}),
65-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
64+
}).
65+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
6666
if prepResult == nil || execResult == nil {
6767
fmt.Println("\nGoodbye!")
6868
return "end", nil
@@ -81,8 +81,8 @@ func CreateChatNode(llm *LLM) flyt.Node {
8181
shared.Set("messages", messageList)
8282

8383
return "continue", nil
84-
}),
85-
)
84+
}).
85+
Build()
8686
}
8787

8888
func main() {
@@ -99,8 +99,8 @@ func main() {
9999
chatNode := CreateChatNode(llm)
100100

101101
// Create the flow with self-loop
102-
flow := flyt.NewFlow(chatNode)
103-
flow.Connect(chatNode, "continue", chatNode) // Loop back to continue conversation
102+
flow := flyt.NewFlow(chatNode).
103+
Connect(chatNode, "continue", chatNode) // Loop back to continue conversation
104104

105105
// Start the chat
106106
shared := flyt.NewSharedStore()
8.3 MB
Binary file not shown.

cookbook/llm-streaming/main.go

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ import (
1313

1414
// CreateStreamNode creates a node that streams LLM responses
1515
func CreateStreamNode(llm *LLM) flyt.Node {
16-
return flyt.NewNode(
17-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
16+
return flyt.NewNode().
17+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
1818
// Get messages from shared store
1919
messages, ok := shared.Get("messages")
2020
if !ok {
@@ -24,8 +24,8 @@ func CreateStreamNode(llm *LLM) flyt.Node {
2424
return map[string]any{
2525
"messages": messages.([]Message),
2626
}, nil
27-
}),
28-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
27+
}).
28+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
2929
data := prepResult.(map[string]any)
3030
messages := data["messages"].([]Message)
3131

@@ -47,8 +47,8 @@ func CreateStreamNode(llm *LLM) flyt.Node {
4747
fmt.Println() // New line after streaming
4848

4949
return response.String(), nil
50-
}),
51-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
50+
}).
51+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
5252
if execResult != nil {
5353
// Add assistant's response to messages
5454
messages, _ := shared.Get("messages")
@@ -61,15 +61,15 @@ func CreateStreamNode(llm *LLM) flyt.Node {
6161
}
6262

6363
return flyt.DefaultAction, nil
64-
}),
65-
)
64+
}).
65+
Build()
6666
}
6767

6868
// CreateInteractiveChatFlow creates a flow that continuously prompts for input and streams responses
6969
func CreateInteractiveChatFlow(llm *LLM) *flyt.Flow {
7070
// Create input node
71-
inputNode := flyt.NewNode(
72-
flyt.WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
71+
inputNode := flyt.NewNode().
72+
WithPrepFuncAny(func(ctx context.Context, shared *flyt.SharedStore) (any, error) {
7373
// Initialize messages if this is the first run
7474
messages, ok := shared.Get("messages")
7575
if !ok {
@@ -78,8 +78,8 @@ func CreateInteractiveChatFlow(llm *LLM) *flyt.Flow {
7878
fmt.Println("Welcome to the streaming chat! Type 'exit' to end the conversation.")
7979
}
8080
return nil, nil
81-
}),
82-
flyt.WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
81+
}).
82+
WithExecFuncAny(func(ctx context.Context, prepResult any) (any, error) {
8383
reader := bufio.NewReader(os.Stdin)
8484
fmt.Print("\n💭 You: ")
8585
input, err := reader.ReadString('\n')
@@ -93,8 +93,8 @@ func CreateInteractiveChatFlow(llm *LLM) *flyt.Flow {
9393
}
9494

9595
return input, nil
96-
}),
97-
flyt.WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
96+
}).
97+
WithPostFuncAny(func(ctx context.Context, shared *flyt.SharedStore, prepResult, execResult any) (flyt.Action, error) {
9898
if execResult == nil {
9999
fmt.Println("\n👋 Goodbye!")
100100
return "exit", nil
@@ -110,18 +110,16 @@ func CreateInteractiveChatFlow(llm *LLM) *flyt.Flow {
110110
shared.Set("messages", messageList)
111111

112112
return "stream", nil
113-
}),
114-
)
113+
}).
114+
Build()
115115

116116
// Create stream node
117117
streamNode := CreateStreamNode(llm)
118118

119119
// Create flow
120-
flow := flyt.NewFlow(inputNode)
121-
flow.Connect(inputNode, "stream", streamNode)
122-
flow.Connect(streamNode, flyt.DefaultAction, inputNode) // Loop back for next input
123-
124-
return flow
120+
return flyt.NewFlow(inputNode).
121+
Connect(inputNode, "stream", streamNode).
122+
Connect(streamNode, flyt.DefaultAction, inputNode) // Loop back for next input
125123
}
126124

127125
func main() {

cookbook/mcp/main.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,10 @@ func main() {
4545
executeNode := NewExecuteToolNode(mcpClient)
4646

4747
// Create flow
48-
flow := flyt.NewFlow(getToolsNode)
49-
50-
// Connect nodes
51-
flow.Connect(getToolsNode, "decide", decideNode)
52-
flow.Connect(decideNode, "execute", executeNode)
53-
flow.Connect(executeNode, "decide", decideNode) // Loop back for continued conversation
48+
flow := flyt.NewFlow(getToolsNode).
49+
Connect(getToolsNode, "decide", decideNode).
50+
Connect(decideNode, "execute", executeNode).
51+
Connect(executeNode, "decide", decideNode) // Loop back for continued conversation
5452

5553
// Create shared store and initialize with the user's question
5654
shared := flyt.NewSharedStore()

cookbook/mcp/mcp

9.35 MB
Binary file not shown.

0 commit comments

Comments
 (0)