{ "cells": [ { "cell_type": "markdown", "id": "20e46c2f", "metadata": {}, "source": [ "# Python docstring generation with Azure Open AI" ] }, { "cell_type": "code", "execution_count": 1, "id": "a9ed10c4", "metadata": {}, "outputs": [], "source": [ "import datetime\n", "import openai\n", "import os\n", "import sys\n", "\n", "from dotenv import load_dotenv" ] }, { "cell_type": "code", "execution_count": 2, "id": "bacb98f7", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Open AI version: 0.28.1\n" ] } ], "source": [ "load_dotenv(\"azure.env\")\n", "\n", "openai.api_type: str = \"azure\"\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n", "openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n", "\n", "print(\"Open AI version:\", openai.__version__)" ] }, { "cell_type": "code", "execution_count": 3, "id": "6b463bec", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Today is: 12-Oct-2023 14:44:29\n" ] } ], "source": [ "print(\"Today is:\", datetime.datetime.today().strftime(\"%d-%b-%Y %H:%M:%S\"))" ] }, { "cell_type": "code", "execution_count": 4, "id": "3dd263bd", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'3.10.10 (main, Mar 21 2023, 18:45:11) [GCC 11.2.0]'" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sys.version" ] }, { "cell_type": "markdown", "id": "bf083314", "metadata": {}, "source": [ "## Function" ] }, { "cell_type": "code", "execution_count": 5, "id": "1a4b5443", "metadata": {}, "outputs": [], "source": [ "model = \"text-davinci-003\"" ] }, { "cell_type": "code", "execution_count": 6, "id": "c2dd2175", "metadata": {}, "outputs": [], "source": [ "def azure_openai(prompt, temperature=0.8, usecontext=False):\n", " \"\"\"\n", " Get Azure Open AI results\n", " \"\"\"\n", " if usecontext:\n", " prompt = context + \"\\n\" + mycode\n", " else:\n", " prompt = prompt + \"\\n\" + mycode\n", "\n", " results = openai.Completion.create(\n", " engine=model,\n", " prompt=prompt,\n", " temperature=temperature,\n", " max_tokens=800,\n", " )\n", "\n", " answer = results[\"choices\"][0][\"text\"].strip(\"\\n\")\n", "\n", " return answer" ] }, { "cell_type": "markdown", "id": "6ba8838d", "metadata": {}, "source": [ "## Examples" ] }, { "cell_type": "markdown", "id": "4ebc2fa7", "metadata": {}, "source": [ "### Test 1" ] }, { "cell_type": "code", "execution_count": 7, "id": "ec190f96", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "def resize(DIR1, DIR2):\n", " import os\n", " import cv2\n", "\n", " for img_name in os.listdir(DIR1):\n", " img = cv2.imread(f'{DIR1}/{img_name}', cv2.IMREAD_UNCHANGED)\n", " resized_img = cv2.resize(img, (640, 640))\n", " cv2.imwrite(f'{DIR2}/{img_name}', resized_img)\n", "\n" ] } ], "source": [ "mycode = \"\"\"\n", "def resize(DIR1, DIR2):\n", " import os\n", " import cv2\n", "\n", " for img_name in os.listdir(DIR1):\n", " img = cv2.imread(f'{DIR1}/{img_name}', cv2.IMREAD_UNCHANGED)\n", " resized_img = cv2.resize(img, (640, 640))\n", " cv2.imwrite(f'{DIR2}/{img_name}', resized_img)\n", "\"\"\"\n", "\n", "print(mycode)" ] }, { "cell_type": "code", "execution_count": 8, "id": "0e7daa11", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " \n", "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.\n" ] } ], "source": [ "answer = azure_openai(\"Generate a description for this function\")\n", "\n", "print(answer)" ] }, { "cell_type": "code", "execution_count": 9, "id": "3bbd08a9", "metadata": {}, "outputs": [], "source": [ "context = \"\"\"Generate a docstring comment for this function. Use this format: \\\n", "description of the function.\n", "\n", "Arguments:\n", "- List all the argmuments with a quick description\n", "\n", "Returns:\n", "- List the final results provided by the function\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 10, "id": "da3f3752", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\"\"\"Resize images from one directory to another.\n", "\n", "Arguments:\n", "- DIR1 (str): Path of the directory containing images to be resized.\n", "- DIR2 (str): Path of the directory to which resized images will be saved.\n", "\n", "Returns:\n", "- Resized images saved to the directory specified by DIR2.\n" ] } ], "source": [ "answer = azure_openai(mycode, usecontext=True)\n", "\n", "print(answer)" ] }, { "cell_type": "markdown", "id": "f9151d85", "metadata": {}, "source": [ "### Test 2" ] }, { "cell_type": "code", "execution_count": 11, "id": "902c82d3", "metadata": {}, "outputs": [], "source": [ "mycode = \"\"\"\n", "\n", "def image_embeddings(local_image):\n", " headers = {\n", " \"Content-type\": \"application/octet-stream\",\n", " \"Ocp-Apim-Subscription-Key\": acv_key,\n", " }\n", " version = \"?api-version=2023-02-01-preview&modelVersion=latest\"\n", " vec_img_url = acv_endpoint + \"/computervision/retrieval:vectorizeImage\" + version\n", "\n", " # Reading the images in binary\n", " with open(local_image, \"rb\") as f:\n", " data = f.read()\n", " # Sending the request\n", " r = requests.post(vec_img_url, data=data, headers=headers)\n", " # Get the vector embeddings\n", " image_emb = r.json()[\"vector\"]\n", "\n", " return image_emb\n", "\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 12, "id": "dd824f50", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "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.\n" ] } ], "source": [ "answer = azure_openai(\"Generate a description for this function\")\n", "print(answer)" ] }, { "cell_type": "code", "execution_count": 13, "id": "6ba2c3a9", "metadata": {}, "outputs": [], "source": [ "context = \"\"\"Generate a docstring comment for this function. Use this format: \\\n", "Description: description of the function.\n", "\n", "Arguments:\n", "- List all the argmuments with a quick description with their types (integer, string, pathname, list, dict ...)\n", "\n", "Returns:\n", "- List the result of the function and their types (integer, string, pathname, list, dict...) \n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 14, "id": "9b495ae8", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Description: This function returns the vector embeddings of an image.\n", "\n", "Arguments:\n", "- local_image (string): Pathname of the local image.\n", "\n", "Returns:\n", "- image_emb (list): Vector embeddings of an image.\n" ] } ], "source": [ "answer = azure_openai(mycode, usecontext=True)\n", "\n", "print(answer)" ] }, { "cell_type": "code", "execution_count": null, "id": "6b1ec71b", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3.10 - SDK v2", "language": "python", "name": "python310-sdkv2" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.10" } }, "nbformat": 4, "nbformat_minor": 5 }