From the course: Azure AI Services Essential Training (2023)

Implement a learning chatbot

- [Instructor] At the time of recording this course, OpenAI in Azure is not available for individuals. It's only available for companies, so, well, people like me are out of luck, or are we? Well, actually, the APIs are the same, so I headed over to openai.com to play around with the APIs. I went ahead and created an account, and soon as I log in, I can see some examples that they've given me. Under examples over here, there are some really impressive things I can do with OpenAI APIs. Let's see, Python to natural language, so I can give it some Python code and it tells me in English what that code does. Wouldn't that be nice for documenting your code? This is really, really impressive. There are many other impressive things you can do over here. If you dive into API reference, well, this is a whole universe to discover. There is so much that you can do over here, but to do any of this, you are going to need an API key, for which you're going to need a paid account. So go under API keys and go ahead and create yourself an API key. I've already done that, and it's already part of my code, and before we look at code, let's look at what I'm about to build. So, as it turns out that I was bragging to some of my friends about what an expert in photography I am, but I'm not. I know nothing about photography, but I discovered this huge PDF. It's like 60 pages, and this PDF talks everything I need to know about photography, so I'm going to feed this to OpenAPI so I can ask it some simple questions. That is what we're about to build. So, let's dive into VSCode, and let's see what we're doing here. So inside of VSCode, let's scroll to the top. At the very top, I am importing a few things. This is a Python file. There is a requirement start.txt that I've taken some dependencies on, and I've already installed them. So the first thing I'm doing is that from a file called config.ini where I've stored my OpenAI API key, I'm reading that in. Okay, so that is step number one. Next, I need to create embeddings. Now, these embeddings are how I'm going to create a VectorDB from, which I'm going to feed into OpenAPI so I can ask questions. To do so, I'm going to follow a two-step process. First, I have the PDF stored locally, this PixelfordPhotoGraphy.pdf file, and I'm going to use NLTK, an unstructured file loader, for tokenization, as you can see over here. Next, I'm going to use Character Text Splitter to break that content into manageable chunks, which you can see on line number 26. On line number 27, I should have the data of that PDF available as text, which I then submit to OpenAI API to create a VectorDB. And because this step is going to cost me money, I'm going to save this VectorDB locally. Okay, so far so good. Now, once this is created, then I can load that VectorDB from the disk and I can start asking it questions, and these questions over here, as you can see, I'm running in a while true infinite loop until the user says exit. And here, I can simply ask questions in plain English, I can submit them to OpenAI, and then hopefully, OpenAI is going to give me the answers I'm looking for. Okay, great. Let's go ahead and run this. Press F5. Now, depending upon how fast or slow your computer is, this is going to take some time, so let's wait for this to finish. Okay, so now we can start asking questions, so let's ask a question in plain English. What is pinhole... Let's give ourselves some space. What is pinhole photography? Enter. It's submitting it to OpenAI, and it returns me the answer. Pinhole photography is a type of photography, et cetera, et cetera. Okay, that is pretty impressive. Let's ask it another question. What are conical portraits? Enter, and it gives me the answer. This is pretty impressive. Now, feel free to ask it more interesting questions or even supply it with a PDF that is of interest to you, some interesting, complicated topic that you're trying to learn. This is truly amazing. Imagine what you can do with the help documents for your product. This is very, very impressive, isn't it? And this is just scratching the surface of OpenAI. There is so much more you can do.

Contents