Posts

Happy New Year, 2024 from DALL-E

Image
generated by DALL-E Wishing everyone a happy new year. Year 2023 is great for me because I learned much about OpenAI. I am involved in a live deployment with it, and involved in a handful of interesting user scenarios around it. On top of this, a copilot hackathon. The image above is generated by DALL-E and following is the code. 1. Dependencies python = "^3.10" pydantic-settings = "^2.1.0" openai = "^1.3.8" 2. Environment Parameters OPENAI_AZURE_ENDPOINT="<your azure openai endpoint>" AZURE_OPENAI_API_KEY="<your azure openai secret>" OPENAI_API_VERSION="2023-12-01-preview" OPENAI_DALLE_MODEL="dalle" Note: OPENAI_API_VERSION has to be 2023-12-01-preview otherwise you will get an exception openai.NotFoundError: Error code: 404 - {'error': {'code': '404', 'message': 'Resource not found'}} dalle  is the name of my dall-e-3 deployment model. You need to change it to

Bing Search as tool for OpenAI

Image
  Image from https://www.pexels.com/@skitterphoto/ Many times, we need to search for information in the web when we are working with OpenAI. One of the reasons is that the dataset for training for OpenAI is never up to date. For instance, the OpenAI GPT-4 Turbo has knowledge of events up to April 2023. Here is the Python implementation with Pydantic model. Dependencies python = "^3.10" pydantic-settings = "^2.1.0" pydantic = "^2.5.2" azure-cognitiveservices-search-websearch = "^2.0.0" Environment These are the environment parameters needed. Have the following in a .env file. azure_bing_search_endpoint="https://api.bing.microsoft.com" azure_bing_search_key="<key>" We look under "Keys and Endpoint" for these values. Pydantic Setting Model from pydantic_settings import BaseSettings class BingSearchSettings(BaseSettings): azure_bing_search_endpoint: str azure_bing_search_key: str class Config:

OpenAI: Functions Feature in 2023-07-01-preview API version

Image
  Image from https://www.pexels.com/@pavel-danilyuk/ One of the cool features of OpenAI version 2023-07-01 is its custom Functions feature. This feature is close to me because I am working on a project where we require OpenAI to generate JSON responses. We always want the generated JSON to be consistent, otherwise, we need to do some post-processing tasks to get the JSON object in the correct schema. In the blog, we observe the completion with and without this new feature. Development Setup python -m venv .venv poetry init poetry shell poetry add openai poetry add python-dotenv poetry add pydantic I am using Python version 3.11 Create .env file OPENAI_API_TYPE="azure" OPENAI_API_BASE="<OpenAI endpoint>" OPENAI_API_KEY="<OpenAI secret>" OPENAI_API_VERSION="2023-07-01-preview" OPENAI_DEPLOYMENT_ID="<deployment identifier>" Without OpenAI functions feature We use two sample texts for experimentation. We want to get the na

Storing embedding in Azure Database for PostgreSQL

Image
  Image from https://www.pexels.com/@tara-winstead/ There are many options to store text embeddings. In this blog, we shall look at storing embedding in Azure Database for PostgreSQL. Installing Azure Database for PostgreSQL Install Azure Database for PostgreSQL Flexible Server.  Once it is deployed, enable two extensions. Install Python modules python -m venv .venv poetry init poetry shell poetry add openai[datalib] poetry add python-dotenv poetry add psycopg2-binary My Python version is 3.10. Create .env OPENAI_API_TYPE="azure" OPENAI_API_BASE="<HTTP endpoint to Azure OpenAI Service>" OPENAI_API_KEY="<secret to Azure OpenAI Service>" OPENAI_API_VERSION="2023-05-15" TEXT_MODEL="<deployment id of text embedding model>" PGHOST="<my-service>.postgres.database.azure.com" PGUSER="<user name>" PGPASSWORD="<secret>" PGPORT="5432" PGDATABASE="< database name

Pinecone Vector Store

Image
Image by https://www.pexels.com/@pixabay/  In this blog, we use Pinecone vector store to perform upsert and query operations. Like more of the implementation these days, it is effortless and the code is concise and simple. I am going to use the same data from my previous blog, Azure OpenAI: Similarity Search . 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.9" langchain = "^0.0.228" openai = "^0.27.8" python-dotenv = "^1.0.0" pinecone-client = "^2.2.2" Environment Parameters OPENAI_API_TYPE="azure" OPENAI_API_BASE="https://<my-azure-instance>.openai.azure.com/" OPENAI_API_KEY="<key>" OPENAI_API_VERSION="2023-05-15" TEXT_ENGINE="text-embedding-ada-002" EMBEDDING_DIMENSIONS="1536" PINECONE_VECTOR_STORE_KEY="<pinecone key>" PINECONE_VECTOR_STORE_ENV="<pine

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