Lab 1 Function Calling Power
Introduction¶
What is Function Calling¶
Function calling enables Large Language Models (LLMs) to interact with external systems, execute tasks, and integrate with APIs. The LLM determines when to invoke a function based on user prompts and returns structured data for app use. Developers then implement the function logic within the app.
In this workshop, the function logic is used to execute the LLM dynamically generated SQL queries against the SQLite database.
Enabling Function Calling¶
If you’re familiar with Azure OpenAI Function Calling, it requires defining a function schema for the LLM. Azure AI Agent Service supports this approach and also offers a more flexible option.
With the Azure AI Agent Service and its Python SDK, you can define the function schema directly within the Python function’s docstring. This approach keeps the definition and implementation together, simplifying maintenance and enhancing readability.
For example, in the sales_data.py file, the async_fetch_sales_data_using_sqlite_query function uses a docstring to specify its signature, inputs, and outputs. The SDK parses this docstring to generate the callable function for the LLM:
async def async_fetch_sales_data_using_sqlite_query(self: "SalesData", sqlite_query: str) -> str:
"""
This function is used to answer user questions about Contoso sales data by executing SQLite queries against the database.
:param sqlite_query: The input should be a well-formed SQLite query to extract information based on the user's question. The query result will be returned as a JSON object.
:return: Return data in JSON serializable format.
:rtype: str
"""
Dynamic SQL Generation¶
When the app starts, it incorporates the database schema and key data into the instructions for the Azure AI Agent Service. Using this input, the LLM generates SQLite-compatible SQL queries to respond to user requests expressed in natural language.
Lab Exercise¶
In this lab, you'll enable the function logic to execute dynamic SQL queries against the SQLite database. The function will be called by the LLM to answer user questions about Contoso sales data.
-
Open the
main.py
. -
Uncomment the following lines by removing the "# " characters
# INSTRUCTIONS_FILE = "instructions/instructions_function_calling.txt" # toolset.add(functions)
Warning
The lines to be uncommented are not adjacent. When removing the # character, ensure you also delete the space that follows it.
-
Review the Code in main.py.
After uncommenting, your code should look like this:
INSTRUCTIONS_FILE = "instructions/instructions_function_calling.txt" # INSTRUCTIONS_FILE = "instructions/instructions_code_interpreter.txt" # INSTRUCTIONS_FILE = "instructions/instructions_file_search.txt" async def add_agent_tools(): """Add tools for the agent.""" # Add the functions tool toolset.add(functions) # Add the code interpreter tool # code_interpreter = CodeInterpreterTool() # toolset.add(code_interpreter) # Add the tents data sheet to a new vector data store # vector_store = await utilities.create_vector_store( # project_client, # files=[TENTS_DATA_SHEET_FILE], # vector_name_name="Contoso Product Information Vector Store", # ) # file_search_tool = FileSearchTool(vector_store_ids=[vector_store.id]) # toolset.add(file_search_tool)
Review the Instructions¶
Open the instructions/instructions_function_calling.txt file and review the Tools section for details on the function-calling instructions.
In VS Code, press Alt + Z (Windows/Linux) or Option + Z (Mac) to enable word wrap mode, making the instructions easier to read.
Info
The {database_schema_string} placeholder in the instructions is replaced with the actual database schema when the app initializes.
# Replace the placeholder with the database schema string
instructions = instructions.replace("{database_schema_string}", database_schema_string)
Run the Agent App¶
- Press F5 to run the app.
-
In the terminal, you'll see the app start, and the agent app will prompt you to enter your query.
Start a Conversation with the Agent¶
Start asking questions about Contoso sales data. For example:
-
Help
Here is an example of the LLM response to the help query:
I’m here to help with your sales data inquiries at Contoso. Could you please provide more details about what you need assistance with? Here are some example queries you might consider:
- What were the sales by region?
- What was last quarter's revenue?
- Which products sell best in Europe?
- Total shipping costs by region?
Feel free to ask any specific questions related to Contoso sales data!
Tip
The LLM will provide a list of starter questions that were defined in the instructions file. Try asking help in your language, for example
help in Hindi
orhelp in Italian
. -
What are the sales by region?
Here is an example of the LLM response to the sales by region query:
| Region | Total Revenue | |----------------|----------------| | AFRICA | $5,227,467 | | ASIA-PACIFIC | $5,363,718 | | CHINA | $10,540,412 | | EUROPE | $9,990,708 | | LATIN AMERICA | $5,386,552 | | MIDDLE EAST | $5,312,519 | | NORTH AMERICA | $15,986,462 |
Info
So, what’s happening behind the scenes to make it all work?
The LLM orchestrates the following steps:
-
The LLM generates an SQL query to answer the user's question. For the question "What are the sales by region?", the following SQL query is generated:
SELECT region, SUM(revenue) AS total_revenue FROM sales_data GROUP BY region;
-
The LLM then asks the agent app to call the async_fetch_sales_data_using_sqlite_query function, which retrieves the required data from the SQLite database and returns it to the LLM.
- Using the retrieved data, the LLM generates a table in Markdown format and returns it to the user. If you check the instructions file, you'll notice that the default output format is Markdown.
-
-
Show the 3 most recent transactions
Info
This query is valuable for learning purposes because it provides insight into the structure of the underlying SQLite Contoso sales database.
-
Show sales by category for europe
- Breakout sales by footwear
Debug the App (Optional)¶
Set a breakpoint in the async_fetch_sales_data_using_sqlite_query
function located in sales_data.py
to observe how the LLM requests data.
Ask More Questions¶
Now that you’ve set a breakpoint, ask additional questions about Contoso sales data to observe the function logic in action. Step through the function to execute the database query and return the results to the LLM.
Try these questions:
- What regions have the highest sales?
- What were the sales of tents for Contoso in the United States in April 2022?
Stop the Agent App¶
When you're done, type exit, or press Shift+F5 to stop the agent app.
Disable the Breakpoint¶
Remember to disable the breakpoint before running the app again.