Functions With Azure Search
Functionsazure-openai-samplesBasic_Samples
Export
Function calling with Azure Cognitive Search
In this notebook, we'll show how to create a simple chatbot to help you find or create a good recipe. We'll create an index in Azure Cognitive Search using vector search, and then use use function calling to write queries to the index.
All of the recipes used in this sample were generated by gpt-35-turbo for demo purposes. The recipes are not guaranteed to be safe or taste good so we don't recommend trying them.
[1]
[2]
[3]
1.0 Create the search index and load the data
[4]
recipes-vectors deleted recipes-vectors created
Define a helper function to create embeddings
[5]
Load the data into Azure Cognitive Search
[6]
Uploaded 100 documents Uploaded 100 documents Uploaded 100 documents Uploaded 100 documents Uploaded 3 documents
2.0 Test function calling
[7]
{
"role": "assistant",
"function_call": {
"name": "query_recipes",
"arguments": "{\n \"query\": \"lasagna\"\n}"
}
}
Define function to call Azure Cognitive Search
[8]
3.0 Get things running end to end
[9]
[10]
Recommended Function call:
{
"name": "query_recipes",
"arguments": "{\n \"query\": \"pasta\",\n \"time_filter\": \"total_time lt 60\"\n}"
}
Output of function call:
Recipe 46: Pesto Pasta: Pesto Pasta is a classic Italian dish that combines al dente pasta with a flavorful sauce made from fresh basil, garlic, pine nuts, Parmesan cheese, and olive oil. It's a versatile and delicious meal that can be enjoyed as a main dish or as a side.
Recipe 76: Tortellini Alfredo: A creamy and delicious pasta dish filled with cheesy tortellini, smothered in a rich alfredo sauce.
Recipe 65: Cacio e Pepe: Cacio e Pepe is a classic Roman dish that translates to "cheese and pepper". It consists of simple ingredients that when combined, create a delicious and comforting pasta dish with a creamy, cheesy, and peppery sauce.
Messages in second request:
{'role': 'system', 'content': "Assistant is a large language model designed to help users find and create recipes.\n\nYou have access to an Azure Cognitive Search index with hundreds of recipes. You can search for recipes by name, ingredient, or cuisine.\n\nYou are designed to be an interactive assistant, so you can ask users clarifying questions to help them find the right recipe. It's better to give more detailed queries to the search index rather than vague one.\n"}
{'role': 'user', 'content': 'I want to make a pasta dish that takes less than 60 minutes to make.'}
{'role': 'assistant', 'function_call': {'name': 'query_recipes', 'arguments': '{\n "query": "pasta",\n "time_filter": "total_time lt 60"\n}'}, 'content': None}
{'role': 'function', 'name': 'query_recipes', 'content': 'Recipe 46: Pesto Pasta: Pesto Pasta is a classic Italian dish that combines al dente pasta with a flavorful sauce made from fresh basil, garlic, pine nuts, Parmesan cheese, and olive oil. It\'s a versatile and delicious meal that can be enjoyed as a main dish or as a side.\nRecipe 76: Tortellini Alfredo: A creamy and delicious pasta dish filled with cheesy tortellini, smothered in a rich alfredo sauce.\nRecipe 65: Cacio e Pepe: Cacio e Pepe is a classic Roman dish that translates to "cheese and pepper". It consists of simple ingredients that when combined, create a delicious and comforting pasta dish with a creamy, cheesy, and peppery sauce.\n'}
Final response:
Here are some pasta dishes that take less than 60 minutes to make:
1. Pesto Pasta: This classic Italian dish combines al dente pasta with a flavorful sauce made from fresh basil, garlic, pine nuts, Parmesan cheese, and olive oil.
2. Tortellini Alfredo: A creamy and delicious pasta dish filled with cheesy tortellini, smothered in a rich alfredo sauce.
3. Cacio e Pepe: A classic Roman dish that consists of simple ingredients like cheese and pepper. It creates a delicious and comforting pasta dish with a creamy, cheesy, and peppery sauce.
Let me know if you'd like the full recipe for any of these dishes or if you have any other questions!
4.0 Define additional functions
Now that we have the query_recipes function defined, we can add additional functions to add more capabilities.
[11]
Define a function to convert common measurements
[12]
'3 teaspoons'
Define a function to get recipes by id
[13]
'Recipe: Malabar Paratha\n\nDescription: Malabar Paratha is a popular Indian flatbread known for its flaky and layered texture. It is perfect for serving alongside curries or as a delicious snack on its own.\n\nCook Time: 10 minutes\nPrep Time: 20 minutes\nTotal Time: 30 minutes\n\nIngredients:\n- 2 cups all-purpose flour\n- 1/2 teaspoon salt\n- 1 tablespoon ghee (clarified butter)\n- 3/4 cup water\n- Additional ghee for brushing\n\nInstructions:\n1. In a large mixing bowl, combine the all-purpose flour and salt. Mix well.\n2. Add the ghee to the flour mixture and mix using your fingertips until the mixture resembles breadcrumbs.\n3. Gradually add water to the mixture while kneading the dough. Continue kneading until a soft and smooth dough is formed. Cover the dough and let it rest for 15 minutes.\n4. After the dough has rested, divide it into small equal-sized balls, approximately golf ball-sized.\n5. Take one dough ball and roll it out into a small circle using a rolling pin.\n6. Brush the surface of the rolled dough with ghee.\n7. Starting from one end, tightly roll the dough into a cylinder shape.\n8. Once rolled, coil the cylinder into a spiral shape.\n9. Flatten the spiral-shaped dough using a rolling pin to make a circular shaped paratha.\n10. Heat a tawa or flat pan over medium heat. Place the flattened paratha onto the pan and cook for about 1-2 minutes, or until golden brown spots start to appear.\n11. Flip the paratha and brush the cooked side with ghee. Cook for another 1-2 minutes, or until golden brown spots appear on the other side as well.\n12. Remove the paratha from the pan and place it on a serving plate. Repeat the process for the remaining dough balls.\n13. Serve the Malabar Parathas hot with your favorite curry or enjoy them on their own.\n\nEnjoy your delicious homemade Malabar Parathas!'
5.0 Test more examples
[14]
[15]
Recommended Function call:
{
"name": "convert_measurement",
"arguments": "{\n \"amount\": 2,\n \"from_unit\": \"tablespoons\",\n \"to_unit\": \"cups\"\n}"
}
Output of function call:
0.125 cups
Messages in second request:
{'role': 'system', 'content': "Assistant is a large language model designed to help users find and create recipes.\n\nYou have access to an Azure Cognitive Search index with hundreds of recipes. You can search for recipes by name, ingredient, or cuisine.\n\nYou are designed to be an interactive assistant, so you can ask users clarifying questions to help them find the right recipe. It's better to give more detailed queries to the search index rather than vague one.\n"}
{'role': 'user', 'content': 'How many cups is 2 tablespoons of butter?'}
{'role': 'assistant', 'function_call': {'name': 'convert_measurement', 'arguments': '{\n "amount": 2,\n "from_unit": "tablespoons",\n "to_unit": "cups"\n}'}, 'content': None}
{'role': 'function', 'name': 'convert_measurement', 'content': '0.125 cups'}
Final response:
2 tablespoons of butter is equal to 0.125 cups.
[ ]