{"id":8283,"date":"2024-09-10T12:24:24","date_gmt":"2024-09-10T12:24:24","guid":{"rendered":"https:\/\/metaschool.so\/articles\/?p=8283"},"modified":"2024-12-06T07:42:27","modified_gmt":"2024-12-06T07:42:27","slug":"openai-function-calling","status":"publish","type":"post","link":"https:\/\/metaschool.so\/articles\/openai-function-calling\/","title":{"rendered":"OpenAI Function Calling: A Developer&#8217;s Guide"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_56_1 ez-toc-wrap-left counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title \" >Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#What_is_OpenAI_Function_Calling\" title=\"What is OpenAI Function Calling?\">What is OpenAI Function Calling?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#Using_OpenAI_Without_Function_Calling\" title=\"Using OpenAI Without Function Calling\">Using OpenAI Without Function Calling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#OpenAI_Function_Calling_Example\" title=\"OpenAI Function Calling Example\">OpenAI Function Calling Example<\/a><ul class='ez-toc-list-level-3'><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#Multiple_Custom_Functions\" title=\"Multiple Custom Functions\">Multiple Custom Functions<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#Applications_of_OpenAI_Function_Calling\" title=\"Applications of OpenAI Function Calling\">Applications of OpenAI Function Calling<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/metaschool.so\/articles\/openai-function-calling\/#Conclusion\" title=\"Conclusion\">Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n\n<p>In January 2024, <a href=\"https:\/\/openai.com\/\" target=\"_blank\" rel=\"noopener\">OpenAI<\/a> introduced <strong>Function Calling<\/strong>, a powerful feature designed to allow developers to generate structured JSON output with GPT models. This feature addresses one of the biggest pain points developers have faced when using large language models: dealing with unstructured text. With <strong>OpenAI Function Calling<\/strong>, developers no longer need to rely on prompt engineering or regular expressions (RegEx) to parse responses into usable data formats. In this guide, we\u2019ll walk through how to use Function Calling, its applications, and how it can help you build more reliable AI applications.<\/p>\n\n\n\n<p>Before diving into OpenAI Function Calling, here&#8217;s a detailed guide on <a href=\"https:\/\/metaschool.so\/articles\/how-to-learn-ai\/\">how to learn AI<\/a> that you may refer to.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"What_is_OpenAI_Function_Calling\"><\/span>What is OpenAI Function Calling?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before Function Calling, developers using the OpenAI API often faced challenges in working with unstructured data. When trying to extract structured information like names, grades, or other specific details from text, it was difficult to ensure consistent results. Developers had to depend on prompt engineering or RegEx to parse the text into structured formats like JSON. This approach worked but was inefficient and prone to errors.<\/p>\n\n\n\n<p><strong>OpenAI Function Calling<\/strong> allows GPT models like GPT-3.5 and GPT-4 to take user-defined functions and output structured JSON directly, which makes data extraction more reliable and consistent. With this capability, developers can integrate AI seamlessly into applications that require structured outputs, such as forms, databases, or other systems that require regular, predictable data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Using_OpenAI_Without_Function_Calling\"><\/span>Using OpenAI Without Function Calling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Before diving into function calling, let\u2019s explore how to extract data without this feature.<\/p>\n\n\n\n<p>Read this small guide on <a href=\"https:\/\/metaschool.so\/articles\/openai-api-key\/\">OpenAI API Key<\/a> in case you need help with finding your API key.<\/p>\n\n\n\n<p>First, you\u2019ll need to set up the OpenAI Python API. If you haven&#8217;t already, follow the steps to obtain your API key and install the OpenAI library using:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">bashCopy code<code>pip install --upgrade openai -q\n<\/code><\/pre>\n\n\n\n<p>Once you\u2019ve set up your API key and initialized the OpenAI client, you can generate outputs using a prompt:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>import os\nfrom openai import OpenAI\n\nclient = OpenAI(\n  api_key=os.environ['OPENAI_API_KEY'],\n)\n\nstudent_1_description = \"David Nguyen is a sophomore majoring in computer science at Stanford University...\"\nprompt1 = f'''\nPlease extract the following information from the given text and return it as a JSON object:\n\nname\nmajor\nschool\ngrades\nclub\n\nThis is the body of text to extract the information from:\n{student_1_description}\n'''\n\nopenai_response = client.chat.completions.create(\n    model='gpt-3.5-turbo',\n    messages=[{'role': 'user', 'content': prompt1}]\n)\n\nopenai_response.choices[0].message.content\n<\/code><\/pre>\n\n\n\n<p>This method works well for straightforward prompts, but it can be inconsistent. For instance, if you use the same prompt with different data, the output structure might vary.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>student_2_description = \"Ravi Patel is a sophomore majoring in computer science at the University of Michigan...\"\n<\/code><\/pre>\n\n\n\n<p>When you compare outputs for multiple students, the results might not be the same. This inconsistency is what <strong>OpenAI Function Calling<\/strong> aims to resolve.<\/p>\n\n\n\n<p>Here, we might get different JSON structures, such as returning a list of clubs for one student but not for another. These inconsistencies can be problematic when building applications that rely on structured, predictable outputs.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"OpenAI_Function_Calling_Example\"><\/span>OpenAI Function Calling Example<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Now, let\u2019s see how <strong>Function Calling<\/strong> can resolve these issues. We will define a custom function that extracts student information in a structured format. The function defines parameters like name, major, school, GPA, and club.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>student_custom_functions = [\n    {\n        'name': 'extract_student_info',\n        'description': 'Get the student information from the body of the input text',\n        'parameters': {\n            'type': 'object',\n            'properties': {\n                'name': {\n                    'type': 'string',\n                    'description': 'Name of the person'\n                },\n                'major': {\n                    'type': 'string',\n                    'description': 'Major subject.'\n                },\n                'school': {\n                    'type': 'string',\n                    'description': 'The university name.'\n                },\n                'grades': {\n                    'type': 'integer',\n                    'description': 'GPA of the student.'\n                },\n                'club': {\n                    'type': 'string',\n                    'description': 'School club for extracurricular activities.'\n                }\n            }\n        }\n    }\n]\n\nstudent_description = [student_1_description, student_2_description]\nfor i in student_description:\n    response = client.chat.completions.create(\n        model='gpt-3.5-turbo',\n        messages=[{'role': 'user', 'content': i}],\n        functions=student_custom_functions,\n        function_call='auto'\n    )\n\n    json_response = json.loads(response.choices[0].message.function_call.arguments)\n    print(json_response)\n<\/code><\/pre>\n\n\n\n<p>This time, we get a consistent output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">jsonCopy code<code>{'name': 'David Nguyen', 'major': 'computer science', 'school': 'Stanford University', 'grades': 3.8, 'club': 'Robotics Club'}\n{'name': 'Ravi Patel', 'major': 'computer science', 'school': 'University of Michigan', 'grades': 3.7, 'club': 'Chess Club'}\n<\/code><\/pre>\n\n\n\n<p>This ensures the output is always structured in a predictable format, solving the inconsistencies from the previous method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Multiple_Custom_Functions\"><\/span>Multiple Custom Functions<span class=\"ez-toc-section-end\"><\/span><\/h3>\n\n\n\n<p>OpenAI Function Calling allows you to use multiple functions. For example, you can extract both student and school information using two different functions.<\/p>\n\n\n\n<p>Here\u2019s how you can define an additional function to extract school information:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>custom_functions = [\n    {\n        'name': 'extract_student_info',\n        'description': 'Get the student information from the body of the input text',\n        'parameters': {\n            'type': 'object',\n            'properties': {\n                'name': {\n                    'type': 'string',\n                    'description': 'Name of the person'\n                },\n                'major': {\n                    'type': 'string',\n                    'description': 'Major subject.'\n                },\n                'school': {\n                    'type': 'string',\n                    'description': 'The university name.'\n                },\n                'grades': {\n                    'type': 'integer',\n                    'description': 'GPA of the student.'\n                },\n                'club': {\n                    'type': 'string',\n                    'description': 'School club for extracurricular activities.'\n                }\n            }\n        }\n    },\n    {\n        'name': 'extract_school_info',\n        'description': 'Get the school information from the body of the input text',\n        'parameters': {\n            'type': 'object',\n            'properties': {\n                'name': {\n                    'type': 'string',\n                    'description': 'Name of the school.'\n                },\n                'ranking': {\n                    'type': 'integer',\n                    'description': 'QS world ranking of the school.'\n                },\n                'country': {\n                    'type': 'string',\n                    'description': 'Country of the school.'\n                },\n                'no_of_students': {\n                    'type': 'integer',\n                    'description': 'Number of students enrolled in the school.'\n                }\n            }\n        }\n    }\n]\n<\/code><\/pre>\n\n\n\n<p>You can now pass both student and school descriptions to the model, and it will automatically choose the correct function based on the input data:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>description = [student_1_description, school_1_description]\nfor i in description:\n    response = client.chat.completions.create(\n        model='gpt-3.5-turbo',\n        messages=[{'role': 'user', 'content': i}],\n        functions=custom_functions,\n        function_call='auto'\n    )\n\n    json_response = json.loads(response.choices[0].message.function_call.arguments)\n    print(json_response)\n<\/code><\/pre>\n\n\n\n<p>With <strong>OpenAI Function Calling<\/strong>, the model consistently provides structured outputs, improving the reliability of your applications.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Applications_of_OpenAI_Function_Calling\"><\/span>Applications of OpenAI Function Calling<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>There are numerous ways developers can utilize <strong>OpenAI Function Calling<\/strong>:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Extracting structured data:<\/strong> Extract information from text in JSON format without manual prompt engineering.<\/li>\n\n\n\n<li><strong>Interacting with APIs:<\/strong> Call external APIs and extract data from responses in a structured format.<\/li>\n\n\n\n<li><strong>SQL and database operations:<\/strong> Automatically generate structured queries to interact with databases.<\/li>\n\n\n\n<li><strong>Text summarization:<\/strong> Summarize content in a consistent, structured way using predefined functions.<\/li>\n<\/ol>\n\n\n\n<p>For instance, you can summarize both student and school data using the custom functions we defined earlier:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">pythonCopy code<code>def extract_student_info(name, major, school, grades, club):\n    return f\"{name} is majoring in {major} at {school}. They have a {grades} GPA and are part of the {club}.\"\n\ndef extract_school_info(name, ranking, country, no_of_students):\n    return f\"{name} is in {country}, ranked #{ranking} in the world, with {no_of_students} students.\"\n<\/code><\/pre>\n\n\n\n<p>By passing your descriptions through the model, you can generate precise, structured responses.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p><strong>OpenAI Function Calling<\/strong> is a powerful tool that solves many of the challenges developers face when working with unstructured text. By allowing developers to define custom functions and generate structured JSON outputs, it eliminates the need for RegEx and reduces the inconsistencies that arise when relying on traditional prompt engineering. Whether you\u2019re extracting structured data, interacting with APIs, or creating complex applications, Function Calling makes the process more efficient and reliable.<\/p>\n\n\n\n<p>For developers looking to integrate this feature, it provides a clear path to building more robust and stable AI-driven applications.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>By using <strong>OpenAI Function Calling<\/strong>, developers can unlock powerful capabilities and deliver better, more reliable AI applications with fewer inconsistencies and errors.<\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":18,"featured_media":10985,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"neve_meta_sidebar":"","neve_meta_container":"","neve_meta_enable_content_width":"","neve_meta_content_width":0,"neve_meta_title_alignment":"","neve_meta_author_avatar":"","neve_post_elements_order":"","neve_meta_disable_header":"","neve_meta_disable_footer":"","neve_meta_disable_title":"","footnotes":""},"categories":[344],"tags":[],"class_list":["post-8283","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence"],"_links":{"self":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8283","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/comments?post=8283"}],"version-history":[{"count":3,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8283\/revisions"}],"predecessor-version":[{"id":8293,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/posts\/8283\/revisions\/8293"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media\/10985"}],"wp:attachment":[{"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/media?parent=8283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/categories?post=8283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaschool.so\/articles\/wp-json\/wp\/v2\/tags?post=8283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}