Mosaic AI Agent Framework: Author a custom schema tool-calling LangGraph agent
This notebook demonstrates how to author a LangGraph agent that's compatible with Mosaic AI Agent Framework features. In this notebook you learn to:
- Author a tool-calling LangGraph agent wrapped with
ChatAgent
with custom inputs and outputs - Manually test the agent's output
- Log and deploy the agent
To learn more about authoring an agent using Mosaic AI Agent Framework, see Databricks documentation (AWS | Azure).
Prerequisites
- Address all
TODO
s in this notebook.
Define the agent in code
Define the agent code in a single cell below. This lets you easily write the agent code to a local Python file, using the %%writefile
magic command, for subsequent logging and deployment.
Agent tools
This agent code adds the built-in Unity Catalog function system.ai.python_exec
to the agent. The agent code also includes commented-out sample code for adding a vector search index to perform unstructured data retrieval.
For more examples of tools to add to your agent, see Databricks documentation (AWS | Azure)
Wrap the LangGraph agent using the ChatAgent
interface
For compatibility with Databricks AI features, the LangGraphChatAgent
class implements the ChatAgent
interface to wrap the LangGraph agent. This example uses the provided convenience APIs ChatAgentState
and ChatAgentToolNode
for ease of use.
Databricks recommends using ChatAgent
as it simplifies authoring multi-turn conversational agents using an open source standard. See MLflow's ChatAgent documentation.
Custom inputs and outputs
The agent is designed to handle custom inputs and outputs using the predict
methods and the add_custom_outputs
function.
In the
predict
andpredict_stream
methods of theLangGraphChatAgent
class:- Custom inputs are passed as an optional parameter and included in the request dictionary.
- Custom outputs are captured from the agent's response and added to the
ChatAgentResponse
object.
In the
add_custom_outputs
function:- This function is added as a node in the agent's workflow to append custom outputs to the state before returning the final response.
Test the agent
Interact with the agent to test its output. Since this notebook called mlflow.langchain.autolog()
you can view the trace for each step the agent takes.
Replace this placeholder input with an appropriate domain-specific example for your agent.
Log the agent as an MLflow model
Log the agent as code from the agent.py
file. See MLflow - Models from Code.
Enable automatic authentication for Databricks resources
For the most common Databricks resource types, Databricks supports and recommends declaring resource dependencies for the agent upfront during logging. This enables automatic authentication passthrough when you deploy the agent. With automatic authentication passthrough, Databricks automatically provisions, rotates, and manages short-lived credentials to securely access these resource dependencies from within the agent endpoint.
To enable automatic authentication, specify the dependent Databricks resources when calling mlflow.pyfunc.log_model().
- TODO: If your Unity Catalog tool queries a vector search index or leverages external functions, you need to include the dependent vector search index and UC connection objects, respectively, as resources. See docs (AWS | Azure).
Pre-deployment agent validation
Before registering and deploying the agent, perform pre-deployment checks using the mlflow.models.predict() API. See Databricks documentation (AWS | Azure).
Register the model to Unity Catalog
Before you deploy the agent, you must register the agent to Unity Catalog.
- TODO Update the
catalog
,schema
, andmodel_name
below to register the MLflow model to Unity Catalog.