LangChain
This quickstart guide will help you build agents using Langchain Framework and add tool calling ability natively to the agent.
What you’ll build
Section titled “What you’ll build”In this quickstart, you’ll build a simple integration that:
- Authenticates a user with GMail to authenticate their Gmail account via OAuth 2.0
- Fetches last 5 unread emails from the user’s inbox
Prerequisites
Section titled “Prerequisites”Before you start, make sure you have:
- Scalekit API credentials (Client ID and Client Secret)
- Development environment (Node.js, Python, or similar)
Step 1: Set up your environment
Section titled “Step 1: Set up your environment”First, install the Scalekit Python SDK and initialize the client with your API credentials:
pip install scalekit-sdk-python
import scalekit.clientimport os
scalekit = scalekit.client.ScalekitClient( client_id=os.getenv("SCALEKIT_CLIENT_ID"), client_secret=os.getenv("SCALEKIT_CLIENT_SECRET"), env_url=os.getenv("SCALEKIT_ENV_URL"),)connect = scalekit.connect
Step 2: Create a connected account
Section titled “Step 2: Create a connected account”Let’s authorize a user to access their Gmail account by creating a connected account. This represents the user’s connection to their Gmail account:
# Create a connected account for user if it doesn't exist alreadyconnected_account = connect.get_or_create_connected_account( connection_name="gmail", identifier="user_123" )
print(f'Connected account created: {connected_account.id}')
Step 3: Authenticate the user
Section titled “Step 3: Authenticate the user”Inorder to execute any tools on behalf of the user, the user first needs to grant authorization to access their gmail account. Scalekit automatically handles the entire OAuth workflow with the Gmail provider, gets the access token, refreshes the access token periodically based on the refresh token etc.
If the user’s access token is expired, Scalekit will automatically refresh the access token using the refresh token. At any time, you can check the authorization status of the connected account and determine if the user needs to re-authorize the connection.
# If the user hasn't yet authorized the gmail connection or if the user's access token is expired,# generate a magic link and redirect the user to this link so that the user can complete authorizationif(connected_account.status != "ACTIVE"): link_response = connect.get_authorization_link( connection_name="gmail", identifier="user_123" ) print(f"🔗click on the link to authorize gmail", link_response.link) input(f"⎆ Press Enter after authorizing gmail...")
# In a real app, redirect the user to this URL so that the user can complete the authentication process for their gmail account
Step 4: Build a Langchain Agent
Section titled “Step 4: Build a Langchain Agent”Let’s build a simple agent that can fetch the last 5 unread emails from the user’s inbox and generate a summary of the emails.
- Fetch the last 5 unread emails from the user’s inbox
- Generate a summary of the emails.
from langchain_openai import ChatOpenAIfrom langchain.agents import AgentExecutor, create_openai_tools_agent
# use scalekit SDK to fetch all GMAIL tools in langchain formattools = connect.langchain.get_tools( identifier=identifier, filter = Filter(provider = "GMAIL") # all tools for provider used by default)
llm = ChatOpenAI(model="gpt-4o")agent = create_openai_tools_agent(llm, tools, prompt)agent_executor = AgentExecutor(agent=agent, tools=tools,verbose=True)result = agent_executor.invoke({"input":"fetch my last 5 unread emails and summarize it"})
Next steps
Section titled “Next steps”Congratulations! You’ve successfully built a langchain agent and gave it access to gmail tools to execute tool calls on behalf of a user based on the user’s query.