1

Prerequisite 1: Install xpander-sdk and build

Log in to your Xpander account. - Navigate to the Connectors section. Create connectors for Notion and Slack. Follow the provided instructions to authorize access.

2

Prerequisite 2: Define Your AI App

  • In Xpander, create a new AI App (e.g., Notion AI Agent) using OpenAI Python SDK or LangChain Python SDK. - Set up the AI App to access your Notion and Slack connectors. - Obtain the API key for the AI App from the Xpander dashboard.
3

Prerequisite 3: Obtain API Keys

  • Obtain API keys from both your Xpander AI App and your LLM provider.
python3 -m venv .venv
source .venv/bin/activate
pip install https://assets.xpanderai.io/xpander-sdk.tar.gz
pip freeze > requirements.txt #Optional

Next, create a Python script (agent_example.py):

import os
from dotenv import load_dotenv  # Optional: Used for loading environment variables from a .env file
from xpander_sdk import XpanderClient, LLMProvider  # SDK for interacting with Xpander services
from openai import OpenAI  # SDK for interacting with OpenAI services
from loguru import logger  # Optional: Logging library for detailed logging

# Load environment variables from a .env file (if available)
load_dotenv()  # Optional

# Initialize the XpanderClient with your agent key and URL
xpanderAPIKey = os.environ.get("XPANDER_API_KEY", "")
client = XpanderClient(
    agent_key=xpanderAPIKey,
    agent_url="https://inbound.xpander.ai/agent/5d<AgentID>4",
    llm_provider=LLMProvider.OPEN_AI
)

# Retrieve available tools for the OpenAI plugin from Xpander
tools = client.tools(llm_provider=LLMProvider.OPEN_AI)

# Initialize the OpenAI client with your API key
OpenAPIKey = os.environ.get("OPENAI_API_KEY", "")
openai_client = OpenAI(api_key=OpenAPIKey)

# Define the messages to be sent to the OpenAI API
messages = [
    {
        "role": "user",
        "content": "Get the last blogpost from ContentDB"
    }
]

# Create a chat completion request using the OpenAI client
response = openai_client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    tools=tools,  # Provide the tools retrieved from Xpander
    tool_choice="required"  # Indicate that tool usage is required
)

# Process the chat completion response with the Xpander client
tool_responses = client.xpander_tool_call(tool_selector_response=response.model_dump())

# Output the responses from the tools
for tool_response in tool_responses:
    print(tool_response.response_message)

Running the Single Agent Example

Run the Python script:

python agent_example.py

View the response: This will retrieve the latest blog post from your ContentDB database in Notion.