Do you want a simple way to integrate Salesforce into your smolagents agents? There is an simple way to achieve this.

First, let’s add smolagents, python-dotenv and simple-salesforce cli, in requirements.txt file:

smolagents[gradio]
python-dotenv
simple-salesforce

Next, let’s code a simple AI Agent:

import os
import yaml
from dotenv import load_dotenv
from simple_salesforce import Salesforce
from smolagents import (CodeAgent, DuckDuckGoSearchTool, HfApiModel, load_tool,
                        tool)

from Gradio_UI import GradioUI

load_dotenv('.env')


@tool
def get_insurance_policies_by_status(status: str) -> str:
    """A tool that fetches the number of insurance policies from Salesforce based on their status.
    Args:
        status: The status of the insurance policies to fetch (e.g., 'Active', 'Inactive').
    Returns:
        A string indicating the number of insurance policies with the specified status.
    """
    try:
        # Load Salesforce credentials from environment variables
        sf_username = os.getenv("SALESFORCE_USERNAME")
        sf_password = os.getenv("SALESFORCE_PASSWORD")
        sf_token = os.getenv("SALESFORCE_SECURITY_TOKEN")

        # Connect to Salesforce
        sf = Salesforce(username=sf_username, password=sf_password, security_token=sf_token)

        print("Connected to Salesforce.")

        # Query Salesforce for active insurance policies
        query = f"SELECT COUNT() FROM InsurancePolicy__c WHERE Status__c = '{status}'"
        result = sf.query(query)

        # Extract the count from the query result
        policies_count = result['totalSize']

        return f"The number of insurance policies with status '{status}' is: {policies_count}"
    except Exception as e:
        return f"Error fetching insurance policies with status '{status}': {str(e)}"

model = HfApiModel(
    max_tokens=2096,
    temperature=0.5,
    model_id='Qwen/Qwen2.5-Coder-32B-Instruct',
    custom_role_conversions=None,
)

with open("prompts.yaml", 'r') as stream:
    prompt_templates = yaml.safe_load(stream)

agent = CodeAgent(
    model=model,
    tools=[get_insurance_policies_by_status],
    max_steps=6,
    verbosity_level=1,
    grammar=None,
    planning_interval=None,
    name=None,
    description=None,
    prompt_templates=prompt_templates
)

GradioUI(agent).launch()

The core of our AI Agent is the tool get_insurance_policies_by_status that get the status from the LLM model, calls Salesforce using simple_salesforce and returns the number of active/inactive policies.

If everything it’s ok, then in the end the result should look like:

chat

And of course we can check in Salesforce if the result was correct:

salesforce