AI Developer Agents for Salesforce Engineering using Autogen and OpenAI
Sri Kolagani
CRM Leader | 5XDreamforce Speaker | Certified Salesforce Application Architect | 3XTrailHeaDX Speaker | SFDC Brewery |Certified AWS Solutions Arch, Copado Admin, Slack Admin & Mulesoft Developer-1
Boosting Salesforce Team Productivity
For Salesforce teams, AI-powered agents provide tangible benefits, including:
Key Features
Frameworks and Architecture
The agent leverages several frameworks and technologies to deliver its functionalities:
1. Streamlit for UI
Streamlit is utilized to create a responsive and interactive web interface. It allows users to input task descriptions and view generated code and execution results in real-time.
Placeholder for Streamlit UI setup code
2. Simple-Salesforce for Salesforce Integration
The agent uses the simple-salesforce library to interact with the Salesforce API. This enables seamless authentication and execution of Salesforce operations.
Placeholder for Simple-Salesforce integration code
3. Microsoft Autogen for AI Capabilities
To interpret natural language tasks and generate appropriate code, the agent employs Microsoft Autogen. This framework facilitates the creation of conversational AI agents capable of complex reasoning and code generation.
Placeholder for Microsoft Autogen assistant agent initialization code
领英推荐
4. Local Command Line Code Execution
For executing the generated code, the agent incorporates a local command-line code executor. This ensures that the code runs in a controlled environment, and results are captured and displayed to the user.
Placeholder for Local Command Line Code Executor setup code
5. Group Chat Manager for Agent Coordination
The agent system includes a group chat manager to coordinate interactions between the assistant agent and the executor agent. This setup ensures a seamless flow from task interpretation to code execution.
Placeholder for Group Chat and Manager initialization code
Full Code of the Agent
# app.py
import re
import streamlit as st
from simple_salesforce import Salesforce
from autogen import AssistantAgent, ConversableAgent, GroupChat, GroupChatManager
from autogen.coding import LocalCommandLineCodeExecutor
# Load credentials from Streamlit secrets
sf_creds = st.secrets["salesforce"]
azure_config = st.secrets["azure"]
# Initialize Salesforce connection
sf = Salesforce(
username=sf_creds["username"],
password=sf_creds["password"],
security_token=sf_creds["security_token"],
domain=sf_creds["domain"]
)
# Initialize Assistant Agent
assistant = AssistantAgent(
name="assistant",
system_message="""You are a Salesforce CPQ expert. Generate Python code using simple_salesforce library.
Ensure the code is executable and handles Salesforce API responses properly.""",
llm_config={
"config_list": [{
"model": "productgpt-4", #Add your model name
"api_type": "azure",
"api_key": azure_config["api_key"],
"base_url": azure_config["base_url"],
"api_version": azure_config["api_version"]
}]
},
human_input_mode="NEVER"
)
# Initialize Code Executor
code_executor = LocalCommandLineCodeExecutor(timeout=30)
executor_agent = ConversableAgent(
name="executor",
system_message="Execute Python code and return results.",
code_execution_config={"executor": code_executor},
human_input_mode="NEVER"
)
# Initialize Group Chat and Manager
group_chat = GroupChat(
agents=[assistant, executor_agent],
messages=[],
max_round=6,
speaker_selection_method="round_robin"
)
manager = GroupChatManager(
groupchat=group_chat,
llm_config=assistant.llm_config
)
# Function to extract Python code from assistant's response
def extract_python_code(response):
"""Extract Python code from a response string."""
patterns = [
r'```python\n(.*?)\n```', # Standard markdown
r'```\n(.*?)\n```', # Code block without language
r'%%\n(.*?)\n%%', # Alternative block format
r'"""(.*?)"""', # Triple-quoted string
r"'''(.*?)'''" # Triple-single-quoted string
]
for pattern in patterns:
match = re.search(pattern, response, re.DOTALL)
if match:
code = match.group(1).strip()
if code.startswith('python'):
code = code[6:].lstrip()
return code
return None
# Streamlit UI setup
st.set_page_config(page_title="SFDC Brewery Salesforce Developer Agent", page_icon=":robot_face:", layout="wide")
st.title("SFDC Brewery Salesforce Developer Agent ??")
st.markdown("Welcome! Describe your JIRA task below, and I'll generate and execute the corresponding Salesforce code for you.")
# Initialize session state for chat history
if "messages" not in st.session_state:
st.session_state.messages = []
# Display chat history
for message in st.session_state.messages:
with st.chat_message(message["role"]):
st.markdown(message["content"])
# Chat input
if prompt := st.chat_input("Describe your Salesforce task:"):
# Add user message to chat history
st.session_state.messages.append({"role": "user", "content": prompt})
with st.chat_message("user"):
st.markdown(prompt)
# Generate assistant response
with st.chat_message("assistant"):
with st.spinner("Thinking..."):
try:
# Initiate chat with the manager
chat_result = assistant.initiate_chat(
recipient=manager,
message=prompt
)
# Collect all assistant responses
assistant_responses = [msg for msg in chat_result.chat_history if msg['name'] == 'assistant']
# Try all responses until we find valid code
python_code = None
for response in reversed(assistant_responses):
python_code = extract_python_code(response['content'])
if python_code:
break
if python_code:
st.code(python_code, language="python")
# Execute the code
execution_result = executor_agent.initiate_chat(
recipient=manager,
message=python_code
)
# Collect execution output
execution_output = ""
for msg in execution_result.chat_history:
if msg['name'] == 'executor':
execution_output += msg['content'] + "\n"
# Display results
st.subheader("Execution Result:")
if execution_output.strip():
st.text(execution_output)
else:
st.warning("No output was returned from the code execution.")
else:
st.error("No valid Python code generated")
st.text("Assistant responses for debugging:")
st.json([r['content'] for r in assistant_responses])
except Exception as e:
st.error(f"Error: {str(e)}")
Demo
How It Works
This architecture ensures that users can automate Salesforce development tasks efficiently, reducing manual effort and the potential for errors.
For a hands-on experience and to explore the codebase, visit the GitHub repository.
Note: Ensure that all credentials and sensitive information are securely managed and not hard-coded in production environments.
Keywords: #SFDCBrewery #OpenAI #Agentforce #Microsoft #Azure #Autogen #SriharideepKolagani #SalesforceDX #SalesforceDevelopment #Salesforce #SFDCBrewery #Brewery #SalesforceOnlineTraining #SalesforceTutorials #SalesforceCertification