在 Salesforce apex 中執行大型語言模型 (GPT) 代理程式。
在此觀看完整演示
「代理」是一種向法學碩士灌輸「推理」和採取「行動」能力的技術。這種方法由 ReAct Paper(Reason → Act)引入,並在 langchain 和 auto-gpt 等流行庫中使用。
Model
:LLM/GTP模型。目前僅支援 OpenAI GPT Completion & Chat 模型。Prompt
:模型與系統之間的通訊「協定」。目前僅支援 ReAct 風格的提示。Agent
:為解決給定目標而形成的實例。Tools
:代理可以使用的命令Action
:呼叫工具的行為force:source:push
) 或開發人員版本 ( force:mdapi:deploy
)警告Scratch 和開發人員組織的連結佇列作業數量限制為 5 個,這將限制代理可以執行的工作量。
將您自己指派給Apex Agent
權限集。 sfdx force:user:permset:assign -n Apex_Agents
開啟Setup -> Named Credential -> External Credential -> OpenAI
。編輯“權限集映射”以新增“身份驗證參數”,名稱為API_KEY
,值是您的 OpenAI API 金鑰
(可選)開啟Setup -> Custom Settings -> Apex Agent Settings -> manage
並新增您的 SERP API 金鑰和 ExtactorAPI 金鑰。這是啟用網路搜尋功能所必需的。兩者都有免費套餐。
導航至“Apex Agents”應用程式(從應用程式啟動器)並執行任務。或使用下面的範例程式碼啟動代理程式。
該庫尚未準備好投入生產,並且可能永遠不會:
警告Salesforce 似乎存在錯誤,
aborting
具有長時間運行的 HTTP 標註的可排隊作業。如果您中止一個作業,它仍然會運行到完成,並繼續排程以下作業!
OpenAIChatModel chatLLM = new OpenAIChatModel ();
// chatLLM.model = 'gpt-3.5-turbo';
chatLLM . model = 'gpt-4' ;
SerpAPIKey__c mc = SerpAPIKey__c . getInstance ( UserInfo . getUserId ());
Map < String , IAgentTool > tools = new Map < String , IAgentTool >{
'search_internet' => new InternetSearchAgentTool ( mc . API_Key__c ),
'find_records' => new SOSLSearchAgentTool (),
'send_notification' => new SentNotificationAgentTool (),
'create_records' => new CreateRecordAgentTool (),
'get_fields' => new GetSObjectFieldsAgentTool (),
'list_custom_objects' => new ListSObjectAgentTool (),
'execute_soql' => new RunSQLAgentTool ()
};
ReActZeroShotChatPrompt prompt = new ReActZeroShotChatPrompt ( tools );
ReActChatAgent agent = new ReActChatAgent ( 'Find 3 accounts with missing phone numbers and try to populate them from the internet' , prompt , chatLLM );
agent . maxInvocations = 15 ;
AgentQueueable manager = new AgentQueueable ( agent );
manager . startAgent ();
注意為了獲得最佳結果,建議您將
OpenAIChatModel
與gpt-4
和ReActZeroShotChatPromptManager
結合使用,並且僅包含所需的最少數量的工具。
建立自訂工具很容易。只需建立一個實作 IAgentTool 介面的類,並在建立提示時將其新增至工具圖中即可。
public class GreetingAgentTool implements IAgentTool {
public string getDescription () {
return 'Get a greeting' ;
}
public Map < string , string > getParameters () {
return new Map < string , string >({
'name' => 'The name to greet'
});
}
public string execute ( Map < string , string > args ) {
if ( String . isEmpty ( args . get ( 'name' )){
// throw an error with instructions so the agent can self correct
throw new Agent . ActionRuntimeException ( 'missing required parameter: name' );
}
return 'hello ' + args . get ( 'name' );
}
尖端:
JSON.serialize
返回 JSON 或 YAML您可以透過實作Prompt.IReAct
來編寫自己的 ReAct 風格提示。
目前,物件呼叫Agent_Log__c
有一些原始日誌記錄位置。代理步驟會觸發 Agent_Event__e 即時平台事件,進而導致日誌即時更新。
{thought/reasoning} {action_result}
測試提示:
Search for accounts containing the word "sample" and send a notification to the user with name = "charlie jonas", notifying them that they should remove the account.
write a SOQL query that returns all billing related fields for an account. Send me a notification with the results of the query
Get the weather tomorrow in Lander, wyoming. Send a notification to Charlie Jonas letting him know how to dress
Research 3 companies that offer leading edge solutions for building API. Insert the new account with basic information about the business, but only if the account does not already exist.
Find out how many employees work at amazon and update the account
query 3 accounts that do not have Number of Employees set. Update the account with the number of employees from the internet.
See if you can fill in any missing information on the amazon account. Send me a notification with the summary
Search for accounts containing the word "sample". Create a task assigned to me with the subject "Remove Sample Account
write a SOQL query to group invoice total by project name. Send me the results in a notification
在這個階段做出貢獻的最簡單方法是創建新的「AgentTools」(歡迎 PR!)並進行實驗,看看這個東西可以做什麼。