Posts

Showing posts from June, 2023

Azure OpenAI | LangChain SDK with MSI

Image
Image from https://www.pexels.com/@tara-winstead/ In this blog, we show how to set up an Azure Function App to use Azure OpenAI Service with Managed Service Identity (MSI) with Python SDK  Pre-requisite Create an Azure Function App and turn on its Managed Service Identity Create an Azure OpenAI Service and grant the function app MSI to the Cognitive Services OpenAI User role. These are the function app configurations, below is an example "OPENAI_API_TYPE": "azure_ad", "OPENAI_API_BASE": "https://sample.openai.azure.com/", "OPENAI_API_VERSION": "2023-03-15-preview", "COMPLETIONS_MODEL": "gpt-35-turbo" Your local.setttings.json looks like this { "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "", "FUNCTIONS_WORKER_RUNTIME": "python", "OPENAI_API_TYPE": "azur

Azure OpenAI: Prompts to ChatGPT

Image
Image from https://www.pexels.com/@mizunokozuki/ Yet another blog on OpenAI (previous blogs, Similarity Search and  Summarize an article ). This is about prompt creation. Most of the time, I want to generate chat responses that are in a machine-readable format like JSON . We shall work on it in the blog. Dependencies python = "^3.10" openai = "^0.27.7" langchain = "^0.0.189" python-dotenv = "^1.0.0" Environment Parameters OPENAI_API_TYPE="azure" OPENAI_API_BASE="<azure openai endpoint>" OPENAI_API_KEY="<azure openai key>" OPENAI_API_VERSION="<azure openai API version>" I have the OPENAI_API_VERSION as 2023-03-15-preview Source Code After some effort in writing the prompts, I landed on this. from dotenv import load_dotenv from langchain import LLMChain from langchain.chat_models import AzureChatOpenAI from langchain.schema import AIMessage, HumanMessage, SystemMessage from langchain.pr

Azure OpenAI: Similarity Search

Image
Image from https://www.pexels.com/@cottonbro/ In this blog, we look at generating search scores with OpenAI Similarity Search. We randomly picked a few Yelp reviews online and added some random texts. Then we want to score them by "Restaurant Reviews" I have an Azure OpenAI service. Please get one if you wish to run the code. Or you can get any OpenAI service. Dependencies python = "^3.10" bs4 = "^0.0.1" openai = "^0.27.7" langchain = "^0.0.189" python-dotenv = "^1.0.0" pandas = "^2.0.2" tiktoken = "^0.4.0" matplotlib = "^3.7.1" plotly = "^5.14.1" scikit-learn = "^1.2.2" Environment Parameters OPENAI_API_TYPE="azure" OPENAI_API_BASE="<azure openai endpoint> " OPENAI_API_KEY="<azure openai key> " OPENAI_API_VERSION="<azure openai API version> " I have the OPENAI_API_VERSION as 2023-03-15-preview Source Code import pandas

Azure OpenAI: Summarize an article

Image
  Images from https://www.pexels.com/@shantanu-kumar-433289388/ In this blog, we use OpenAI to summarize a document . The code is very succinct and it is easy for us to get a summary from different personas. I have an Azure OpenAI service. Please get one if you wish to run the code. Or you can get any OpenAI service, and change  AzureChatOpenAI in the code to your provider. Dependencies python = "^3.10" bs4 = "^0.0.1" openai = "^0.27.7" langchain = "^0.0.189" python-dotenv = "^1.0.0" Environment Parameters OPENAI_API_TYPE="azure" OPENAI_API_BASE="<azure openai endpoint>" OPENAI_API_KEY="<azure openai key>" OPENAI_API_VERSION="<azure openai API version>" I have the OPENAI_API_VERSION as  2023-03-15-preview Source code We use BeautifulSoup to extract text content from the URL. And, we have two prompt templates. from urllib.request import urlopen from bs4 import BeautifulSoup from