quick_start/legacy/05_OpenAI_parameters.ipynb (198 lines of code) (raw):

{ "cells": [ { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import os\n", "import openai\n", "from dotenv import load_dotenv\n", "load_dotenv()\n", "\n", "openai.api_type = \"azure\"\n", "openai.api_version = os.getenv(\"OPENAI_API_VERSION\")\n", "openai.api_key = os.getenv(\"OPENAI_API_KEY\")\n", "openai.api_base = os.getenv(\"OPENAI_API_BASE\")\n", "\n", "CHAT_COMPLETIONS_MODEL = os.getenv('CHAT_COMPLETION_NAME')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# temperature\n", "\n", "Defaults to 1\n", "\n", "What sampling temperature to use. Higher values means the model will take more risks. Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.\n", "\n", "We generally recommend altering this or top_p but not both." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "def call_openai(num_times, prompt, temperature):\n", " for i in range(num_times):\n", " response = openai.ChatCompletion.create(\n", " engine=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": prompt}],\n", " max_tokens=60,\n", " temperature = temperature\n", " )\n", " print(response['choices'][0]['message']['content'])" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The best pet is a subjective choice and depends on individual preferences, lifestyle, and circumstances. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and small mammals. It's important to\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and circumstances. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and small mammals. It's important to\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and living situation. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and guinea pigs. It's\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and circumstances. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and small mammals. It's important to\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and living situation. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and reptiles. It's important\n", "The best pet is a matter of personal preference and lifestyle. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, and small mammals like rabbits or guinea pigs. It's important to consider your\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and living situation. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and small mammals. It's important\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and living situation. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and reptiles. It's important\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and living situation. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and guinea pigs. It's\n", "The best pet is a subjective choice and depends on individual preferences, lifestyle, and circumstances. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence and low maintenance. Other popular pets include birds, fish, rabbits, and small mammals. It's important to\n" ] } ], "source": [ "call_openai(10, 'The best pet is a ', temperature = 0)" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "It depends on your lifestyle and preferences. Some people may find that a dog is the best pet because of their loyalty and companionship, while others may prefer having a cat for their independence and low maintenance. Others may enjoy the companionship of smaller animals like rabbits, guinea pigs, or birds.\n", "The best pet is subjective and depends on an individual's lifestyle, preferences, and needs. Some people may find dogs to be the best pets because of their loyalty and companionship, while others may prefer cats for their independent nature. Other options like birds, fish, or small mammals may also be considered\n", "pet that suits your lifestyle and needs. It's important to consider factors such as space, time, and commitment before choosing a pet. Each animal has its own unique qualities, so the best pet for one person may not be the best for another. Consider researching different types of pets to find one that\n", "The best pet is a subjective choice and depends on one's lifestyle and preferences. Some people may prefer dogs for their loyalty and companionship, while others may prefer cats for their independence. Other options may include birds, fish, or small mammals. It is important to consider the needs of the pet,\n", "The best pet is ultimately a personal choice and depends on your lifestyle and preferences. Different animals have different needs, so it's important to consider factors like space, time, and commitment before getting a pet. Some people may prefer the companionship of a dog or cat, while others may enjoy the low\n", "It really depends on individual preferences and lifestyle. Some people may enjoy dogs for their companionship and loyalty, while others may prefer the low maintenance of a cat. Other popular pets include fish, birds, and small mammals. Ultimately, the best pet for you will be one that fits into your home and\n", "That's a matter of personal preference, but many people enjoy having a dog or cat as a companion. Dogs are known for their loyalty and companionship, while cats are often appreciated for their independence and low maintenance. It ultimately depends on your lifestyle and the type of pet you're looking for.\n", "That's a matter of personal preference, but many people find dogs to be wonderful pets because of their loyalty, companionship, and ability to provide emotional support. Cats, birds, and other animals are also beloved by many. It ultimately depends on what kind of pet best suits your lifestyle and preferences.\n", "It depends on your lifestyle and preferences. Some people might prefer a dog for companionship and protection, while others might appreciate the low-maintenance nature of a cat. Some may enjoy the intelligence and playfulness of a parrot, while others may prefer the companionship of a fish or the independence of\n", "That's subjective, as everyone has their own opinion on the best pet. Some people might think dogs make the best pets because they are loyal and friendly, while others might prefer cats for their independence and low maintenance. Some people might even consider exotic pets like birds or reptiles to be the best option\n" ] } ], "source": [ "call_openai(10, 'The best pet is a ', temperature = 1)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# n\n", "\n", "Defaults to 1\n", "\n", "How many completions to generate for each prompt.\n", "\n", "Note: Because this parameter generates many completions, it can quickly consume your token quota. Use carefully and ensure that you have reasonable settings for max_tokens and stop." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The best pet is a matter of personal preference and depends on your lifestyle and circumstances. Some people may find dogs to be the best pet for their active lifestyle and need for companionship, while others may prefer the low-maintenance nature of cats or the unique companionship of smaller animals like rabbits or ham\n", "The best pet is subjective and can depend on a person's lifestyle, preferences, and living situation. Some people may prefer the companionship of a dog, while others may enjoy the low-maintenance nature of a cat or the colorful presence of a fish. It's important to consider factors such as the\n" ] } ], "source": [ "response = openai.ChatCompletion.create(\n", " engine=CHAT_COMPLETIONS_MODEL,\n", " messages = [{\"role\":\"system\", \"content\":\"You are a helpful assistant.\"},\n", " {\"role\":\"user\",\"content\": \"The best pet is a \"}],\n", " max_tokens=60,\n", " n=2\n", " )\n", "\n", "for c in response['choices']:\n", " print(c['message']['content'])" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# presence_penalty\n", "\n", "Defaults to 0\n", "\n", "Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics.\n", "\n", "# frequency_penalty\n", "\n", "Defaults to 0\n", "\n", "Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [] } ], "metadata": { "kernelspec": { "display_name": "azureml_py310_sdkv2", "language": "python", "name": "python3" }, "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.11.7" }, "orig_nbformat": 4, "vscode": { "interpreter": { "hash": "2139c70ac98f3202d028164a545621647e07f47fd6f5d8ac55cf952bf7c15ed1" } } }, "nbformat": 4, "nbformat_minor": 2 }