Level Up Your Chatbots with OpenAI Function Calling
Tags: openai, javascript, chatgpt, webdev
Published: 2023-07-10 01:34:01

Photo Credit: Photo by <a href="https://unsplash.com/@walkator?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Walkator</a> on <a href="https://unsplash.com/photos/klMii3cR9iI?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText">Unsplash</a>
  
Hello again, code whisperers and tech wizards! Last time, we plunged into the world of function calling with OpenAI's GPT-4 and GPT-3.5 Turbo, a journey that brought structure to chaos. Today, we're strapping on our jetpacks again to soar through a tantalizing, real-world example - a travel booking chatbot.

If you're just tuning in and the term 'function calling' has you scratching your head, make a pit stop at our p[revious post](https://dev.to/stonediggity/openai-function-calling-ok9) to catch up. Buckled up and ready to go? Let's do it.

Before we start I just want to highlight the really cool thing about incorporating functions into your Chat Bot. The functions allow endless options for parsing unstructured chat text into some sort of API call, the response of which, can be handled by your bot! I think this is a total game changer, especially with how powerful fine tuned bots and those working with vector databases can be.

##Scenario: The All-Knowing Travel Booking Chatbot
You're coding a chatbot for a travel booking app, and you hit a hurdle. A user input like, "I need to book a trip from Bonn to Amsterdam for my family. The airline must fly direct." is stumping your bot. Our mission? Translating this conversational chaos into structured data for an API call.

##Crafting Functions in Node.js


Remember our Python dance from last time? We're switching the rhythm to Node.js now. Just like before, we're going to define the function book_travel. This function needs parameters - destination, departure, number_people, and travel_mode.

Here's your first dance move:
```
const functions = [
  {
    name: 'book_travel',
    description: 'Book travel',
    parameters: {
      type: 'object',
      properties: {
        destination: {type: 'string', description: 'Your travel destination.'},
        departure: {type: 'string', description: 'From where are you traveling'},
        number_people: {type: 'string', description: 'How many people are traveling'},
        travel_mode: {type: 'string', description: 'What mode of travel will it be.'}
      }
    }
  }
];
```
This function will be included during the chat as defined in our previous post so I won't go into it here. The Open AI [docs](https://platform.openai.com/docs/introduction) are also fantastic and will give you all the syntax you need. The amazing thing is that it will determine when it needs to call the function in order to get the data it needs. Pretty amazing!

The Call to Action
Now that we have our function defined, GPT-3.5 Turbo will step in to wrestle the user's input into a structured JSON object. This neat little package is then used to call the book_travel function, and voila! Our bot is booking trips like a pro.

This is what your bot's triumph looks like:

```
{
  "id": "chatcmpl-7R3vsPC6JndweAQZXvCIvSWnaXDMP",
  "object": "chat.completion",
  "created": 1686684328,
  "model": "gpt-3.5-turbo-0613",
  "choices": [
    {
      "index": 0,
      "message": {
        "role": "assistant",
        "content": null,
        "function_call": {
          "name": "book_travel",
          "arguments": "{\n  \"destination\": \"Amsterdam\",\n  \"departure\": \"Bonn\",\n  \"number_people\": \"6\",\n  \"travel_mode\": \"airline\"\n}"
        }
      },
      "finish_reason": "function_call"
    }
  ],
  "usage": {
    "prompt_tokens": 122,
    "completion_tokens": 42,
    "total_tokens": 164
  }
}
```
Look at that! GPT-3.5 Turbo parsed the user's input and whipped up the arguments for the book_travel function call.

##Use Cases
Let's not forget, function calling isn't just a one-trick pony. It can flex its muscles for:

- Building chatbots that can wrestle with external APIs.
- Converting the chaos of natural language into structured JSON data.
- Extracting structured data from towering blocks of text.

##Conclusion
So, that's the view from the top! By building on the sturdy foundations of our previous post, we've unveiled the magic of function calling with GPT-3.5 Turbo. This magic lets your chatbots and apps interact with users on a whole new level. 

The power here is really in turn unstructured chat text into a structured JSON or other type. This can then be used in API calls and returned back to structured/unstructured data in your Chat Bot. No more annoying forms or Bots that breakdown as soon as they get to the end of their pre-programmed decision tree!

So, what are you waiting for? Put your coding cap on and launch your projects into the AI stratosphere!