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
- 去设置
- 点击"重置我的安全令牌"按钮,位于"个人信息"部分下
- 检查您的邮箱以获取新令牌
Instantiation
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
)
Invocation
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 参考:提示模板 |ChatOpenAI