定義 Playbook 時,您可以視需要提供程式碼區塊,也就是內嵌 Python 程式碼,以便進一步控管代理行為。這段程式碼包含具有特殊裝飾器的函式,以及您需要的任何公用函式。
編寫程式碼時,您可以使用程式碼區塊系統程式庫控制代理行為。
限制
限制如下:
- 程式碼區塊不得包含任何會保留資料的物件。不過,您可以使用工具來保存資料及維護狀態。
- 程式碼區塊無法直接發出遠端呼叫。例如,您無法使用 Python 要求程式庫。不過,您可以使用工具間接發出遠端呼叫。
- 轉換為 Python 名稱的資源名稱必須是合法的 Python 名稱。
- 除非發生流程轉換,否則程式碼區塊無法寫入工作階段參數。
內嵌動作
內嵌動作的行為與工具動作類似。這些函式有定義好的輸入和輸出結構定義,由 Python 函式簽章決定,包括類型註解和 docstring。與工具呼叫一樣,LLM 不知道實作動作的程式碼。
例如:
@Action
def is_prime(n: int) -> bool:
"""Returns true if n is prime."""
if not isinstance(n, int) or n < 2:
return False
for i in range(2, int(n ** 0.5) + 1):
if n % i == 0:
return False
return True
對於這個函式,LLM 會收到動作、輸入和輸出的相關資訊。
如要在 playbook 指令中參照內嵌動作,請在反引號中參照動作名稱,並說明使用方式。
以下是 place_order 內嵌動作的範例:
Take the customer's order, and then call the `place_order` action when the
order is ready.
如要建立使用內嵌動作的範例,請在「Input & Output」(輸入和輸出) 部分,使用 inline-action 工具類型。
詳情請參閱 @Action 參考文件。
觸發函式
觸發條件函式用於在程式碼中定義條件式動作。
您可以使用裝飾器宣告觸發函式。
您可以使用下列觸發條件函式裝飾器:
| 裝飾器 | 裝飾器參數 | 說明 |
|---|---|---|
| @EventTrigger | event: str, condition: str,其中條件為選用 |
由事件觸發 |
| @BeforeModelTrigger | condition: str,其中條件為選用 |
每次 LLM 預測下一個動作前,都會觸發這個事件。 |
| @BeforeActionTrigger | condition: str,其中條件為選用 |
每次 LLM 執行動作前觸發。 |
| @BeforePlaybookTrigger | condition: str,其中條件為選用 |
在首次啟動應對手冊時觸發。 |
舉例來說,這些函式會示範如何使用這些裝飾器和裝飾器參數,以及如何使用 respond 程式碼區塊系統程式庫函式。
# No decorator parameter
@PlaybookStartTrigger
def my_playbook_conditional_action():
respond("How can I help?")
# With a condition decorator parameter
@BeforeActionTrigger('$next-action.name = "search"')
def my_before_action_conditional_action():
respond("One moment please")
# Event
@EventTrigger(event="welcome")
def my_welcome_event():
respond("hi")
# Event with a condition:
@EventTrigger(event="welcome",
condition="$sys.func.NOW().hours < 10")
def my_good_morning_event():
respond("Good morning ☕")
參照流程、應對手冊和工具
在程式碼區塊中,您可以使用 flows、playbooks 和 tools 全域變數,參照特定流程、playbook 和工具。
每個物件都有與對應資源名稱相符的成員。這些名稱必須是合法的 Python 名稱。
例如:
add_override(playbooks.troubleshooting, {})
add_override(flows.billing)
add_override(tools.weather_api.get_weather, {"location": "San Francisco"})
在程式碼區塊中參照流程和 playbook 時,也必須使用下列語法在 playbook 指示中參照:
${RESOURCE_TYPE: my_resource_name}
舉例來說,如果程式碼區塊包含 flows.myflow 和 playbooks.myplaybook,則應在教戰手冊指令中加入:
${FLOW: myflow}
${PLAYBOOK: myplaybook}
動作覆寫
使用程式碼區塊建立動作佇列,這些動作會在 LLM 決定任何進一步動作之前執行,並可能覆寫這些動作。使用 add_override 全域函式建立動作覆寫。
所有排入佇列的覆寫動作都會依序執行,且 LLM 可使用動作輸出內容。佇列清空後,作業會返回 LLM 進行動作和輸入內容選取,除非覆寫作業以 respond 或其他函式完成該輪對話。
函式引數:
- 動作:指定要執行的動作。如果是內嵌動作,請使用
my_function_name。如果是工具動作,請使用tools.my_tool_name.my_tool_action。如果是流程動作,請使用flows.my_flow_name。 - 輸入內容:指定動作的選用輸入內容,例如
{"location": "Omaha"}。
例如:
# Assuming remote tool named "dmv" with operationId "search_offices"
# remote tool with only requestBody
add_override(tools.dmv.search_offices,{"address": "95030"})
# remote tool with only parameters
add_override(tools.pets.get_pet, {"id": "123"})
# remote tool with requestBody + parameters:
add_override(tools.pets.create_pet, {"requestBody": {"arg1":"foo"}, "param1": "bar"})
# datastore. Assumes existing datastore tool named "Menu".
add_override(tools.menu.Menu, {"query": "what is the menu"})
# code block action. Assumes another code block @Action my_action.
add_override(my_action)
回應覆寫
您可以使用 respond 全域函式,強制代理使用特定內容回覆使用者,這與動作覆寫類似,但專門用於代理回覆。
例如:
respond("Hello")
呼叫工具
您可以在程式碼區塊函式中,呼叫為代理定義的工具。與覆寫工具動作不同,直接呼叫工具時,LLM 無法存取工具執行結果。
例如:
# Assumes existing tool named "DMV" with operationId "search_offices"
# remote tool with only request body.
offices = tools.dmv.search_offices({"address": "95030"})
# remote tool with parameters and request body
offices = tools.dmv.search_offices({"requestBody": {"address":"95030"}, "param1":"foo"})
# datastore actions. Assumes existing datastore tool named "Menu".
data = tools.menu.Menu({"query": "get the menu"})["snippets"]
比對意圖
程式碼區塊可以使用 Flow.match_intent 函式,以程式輔助方式比對特定流程的意圖。
例如:
matches = flows.flow1.match_intent(history.last_user_utterance).matches
if matches and matches[0].intent == "some_intent":
to_country = matches[0].parameters.get("to_country")
if to_country:
respond(f"To confirm, you're going to {to_country}, right?")
讀取工作階段參數
程式碼區塊可以讀取執行時可用的參數。
例如:
state["$session.params.my-param"]
state["$request.session-id"]
偵錯
使用模擬器對程式碼區塊函式進行偵錯。這些函式會以動作的形式顯示在模擬器中,並視需要提供偵錯詳細資料。
其他控制項
本文說明程式碼區塊系統程式庫的一些常見用途。如需其他類型的控制項,請參閱程式庫說明文件。