提示

提示操弄是應用程式開發人員的主要影響力 生成式 AI 模型的輸出內容舉例來說,使用 LLM 時 提示文字的語調、格式、長度和其他特徵 根據模型產生的回應

Genkit 主要針對作業環境設計,提示就是程式碼。您編寫 維護來源檔案中的提示,並使用相同版本追蹤這些事件的變更 用於程式碼的控管系統,而且會與程式碼一起部署 會呼叫生成式 AI 模型

大部分開發人員都會找到隨附的 Dotprompt 程式庫 可滿足他們在 Genkit 提示的使用需求。不過, 直接使用提示即可提供支援。

定義提示

Genkit 的生成輔助函式接受字串提示,而且您可以 ,以這種方式直接呼叫模型

Go

request := ai.GenerateRequest{Messages: []*ai.Message{
  {Content: []*ai.Part{ai.NewTextPart("You are a helpful AI assistant named Walt.")}},
}}
model.Generate(context.Background(), &request, nil)

在多數情況下,您需要在提示中加入使用者提供的輸入內容。 您可以定義函式,如下所示:

Go

func helloPrompt(name string) *ai.Part {
  prompt := fmt.Sprintf("You are a helpful AI assistant named Walt. Say hello to %s.", name)
  return ai.NewTextPart(prompt)
}
request := ai.GenerateRequest{Messages: []*ai.Message{
  {Content: []*ai.Part{helloPrompt("Fred")}},
}}
response, err := model.Generate(context.Background(), &request, nil)

然而,在程式碼中定義提示的缺點之一是,測試需要執行 做為流程的一部分為了加快疊代速度,Genkit 提供專用設備 ,在開發人員 UI 中定義提示並執行。

Go

使用 DefinePrompt 函式在 Genkit 註冊提示。

type HelloPromptInput struct {
  UserName string
}
helloPrompt := ai.DefinePrompt(
  "prompts",
  "helloPrompt",
  nil, // Additional model config
  jsonschema.Reflect(&HelloPromptInput{}),
  func(ctx context.Context, input any) (*ai.GenerateRequest, error) {
      params, ok := input.(HelloPromptInput)
      if !ok {
          return nil, errors.New("input doesn't satisfy schema")
      }
      prompt := fmt.Sprintf(
          "You are a helpful AI assistant named Walt. Say hello to %s.",
          params.UserName)
      return &ai.GenerateRequest{Messages: []*ai.Message{
          {Content: []*ai.Part{ai.NewTextPart(prompt)}},
      }}, nil
  },
)

提示動作會定義傳回 GenerateRequest 的函式。 可與任何模型搭配使用您也可以選擇定義輸入結構定義 這點與流程的輸入結構定義相似。 提示也能定義任何常見的模型設定選項,例如 隨機性參數或輸出分詞的數量

您可以使用提供的輔助函式將這個提示轉譯至模型要求。 請提供提示預期的輸入變數,以及要呼叫的模型。

Go

request, err := helloPrompt.Render(context.Background(), HelloPromptInput{UserName: "Fred"})
if err != nil {
  return err
}
response, err := model.Generate(context.Background(), request, nil)

在 Genkit 開發人員 UI 中,您可以執行以這種方式定義的任何提示。 這包括實驗範圍外的個別提示 可在各種流程中使用

圓點

Genkit 包含 Dotprompt 程式庫 提示功能

  • 正在從 .prompt 個來源檔案載入提示
  • 處理常式型範本
  • 支援多輪提示範本和多媒體內容
  • 精簡輸入和輸出結構定義定義
  • generate() 常用