在 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!)并进行实验,看看这个东西可以做什么。