How to Create an AI Chatbot that uses OpenAI Functions

Checkout the open-source codebase here: https://github.com/peterw/NotionWizard

Hey there, code enthusiasts! Today, we’re diving into an intriguing code base named Notion Wizard. This amazing tool harnesses the power of OpenAI functions. Excited? Let’s get started.

Notion Wizard is a streamlined app with two main paths. It either creates a Notion page based on user requests or responds with a standard GPT response. But what makes it special?

The answer lies in OpenAI functions. These functions empower GPT to decide the path: creating a Notion page or a typical GPT response. Intrigued to see it in action? Let’s walk through the code base.

You can find Notion Wizard on my Github. There are two main files: main.py and API calls. Let’s start with main.py.

main.py is a standard Streamlit chat app, allowing user inputs and showcasing chat responses. The magic happens in the function section. There are two main functions here.

The first function simply gets a normal GPT response. It replies with a ‘reply’ key. The second function creates a Notion page, returning a title and content based on user input.

But here’s where OpenAI functions shine. We can describe what the API should do based on user input. One function responds with the GPT model, the other creates a Notion page. This flexibility is powerful when you have different APIs.

Imagine if we had another function, say save_file_S3. Let’s say a user asks to create a Notion page about llamas. OpenAI will return the necessary response. But if the user wants to save a file to S3, OpenAI can recognize that and return the corresponding function.

This is how OpenAI controls and determines which API we should call. We include the function list in the payload passed into OpenAI. The other interesting part is determining whether to return the response to the user or to create a Notion page.

We check the function argument. If it has a ‘reply’, we return the response. If it has ‘content’, we create a Notion page. This is how we handle what OpenAI functions give us.

In the API calls file, you’ll find the code calling the Notion API to create the page.

So, what’s the takeaway? OpenAI functions can handle the logic of determining which API to call. If you have multiple APIs, OpenAI can decide which one to call.

Moreover, OpenAI can provide a response in a specific format. We needed the title and content. OpenAI gave us exactly that, ready for our Notion API call.

The power of OpenAI functions lies in its flexibility and ability to clean up logic. It can take the decision-making load off your shoulders.

Hopefully, this gives you a fresh insight into OpenAI functions. Happy coding!

Checkout the open-source codebase here: https://github.com/peterw/NotionWizard