| title | Tool Use Design Pattern | |
|---|---|---|
| subtitle | Lesson 4: AI Agents for Beginners | |
| theme | seriph | |
| transition | slide-left | |
| class | text-center | |
| highlighter | shiki | |
| lineNumbers | false | |
| drawings |
|
|
| download | true | |
| exportFilename | 04-tool-use-slides | |
| info | ## Tool Use Design Pattern This lesson explores how AI agents can use tools to achieve their goals. | |
| layout | cover | |
| background | ./images/lesson-4-thumbnail.png |
(Click the video icon above to view video of this lesson)
Tools are interesting because they allow AI agents to have a broader range of capabilities. Instead of the agent having a limited set of actions it can perform, by adding a tool, the agent can now perform a wide range of actions.
In this lesson, we're looking to answer the following questions:
- What is the tool use design pattern?
- What are the use cases it can be applied to?
- What are the elements/building blocks needed to implement the design pattern?
- What are the special considerations for using the Tool Use Design Pattern to build trustworthy AI agents?
After completing this lesson, you will be able to:
- Define the Tool Use Design Pattern and its purpose.
- Identify use cases where the Tool Use Design Pattern is applicable.
- Understand the key elements needed to implement the design pattern.
- Recognize considerations for ensuring trustworthiness in AI agents using this design pattern.
The Tool Use Design Pattern focuses on giving LLMs the ability to interact with external tools to achieve specific goals.
- Tools are code that can be executed by an agent to perform actions.
- A tool can be a simple function (e.g., calculator) or an API call to a third-party service (e.g., stock price lookup, weather forecast).
- In AI agents, tools are designed to be executed in response to model-generated function calls.
AI Agents can leverage tools to complete complex tasks, retrieve information, or make decisions. This pattern is useful for:
- Dynamic Information Retrieval: Querying external APIs/databases for up-to-date data (e.g., SQLite, stock prices, weather).
- Code Execution and Interpretation: Executing code/scripts for math problems, reports, or simulations.
- Workflow Automation: Automating multi-step workflows (e.g., task schedulers, email services, data pipelines).
- Customer Support: Interacting with CRM systems, ticketing platforms, or knowledge bases.
- Content Generation and Editing: Using tools like grammar checkers, summarizers, or content safety evaluators.
Key elements needed to implement the Tool Use Design Pattern:
Next, let's look at Function/Tool Calling in more detail.
- Function/Tool Calling: Primary way for LLMs to interact with tools. Functions (reusable code blocks) are the tools.
- Dynamic Information Retrieval: Querying external sources for current data.
- Code Execution and Interpretation: Running code/scripts.
- Workflow Automation: Integrating tools for multi-step processes.
- Customer Support: Interacting with support systems.
- Content Generation and Editing: Leveraging content-focused tools.
Function calling enables LLMs to interact with tools. 'Function' and 'Tool' are often used interchangeably.
::left::
Process:
- An LLM compares the user's request against a schema of available functions/tools.
- The LLM selects the most appropriate function and returns its name and arguments.
- The selected function's code is invoked.
- The function's response is sent back to the LLM.
- The LLM uses this information to respond to the user's original request.
::right::
Requirements for Developers:
- An LLM model that supports function calling.
- A schema containing function descriptions.
- The code for each described function.
1. Initialize an LLM that supports function calling:
- Check if your LLM supports this (e.g., Azure OpenAI).
# Initialize the Azure OpenAI client
# Refer to original README for full code
import os
from openai import AzureOpenAI
client = AzureOpenAI(
azure_endpoint = os.getenv("AZURE_OPENAI_ENDPOINT"),
api_key=os.getenv("AZURE_OPENAI_API_KEY"),
api_version="2024-05-01-preview"
)
# deployment_name = "YOUR_DEPLOYMENT_NAME" # Set your deployment name2. Create a Function Schema:
- Define a JSON schema with function name, description, and parameters.
- Pass this schema and user request to the LLM.
- The LLM returns a tool call, not the final answer.
# Function description for the model to read
# Refer to original README for full code
tools = [
{
"type": "function",
"function": {
"name": "get_current_time",
"description": "Get the current time in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city name, e.g. San Francisco",
},
},
"required": ["location"],
},
}
}
]messages = [{"role": "user", "content": "What's the current time in San Francisco"}]
response = client.chat.completions.create(
model=deployment_name,
messages=messages,
tools=tools,
tool_choice="auto",
)
response_message = response.choices[0].message
print(f"Model's response: {response_message}")-
SQL Injection Risk with LLM-generated SQL:
- A common concern for dynamically generated SQL.
- Mitigation: Properly configure database access permissions.
- Set database to read-only for the agent.
- Assign a read-only (SELECT) role (e.g., PostgreSQL, Azure SQL).
-
Secure Environment:
- Run the application in a secure environment.
- Enterprise Best Practice: Extract, Transform, Load (ETL) data from operational systems into a read-only data warehouse or database with a user-friendly schema.
- This ensures data is secure, optimized, and the app has restricted, read-only access.
- Azure AI Agents Service Workshop
- Contoso Creative Writer Multi-Agent Workshop
- Semantic Kernel Function Calling Tutorial
- Semantic Kernel Code Interpreter
- Autogen Tools
Review and Next Steps