Posts

GenerativeAI Python Libaries

Image
https://www.pexels.com/photo/two-women-looking-at-the-code-at-laptop-1181263/ After deploying GenerativeAI chat completion models from Azure GenerativeAI catalog, we need to know which Python SDK to used. OpenAI Models After deploying Azure OpenAI model, we use openai Python library. Either pip install openai or poetry add openai and we suggest that you use the asynchronous client. from openai import AsyncAzureOpenAI client = AsyncAzureOpenAI( api_key=<azure_openai_key>, api_version=<azure_openai_api_version>, azure_endpoint=<azure_openai_endpoint>, ) or (even better, use the DefaultAzureCredential ) from azure.identity import DefaultAzureCredential, get_bearer_token_provider from openai import AsyncAzureOpenAI azure_credential = DefaultAzureCredential() token_provider = get_bearer_token_provider( azure_credential, "https://cognitiveservices.azure.com/.default" ) client = AsyncAzureOpenAI( api_version=<azure_openai_api_version>,

hey GenAI, who buy what for Christmas

Image
Generated by DALL.E 3 What if we ask GenerativeAI to generate a set of user profiles. We generate these . That's  male/female resides in one of these states, California, Oregon and Washington annual household income of 70K, 100K, 150K. With these different profiles, we ask GenerativeAI if they would purchase a list of electronics for Christmas. Results are posted here . For example. all will likely to purchase a "Bose Bluetooth Headphones" gender state annual_income_k decision 0 female California 70 1.0 1 male California 70 1.0 2 female California 100 1.0 3 male California 100 1.0 4 female California 150 1.0 5 male California 150 1.0 6 female Oregon 70 1.0 7 male Oregon 70 1.0 8 female Oregon 100 1.0 9 male Oregon 100

Azure AI Content Safety Service

Image
  https://www.pexels.com/photo/grayscale-photo-of-barbed-wire-274886/ We looked at  Azure AI Content Safety Service  to see what the services that are offered. It is good that we have a set of services to detect harmful content that may be sent or generated by Generative AI.  Some of the services have Python client and some (new ones) do not. Hence, we create a Python library for all of them. (see github ). This library offers asynchronized function calls so we can make concurrent requests. We tested that HTTP overheads and it is extremely fast. There is also a terraform script in  github , so you can deploy the Azure Content Safety Service under a resource group easily for testing. All the instructions are in the README.md. We also added simple unit tests.

PII Anonymizer

Image
  I was playing around with  Presidio Anonymizer . Quote The Presidio anonymizer is a Python based module for anonymizing detected PII text entities with desired values. Presidio anonymizer supports both anonymization and deanonymization by applying different operators. Operators are built-in text manipulation classes which can be easily extended. I am particular interested in creating operators for it. As a result, I have created a python library. pii_anonymizer . For sample, I have a text Her name is Mary Ann. My name is James Bond. My phone number is 212-555-5555. My credit card is 5548364515335857. Again my name is James Bond and number is 212-555-5555. and I want to anonymize it to Her name is Monique Hamilton. My name is Jesse Townsend. My phone number is <phone_number_1>. My credit card is XXXXXXXXXXXXXXXX. Again my name is Jesse Townsend and number is <phone_number_1>. The names are replaced. When "James Bond" appeared twice, we replaced with the same name

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