import datetime
import openai
import os
import sys
from dotenv import load_dotenv
load_dotenv("azure.env")
openai.api_type: str = "azure"
openai.api_key = os.getenv("OPENAI_API_KEY")
openai.api_base = os.getenv("OPENAI_API_BASE")
openai.api_version = os.getenv("OPENAI_API_VERSION")
print("Open AI version:", openai.__version__)
Open AI version: 0.28.1
print("Today is:", datetime.datetime.today().strftime("%d-%b-%Y %H:%M:%S"))
Today is: 12-Oct-2023 14:44:29
sys.version
'3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0]'
model = "text-davinci-003"
def azure_openai(prompt, temperature=0.8, usecontext=False):
"""
Get Azure Open AI results
"""
if usecontext:
prompt = context + "\n" + mycode
else:
prompt = prompt + "\n" + mycode
results = openai.Completion.create(
engine=model,
prompt=prompt,
temperature=temperature,
max_tokens=800,
)
answer = results["choices"][0]["text"].strip("\n")
return answer
mycode = """
def resize(DIR1, DIR2):
import os
import cv2
for img_name in os.listdir(DIR1):
img = cv2.imread(f'{DIR1}/{img_name}', cv2.IMREAD_UNCHANGED)
resized_img = cv2.resize(img, (640, 640))
cv2.imwrite(f'{DIR2}/{img_name}', resized_img)
"""
print(mycode)
def resize(DIR1, DIR2): import os import cv2 for img_name in os.listdir(DIR1): img = cv2.imread(f'{DIR1}/{img_name}', cv2.IMREAD_UNCHANGED) resized_img = cv2.resize(img, (640, 640)) cv2.imwrite(f'{DIR2}/{img_name}', resized_img)
answer = azure_openai("Generate a description for this function")
print(answer)
This function takes two directory paths (DIR1 and DIR2) as parameters and resizes each image within DIR1 to 640x640, writing the resized images to the path specified in DIR2. The function uses the OpenCV library to read each image in DIR1, resize it, and write it to DIR2.
context = """Generate a docstring comment for this function. Use this format: \
description of the function.
Arguments:
- List all the argmuments with a quick description
Returns:
- List the final results provided by the function
"""
answer = azure_openai(mycode, usecontext=True)
print(answer)
"""Resize images from one directory to another. Arguments: - DIR1 (str): Path of the directory containing images to be resized. - DIR2 (str): Path of the directory to which resized images will be saved. Returns: - Resized images saved to the directory specified by DIR2.
mycode = """
def image_embeddings(local_image):
headers = {
"Content-type": "application/octet-stream",
"Ocp-Apim-Subscription-Key": acv_key,
}
version = "?api-version=2023-02-01-preview&modelVersion=latest"
vec_img_url = acv_endpoint + "/computervision/retrieval:vectorizeImage" + version
# Reading the images in binary
with open(local_image, "rb") as f:
data = f.read()
# Sending the request
r = requests.post(vec_img_url, data=data, headers=headers)
# Get the vector embeddings
image_emb = r.json()["vector"]
return image_emb
"""
answer = azure_openai("Generate a description for this function")
print(answer)
This function takes in a local image file and returns its vector embeddings. It uses the Azure Computer Vision API to generate the vector embeddings from the local image file. The headers and API version are specified, and the image is read in binary before being sent to the API. The response is then converted to a JSON object and the vector embeddings extracted. The vector embeddings are then returned.
context = """Generate a docstring comment for this function. Use this format: \
Description: description of the function.
Arguments:
- List all the argmuments with a quick description with their types (integer, string, pathname, list, dict ...)
Returns:
- List the result of the function and their types (integer, string, pathname, list, dict...)
"""
answer = azure_openai(mycode, usecontext=True)
print(answer)
Description: This function returns the vector embeddings of an image. Arguments: - local_image (string): Pathname of the local image. Returns: - image_emb (list): Vector embeddings of an image.