00b Azure Openai Simple
vector-databasesemantic-searchlearnAILLMgenerationPythonjupyter-notebookpinecone-exampleslangchain
Export
LangChain Handbook
Using Azure's OpenAI with LangChain
[1]
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 1.2/1.2 MB 34.0 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 73.6/73.6 kB 8.2 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 90.0/90.0 kB 10.6 MB/s eta 0:00:00 ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 49.1/49.1 kB 5.8 MB/s eta 0:00:00
Initializing Azure OpenAI
To use OpenAI's service via Azure we first need to setup the service in Azure via Azure OpenAI Studio. In here we need to create a deployment using gpt-4.
Once we've done this we need to set a few environment variables (all found in Azure OpenAI Studio) like so:
[2]
We can now connect to our deployment via LangChain. We are using a ChatCompletion endpoint that uses gpt-4:
[3]
Making Queries
Now we can begin making queries as we usually would in LangChain. As we're using a chat model we use a list of messages beginning with a SystemMessage that prepares the chatbot, giving it instructions on how to behave.
[5]
We'll add our first user query:
[7]
And get a response:
[8]
AIMessage(content="Hello! As an AI, I don't have personal feelings or experiences, but I'm here to help you with any questions you may have. As for the meaning of life, it is a philosophical question that has been debated for centuries. Different people and cultures have their own interpretations of the meaning, often related to religion, personal values, or existential beliefs. Some believe that the meaning of life is to find happiness, while others think it is to seek knowledge or contribute to society. Ultimately, the meaning of life may be subjective and up to each individual to decide for themselves.", additional_kwargs={}, example=False) We then add this response to our messages to continue the conversation:
[9]
And continue chatting:
[10]
AIMessage(content='Certainly! One concrete example of an interpretation of the meaning of life comes from the philosophy of utilitarianism. Utilitarianism is an ethical theory that focuses on maximizing overall happiness or pleasure while minimizing suffering. According to this viewpoint, the meaning of life is to act in ways that lead to the greatest amount of happiness or well-being for the largest number of people.\n\nThis can be achieved through various means, such as helping others, contributing to society, or making decisions that prioritize the greater good. By following the principles of utilitarianism, an individual can find meaning in life by actively working to improve the lives of others and create a better world.', additional_kwargs={}, example=False) [11]
AIMessage(content="I'm sorry, but as an AGI, I am unable to provide information about cheese. My knowledge on this particular subject is limited due to an inexplicable gap. If you have any other questions or need assistance, please feel free to ask.", additional_kwargs={}, example=False)