|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "759f9ec0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "<h1 align =\"center\"> REST API Basic Samples</h1>\n", |
| 9 | + "<hr>\n", |
| 10 | + " \n", |
| 11 | + "# Chat Completions" |
| 12 | + ] |
| 13 | + }, |
| 14 | + { |
| 15 | + "cell_type": "code", |
| 16 | + "execution_count": 10, |
| 17 | + "id": "f4b3d21a", |
| 18 | + "metadata": {}, |
| 19 | + "outputs": [], |
| 20 | + "source": [ |
| 21 | + "import json\n", |
| 22 | + "import os\n", |
| 23 | + "import requests\n", |
| 24 | + "import base64" |
| 25 | + ] |
| 26 | + }, |
| 27 | + { |
| 28 | + "cell_type": "markdown", |
| 29 | + "id": "5b2d4a0f", |
| 30 | + "metadata": {}, |
| 31 | + "source": [ |
| 32 | + "### Setup Parameters\n", |
| 33 | + "\n", |
| 34 | + "\n", |
| 35 | + "Here we will load the configurations from _config.json_ file to setup deployment_name, openai_api_base, openai_api_key and openai_api_version." |
| 36 | + ] |
| 37 | + }, |
| 38 | + { |
| 39 | + "cell_type": "code", |
| 40 | + "execution_count": 11, |
| 41 | + "id": "fd85fb30", |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "# Load config values\n", |
| 46 | + "with open(r'config.json') as config_file:\n", |
| 47 | + " config_details = json.load(config_file)\n", |
| 48 | + " \n", |
| 49 | + "# Setting up the deployment name\n", |
| 50 | + "deployment_name = config_details['GPT-4V_MODEL']\n", |
| 51 | + "\n", |
| 52 | + "# The base URL for your Azure OpenAI resource. e.g. \"https://<your resource name>.openai.azure.com\"\n", |
| 53 | + "openai_api_base = config_details['OPENAI_API_BASE']\n", |
| 54 | + "\n", |
| 55 | + "# The API key for your Azure OpenAI resource.\n", |
| 56 | + "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", |
| 57 | + "\n", |
| 58 | + "# Currently OPENAI API have the following versions available: 2022-12-01. All versions follow the YYYY-MM-DD date structure.\n", |
| 59 | + "openai_api_version = config_details['OPENAI_API_VERSION']" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 12, |
| 65 | + "id": "aef62557", |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [ |
| 68 | + { |
| 69 | + "name": "stdout", |
| 70 | + "output_type": "stream", |
| 71 | + "text": [ |
| 72 | + "### Tropical Paradise Escape: Your Ideal Vacation Awaits!\n", |
| 73 | + "\n", |
| 74 | + "Step into a world where luxury and nature intertwine seamlessly, and every corner promises relaxation and grandeur. This exquisite vacation home is nestled amidst lush tropical greenery, offering an oasis of calm and elegance. Designed to perfection, the architecture blends modern amenities with elements that echo the natural surroundings, ensuring a stay that is both comfortable and visually stunning.\n", |
| 75 | + "\n", |
| 76 | + "Bask in the glory of the sun on the plush loungers by the pristine pool, its crystal-clear waters inviting you to take a refreshing dip and wash your worries away. The manicured landscape, adorned with vibrant flowers and towering palms, adds an element of enchantment to your private sanctuary. Every inch of this property is designed to offer a visual and sensory delight, ensuring that your stay is nothing short of magical.\n", |
| 77 | + "\n", |
| 78 | + "The spacious balconies open a world of tranquility, where the gentle rustling of the leaves and the harmonious songs of the tropical birds become a sweet serenade. As the sun kisses the earth goodbye, the warm hues of the sunset paint a masterpiece across the sky, offering a spectacle of colors that promise to take your breath away. The evenings are tranquil, and the starlit sky brings a sense of calm, making every moment spent here a cherished memory.\n", |
| 79 | + "\n", |
| 80 | + "Inside, elegance and comfort take center stage, with spacious interiors that are as inviting as they are lavish. Every piece of furniture, every amenity is handpicked to ensure your utmost comfort. Large windows allow natural light to dance gracefully across the rooms, illuminating the space with an ethereal glow.\n", |
| 81 | + "\n", |
| 82 | + "Your tropical paradise is not just a stay but an experience that promises to be as enchanting as the tales of old. It is a haven where luxury, comfort, and nature dance in harmony, offering a symphony of experiences that promise to caress your soul, invigorate your senses, and paint your memories with strokes of gold. Your escape to paradise awaits - a world where every sunrise is a sonnet and every sunset, a poem; where every moment is a treasure, and every sound, a melody.\n", |
| 83 | + "\n", |
| 84 | + "Indulge in the magic, embrace the elegance, and let the symphony of luxury and nature whisk you away to a world where dreams aren’t just lived but are also felt with every breath. Your paradise awaits, promising not just a stay, but a story that will be told for generations - a tale of magic, luxury, and the harmonious dance of nature and elegance. Welcome to a world where every moment is a masterpiece, every view a painting, and every stay, a journey into the heart of paradise.\n" |
| 85 | + ] |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "\n", |
| 90 | + "#Image Description Assistant\n", |
| 91 | + "image_file_path = \"../../common/images/ImageDescriptionAssistant.jpg\" # Update with your image path\n", |
| 92 | + "sys_message = \"You are an AI assistant that helps people craft a clear and detailed sentence that describes the content depicted in an image.\"\n", |
| 93 | + "user_prompt = \"Describe image\"\n", |
| 94 | + "\n", |
| 95 | + "#Image Tagging Assistant\n", |
| 96 | + "\"\"\"\n", |
| 97 | + "image_file_path = \"../../common/images/ImageTaggingAssistant.jpg\" \n", |
| 98 | + "sys_message = \"Generate a list of descriptive tags for the following image. Analyze the image carefully and produce tags that accurately represent the image. Ensure the tags are relevant.\"\n", |
| 99 | + "user_prompt = \"Provide tags for this image.\"\n", |
| 100 | + "\"\"\"\n", |
| 101 | + "\n", |
| 102 | + "#Listing Assistant\n", |
| 103 | + "\"\"\"\n", |
| 104 | + "image_file_path = \"../../common/images/ListingAssistant.jpg\" \n", |
| 105 | + "sys_message = \"You are an AI assistant which generates listings for vacation rentals. Please generate exciting and inviting content for this image but don't talk about content that you cannot see. Follow the format of an attention-grabbing title and provide a description that is 6 sentences long.\"\n", |
| 106 | + "user_prompt = \"Generate content.\"\n", |
| 107 | + "\"\"\"\n", |
| 108 | + "\n", |
| 109 | + "# Encode the image in base64\n", |
| 110 | + "with open(image_file_path, 'rb') as image_file:\n", |
| 111 | + " encoded_image = base64.b64encode(image_file.read()).decode('ascii')\n", |
| 112 | + "\n", |
| 113 | + "# Construct the API request URL\n", |
| 114 | + "api_url = f\"{openai_api_base}/openai/deployments/{deployment_name}/chat/completions?api-version={openai_api_version}\"\n", |
| 115 | + "\n", |
| 116 | + "# Including the api-key in HTTP headers\n", |
| 117 | + "headers = {\n", |
| 118 | + " \"Content-Type\": \"application/json\",\n", |
| 119 | + " \"api-key\": openai_api_key,\n", |
| 120 | + "}\n", |
| 121 | + "\n", |
| 122 | + "# Payload for the request\n", |
| 123 | + "payload = {\n", |
| 124 | + " \"messages\": [\n", |
| 125 | + " {\n", |
| 126 | + " \"role\": \"system\",\n", |
| 127 | + " \"content\": [\n", |
| 128 | + " sys_message\n", |
| 129 | + " ]\n", |
| 130 | + " },\n", |
| 131 | + " {\n", |
| 132 | + " \"role\": \"user\",\n", |
| 133 | + " \"content\": [\n", |
| 134 | + " user_prompt, # Pass the prompt\n", |
| 135 | + " {\n", |
| 136 | + " \"image\": encoded_image #Pass the encoded image\n", |
| 137 | + " }\n", |
| 138 | + " ]\n", |
| 139 | + " }\n", |
| 140 | + " ],\n", |
| 141 | + " \"temperature\": 0.7,\n", |
| 142 | + " \"top_p\": 0.95,\n", |
| 143 | + " \"max_tokens\": 800\n", |
| 144 | + "}\n", |
| 145 | + "\n", |
| 146 | + "# Send the request and handle the response\n", |
| 147 | + "try:\n", |
| 148 | + " response = requests.post(api_url, headers=headers, json=payload)\n", |
| 149 | + " response.raise_for_status() # Raise an error for bad HTTP status codes\n", |
| 150 | + " response_content = response.json()\n", |
| 151 | + " print(response_content['choices'][0]['message']['content']) # Print the content of the response\n", |
| 152 | + "except requests.RequestException as e:\n", |
| 153 | + " raise SystemExit(f\"Failed to make the request. Error: {e}\")" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": null, |
| 159 | + "id": "b6165c63", |
| 160 | + "metadata": {}, |
| 161 | + "outputs": [], |
| 162 | + "source": [] |
| 163 | + } |
| 164 | + ], |
| 165 | + "metadata": { |
| 166 | + "kernelspec": { |
| 167 | + "display_name": "Python 3 (ipykernel)", |
| 168 | + "language": "python", |
| 169 | + "name": "python3" |
| 170 | + }, |
| 171 | + "language_info": { |
| 172 | + "codemirror_mode": { |
| 173 | + "name": "ipython", |
| 174 | + "version": 3 |
| 175 | + }, |
| 176 | + "file_extension": ".py", |
| 177 | + "mimetype": "text/x-python", |
| 178 | + "name": "python", |
| 179 | + "nbconvert_exporter": "python", |
| 180 | + "pygments_lexer": "ipython3", |
| 181 | + "version": "3.9.18" |
| 182 | + } |
| 183 | + }, |
| 184 | + "nbformat": 4, |
| 185 | + "nbformat_minor": 5 |
| 186 | +} |
0 commit comments