Hey everyone, today we’re going to be exploring the exciting new feature in ChatGPT called functions. OpenAI has introduced this feature to allow for a more reliable and powerful integration of ChatGPT into your own code.
Up until now, the ChatGPT API was primarily focused on text-based conversations. You would send some text to ChatGPT and receive a response. The text you sent is often referred to as the prompt, and the response you received could be anything. Prompt engineering was used to fine-tune the text you sent in order to control the response you received.
While this was great for chatbots and casual conversations, it wasn’t ideal for integrating the response messages into your own code. For example, if you wanted to extract specific JSON data from the response, you would need to write custom code or perform prompt engineering to get the desired output.
This is where ChatGPT functions come in. Functions in ChatGPT allow you to describe specific functions with an API call and instruct ChatGPT on how to call those functions. You can define functions that don’t actually exist in your code but provide instructions on how ChatGPT can call them. The response from ChatGPT will indicate how it will call your function and with what parameters.
To demonstrate this, let’s walk through an example. We’ll use TypeScript and the OpenAI API to create a function call in ChatGPT. First, we’ll install the OpenAI package and set up our API key. Then, we’ll create a main function that will asynchronously call the ChatGPT completion API. In the completion request, we’ll define our function and its description, as well as any parameters it requires.
Once we receive the response from ChatGPT, we’ll check if it’s a function call and extract the argument string. We’ll parse the argument string as JSON and use the values to call our function. In this example, we’ll create a function called ‘restaurant lookup’ that takes a city and cuisine as parameters. The function will return a string describing a restaurant serving the specified cuisine in the chosen city.
This integration of functions in ChatGPT opens up a world of possibilities for software developers. Instead of simply generating text, we can now engage in a two-way dialogue with AI models and dynamically influence our programs. Imagine receiving structured data from a language model and using that data to execute specific functions in your code. It’s like giving your AI applications a brand new set of tools.
I encourage you to explore this powerful new feature and experiment with integrating ChatGPT into your own programs. The world of AI is constantly evolving, and there’s so much to learn and discover. Stay curious and keep pushing the boundaries of what’s possible!
If you found this walkthrough helpful, please subscribe to my channel for more exciting AI content. Thank you for watching, and I’ll see you in the next video!