定義劇本時,您可以選擇性提供程式碼區塊,也就是可用於進一步控管代理程式行為的內嵌 Python 程式碼。這段程式碼是由具有特殊裝飾符的函式,以及您需要的任何公用程式函式組成。
編寫程式碼時,您可以使用程式碼區塊系統程式庫控制代理程式行為。
限制
限制如下:
- 程式碼區塊不得包含任何會保留資料的物件。 不過,您可以使用工具來保存資料及維護狀態。
- 程式碼區塊無法直接發出遠端呼叫。舉例來說,您無法使用 Python requests 程式庫。不過,您可以利用工具間接撥打遠端電話。
- 轉換為 Python 名稱的資源名稱必須是合法的 Python 名稱。
- 除非發生流程轉換,否則程式碼區塊無法讀取或寫入工作階段參數。
內嵌動作
內嵌動作的行為與工具動作類似。 這些函式具有已定義的輸入和輸出結構定義,由 Python 函式簽章決定,包括型別註解和說明字串。與工具呼叫類似,LLM 不知道實作動作的程式碼。
範本:
@Action
def is_prime(n: int): bool
"""Returns true if n is prime."""
import math
return (all([False for i in range(2, math.sqrt(n)
if n % i == 0 ]) and not n < 2)
對於這項函式,LLM 會收到有關動作、動作輸入內容和輸出內容的資訊。
如要在劇本指示中參照內嵌動作,只要在反引號中參照動作名稱,並說明應如何使用即可。
place_order
內嵌動作的範例:
Take the customer's order, then call the `place_order` action when the order is ready.
如要建立使用內嵌動作的範例,請在「輸入和輸出」部分使用 inline-action 工具類型。
詳情請參閱 @Action 參考說明文件。
觸發函式
觸發函式用於在程式碼中定義條件動作。
觸發函式是使用修飾符宣告。您可以使用下列觸發函式修飾符:
裝飾器 | 裝飾器參數 | 說明 |
---|---|---|
@EventTrigger | event: str, condition: str ,其中條件為選用 |
由事件觸發 |
@BeforeModelTrigger | condition: str ,其中條件為選用 |
每次 LLM 預測下一個動作前都會觸發。 |
@BeforeActionTrigger | condition: str ,其中條件為選用 |
每次 LLM 執行動作前都會觸發。 |
@BeforePlaybookTrigger | condition: str ,其中條件為選用 |
首次啟動 Playbook 時觸發。 |
舉例來說,這些函式會顯示如何使用這些裝飾器和裝飾器參數,以及如何使用 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 全域變數,參照特定流程、劇本和工具。
這些物件的成員名稱與對應資源的名稱相符。這些名稱必須是合法的 Python 名稱。
範本:
add_override(playbooks.troubleshooting, {})
add_override(flows.billing)
add_override(tools.weather_api.get_weather, {"location": "San Francisco"})
在程式碼區塊中參照流程和劇本時,您也必須在劇本指令中參照這些項目,語法如下:
${RESOURCE_TYPE: my_resource_name}
舉例來說,如果程式碼區塊包含 flows.myflow
和 playbooks.myplaybook
,劇本指示應包含:
${FLOW: myflow}
${PLAYBOOK: myplaybook}
動作覆寫
您可以使用程式碼區塊建立動作佇列,在 LLM 判斷任何後續動作之前執行,並可能覆寫這些動作。您可以使用 add_override 全域函式建立動作覆寫。
所有已加入佇列的覆寫動作都會依序執行,且動作輸出內容會提供給 LLM。佇列清空後,作業會返回 LLM 進行動作和輸入內容選取,除非覆寫作業以 respond 或其他完成回合的函式結束回合。
函式引數:
- action:要執行的動作。
如果是內嵌動作,請使用
my_function_name
。 如果是工具動作,請使用tools.my_tool_name.my_tool_action
。 如果是流程動作,請使用flows.my_flow_name
。 - inputs:動作的選用輸入內容。
例如
{"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?")
偵錯
您可以使用模擬器偵錯程式碼區塊函式。這些函式會以動作的形式顯示在模擬器中,並視需要提供詳細資料。
其他控制項
本指南將介紹程式碼區塊系統程式庫的常見用途。如需其他類型的控制項,請參閱程式庫說明文件。