diff --git a/00-course-setup/README.md b/00-course-setup/README.md index e69de29bb..f2457e589 100644 --- a/00-course-setup/README.md +++ b/00-course-setup/README.md @@ -0,0 +1,68 @@ +# Getting Started with "Generative AI for Beginners" + +We are very excited for you to start this course and see what you get inspired to build with Generative AI! + +To make your time successful, we have created this page that outlines any setup steps, technical requirements and how to get help when you need it. + + +## Setup Steps + + +### 1. Fork this Repo + +[Fork this entire repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo) to your own GitHub account to be able to change any code and complete the challenges. You can also [star (🌟) this repo](https://docs.github.com/en/get-started/exploring-projects-on-github/saving-repositories-with-stars) to find it and related repos easier. + + +### 2. Create a Codespace +To avoid any depenency issues when running the code, we reccomend running this course in a GitHub codespace. + +This can be created by selecting the `Code` option on your forked version of this repo and selecting the `Codespaces` option. + + + + + + + +### 3. Storing Your API Keys + + + + +## Lessons and Technical Requriments + +The course has 6 concept lessons and 6 coding lessons. + +For the coding lessons, we are using the Azure OpenAI Service. You will need access to the Azure OpenAI service and an API key to run this code. You can apply to get access by [completing this appplication](https://customervoice.microsoft.com/Pages/ResponsePage.aspx?id=v4j5cvGGr0GRqy180BHbR7en2Ais5pxKtso_Pz4b1_xUOFA5Qk1UWDRBMjg0WFhPMkIzTzhKQ1dWNyQlQCN0PWcu&culture=en-us&country=us). + +While you wait for your application to be processed, each coding lesson also includes a `README.md` file where you can view the code and outputs. + +### Setting Up Azure OpenAI Service for the First Time + + + +## Meet Other Learners + +We have created channels in our official [AI Community Discord server](https://aka.ms/genai-discord) for meeting other learners. This is a great way to create way to network with other like minded enterperneurs, builders, students and anyone looking to level up in Generative AI. + +[![](https://dcbadge.vercel.app/api/server/ByRwuEEgH4)](https://aka.ms/genai-discord) + +The project team will also be in this Discord server to help any learners. + + +## Contribute + +This course is an open-source initiative. If you see areas of imporvement or issues, please create a [Pull Request](https://github.com/microsoft/generative-ai-for-beginners/pulls) or log a [Github issue](https://github.com/microsoft/generative-ai-for-beginners/issues). + +The project team will be tracking all contributions and contributing to open source is an amazing way to build your career in Generative AI. + +Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com. + +Important: when translating text in this repo, please ensure that you do not use machine translation. We will verify translations via the community, so please only volunteer for translations in languages where you are proficient. + +When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repositories using our CLA. + +This project has adopted the Microsoft Open Source Code of Conduct. For more information read the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments. + + + diff --git a/07-building-chat-applications/README.md b/07-building-chat-applications/README.md index 01f116c04..ffe4df0e7 100644 --- a/07-building-chat-applications/README.md +++ b/07-building-chat-applications/README.md @@ -2,6 +2,9 @@ [![Building Generative AI-Powered Chat Applications](./img/genai_course_7[8].png)](https://youtu.be/Kw4i-tlKMrQ) +*(Click the image above to view video of this lesson)* + + Chat applications have become integrated into our daily lives, offering more than just a means of casual conversation. They're integral parts of customer service, technical support, and even sophisticated advisory systems. It's likely that you've gotten some help from a chat application not too long ago. As we integrate more advanced technologies like generative AI into these platforms, the complexity increases and so does the challenges. How do we efficiently build and seamlessly integrate these AI-powered applications for specific use cases? Once deployed, how can we monitor and ensure that the applications are operating at the highest level of quality, both in terms of functionality and adhering to the [six principles of responsible AI](https://www.microsoft.com/ai/responsible-ai)? As we move further into an age defined by automation and seamless human-machine interactions, understanding how generative AI transforms the scope, depth, and adaptability of chat applications becomes essential. This lesson will investigate the aspects of architecture that support these intricate systems, delve into the methodologies for fine-tuning them for domain-specific tasks, and evaluate the metrics and considerations pertinent to ensuring responsible AI deployment. diff --git a/10-building-low-code-ai-applications/README.md b/10-building-low-code-ai-applications/README.md index 150273b99..bddc65810 100644 --- a/10-building-low-code-ai-applications/README.md +++ b/10-building-low-code-ai-applications/README.md @@ -3,7 +3,7 @@ [![Building Low Code AI Applications](./images/genai_course_10[68].png)](https://youtu.be/XX8491SAF44) -*(Click the image abvoe to view video of this lesson)* +*(Click the image above to view video of this lesson)* ## Introduction diff --git a/11 - intergating-with-function-calling /Lesson 11 - Function Calling.ipynb b/11 - intergating-with-function-calling /Lesson 11 - Function Calling.ipynb index e2c597955..35f833b87 100644 --- a/11 - intergating-with-function-calling /Lesson 11 - Function Calling.ipynb +++ b/11 - intergating-with-function-calling /Lesson 11 - Function Calling.ipynb @@ -59,7 +59,7 @@ }, { "cell_type": "code", - "execution_count": 42, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -87,21 +87,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'student_1_description' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/Users/koreypace/Documents/Projects/function-calling/function-calling-final.ipynb Cell 5\u001b[0m line \u001b[0;36m1\n\u001b[1;32m 1\u001b[0m prompt1 \u001b[39m=\u001b[39m \u001b[39mf\u001b[39m\u001b[39m'''\u001b[39m\n\u001b[1;32m 2\u001b[0m \u001b[39mPlease extract the following information from the given text and return it as a JSON object:\u001b[39m\n\u001b[1;32m 3\u001b[0m \n\u001b[1;32m 4\u001b[0m \u001b[39mname\u001b[39m\n\u001b[1;32m 5\u001b[0m \u001b[39mmajor\u001b[39m\n\u001b[1;32m 6\u001b[0m \u001b[39mschool\u001b[39m\n\u001b[1;32m 7\u001b[0m \u001b[39mgrades\u001b[39m\n\u001b[1;32m 8\u001b[0m \u001b[39mclub\u001b[39m\n\u001b[1;32m 9\u001b[0m \n\u001b[1;32m 10\u001b[0m \u001b[39mThis is the body of text to extract the information from:\u001b[39m\n\u001b[0;32m---> 11\u001b[0m \u001b[39m{\u001b[39;00mstudent_1_description\u001b[39m}\u001b[39;00m\n\u001b[1;32m 12\u001b[0m \u001b[39m'''\u001b[39m\n\u001b[1;32m 15\u001b[0m prompt2 \u001b[39m=\u001b[39m \u001b[39mf\u001b[39m\u001b[39m'''\u001b[39m\n\u001b[1;32m 16\u001b[0m \u001b[39mPlease extract the following information from the given text and return it as a JSON object:\u001b[39m\n\u001b[1;32m 17\u001b[0m \n\u001b[0;32m (...)\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[39m{\u001b[39;00mstudent_2_description\u001b[39m}\u001b[39;00m\n\u001b[1;32m 26\u001b[0m \u001b[39m'''\u001b[39m\n", - "\u001b[0;31mNameError\u001b[0m: name 'student_1_description' is not defined" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "prompt1 = f'''\n", "Please extract the following information from the given text and return it as a JSON object:\n", @@ -140,7 +128,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -157,26 +145,14 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Now we can send both requests to the LLM and examine the repose we receive. " + "Now we can send both requests to the LLM and examine the reponse we receive. " ] }, { "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "ename": "NameError", - "evalue": "name 'prompt1' is not defined", - "output_type": "error", - "traceback": [ - "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", - "\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)", - "\u001b[1;32m/Users/koreypace/Documents/Projects/function-calling/function-calling-final.ipynb Cell 11\u001b[0m line \u001b[0;36m3\n\u001b[1;32m 1\u001b[0m openai_response1 \u001b[39m=\u001b[39m openai\u001b[39m.\u001b[39mChatCompletion\u001b[39m.\u001b[39mcreate(\n\u001b[1;32m 2\u001b[0m engine\u001b[39m=\u001b[39m\u001b[39m\"\u001b[39m\u001b[39mgpt-function\u001b[39m\u001b[39m\"\u001b[39m, \n\u001b[0;32m----> 3\u001b[0m messages \u001b[39m=\u001b[39m [{\u001b[39m'\u001b[39m\u001b[39mrole\u001b[39m\u001b[39m'\u001b[39m: \u001b[39m'\u001b[39m\u001b[39muser\u001b[39m\u001b[39m'\u001b[39m, \u001b[39m'\u001b[39m\u001b[39mcontent\u001b[39m\u001b[39m'\u001b[39m: prompt1}]\n\u001b[1;32m 4\u001b[0m )\n\u001b[1;32m 5\u001b[0m openai_response1[\u001b[39m'\u001b[39m\u001b[39mchoices\u001b[39m\u001b[39m'\u001b[39m][\u001b[39m0\u001b[39m][\u001b[39m'\u001b[39m\u001b[39mmessage\u001b[39m\u001b[39m'\u001b[39m][\u001b[39m'\u001b[39m\u001b[39mcontent\u001b[39m\u001b[39m'\u001b[39m] \n", - "\u001b[0;31mNameError\u001b[0m: name 'prompt1' is not defined" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "\n", "openai_response1 = openai.ChatCompletion.create(\n", @@ -188,20 +164,9 @@ }, { "cell_type": "code", - "execution_count": 47, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "'{\\n \"name\": \"Michael Lee\",\\n \"major\": \"computer science\",\\n \"school\": \"Stanford University\",\\n \"grades\": \"3.8 GPA\",\\n \"club\": \"Robotics Club\"\\n}'" - ] - }, - "execution_count": 47, - "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "\n", "\n", @@ -215,51 +180,20 @@ }, { "cell_type": "code", - "execution_count": 48, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'name': 'Emily Johnson',\n", - " 'major': 'computer science',\n", - " 'school': 'Duke University',\n", - " 'grades': '3.7',\n", - " 'club': 'Chess Club'}" - ] - }, - "execution_count": 48, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Loading the response as a JSON object\n", "json_response1 = json.loads(openai_response1['choices'][0]['message']['content'])\n", - "json_response1\n", - "\n" + "json_response1" ] }, { "cell_type": "code", - "execution_count": 49, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "{'name': 'Michael Lee',\n", - " 'major': 'computer science',\n", - " 'school': 'Stanford University',\n", - " 'grades': '3.8 GPA',\n", - " 'club': 'Robotics Club'}" - ] - }, - "execution_count": 49, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Loading the response as a JSON object\n", "json_response2 = json.loads(openai_response2['choices'][0]['message']['content'])\n", @@ -347,7 +281,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -367,7 +301,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -406,21 +340,27 @@ "**Definitions** \n", "\n", "`name` - The name of the function that we want to have called. \n", + "\n", "`description` - This is the description of how the function works. Here its important to be specific and clear \n", + "\n", "`parameters` - A list of values and format that you want the model to produce in its response \n", "\n", "\n", "`type` - The data type of the properties will be stored in \n", + "\n", "`properties` - List of the specific values that the model will use for its response \n", "\n", "\n", "`name` - the name of the property that model will use in its formatted response \n", + "\n", "`type` - The data type of the this property \n", + "\n", "`description` - Description of the specific property \n", "\n", "\n", "**Optional**\n", - "`required` - **Add what this means**\n" + "\n", + "`required` - required property for the function call to be completed \n" ] }, { @@ -430,28 +370,14 @@ "### Making the function call \n", "After defining a function, we now need to include it in the call to the Chat Completion API. We do this by adding `functions` to the request. In this case `functions=functions` . \n", "\n", - "There is also an option to set `function_call`to `auto`. This means we will let the LLM decide which function should be called based on the user message rather than assigning it ourselves." + "There is also an option to set `function_call` to `auto`. This means we will let the LLM decide which function should be called based on the user message rather than assigning it ourselves." ] }, { "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "{\n", - " \"role\": \"assistant\",\n", - " \"function_call\": {\n", - " \"name\": \"search_courses\",\n", - " \"arguments\": \"{\\n \\\"role\\\": \\\"student\\\",\\n \\\"product\\\": \\\"Azure\\\",\\n \\\"level\\\": \\\"beginner\\\"\\n}\"\n", - " }\n", - "}\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "response = openai.ChatCompletion.create( engine=\"gpt-function\", \n", " messages=messages, \n", @@ -496,7 +422,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -512,7 +438,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -549,25 +475,9 @@ }, { "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Recommended Function call:\n", - "{\n", - " \"name\": \"search_courses\",\n", - " \"arguments\": \"{\\n \\\"role\\\": \\\"student\\\",\\n \\\"product\\\": \\\"Azure\\\",\\n \\\"level\\\": \\\"beginner\\\"\\n}\"\n", - "}\n", - "\n", - "Output of function call:\n", - "[{'title': 'Describe concepts of cryptography', 'url': 'https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi'}, {'title': 'Introduction to audio classification with TensorFlow', 'url': 'https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi'}, {'title': 'Design a Performant Data Model in Azure SQL Database with Azure Data Studio', 'url': 'https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi'}, {'title': 'Getting started with the Microsoft Cloud Adoption Framework for Azure', 'url': 'https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi'}, {'title': 'Set up the Rust development environment', 'url': 'https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi'}]\n", - "\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# Check if the model wants to call a function\n", "if response_message.get(\"function_call\"):\n", @@ -613,25 +523,18 @@ "\n" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Now we will send the updated message to the LLM so we can recieve a natural language response instead of an API JSON formatted response. " + ] + }, { "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Messages in next request:\n", - "[{'role': 'user', 'content': 'Find me a good course for a beginner students to learn Azure.'}, {'role': 'assistant', 'function_call': {'name': 'search_courses', 'arguments': '{\\n \"role\": \"student\",\\n \"product\": \"Azure\",\\n \"level\": \"beginner\"\\n}'}, 'content': None}, {'role': 'function', 'name': 'search_courses', 'content': '\"[{\\'title\\': \\'Describe concepts of cryptography\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Introduction to audio classification with TensorFlow\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Design a Performant Data Model in Azure SQL Database with Azure Data Studio\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Getting started with the Microsoft Cloud Adoption Framework for Azure\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Set up the Rust development environment\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi\\'}]\"'}, {'role': 'assistant', 'function_call': {'name': 'search_courses', 'arguments': '{\\n \"role\": \"student\",\\n \"product\": \"Azure\",\\n \"level\": \"beginner\"\\n}'}, 'content': None}, {'role': 'function', 'name': 'search_courses', 'content': '\"[{\\'title\\': \\'Describe concepts of cryptography\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Introduction to audio classification with TensorFlow\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Design a Performant Data Model in Azure SQL Database with Azure Data Studio\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Getting started with the Microsoft Cloud Adoption Framework for Azure\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi\\'}, {\\'title\\': \\'Set up the Rust development environment\\', \\'url\\': \\'https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi\\'}]\"'}, {'role': 'assistant', 'function_call': {'name': 'search_courses', 'arguments': '{\\n \"role\": \"student\",\\n \"product\": \"Azure\",\\n \"level\": \"beginner\"\\n}'}, 'content': None}, {'role': 'function', 'name': 'search_courses', 'content': \"[{'title': 'Describe concepts of cryptography', 'url': 'https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi'}, {'title': 'Introduction to audio classification with TensorFlow', 'url': 'https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi'}, {'title': 'Design a Performant Data Model in Azure SQL Database with Azure Data Studio', 'url': 'https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi'}, {'title': 'Getting started with the Microsoft Cloud Adoption Framework for Azure', 'url': 'https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi'}, {'title': 'Set up the Rust development environment', 'url': 'https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi'}]\"}]\n", - "\n", - "{\n", - " \"role\": \"assistant\",\n", - " \"content\": \"I found some good courses for beginner students to learn Azure:\\n\\n1. [Describe concepts of cryptography](https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi)\\n2. [Introduction to audio classification with TensorFlow](https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi)\\n3. [Design a Performant Data Model in Azure SQL Database with Azure Data Studio](https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi)\\n4. [Getting started with the Microsoft Cloud Adoption Framework for Azure](https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi)\\n5. [Set up the Rust development environment](https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi)\\n\\nYou can click on the links to access the courses.\"\n", - "}\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(\"Messages in next request:\")\n", "print(messages)\n", diff --git a/11 - intergating-with-function-calling /README.md b/11 - intergating-with-function-calling /README.md new file mode 100644 index 000000000..4e72b98c7 --- /dev/null +++ b/11 - intergating-with-function-calling /README.md @@ -0,0 +1,444 @@ +![](./images/genai_course_11[90].png) + +**Video Coming Soon** +## Introduction + +This lesson will cover: +- What is function calling and its use cases +- How to create a function call using Azure OpenAI +- How to integrate a function call into an application + +## Learning Goals + +After completing this lesson you will know how to and understand: + +- The purpose of using function calling +- Setup Function Call using the Azure Open AI Service +- Design effective function calls for your applications use case + + +## Understanding Function Calls + +For this lesson, we want to build a feature for our education startup that allows users to use a chatbot to find technical courses. We will recommend courses that fit their skill level, current role and technology of interest. + +To complete this we will use a combination of: + - `Azure Open AI` to create a chat experience for the user + - `Microsoft Learn Catalog API` to help users find courses based on the request of the user + - `Function Calling` to take the user's query and send it to a function to make the API request. + +To get started, let's look at why we would want to use function calling in the first place: + +### Why Function Calling + +If you have completed any other lesson in this course, you probably understand the power of using Large Language Models (LLMs). Hopefully you can also see some of their limitations as well. + +Function Calling is a feature of the Azure Open AI Service to overcome to the following limitations: +1) Consistent response format +2) Ability to use data from other sources of an application in a chat context + +Before function calling, responses from an LLM were unstructured and inconsistent. Developers were required to write complex validation code to make sure they are able to handle each variation of a response. + +Users could not get answers like "What is the current weather in Stockholm?". This is because models were limited to the time the data was trained on. + +Let's look at the example below that illustrates this problem: + +Let's say we want to create a database of student data so we can suggest the right course to them. Below we have two descriptions of students that are very similar in the data they contain. + + +```python +student_1_description="Emily Johnson is a sophomore majoring in computer science at Duke University. She has a 3.7 GPA. Emily is an active member of the university's Chess Club and Debate Team. She hopes to pursue a career in software engineering after graduating." + +student_2_description = "Michael Lee is a sophomore majoring in computer science at Stanford University. He has a 3.8 GPA. Michael is known for his programming skills and is an active member of the university's Robotics Club. He hopes to pursue a career in artificial intelligence after finshing his studies." + +``` + +We want send this to an LLM to parse the data. This can later be used in our an application to send this to an API or store into a database. + +Let's create two identical prompts that we instruct the LLM on what information that we are interested in: + +```python +prompt1 = f''' +Please extract the following information from the given text and return it as a JSON object: + +name +major +school +grades +club + +This is the body of text to extract the information from: +{student_1_description} +''' + + +prompt2 = f''' +Please extract the following information from the given text and return it as a JSON object: + +name +major +school +grades +club + +This is the body of text to extract the information from: +{student_2_description} +''' + + +``` + +After creating these two prompts, we will send them to the LLM by using `openai.ChatCompletion`. We store the prompt in the `messages` variable and assign the role to `user`. This is to mimic a message from a user being written to a chatbot. + +```python +import os +import openai +import json +openai.api_type = "azure" +openai.api_base = "YOUR OPENAI API BASE URL" +openai.api_version = "2023-07-01-preview" +openai.api_key = os.getenv("OPENAI_API_KEY") + +``` + + +Now we can send both requests to the LLM and examine the response we receive. +```python + +openai_response1 = openai.ChatCompletion.create( + engine="gpt-function", + messages = [{'role': 'user', 'content': prompt1}] +) +openai_response1['choices'][0]['message']['content'] + +``` + +```python +openai_response2 = openai.ChatCompletion.create( + engine="gpt-function", + messages = [{'role': 'user', 'content': prompt2 }] +) +openai_response2['choices'][0]['message']['content'] + +```` + +```python +# Loading the response as a JSON object +json_response1 = json.loads(openai_response1['choices'][0]['message']['content']) +json_response1 + +``` + +Response 1: + +```json +{'name': 'Emily Johnson', + 'major': 'computer science', + 'school': 'Duke University', + 'grades': '3.7', + 'club': 'Chess Club'} +``` + +Response 2: + +```json +{'name': 'Michael Lee', + 'major': 'computer science', + 'school': 'Stanford University', + 'grades': '3.8 GPA', + 'club': 'Robotics Club'} + +``` + + +Even though the prompts are the same and the descriptions are similar, we can different formats of the `Grades` property. + +If you run the above cell multiple times, the format can be `3.7` or `3.7 GPA`. + +This is because the LLM takes unstructured data in the form of the written prompt and returns also unstructured data. We need to have structured format so that we know what to expect when storing or using this dat + +By using functional calling, we can make sure that we receive structured data back. When using function calling, the LLM does not actual call or run any functions. Instead, we create a structure for the LLM to follow for its responses. We then use those structured responses to know what function to run in our applications. + + ![](./images/Function-Flow.png) + +We can then take what is returned from the function and send this back to the LLM. The LLM will then respond using natural language to answer the user's query. + +### Use Cases for using function calls + +**Calling External Tools** +Chatbots are great at providing answers to questions from users. By using function calling, the chatbots can use messages from users to complete certain tasks. For example, a student can ask the chatbot to "Send email to my instructor saying I need more assistance with this subject". This can make a function call to `send_email(to: string, body: string)` + + +**Create API or Database Queries** +Users can find information using natural language that gets converted into a formatted query or API request. An example of this could be a teacher that request "Who are the students that completed the last assignment" which could call a function named `get_completed(student_name: string, assignment: int, current_status: string)` + + +**Creating Structured Data** +Users can take the a block of text or CSV and use the LLM to extract important information from it. For example, a student can convert Wikipedia article about peace agreements to create AI flash cards. This can be done by using a function called `get_important_facts(agreement_name: string, date_signed: string, parties_involved: list)` + +## 2. Creating Your First Function Call + +The process of creating a function call includes 3 main steps: +1. Calling the Chat Completions API with a list of your functions and a user message +2. Read the model's response to perform an action ie execute a function or API Call +3. Make another call to Chat Completions API with the response from your function to use that information to create a response to the user. + +![](./images/LLM-Flow.png) + +### Elements of a function call + +#### Users Input + +The first step is to create a user message. This can be dynamically assigned by take the value of a text input or you can assign a value here. If this is your first time working with the Chat Completions API, we need to define the `role` and the `content` of the message. + +The `role` can be either `system` (creating rules) , `assistant` (the model) or `user` (the end-user). For function calling, we will assign this as `user` and an example question. + +```python +messages= [ {"role": "user", "content": "Find me a good course for a beginner students to learn Azure."} ] +``` + +### Creating functions + +Next we will can define a function and their parameters of that function. We will use just one function here called `search_courses` but you can create multiple functions. + +**Important** : Functions are included in thee system message to the LLM and will be included in the amount of available tokens you have available. + +```python +functions = [ + { + "name":"search_courses", + "description":"Retrieves courses from the search index based on the parameters provided", + "parameters":{ + "type":"object", + "properties":{ + "role":{ + "type":"string", + "description":"The role of the learner (i.e. developer, data scientist, student, etc.)" + }, + "product":{ + "type":"string", + "description":"The product that the lesson is covering (i.e. Azure, Power BI, etc.)" + }, + "level":{ + "type":"string", + "description":"The level of experience the learner has prior to taking the course (i.e. beginner, intermediate, advanced)" + } + }, + "required":[ + "role" + ] + } + } +] + +``` + +**Definitions** + +`name` - The name of the function that we want to have called. + +`description` - This is the description of how the function works. Here its important to be specific and clear + +`parameters` - A list of values and format that you want the model to produce in its response + + +`type` - The data type of the properties will be stored in +`properties` - List of the specific values that the model will use for its response + + +`name` - the name of the property that model will use in its formatted response + +`type` - The data type of the this property + +`description` - Description of the specific property + + +**Optional** + +`required` - required property for the function call to be completed + +### Making the function call +After defining a function, we now need to include it in the call to the Chat Completion API. We do this by adding `functions` to the request. In this case `functions=functions` + +There is also an option to set `function_call` to `auto`. This means we will let the LLM decide which function should be called based on the user message rather than assigning it ourselves. + +```python +response = openai.ChatCompletion.create( engine="gpt-function", + messages=messages, + functions=functions, + function_call="auto", ) + +print(response['choices'][0]['message']) +``` + +**Output** +```json +{ + "role": "assistant", + "function_call": { + "name": "search_courses", + "arguments": "{\n \"role\": \"student\",\n \"product\": \"Azure\",\n \"level\": \"beginner\"\n}" + } +} +``` + +Now let's let's look at the response and see how it is formatted: + +{ + "role": "assistant", + "function_call": { + "name": "search_courses", + "arguments": "{\n \"role\": \"student\",\n \"product\": \"Azure\",\n \"level\": \"beginner\"\n}" + } +} + +You can see that the name of the function is called and from the user message, the LLM was able to find the data to fit the arguments of the function. + +## 3.Integrating Function Calls into an Application. + + +After we have tested the formatted response from the LLM, now we can integrate this into an application. + +### Managing the flow + +To integrate this into our application, let's take the following steps: + +First, lets make the call to the Open AI services and store the message in a variable called `response_message`. + +```python +response_message = response["choices"][0]["message"] +``` + +Now we will define the function that will call the Microsoft Learn API to get a list of courses: + +```python +import requests + +def search_courses(role, product, level): + url = "https://learn.microsoft.com/api/catalog/" + params = { + "role": role, + "product": product, + "level": level + } + response = requests.get(url, params=params) + modules = response.json()["modules"] + results = [] + for module in modules[:5]: + title = module["title"] + url = module["url"] + results.append({"title": title, "url": url}) + return str(results) + +``` + +As a best practice, we will then see if the model wants to call a function. After that, we will create a of the available functions and match it to the function that is being called. +We will then take the arguments of the function and map them to arguments of from the LLM. + +Lastly, we will append the function call message and the values that were returned by the `search_courses` message. This gives the LLM all the information it needs to +respond to the user using natural language. + +```python +# Check if the model wants to call a function +if response_message.get("function_call"): + print("Recommended Function call:") + print(response_message.get("function_call")) + print() + + + # Call the function. + function_name = response_message["function_call"]["name"] + + available_functions = { + "search_courses": search_courses, + } + function_to_call = available_functions[function_name] + + function_args = json.loads(response_message["function_call"]["arguments"]) + function_response = function_to_call(**function_args) + + print("Output of function call:") + print(function_response) + print(type(function_response)) + + + # Add the assistant response and function response to the messages + messages.append( # adding assistant response to messages + { + "role": response_message["role"], + "function_call": { + "name": function_name, + "arguments": response_message["function_call"]["arguments"], + }, + "content": None + } + ) + messages.append( # adding function response to messages + { + "role": "function", + "name": function_name, + "content":function_response, + } + ) +``` + +**Output** +```Recommended Function call: +{ + "name": "search_courses", + "arguments": "{\n \"role\": \"student\",\n \"product\": \"Azure\",\n \"level\": \"beginner\"\n}" +} + +Output of function call: +[{'title': 'Describe concepts of cryptography', 'url': 'https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi'}, {'title': 'Introduction to audio classification with TensorFlow', 'url': 'https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi'}, {'title': 'Design a Performant Data Model in Azure SQL Database with Azure Data Studio', 'url': 'https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi'}, {'title': 'Getting started with the Microsoft Cloud Adoption Framework for Azure', 'url': 'https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi'}, {'title': 'Set up the Rust development environment', 'url': 'https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi'}] + + +``` + + + +Now we will send the updated message to the LLM so we can recieve a natural language response instead of an API JSON formatted response. + +```python + +print("Messages in next request:") +print(messages) +print() + +second_response = openai.ChatCompletion.create( + messages=messages, + engine="gpt-function", + function_call="auto", + functions=functions, + temperature=0 + ) # get a new response from GPT where it can see the function response + + +print(second_response["choices"][0]["message"]) + +``` + +**Output** + +```python +{ + "role": "assistant", + "content": "I found some good courses for beginner students to learn Azure:\n\n1. [Describe concepts of cryptography](https://learn.microsoft.com/en-us/training/modules/describe-concepts-of-cryptography/?WT.mc_id=api_CatalogApi)\n2. [Introduction to audio classification with TensorFlow](https://learn.microsoft.com/en-us/training/modules/intro-audio-classification-tensorflow/?WT.mc_id=api_CatalogApi)\n3. [Design a Performant Data Model in Azure SQL Database with Azure Data Studio](https://learn.microsoft.com/en-us/training/modules/design-a-data-model-with-ads/?WT.mc_id=api_CatalogApi)\n4. [Getting started with the Microsoft Cloud Adoption Framework for Azure](https://learn.microsoft.com/en-us/training/modules/cloud-adoption-framework-getting-started/?WT.mc_id=api_CatalogApi)\n5. [Set up the Rust development environment](https://learn.microsoft.com/en-us/training/modules/rust-set-up-environment/?WT.mc_id=api_CatalogApi)\n\nYou can click on the links to access the courses." +} + +``` + +## Code Challenge + +To continue your learning of Azure Open AI Function Calling you can build: + - More parameters of the function that might help learners find more courses. You can find the available API parameters here: + - Create another function call that takes more information from the learner like their native language + - Create error handling on when the function call and/or API call does not return any suitable courses + + Hint: Follow the [Learn API reference documentation](https://learn.microsoft.com/en-us/training/support/catalog-api-developer-reference) page to see how and where this data is available. + + ## Great Work! Continue the Journey + +Want to learn more about different Function Calling? Go to the [contiuned learning page](/13%20-%20contiuned-learning/README.md) to find other great resources on this topic. + + +Head over to the Lesson 12 where we will look at how to [design UX for AI applications](/12-designing-ux-for-ai-applications/README.md)! diff --git a/11 - intergating-with-function-calling /images/genai_course_11[90].png b/11 - intergating-with-function-calling /images/genai_course_11[90].png new file mode 100644 index 000000000..5663bf252 Binary files /dev/null and b/11 - intergating-with-function-calling /images/genai_course_11[90].png differ diff --git a/README.md b/README.md index 03a125207..e98324f7a 100644 --- a/README.md +++ b/README.md @@ -8,51 +8,56 @@ [![GitHub forks](https://img.shields.io/github/forks/microsoft/Generative-AI-For-Beginners.svg?style=social&label=Fork)](https://GitHub.com/microsoft/Generative-AI-For-Beginners/network/) [![GitHub stars](https://img.shields.io/github/stars/microsoft/Generative-AI-For-Beginners.svg?style=social&label=Star)](https://GitHub.com/microsoft/Generative-AI-For-Beginners/stargazers/) -![GitHub stars](https://codespaces.new/OWNER/REPO-NAME/) +[![Open in GitHub Codespaces](https://img.shields.io/static/v1?style=for-the-badge&label=GitHub+Codespaces&message=Open&color=lightgrey&logo=github)](https://codespaces.new/microsoft/generative-ai-for-beginners) [![](https://dcbadge.vercel.app/api/server/ByRwuEEgH4)](https://aka.ms/genai-discord) -# Generative AI for Beginners - A 12 lesson curriculum on building Generative AI Applications +# Generative AI for Beginners - A Course on Building Generative AI Applications -Learn the fundamentals of web development with our 12-week comprehensive course by Microsoft Cloud Advocates. Each of the 24 lessons dive into JavaScript, CSS, and HTML through hands-on projects like terrariums, browser extensions, and space games. Engage with quizzes, discussions, and practical assignments. Enhance your skills and optimize your knowledge retention with our effective project-based pedagogy. Start your coding journey today! +Learn the fundamentals of building Generative AI applications with our 12-lesson comprehensive course by Microsoft Cloud Advocates. Each lesson covers a key aspect of Generative AI principles and application development. This course will take you from learning Generative AI concepts like LLMs and prompt engineering to applying those ideas by building real Generative AI applications. -## Each lesson includes: +Throughout this course, we will be building our own Generative AI startup so you can get an understanding of what it takes to launch your ideas. -- sketchnote +Build a strong foundation of Generative AI knowledge and start applying it today! + +## 📂 Each lesson includes: - optional supplemental video - written lesson - for project-based lessons, step-by-step guides on how to build the project - a challenge -- supplemental reading +- links + +## 🌱 Getting Started +To get started, [fork this entire repo](https://docs.github.com/en/get-started/quickstart/fork-a-repo) to your own GitHub account to be able to change any code and complete the challenges. You can also [star (🌟) this repo](https://docs.github.com/en/get-started/exploring-projects-on-github/saving-repositories-with-stars) to find it easier later! -🧑‍🎓 Are you a student? -Visit Student Hub page where you will find beginner resources, Student packs and even ways to get a free certificate voucher. This is the page you want to bookmark and check from time to time as we switch out content monthly. +This course is divided into 6 concept lessons and 6 coding lessons. The coding lessons have both a Juypter Notebook and README included if you only want to view the results of the code and text. To help you get up and running faster, we recommend using this course with Github Codespaces (no extra installs needed). You can open this by using the link below: +[![Open in GitHub Codespaces](https://img.shields.io/static/v1?style=for-the-badge&label=GitHub+Codespaces&message=Open&color=lightgrey&logo=github)](https://codespaces.new/microsoft/generative-ai-for-beginners) -## 🌱 Getting Started -Teachers, we have included some suggestions on how to use this curriculum. We'd love your feedback in our discussion forum! +To make sure you have access to the right tools for the coding projects, go to the [Course Introduction Page](/00-course-setup/README.md) -Learners, for each lesson, start with a pre-lecture quiz and follow through with reading the lecture material, completing the various activities and chck your understanding with the post-lecture quiz. +If you enjoyed this course, we would really apperciate it if you starred (🌟) this repo! -To enhance your learning experience, connect with your peers to work on the projects together! Discussions are encouraged in our discussion forum where our team of moderators will be available to answer your questions. +## 🗣️ Meet Other Learners, Get Support +We believe one of the best ways to learn is learning with others! Join our [official AI Discord server](https://aka.ms/genai-discord) to meet and network with other learners taking this course and get support. Who knows? You might find your next co-founder there! -## Lessons -| | Lesson Name | Concepts Taught | Learning Goal | +## 🗃️ Lessons +| | Lesson Link | Concepts Taught | Learning Goal | | :---: | :------------------------------------: | :---------------------------------------------------------: | ----------------------------------------------------------- | -| 00 | [Course Introduction - How to Take This Cours](/00-course-setup/README.md) | Tech setup and course structure | Setting you up for success while learning in this course| [Introduction to Generative AI](./1-getting-started/lessons/1-introduction-to-generative-ai/README.md) -| 01 | [Introduction to Generative AI and LLMs](./1-getting-started/README.md) | Generative AI and how we landed on the current technology landscape| What generative AI is and how Large Language Models work. | +| 00 | [Course Introduction - How to Take This Course](/00-course-setup/README.md) | Tech setup and course structure | Setting you up for success while learning in this course| [Introduction to Generative AI](./1-getting-started/lessons/1-introduction-to-generative-ai/README.md) +| 01 | [Introduction to Generative AI and LLMs](./1-getting-started/README.md) | Generative AI and how we landed on the current technology landscape| Understanding what Generative AI is and how Large Language Models (LLMs) work. | | 02 | [Exploring and comparing different LLMs](./2-exploring-and-comparing-different-llms/) |Testing, iterating, and comparing different Large Language Models | Select the right model for your use case | -| 03 | [Using Generative AI Responsibly](./03-using-generative-ai-responsibly%20/README.MD)| Understanding the limitations of foundation models and the risks behind AI | Learn how to build Generative AI Applications responsibliy -| 04 | [Understanding Prompt Engineering Fundamentals](./4-prompt-engineering-fundamentals/) | Hands-on application of Prompy Engineering Best Practices | Understand prompt structure & usage| -| 05 | [Creating Advanced Prompts](./05-advanced-prompts/README.md) | Extend your knowledge of prompt engineering by applying different techniques to your prompts | Apply prompt engineering techniques that improves the outcome of your prompts.| -| 06 | [Building Text Generation Applications](./06-text-generation-apps/README.md) | Build a text generation app using Azure OpenAI | Understand how to effecient use tokens and temparture to vary the model's output | | +| 03 | [Using Generative AI Responsibly](./03-using-generative-ai-responsibly%20/README.MD)| Understanding the limitations of foundation models and the risks behind AI | Learn how to build Generative AI Applications responsibly +| 04 | [Understanding Prompt Engineering Fundamentals](./4-prompt-engineering-fundamentals/) | Hands-on application of Prompt Engineering Best Practices | Understand prompt structure & usage| +| 05 | [Creating Advanced Prompts](./05-advanced-prompts/README.md) | Extend your knowledge of prompt engineering by applying different techniques to your prompts | Apply prompt engineering techniques that improve the outcome of your prompts.| +| 06 | [Building Text Generation Applications](./06-text-generation-apps/README.md) | Build a text generation app using Azure OpenAI | Understand how to efficiently use tokens and temperature to vary the model's output | | | 07 | [Building Chat Applications](./07-building-chat-applications/README.md) | Techniques for efficiently building and integrating chat applications.| Identify key metrics and considerations to effectively monitor and maintain the quality of AI-powered chat applications| -| 08 | [Building Search Apps Vector Databases](./08-building-search-applications/README.md) | Semantic vs Keyword search. What are text embeddings and how they apply to search | Create an application that uses Embeddings to search for data. | +| 08 | [Building Search Apps Vector Databases](./08-building-search-applications/README.md) | Semantic vs Keyword search. What are text embeddings and how do they apply to search | Create an application that uses Embeddings to search for data. | | 09 | [Building Image Generation Applications](./09-building-image-applications/README.md) | Image generation and why it's useful in building applications| Build an image generation application | | 10 | [Building Low Code AI Applications](./10-building-low-code-ai-applications/) | Introduction to Generative AI in Power Platform | Build a Student Assignment Tracker App for our education startup with Low Code | | | 11 | [Integrating External Applications with Function Calling](./11%20-%20Integrating%20External%20Applications%20with%20Function%20Calling%20/) | What is function calling and its use cases for applications | Setup a function call to retrieve data from an external API | | @@ -60,19 +65,14 @@ To enhance your learning experience, connect with your peers to work on the proj | xx | [Continue Your Learning](./13%20-%20contiuned-learning/) | Links to continue your learning from each lesson! | Mastering your Generative AI skills | | -## Meet Other Learners and Get Support - - - - -## Help Wanted! +### 🚀 Are you a startup or got an idea you want to launch? -Would you like to contribute a translation? Please read our [translation guidelines](TRANSLATIONS.md) and add input [to one of the translations issues](https://github.com/microsoft/generative-ai-for-beginners/issues?q=is%3Aissue+is%3Aopen+label%3Atranslation). If you want to translate into a new language, please raise a new issue for tracking. +Visit [Microsoft's Founders Hub](https://www.microsoft.com/startups) where you can apply to receive **free OpenAI credits** and up to **$150k towards Azure's leading AI services** as well as **1:1 expert support** with Microsoft's AI experts. -## Other Curricula +## 🎒 Other Courses -Our team produces other curricula! Check out: +Our team produces other courses! Check out: - [ML for Beginners](https://aka.ms/ml-beginners) - [Data Science for Beginners](https://aka.ms/datascience-beginners)