1+ <!-- Autogenerated by weave; DO NOT EDIT -->
2+
13# Flows
24
35Flows are wrapped functions with some additional characteristics over direct
@@ -13,12 +15,11 @@ In its simplest form, a flow just wraps a function:
1315
1416 ``` go
1517 menuSuggestionFlow := genkit.DefineFlow (
16- " menuSuggestionFlow" ,
17- func (ctx context .Context , restaurantTheme string ) (string , error ) {
18- suggestion := makeMenuItemSuggestion (restaurantTheme)
19- return suggestion, nil
20- },
21- )
18+ " menuSuggestionFlow" ,
19+ func (ctx context .Context , restaurantTheme string ) (string , error ) {
20+ suggestion := makeMenuItemSuggestion (restaurantTheme)
21+ return suggestion, nil
22+ })
2223 ```
2324
2425Doing so lets you run the function from the Genkit CLI and developer UI, and is
@@ -38,19 +39,19 @@ type safety of both inputs and outputs:
3839
3940 ``` go
4041 type MenuSuggestion struct {
41- ItemName string ` json:"item_name"`
42- Description string ` json:"description"`
43- Calories int ` json:"calories"`
42+ ItemName string ` json:"item_name"`
43+ Description string ` json:"description"`
44+ Calories int ` json:"calories"`
4445 }
4546 ```
4647
4748 ``` go
4849 menuSuggestionFlow := genkit.DefineFlow (
49- " menuSuggestionFlow" ,
50- func (ctx context .Context , restaurantTheme string ) (MenuSuggestion , error ) {
51- suggestion := makeStructuredMenuItemSuggestion (restaurantTheme)
52- return suggestion, nil
53- },
50+ " menuSuggestionFlow" ,
51+ func (ctx context .Context , restaurantTheme string ) (MenuSuggestion , error ) {
52+ suggestion := makeStructuredMenuItemSuggestion (restaurantTheme)
53+ return suggestion, nil
54+ },
5455 )
5556 ```
5657
@@ -78,32 +79,34 @@ Here's a simple example of a flow that can stream values:
7879
7980 ``` go
8081 // Types for illustrative purposes.
81- type inputType string
82- type outputType string
83- type streamType string
82+ type InputType string
83+ type OutputType string
84+ type StreamType string
85+ ```
8486
85- menuSuggestionFlow := genkit.DefineFlow (
86- " menuSuggestionFlow" ,
87- func (
88- ctx context.Context ,
89- restaurantTheme inputType,
90- callback func (context.Context , streamType) error ,
91- ) (outputType, error ) {
92- var menu strings.Builder
93- menuChunks := make (chan streamType)
94- go makeFullMenuSuggestion (restaurantTheme, menuChunks)
95- for {
96- chunk , ok := <- menuChunks
97- if !ok {
98- break
99- }
100- if callback != nil {
101- callback (context.Background (), chunk)
102- }
103- menu.WriteString (string (chunk))
104- }
105- return outputType (menu.String ()), nil
106- },
87+ ``` go
88+ menuSuggestionFlow := genkit.DefineStreamingFlow (
89+ " menuSuggestionFlow" ,
90+ func (
91+ ctx context.Context ,
92+ restaurantTheme InputType ,
93+ callback func (context.Context , StreamType ) error ,
94+ ) (OutputType, error ) {
95+ var menu strings.Builder
96+ menuChunks := make (chan StreamType)
97+ go makeFullMenuSuggestion (restaurantTheme, menuChunks)
98+ for {
99+ chunk , ok := <- menuChunks
100+ if !ok {
101+ break
102+ }
103+ if callback != nil {
104+ callback (context.Background (), chunk)
105+ }
106+ menu.WriteString (string (chunk))
107+ }
108+ return OutputType (menu.String ()), nil
109+ },
107110 )
108111 ```
109112
@@ -116,16 +119,16 @@ To invoke a flow in streaming mode:
116119
117120 ``` go
118121 genkit.StreamFlow (
119- context.Background (),
120- menuSuggestionFlow,
121- " French" ,
122- )(func (sfv *genkit.StreamFlowValue [outputType, streamType ], err error ) bool {
123- if !sfv.Done {
124- fmt.Print (sfv.Output )
125- return true
126- } else {
127- return false
128- }
122+ context.Background (),
123+ menuSuggestionFlow,
124+ " French" ,
125+ )(func (sfv *genkit.StreamFlowValue [OutputType, StreamType ], err error ) bool {
126+ if !sfv.Done {
127+ fmt.Print (sfv.Output )
128+ return true
129+ } else {
130+ return false
131+ }
129132 })
130133 ```
131134
@@ -150,16 +153,17 @@ first.
150153
151154 ``` go
152155 func main () {
153- genkit.DefineFlow (
154- " menuSuggestionFlow" ,
155- func (ctx context.Context , restaurantTheme string ) (string , error ) {
156- // ...
157- },
158- )
159- err := genkit.StartFlowServer (" :1234" , []string {})
160-
161- // startProdServer always returns a non-nil error: the one returned by
162- // http.ListenAndServe.
156+ genkit.DefineFlow (
157+ " menuSuggestionFlow" ,
158+ func (ctx context.Context , restaurantTheme string ) (string , error ) {
159+ // ...
160+ return " " , nil
161+ },
162+ )
163+ // StartFlowServer always returns a non-nil error: the one returned by
164+ // http.ListenAndServe.
165+ err := genkit.StartFlowServer (" :1234" , []string {})
166+ log.Fatal (err)
163167 }
164168 ```
165169
@@ -179,7 +183,7 @@ first.
179183
180184 ``` go
181185 mainMux := http.NewServeMux ()
182- mainMux.Handle (" POST /flow/" , http.StripPrefix (" /flow/" , genkit.NewFlowServeMux ()))
186+ mainMux.Handle (" POST /flow/" , http.StripPrefix (" /flow/" , genkit.NewFlowServeMux (nil )))
183187 ```
184188
185189## Flow observability
@@ -190,13 +194,14 @@ need to do is wrap the code in the `run` function.
190194
191195``` go
192196genkit.DefineFlow (
193- " menuSuggestionFlow" ,
194- func (ctx context .Context , restaurantTheme string ) (string , error ) {
195- themes , err := genkit.Run (ctx, " find-similar-themes" , func () (string , error ) {
196- // ...
197- })
198-
199- // ...
200- },
201- )
197+ " menuSuggestionFlow" ,
198+ func (ctx context .Context , restaurantTheme string ) (string , error ) {
199+ themes , err := genkit.Run (ctx, " find-similar-themes" , func () (string , error ) {
200+ // ...
201+ return " " , nil
202+ })
203+
204+ // ...
205+ return themes, err
206+ })
202207```
0 commit comments