1
+ <!-- Autogenerated by weave; DO NOT EDIT -->
2
+
1
3
# Flows
2
4
3
5
Flows are wrapped functions with some additional characteristics over direct
@@ -13,12 +15,11 @@ In its simplest form, a flow just wraps a function:
13
15
14
16
``` go
15
17
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
+ })
22
23
```
23
24
24
25
Doing 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:
38
39
39
40
``` go
40
41
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"`
44
45
}
45
46
```
46
47
47
48
``` go
48
49
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
+ },
54
55
)
55
56
```
56
57
@@ -78,32 +79,34 @@ Here's a simple example of a flow that can stream values:
78
79
79
80
``` go
80
81
// 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
+ ```
84
86
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
+ },
107
110
)
108
111
```
109
112
@@ -116,16 +119,16 @@ To invoke a flow in streaming mode:
116
119
117
120
``` go
118
121
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
+ }
129
132
})
130
133
```
131
134
@@ -150,16 +153,17 @@ first.
150
153
151
154
``` go
152
155
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)
163
167
}
164
168
```
165
169
@@ -179,7 +183,7 @@ first.
179
183
180
184
``` go
181
185
mainMux := http.NewServeMux ()
182
- mainMux.Handle (" POST /flow/" , http.StripPrefix (" /flow/" , genkit.NewFlowServeMux ()))
186
+ mainMux.Handle (" POST /flow/" , http.StripPrefix (" /flow/" , genkit.NewFlowServeMux (nil )))
183
187
```
184
188
185
189
## Flow observability
@@ -190,13 +194,14 @@ need to do is wrap the code in the `run` function.
190
194
191
195
``` go
192
196
genkit.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
+ })
202
207
```
0 commit comments