Back to Index

Azure OpenAI in LangChain: Getting Started


Chapters

0:0 Azure OpenAI in LangChain
0:27 OpenAI in Azure Portal
3:54 Creating Deployments in Azure OpenAI Studio
5:17 Connecting LangChain with AzureChatOpenAI

Transcript

Today we're going to take a look at how we can use Lang chain with OpenAI in Azure. Now for those of you that don't know OpenAI can be accessed in one of two ways either through the OpenAI API directly or via the Azure version of OpenAI's API. Now I'm going to show you how to do all of this within Lang chain so we can have Lang chain, OpenAI and Azure.

So the first thing that we're going to do is head on over to portal.azure.com. You will naturally need an Azure account if you don't already but once you have set that up all you're going to do is head on over to your subscriptions, make sure you have a subscription, you may need to create one so you add a subscription there.

Mine is called development and testing so okay I know that is there. Then I'm going to head over to Azure OpenAI. If you can't find any of this stuff you can actually just go up here and type it in. You may also need to create that resource but once you have it, Azure OpenAI, you come over here and we already have a deployment down here but what I'm going to do is create a new one so we can go from start to finish.

So we're going to create a new deployment. I'm going to use that subscription that I showed you earlier. I'm going to use a resource group so you may also need to create a resource group. You can click create new if you don't have one already and I'm going to go to instant details.

The name will be call it YouTube demo. Pricing tier, a standard S0 and we can go on to next. Now here you just set up your security, network security items. For me I'm just going to go with all networks. Tags so this is naturally if you have many people and categories on your Azure instance I'm not going to do that.

Okay and once you've done that you have the terms you just go down to create. You will have to wait a moment while that is deploying and once it has deployed you'll look at this. Your deployment is complete. From here we want to go to I'm not sure the best way but I'm going to go to home.

I'm going to go back to Azure OpenAI and you'll be able to see your new resource or deployment in there. So I'm going to click on that deployment and there's now a few things that we need to grab from here. Okay so we have the endpoint. So let's copy this.

I'm going to go to this notebook so you'll be able to find your own version of this notebook linked near the top of the video right now or if not in the video description. So I'm going to come down to here and you see that we have this OpenAI API base right that is that's the endpoint.

So I'm going to replace that with what I just pasted from the endpoint. So YouTube demo openaiazure.com I also need the OpenAI API version. Right now the one of the most up-to-date versions or the version I'm using at least is this so there might be more recent versions but that's it works right now so I'm using that.

We want to set the OpenAI API type which is Azure and we also want to set the OpenAI API key. Right so where do we get that we want to come over to keys and endpoint and we just copy this okay and we just paste it into here. So once we have our items there what we now need to do is we want to go to Azure OpenAI Studio.

Open that and this is one of the main differences between the default version of OpenAI and the Azure version of OpenAI with Azure or actually with the default version we would just call the model endpoints we would say okay we want to use GPT-4 and we do that. You can't do that with the Azure version you actually need to create a deployment for every model that you'd like to use.

So you'd come over to create new deployment and you see I currently have no deployments okay so I'm going to create a new one I'm going to choose a model so we have a few here let's go with a GPT-4 model version we can let's use the default and deployment name so this is the deployment name that you will use within line chain to connect to this deployment right so it needs to be unique I don't currently have any other deployments so I can call it whatever I want essentially so I'm going to call it chat endpoint okay I'm going to create that okay so I now have chat endpoint so I'm going to come over to the code again I'm just going to run the pip install up at the top here and then I'm going to come down to here so I have deployment name I need to change this because I called it chat endpoint so we have the chat endpoint as our deployment name we have our model name which aligns to our deployment over here now if we try to run this straight away we'll get an error it won't work so after waiting a little while it will work as we can see here so for this it took sort of three to four minutes for that to actually deploy now what we've done here is we're using a chat model so when we're using chat model we use line chains system messages human message and AI message to create that sort of chat log so we initialize it with the system message then we basically system message just tells the model how to behave okay so I'm saying that it's basically it's an AGI but it cannot answer any questions about cheese because it just doesn't understand cheese as a concept okay so we can ask it like hey how are you doing today what's the meaning of life and it's going to respond you know it's really typical like as an AI I don't have emotions or personal experiences so on and so on but I'm here to help and then it gives you kind of generic meaning of life answer okay cool so after that we want to take this message the AI message and append it to our messages okay and then we can feed that back into the model so I'm gonna take all those interactions so far and I'm gonna ask another question can you give me one concrete example one of these interpretations so interpretations that's a very I'm not specifying what exactly I want to know about in that query in order for that to work it has to rely on the previous messages so it's going to need to take a look at this but because it's a chat model and because we appended the response to those messages it will understand that we're asking about the interpretation of something around here so we're saying what are the interpretations of the sort of meaning of life that people have been debating for centuries okay so in order to answer that it needs to have some of that conversational history which it does so we ask that question okay and we get this so one concrete example of an interpretation meaning life is found within the philosophy of existentialism and yeah it's like it's a pretty good kind of like meaningful answer I think so that's great it's working as an expert AGI now just to confirm that it is still considering its system message that we initially set it up with we can say okay something really simple what is the color of cheese so we can ask that and it says as an AGI system I am unable to provide any information related to cheese I apologize for the inconvenience and so on and so on right so it's considering all those previous messages so everything seems to be working and we have rather than use the typical OpenAI API endpoints we're doing this through Azure's like OpenAI offering so everything seems to be working that looks good and that is exactly what I wanted to show you in this video so that is it for now I hope this has been useful and interesting so thank you very much for watching and I will see you again in the next one.

Bye.