流量數

資料流經過包裝的函式,具有一些非直接的特性 都採用強式輸入、可串流、在本機和遠端呼叫 可觀測到完整可觀察的指標 Firebase Genkit 提供 CLI 和開發人員 UI 工具,可用於執行流程及偵錯。

定義流程

資料流只會包裝一個函式 (以最簡單的形式):

Go

menuSuggestionFlow := genkit.DefineFlow(
  "menuSuggestionFlow",
  func(ctx context.Context, restaurantTheme string) (string, error) {
      suggestion := makeMenuItemSuggestion(restaurantTheme)
      return suggestion, nil
  })

如此一來,您就能透過 Genkit CLI 和開發人員 UI 執行函式, 許多 Genkit 功能要求,包括 可觀察到的情況

Genkit 流程與直接呼叫模型 API 的一大優勢是 輸入和輸出的類型安全:

Go

流程的引數和結果類型可以是簡單值,也可以是結構化值。 Genkit 會使用 invopop/jsonschema.

以下流程會將 string 做為輸入內容並輸出 struct

type MenuSuggestion struct {
  ItemName    string `json:"item_name"`
  Description string `json:"description"`
  Calories    int    `json:"calories"`
}
menuSuggestionFlow := genkit.DefineFlow(
  "menuSuggestionFlow",
  func(ctx context.Context, restaurantTheme string) (MenuSuggestion, error) {
      suggestion := makeStructuredMenuItemSuggestion(restaurantTheme)
      return suggestion, nil
  },
)

執行中的流程

如要在程式碼中執行資料流:

Go

suggestion, err := menuSuggestionFlow.Run(context.Background(), "French")

您也可以使用 CLI 執行流程:

genkit flow:run menuSuggestionFlow '"French"'

直播結束

以下舉例說明可串流值的資料流:

Go

// Types for illustrative purposes.
type InputType string
type OutputType string
type StreamType string
menuSuggestionFlow := genkit.DefineStreamingFlow(
  "menuSuggestionFlow",
  func(
      ctx context.Context,
      restaurantTheme InputType,
      callback func(context.Context, StreamType) error,
  ) (OutputType, error) {
      var menu strings.Builder
      menuChunks := make(chan StreamType)
      go makeFullMenuSuggestion(restaurantTheme, menuChunks)
      for {
          chunk, ok := <-menuChunks
          if !ok {
              break
          }
          if callback != nil {
              callback(context.Background(), chunk)
          }
          menu.WriteString(string(chunk))
      }
      return OutputType(menu.String()), nil
  },
)

請注意,串流回呼可以未定義。只有在 呼叫用戶端要求串流回應。

如要在串流模式中叫用資料流:

Go

menuSuggestionFlow.Stream(
  context.Background(),
  "French",
)(func(sfv *genkit.StreamFlowValue[OutputType, StreamType], err error) bool {
  if !sfv.Done {
      fmt.Print(sfv.Output)
      return true
  } else {
      return false
  }
})

如果資料流不實作串流,StreamFlow() 的行為會與 RunFlow()

您也可以使用 CLI 串流資料:

genkit flow:run menuSuggestionFlow '"French"' -s

部署流程

如果想透過 HTTP 存取流程,請先部署 首先。

Go

如要使用 Cloud Run 和類似服務部署流程,請定義流程。 然後呼叫 Init()

func main() {
  genkit.DefineFlow(
      "menuSuggestionFlow",
      func(ctx context.Context, restaurantTheme string) (string, error) {
          // ...
          return "", nil
      },
  )
  if err := genkit.Init(context.Background(), nil); err != nil {
      log.Fatal(err)
  }
}

Init 會啟動 net/http 伺服器,將資料流公開為 HTTP 端點 (例如 http://localhost:3400/menuSuggestionFlow)。

第二個參數是選用 Options,可指定下列項目:

  • FlowAddr:要監聽的地址和通訊埠。如未指定 伺服器會監聽 PORT 環境變數指定的通訊埠。 如果該空白,系統會使用預設的通訊埠 3400。
  • Flows:要提供的資料流。如未指定,Init 會放送到 您定義的流程

如要在與其他端點相同的主機和通訊埠上提供流量,您必須 可以將 FlowAddr 設為 -,並改為呼叫 NewFlowServeMux() 來取得處理常式 Genkit 流程,可搭配其他路徑處理常式進行多工處理:

mainMux := http.NewServeMux()
mainMux.Handle("POST /flow/", http.StripPrefix("/flow/", genkit.NewFlowServeMux(nil)))

流程觀測能力

有時使用未經檢測而實作的第三方 SDK。 建議在開發人員 UI 中列為個別的追蹤步驟。所有你 只要在 run 函式中納入程式碼即可。

genkit.DefineFlow(
    "menuSuggestionFlow",
    func(ctx context.Context, restaurantTheme string) (string, error) {
        themes, err := genkit.Run(ctx, "find-similar-themes", func() (string, error) {
            // ...
            return "", nil
        })

        // ...
        return themes, err
    })