Learn to use AI to boost the efficiency of your business
Since the advent of ChatGPT, it has brought tremendous shock to human society. Especially for us developers, our lives have been reshaped dramatically because of it. ChatGPT can answer all kinds of technical and non-technical questions correctly, accurately, and efficiently.
However, ChatGPT can do more than just answer our questions. We can also make chats programmatically by implementing it in our application and use it to answer customer questions or boost the efficiency of our business in general.
A typical use case is category prediction in the product search service of online shops. We used to build machine learning or deep learning models based on the product category data we could get. However, these models are limited by the training data we can have, no matter how sophisticatedly the models are trained. In comparison, with ChatGPT, the models behind the scenes are built on a lot more data than we can ever have access to and are also trained with more advanced algorithms. Therefore, the predictions by ChatGPT are normally more accurate, even for products we have never indexed before.
In this post, we will introduce how to make chats programmatically using the OpenAI API in Python. Fundamental concepts will be introduced in simple languages so you can get started with it quickly.
Let’s create a virtual environment so we can try out the latest versions of Python and the libraries:
conda create -n openai python=3.12conda activate openai
pip install openai httpx
openai — A library provided by OpenAI which makes working with the OpenAI API in Python simple and efficient.httpx — A modern and fully featured HTTP client library that supports both HTTP/1.1 and HTTP/2 and provides both sync and async APIs.
Authentication
After installing the libraries, we need to get the API key to call the OpenAI APIs. Note that OpenAI API and ChatGPT are managed separately. Therefore, even if you are a paid ChatGPT user, you…