Salesforce
與 Salesforce 互動的工具。
概觀
此筆記本提供使用 LangChain 與 Salesforce 互動的範例。
設定
- 安裝所需的相依性
pip install langchain-salesforce
- 將您的 Salesforce 憑證設定為環境變數
export SALESFORCE_USERNAME="your-username"
export SALESFORCE_PASSWORD="your-password"
export SALESFORCE_SECURITY_TOKEN="your-security-token"
export SALESFORCE_DOMAIN="test" # Use 'test' for sandbox, remove for production
這些環境變數將會由整合自動選取。
取得您的安全性權杖
如果您需要安全性權杖
- 登入 Salesforce
- 前往「設定」
- 按一下「我的個人資訊」下的「重設我的安全性權杖」
- 檢查您的電子郵件以取得新的權杖
例項化
import os
from langchain_salesforce import SalesforceTool
username = os.getenv("SALESFORCE_USERNAME", "your-username")
password = os.getenv("SALESFORCE_PASSWORD", "your-password")
security_token = os.getenv("SALESFORCE_SECURITY_TOKEN", "your-security-token")
domain = os.getenv("SALESFORCE_DOMAIN", "login")
tool = SalesforceTool(
username=username, password=password, security_token=security_token, domain=domain
)
調用
def execute_salesforce_operation(
operation, object_name=None, query=None, record_data=None, record_id=None
):
"""Executes a given Salesforce operation."""
request = {"operation": operation}
if object_name:
request["object_name"] = object_name
if query:
request["query"] = query
if record_data:
request["record_data"] = record_data
if record_id:
request["record_id"] = record_id
result = tool.run(request)
return result
查詢
此範例查詢 Salesforce 以取得 5 個聯絡人。
query_result = execute_salesforce_operation(
"query", query="SELECT Id, Name, Email FROM Contact LIMIT 5"
)
描述物件
擷取特定 Salesforce 物件的中繼資料。
describe_result = execute_salesforce_operation("describe", object_name="Account")
列出可用物件
擷取 Salesforce 執行個體中所有可用的物件。
list_objects_result = execute_salesforce_operation("list_objects")
建立新聯絡人
在 Salesforce 中建立新的聯絡人記錄。
create_result = execute_salesforce_operation(
"create",
object_name="Contact",
record_data={"LastName": "Doe", "Email": "doe@example.com"},
)
更新聯絡人
更新現有的聯絡人記錄。
update_result = execute_salesforce_operation(
"update",
object_name="Contact",
record_id="003XXXXXXXXXXXXXXX",
record_data={"Email": "updated@example.com"},
)
刪除聯絡人
從 Salesforce 刪除聯絡人記錄。
delete_result = execute_salesforce_operation(
"delete", object_name="Contact", record_id="003XXXXXXXXXXXXXXX"
)
鏈結
from langchain.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from langchain_salesforce import SalesforceTool
tool = SalesforceTool(
username=username, password=password, security_token=security_token, domain=domain
)
llm = ChatOpenAI(model="gpt-4o-mini")
prompt = PromptTemplate.from_template(
"What is the name of the contact with the id {contact_id}?"
)
chain = prompt | tool.invoke | llm
result = chain.invoke({"contact_id": "003XXXXXXXXXXXXXXX"})
API 參考:PromptTemplate | ChatOpenAI