本文是《Build Custom AI Agents with Agent API — 构建自定义 AI 代理》的练习操作步骤。阅读原文请见:Build Custom AI Agents with Agent API — 构建自定义 AI 代理。
1. Enable Agentforce and Activate an Agent
- Enable Agentforce in your Salesforce org.
- Activate at least one agent.
- Note: Agent API doesn't support agents of the type "Agentforce (Default)".
2. Create an External Client App and Enable OAuth Settings
- Create an external client app in Salesforce. This app handles the client credential flow, which is essential for secure API calls.
- Enable OAuth settings for API.
3. Obtain the Consumer Key and Consumer Secret
- Find the consumer key and consumer secret in your external client app. These credentials authenticate your API calls.
4. Create a Token
- Create a token using the consumer key, consumer secret, and your domain name. This token authenticates your API calls.
5. Make Your First API Call
- Gather the required information from your org. See "Call the API" in the Agent API Developer Guide.
- Using a command-line interface, run the following curl command to create a new agent session with the Agent API:
```
curl --location -X POST https://api.salesforce.com/einstein/ai-agent/v1/agents/{AGENT_ID}/sessions \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer {ACCESS_TOKEN}' \
--data '{
"externalSessionKey": "{RANDOM_UUID}",
"instanceConfig": {
"endpoint": "https://{MY_DOMAIN_URL}"
},
"streamingCapabilities": {
"chunkTypes": ["Text"]
},
"bypassUser": true
}'
```
- When the call succeeds, you receive a response with a session ID and additional info.
- Start sending messages to the AI agent:
- Synchronous messages: ideal for getting the complete response in a single call.
- Streaming messages: better for real-time interactions like chat conversations.
- When done, close the session.