|
7 | 7 | "source": [
|
8 | 8 | "# Salesforce\n",
|
9 | 9 | "\n",
|
10 |
| - "Tools for interacting with Salesforce.\n", |
| 10 | + "A tool for interacting with Salesforce CRM using LangChain.\n", |
11 | 11 | "\n",
|
12 | 12 | "## Overview\n",
|
13 | 13 | "\n",
|
14 |
| - "This notebook provides examples of interacting with Salesforce using LangChain.\n" |
| 14 | + "The `langchain-salesforce` package integrates LangChain with Salesforce CRM,\n", |
| 15 | + "allowing you to query data, manage records, and explore object schemas\n", |
| 16 | + "from LangChain applications.\n", |
| 17 | + "\n", |
| 18 | + "### Key Features\n", |
| 19 | + "\n", |
| 20 | + "- **SOQL Queries**: Execute Salesforce Object Query Language (SOQL) queries\n", |
| 21 | + "- **Object Management**: Create, read, update, and delete (CRUD) operations on Salesforce objects \n", |
| 22 | + "- **Schema Exploration**: Describe object schemas and list available objects\n", |
| 23 | + "- **Async Support**: Asynchronous operation support\n", |
| 24 | + "- **Error Handling**: Detailed error messages\n", |
| 25 | + "- **Environment Variable Support**: Load credentials from environment variables" |
15 | 26 | ]
|
16 | 27 | },
|
17 | 28 | {
|
18 | 29 | "cell_type": "markdown",
|
| 30 | + "id": "7fb27b941602401d91542211134fc71a", |
19 | 31 | "metadata": {},
|
20 | 32 | "source": [
|
21 | 33 | "## Setup\n",
|
22 | 34 | "\n",
|
23 |
| - "1. Install the required dependencies:\n", |
24 |
| - "```bash\n", |
25 |
| - " pip install langchain-salesforce\n", |
26 |
| - "```\n", |
27 |
| - "\n", |
28 |
| - "2. Set up your Salesforce credentials as environment variables:\n", |
29 |
| - "\n", |
| 35 | + "Install the required dependencies:\n", |
| 36 | + " \n", |
30 | 37 | "```bash\n",
|
31 |
| - " export SALESFORCE_USERNAME=\"your-username\"\n", |
32 |
| - " export SALESFORCE_PASSWORD=\"your-password\" \n", |
33 |
| - " export SALESFORCE_SECURITY_TOKEN=\"your-security-token\"\n", |
34 |
| - " export SALESFORCE_DOMAIN=\"test\" # Use 'test' for sandbox, remove for production\n", |
35 |
| - "```\n", |
| 38 | + " pip install langchain-salesforce\n", |
| 39 | + " ```\n", |
| 40 | + " \n", |
| 41 | + "## Authentication Setup\n", |
36 | 42 | "\n",
|
37 | 43 | "These environment variables will be automatically picked up by the integration.\n",
|
38 |
| - "\n", |
| 44 | + " \n", |
39 | 45 | "## Getting Your Security Token\n",
|
| 46 | + " \n", |
40 | 47 | "If you need a security token:\n",
|
41 |
| - "1. Log into Salesforce\n", |
42 |
| - "2. Go to Settings\n", |
43 |
| - "3. Click on \"Reset My Security Token\" under \"My Personal Information\"\n", |
44 |
| - "4. Check your email for the new token" |
| 48 | + " 1. Log into Salesforce\n", |
| 49 | + " 2. Go to Settings\n", |
| 50 | + " 3. Click on \"Reset My Security Token\" under \"My Personal Information\"\n", |
| 51 | + " 4. Check your email for the new token\n", |
| 52 | + " \n", |
| 53 | + "### Environment Variables (Recommended)\n", |
| 54 | + " \n", |
| 55 | + " Set up your Salesforce credentials as environment variables:\n", |
| 56 | + " \n", |
| 57 | + " ```bash\n", |
| 58 | + " export SALESFORCE_USERNAME=\"[email protected]\"\n", |
| 59 | + " export SALESFORCE_PASSWORD=\"your-password\"\n", |
| 60 | + " export SALESFORCE_SECURITY_TOKEN=\"your-security-token\"\n", |
| 61 | + " export SALESFORCE_DOMAIN=\"login\" # Use \"test\" for sandbox environments\n", |
| 62 | + " ```" |
45 | 63 | ]
|
46 | 64 | },
|
47 | 65 | {
|
|
101 | 119 | " request[\"record_data\"] = record_data\n",
|
102 | 120 | " if record_id:\n",
|
103 | 121 | " request[\"record_id\"] = record_id\n",
|
104 |
| - " result = tool.run(request)\n", |
| 122 | + " result = tool.invoke(request)\n", |
105 | 123 | " return result"
|
106 | 124 | ]
|
107 | 125 | },
|
|
122 | 140 | "outputs": [],
|
123 | 141 | "source": [
|
124 | 142 | "query_result = execute_salesforce_operation(\n",
|
125 |
| - " \"query\", query=\"SELECT Id, Name, Email FROM Contact LIMIT 5\"\n", |
| 143 | + " operation=\"query\", query=\"SELECT Id, Name, Email FROM Contact LIMIT 5\"\n", |
126 | 144 | ")"
|
127 | 145 | ]
|
128 | 146 | },
|
|
142 | 160 | "metadata": {},
|
143 | 161 | "outputs": [],
|
144 | 162 | "source": [
|
145 |
| - "describe_result = execute_salesforce_operation(\"describe\", object_name=\"Account\")" |
| 163 | + "describe_result = execute_salesforce_operation(\n", |
| 164 | + " operation=\"describe\", object_name=\"Account\"\n", |
| 165 | + ")" |
146 | 166 | ]
|
147 | 167 | },
|
148 | 168 | {
|
|
161 | 181 | "metadata": {},
|
162 | 182 | "outputs": [],
|
163 | 183 | "source": [
|
164 |
| - "list_objects_result = execute_salesforce_operation(\"list_objects\")" |
| 184 | + "list_objects_result = execute_salesforce_operation(operation=\"list_objects\")" |
165 | 185 | ]
|
166 | 186 | },
|
167 | 187 | {
|
|
181 | 201 | "outputs": [],
|
182 | 202 | "source": [
|
183 | 203 | "create_result = execute_salesforce_operation(\n",
|
184 |
| - " \"create\",\n", |
| 204 | + " operation=\"create\",\n", |
185 | 205 | " object_name=\"Contact\",\n",
|
186 | 206 | " record_data={\"LastName\": \"Doe\", \"Email\": \"[email protected]\"},\n",
|
187 | 207 | ")"
|
|
204 | 224 | "outputs": [],
|
205 | 225 | "source": [
|
206 | 226 | "update_result = execute_salesforce_operation(\n",
|
207 |
| - " \"update\",\n", |
| 227 | + " operation=\"update\",\n", |
208 | 228 | " object_name=\"Contact\",\n",
|
209 | 229 | " record_id=\"003XXXXXXXXXXXXXXX\",\n",
|
210 | 230 | " record_data={\"Email\": \"[email protected]\"},\n",
|
|
228 | 248 | "outputs": [],
|
229 | 249 | "source": [
|
230 | 250 | "delete_result = execute_salesforce_operation(\n",
|
231 |
| - " \"delete\", object_name=\"Contact\", record_id=\"003XXXXXXXXXXXXXXX\"\n", |
| 251 | + " operation=\"delete\", object_name=\"Contact\", record_id=\"003XXXXXXXXXXXXXXX\"\n", |
232 | 252 | ")"
|
233 | 253 | ]
|
234 | 254 | },
|
|
247 | 267 | "metadata": {},
|
248 | 268 | "outputs": [],
|
249 | 269 | "source": [
|
250 |
| - "from langchain.prompts import PromptTemplate\n", |
251 |
| - "from langchain_openai import ChatOpenAI\n", |
| 270 | + "from langchain_anthropic import ChatAnthropic\n", |
| 271 | + "from langchain_core.messages import HumanMessage\n", |
252 | 272 | "from langchain_salesforce import SalesforceTool\n",
|
253 | 273 | "\n",
|
| 274 | + "# Initialize the Salesforce tool\n", |
254 | 275 | "tool = SalesforceTool(\n",
|
255 | 276 | " username=username, password=password, security_token=security_token, domain=domain\n",
|
256 | 277 | ")\n",
|
257 | 278 | "\n",
|
258 |
| - "llm = ChatOpenAI(model=\"gpt-4o-mini\")\n", |
| 279 | + "# Initialize Anthropic LLM\n", |
| 280 | + "llm = ChatAnthropic(model=\"claude-sonnet-4-20250514\")\n", |
259 | 281 | "\n",
|
260 |
| - "prompt = PromptTemplate.from_template(\n", |
261 |
| - " \"What is the name of the contact with the id {contact_id}?\"\n", |
262 |
| - ")\n", |
| 282 | + "# First, let's query some contacts to get real data\n", |
| 283 | + "contacts_query = {\n", |
| 284 | + " \"operation\": \"query\",\n", |
| 285 | + " \"query\": \"SELECT Id, Name, Email, Phone FROM Contact LIMIT 3\",\n", |
| 286 | + "}\n", |
263 | 287 | "\n",
|
264 |
| - "chain = prompt | tool.invoke | llm\n", |
| 288 | + "contacts_result = tool.invoke(contacts_query)\n", |
265 | 289 | "\n",
|
266 |
| - "result = chain.invoke({\"contact_id\": \"003XXXXXXXXXXXXXXX\"})" |
| 290 | + "# Now let's use the LLM to analyze and summarize the contact data\n", |
| 291 | + "if contacts_result and \"records\" in contacts_result:\n", |
| 292 | + " contact_data = contacts_result[\"records\"]\n", |
| 293 | + "\n", |
| 294 | + " # Create a message asking the LLM to analyze the contact data\n", |
| 295 | + " analysis_prompt = f\"\"\"\n", |
| 296 | + " Please analyze the following Salesforce contact data and provide insights:\n", |
| 297 | + " \n", |
| 298 | + " Contact Data: {contact_data}\n", |
| 299 | + " \n", |
| 300 | + " Please provide:\n", |
| 301 | + " 1. A summary of the contacts\n", |
| 302 | + " 2. Any patterns you notice\n", |
| 303 | + " 3. Suggestions for data quality improvements\n", |
| 304 | + " \"\"\"\n", |
| 305 | + "\n", |
| 306 | + " message = HumanMessage(content=analysis_prompt)\n", |
| 307 | + " analysis_result = llm.invoke([message])\n", |
| 308 | + "\n", |
| 309 | + " print(\"\\nLLM Analysis:\")\n", |
| 310 | + " print(analysis_result.content)" |
267 | 311 | ]
|
268 | 312 | },
|
269 | 313 | {
|
270 | 314 | "cell_type": "markdown",
|
271 | 315 | "id": "b8467ae7",
|
272 | 316 | "metadata": {},
|
273 | 317 | "source": [
|
274 |
| - "## API reference\n", |
275 |
| - "[langchain-salesforce README](https://github.com/colesmcintosh/langchain-salesforce/blob/main/README.md)" |
| 318 | + "## API Reference\n", |
| 319 | + "\n", |
| 320 | + "For comprehensive documentation and API reference, see:\n", |
| 321 | + "\n", |
| 322 | + "- [langchain-salesforce README](https://github.com/colesmcintosh/langchain-salesforce/blob/main/README.md)\n", |
| 323 | + "- [Simple Salesforce Documentation](https://simple-salesforce.readthedocs.io/en/latest/)\n", |
| 324 | + "\n", |
| 325 | + "## Additional Resources\n", |
| 326 | + "\n", |
| 327 | + "- [Salesforce SOQL Reference](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/)\n", |
| 328 | + "- [Salesforce REST API Developer Guide](https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/)" |
276 | 329 | ]
|
277 | 330 | }
|
278 | 331 | ],
|
|
0 commit comments