透過內容傳遞資訊

使用 LLM 的開發人員可能會同時處理不同類別的資訊:

  • 輸入:與引導 LLM 針對特定呼叫回應的相關資訊。例如需要摘要的文字。
  • 生成內容:與 LLM 相關,但不具體指通話的資訊。例如目前時間或使用者名稱。
  • 執行情境:對 LLM 呼叫周圍程式碼而言重要的資訊,但對 LLM 本身而言並非如此。例如使用者的目前驗證權杖。

Genkit 提供一致的 context 物件,可在整個程序中傳播產生和執行情境。此內容可供所有動作使用,包括流程工具提示

系統會自動將內容傳播至執行範圍內呼叫的所有動作:傳遞至流程的內容會提供給流程中執行的提示。傳遞至 generate() 方法的結構定義可供產生迴圈中呼叫的工具使用。

背景的重要性

最佳做法是,請向 LLM 提供完成工作所需的最低資訊。這麼做有以下幾個重要原因:

  • 大型語言模型的額外資訊越少,執行任務的成功機率就越高。
  • 如果 LLM 需要將使用者或帳戶 ID 等資訊傳遞給工具,可能會遭到欺騙而洩漏資訊。

您可以透過背景資訊取得可供任何程式碼使用的側邊通道資訊,但不一定需要傳送至 LLM。舉例來說,您可以將工具查詢限制在目前使用者可用的範圍內。

背景資訊結構

內容必須是物件,但其屬性由您決定。在某些情況下,Genkit 會自動填入內容。舉例來說,使用持續性工作階段時,系統會自動將 state 屬性新增至背景資訊。

背景資訊最常見的用途之一,就是儲存目前使用者相關資訊。建議您以下列格式新增授權背景資訊:

{
  auth: {
    uid: "...", // the user's unique identifier
    token: {...}, // the decoded claims of a user's id token
    rawToken: "...", // the user's raw encoded id token
    // ...any other fields
  }
}

您可以在執行流程中的其他位置儲存背景資料物件,以便在執行流程中取得所需資訊。

在動作中使用內容

如要在動作中使用情境,您可以存取自動提供至函式定義的 context helper:

心流狀態

const summarizeHistory = ai.defineFlow({
  name: 'summarizeMessages',
  inputSchema: z.object({friendUid: z.string()}),
  outputSchema: z.string();
}, async ({friendUid}, {context}) => {
  if (!context.auth?.uid) throw new Error("Must supply auth context.");
  const messages = await listMessagesBetween(friendUid, context.auth.uid);
  const {text} = await ai.generate({
    prompt:
      `Summarize the content of these messages: ${JSON.stringify(messages)}`,
  });
  return text;
});

工具

const searchNotes = ai.defineTool({
  name: 'searchNotes',
  description: "search the current user's notes for info",
  inputSchema: z.object({query: z.string()}),
  outputSchmea: z.array(NoteSchema);
}, async ({query}, {context}) => {
  if (!context.auth?.uid) throw new Error("Must be called by a signed-in user.");
  return searchUserNotes(context.auth.uid, query);
});

提示檔案

使用 Dotprompt 範本時,系統會使用 @ 變數前置字串提供內容。舉例來說,您可以在提示範本中存取 {auth: {name: 'Michael'}} 的內容物件,如下所示:

---
input:
  schema:
    pirateStyle?: boolean
---

{{#if pirateStyle}}
Avast, {{@auth.name}}, how be ye today?
{{else}}
Hello, {{@auth.name}}, how are you today?
{{/if}}

在執行階段提供背景資訊

如要為動作提供結構定義,請在呼叫動作時,將結構定義物件做為選項傳遞。

流量數

const summarizeHistory = ai.defineFlow(/* ... */);

const summary = await summarizeHistory(friend.uid, {context: {auth: currentUser}});

代別

const {text} = await ai.generate({
  prompt: "Find references to ocelots in my notes.",
  // the context will propagate to tool calls
  tools: [searchNotes],
  context: {auth: currentUser},
});

提示

const helloPrompt = ai.prompt('sayHello');
helloPrompt({pirateStyle: true}, {context: {auth: currentUser}});

背景資訊傳播和覆寫

根據預設,當您提供內容時,系統會自動將其傳播至原始呼叫所呼叫的所有動作。如果流程呼叫其他流程,或您使用呼叫工具,則會提供相同的內容。

如果您想在動作中覆寫內容,可以傳遞不同的內容物件來取代現有物件:

const otherFlow = ai.defineFlow(/* ... */);

const myFlow = ai.defineFlow({
  // ...
}, (input, {context}) => {
  // override the existing context completely
  otherFlow({/*...*/}, {context: {newContext: true}});
  // or selectively override
  otherFlow({/*...*/}, {context: {...context, updatedContext: true}});
}); 

當內容取代時,也會以相同方式傳播。在這個範例中,otherFlow 在執行期間呼叫的任何動作都會繼承覆寫的內容。