diff --git a/Basic_Samples/LangChain/README.md b/Basic_Samples/LangChain/README.md new file mode 100644 index 00000000..ef121f83 --- /dev/null +++ b/Basic_Samples/LangChain/README.md @@ -0,0 +1,54 @@ +# Introduction +This repository shows how to setup and use Azure OpenAI models' API with LangChain. + +## Installation +Install all Python modules and packages listed in the requirements.txt file using the below command. + +```python +pip install -r requirements.txt +``` + +### Microsoft Azure Endpoints +In order to use the Open AI library or REST API with Microsoft Azure endpoints, you need to set DEPLOYMENT_NAME, OPENAI_API_BASE & OPENAI_API_VERSION in _config.json_ file. + +```js +{ + "DEPLOYMENT_NAME":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} +``` + +### For getting started: +- Add "OPENAI_API_KEY" as variable name and \ as variable value in the environment variables. +
+One can get the OPENAI_API_KEY value from the Azure Portal. Go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for one of the "Keys" values. +
+ + WINDOWS Users: + setx OPENAI_API_KEY "REPLACE_WITH_YOUR_KEY_VALUE_HERE" + + MACOS/LINUX Users: + export OPENAI_API_KEY="REPLACE_WITH_YOUR_KEY_VALUE_HERE" + +- To find your "DEPLOYMENT_NAME" go to the deployments page of the Azure AI Studio. Create a deployment if one does not already exist. +One can start with using your model name as "gpt-35-turbo-0613" or "gpt-4." + +- To find your "OPENAI_API_BASE" go to https://portal.azure.com, find your resource and then under "Resource Management" -> "Keys and Endpoints" look for the "Endpoint" value. + +- Check out versions [here](https://learn.microsoft.com/en-us/azure/cognitive-services/openai/reference). + + +## Requirements +Python 3.8+
+Jupyter Notebook 6.5.2 + +
+ +## Trademarks + +This project may contain trademarks or logos for projects, products, or services. Authorized use of Microsoft +trademarks or logos is subject to and must follow +[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general). +Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship. +Any use of third-party trademarks or logos are subject to those third-party's policies. \ No newline at end of file diff --git a/Basic_Samples/LangChain/config.json b/Basic_Samples/LangChain/config.json new file mode 100644 index 00000000..b7a3a182 --- /dev/null +++ b/Basic_Samples/LangChain/config.json @@ -0,0 +1,5 @@ +{ + "DEPLOYMENT_NAME":"", + "OPENAI_API_BASE":"https://.openai.azure.com", + "OPENAI_API_VERSION":"" +} \ No newline at end of file diff --git a/Basic_Samples/LangChain/requirements.txt b/Basic_Samples/LangChain/requirements.txt new file mode 100644 index 00000000..521ef18a --- /dev/null +++ b/Basic_Samples/LangChain/requirements.txt @@ -0,0 +1,9 @@ +# Packages needed to call AOAI API with the OpenAI Python API +openai + +# Packages needed to run the notebook samples +jupyter + +# Other packages needed to run the notebook samples +langchain +json \ No newline at end of file diff --git a/Basic_Samples/LangChain/working_with_langchain.ipynb b/Basic_Samples/LangChain/working_with_langchain.ipynb new file mode 100644 index 00000000..606dedfc --- /dev/null +++ b/Basic_Samples/LangChain/working_with_langchain.ipynb @@ -0,0 +1,361 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "6d251ced", + "metadata": {}, + "source": [ + "# How to use LangChain and Azure OpenAI with Python\n", + "\n", + "\n", + "Langchain is an open source framework for developing applications using large language models (LLM).
\n", + "\n", + "This guide will demonstrate how to setup and use Azure OpenAI models' API with LangChain.\n", + " " + ] + }, + { + "cell_type": "markdown", + "id": "9d0ee335", + "metadata": {}, + "source": [ + "## Set Up\n", + "The following libraries must be installed to use LangChain with Azure OpenAI.
" + ] + }, + { + "cell_type": "code", + "execution_count": 36, + "id": "35289cea", + "metadata": {}, + "outputs": [], + "source": [ + "#%pip install --upgrade openai\n", + "#%pip install langchain" + ] + }, + { + "cell_type": "markdown", + "id": "ba880453", + "metadata": {}, + "source": [ + "## API Configuation and Deployed Model Setup\n", + "\n", + "After installing the necessary libraies, the API must be configured. The code below shows how to configure the API directly in your Python environment. \n" + ] + }, + { + "cell_type": "code", + "execution_count": 42, + "id": "a9752fda", + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "import openai\n", + "import json\n", + "import os\n", + "from langchain.chat_models import AzureChatOpenAI\n", + "from langchain.schema import HumanMessage\n", + "from langchain import LLMChain\n", + "\n", + "\n", + "# Load config values\n", + "with open(r'config.json') as config_file:\n", + " config_details = json.load(config_file)\n", + "\n", + "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", + "openai_api_base=config_details['OPENAI_API_BASE']\n", + " \n", + "# API version e.g. \"2023-07-01-preview\"\n", + "openai_api_version=config_details['OPENAI_API_VERSION']\n", + "\n", + "# The name of your Azure OpenAI deployment chat model. e.g. \"gpt-35-turbo-0613\"\n", + "deployment_name=config_details['DEPLOYMENT_NAME']\n", + "\n", + "# The API key for your Azure OpenAI resource.\n", + "openai_api_key = os.getenv(\"OPENAI_API_KEY\")\n", + "\n", + "# This is set to `azure`\n", + "openai_api_type=\"azure\"" + ] + }, + { + "cell_type": "markdown", + "id": "52da840e", + "metadata": {}, + "source": [ + "## Deployed Model Setup" + ] + }, + { + "cell_type": "code", + "execution_count": 43, + "id": "041f0a56", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "AIMessage(content=\"In the realm of dreams, where thoughts take flight,\\nA tapestry of words, I shall now write.\\nWith ink and quill, I'll weave a tale,\\nOf love and hope, where hearts prevail.\\n\\nIn meadows adorned with flowers so fair,\\nA gentle breeze whispers secrets in the air.\\nThe sun shines bright, painting the sky,\\nA canvas of colors, where dreams never die.\\n\\nBeneath a canopy of stars, we shall dance,\\nLost in a moment, in a lover's trance.\\nOur hearts entwined, beats synchronized,\\nA symphony of love, never compromised.\\n\\nThrough valleys of sorrow, we shall tread,\\nWith courage and strength, our fears we'll shed.\\nFor love, a beacon, shall guide our way,\\nThrough darkest nights, to a brighter day.\\n\\nIn the depths of silence, a whispered prayer,\\nFor peace and harmony, beyond compare.\\nMay kindness bloom, like flowers in spring,\\nAnd compassion's song, forever sing.\\n\\nOh, let this poem be a gentle reminder,\\nThat within us all, love is a powerful binder.\\nFor in these words, a message so true,\\nThat love's embrace can heal and renew.\\n\\nSo let us cherish, this gift we possess,\\nThe power of words, to heal and impress.\\nThrough poetry's grace, may hearts be moved,\\nAnd in its beauty, we shall be proved.\", additional_kwargs={}, example=False)" + ] + }, + "execution_count": 43, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# Create an instance of chat llm\n", + "llm = AzureChatOpenAI(\n", + " openai_api_base=openai_api_base,\n", + " openai_api_version=openai_api_version,\n", + " deployment_name=deployment_name,\n", + " openai_api_key=openai_api_key,\n", + " openai_api_type=openai_api_type,\n", + ")\n", + "\n", + "llm([HumanMessage(content=\"Write me a poem\")])" + ] + }, + { + "cell_type": "markdown", + "id": "dc7ea2d4", + "metadata": {}, + "source": [ + "## PromptTemplates\n", + "\n", + "Langchain provides a built in PromptsTemplate module to simplify the construction of prompts to get more specific answers." + ] + }, + { + "cell_type": "code", + "execution_count": 44, + "id": "927d4bac", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Example #1:\n", + "content='A highly recommended face wash for acne-prone skin is the \"Neutrogena Oil-Free Acne Wash.\" This product contains salicylic acid, which helps to treat and prevent acne by unclogging pores and reducing inflammation. It is oil-free, non-comedogenic, and gentle enough for daily use. Additionally, it effectively removes dirt, excess oil, and makeup without over-drying the skin.' additional_kwargs={} example=False\n", + "\n", + "\n", + "Example #2:\n", + "If you're looking for warm weather and beautiful beaches in December, here are a few destinations you might consider:\n", + "\n", + "1. Maldives: This tropical paradise offers pristine beaches, crystal-clear waters, and luxurious resorts. December is a great time to visit, with temperatures averaging around 28°C (82°F).\n", + "\n", + "2. Thailand: Thailand's southern islands, such as Phuket, Krabi, and Koh Samui, offer warm weather and stunning beaches in December. You can relax on the white sands, go snorkeling or diving, and explore the vibrant local culture.\n", + "\n", + "3. Bali, Indonesia: Bali is a popular destination known for its stunning beaches, lush landscapes, and vibrant culture. In December, you can enjoy warm temperatures and take part in water sports or simply unwind by the beach.\n", + "\n", + "4. Cancun, Mexico: Cancun is a favorite destination for beach lovers, with its turquoise waters and soft white sands. December is a great time to visit, with temperatures around 27°C (81°F), and you can also explore the nearby Mayan ruins.\n", + "\n", + "5. Seychelles: This archipelago in the Indian Ocean boasts some of the world's most beautiful beaches. December is an excellent time to visit, as the weather is warm and you can enjoy activities like snorkeling, diving, and island hopping.\n", + "\n", + "Remember to check travel restrictions and safety guidelines before planning your trip, as they may vary due to the ongoing COVID-19 pandemic.\n" + ] + } + ], + "source": [ + "from langchain import PromptTemplate\n", + "\n", + "from langchain.prompts.chat import (\n", + " ChatPromptTemplate,\n", + " SystemMessagePromptTemplate,\n", + " HumanMessagePromptTemplate,\n", + ")\n", + "\n", + "\n", + "# First Example\n", + "template = \"\"\"\n", + "You are a skin care consulant that recommends products based on customer\n", + "needs and preferences.\n", + "\n", + "What is a good {product_type} to help with {customer_request}?\n", + "\"\"\"\n", + "\n", + "prompt = PromptTemplate(\n", + "input_variables=[\"product_type\", \"customer_request\"],\n", + "template=template,\n", + ")\n", + "\n", + "print(\"Example #1:\")\n", + "print(llm([HumanMessage(content=prompt.format(\n", + " product_type=\"face wash\",\n", + " customer_request = \"acne prone skin\"\n", + " ))]\n", + "))\n", + "print(\"\\n\")\n", + "\n", + "# Second Example\n", + "system_message = \"You are an AI assistant travel assistant that provides vacation recommendations.\"\n", + "\n", + "system_message_prompt = SystemMessagePromptTemplate.from_template(system_message)\n", + "human_template=\"{text}\"\n", + "human_message_prompt = HumanMessagePromptTemplate.from_template(human_template)\n", + "chat_prompt = ChatPromptTemplate.from_messages([system_message_prompt, human_message_prompt])\n", + "chain = LLMChain(llm=llm, prompt=chat_prompt)\n", + "result = chain.run(f\"Where should I go on vaction in Decemember for warm weather and beaches?\")\n", + "print(\"Example #2:\")\n", + "print(result)" + ] + }, + { + "cell_type": "markdown", + "id": "7b6723eb", + "metadata": {}, + "source": [ + "## Chains\n", + "There are many applications of chains that allow you to combine numerous LLM calls and actions.
\n", + "\n", + "### Simple Sequential Chains
\n", + "Allow you to feed the output of one LLM Chain as input for another." + ] + }, + { + "cell_type": "code", + "execution_count": 45, + "id": "af7c236f", + "metadata": {}, + "outputs": [], + "source": [ + "from langchain.chains import SimpleSequentialChain" + ] + }, + { + "cell_type": "code", + "execution_count": 46, + "id": "2a4a32f0", + "metadata": {}, + "outputs": [], + "source": [ + "description_template = \"\"\"Your job is to come up with a fun DIY project for the specified gender, age, and description of a kid.\n", + "% CHILD_DESCRIPTION\n", + "{child_description}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "description_prompt_template = PromptTemplate(input_variables=[\"child_description\"], template=description_template)\n", + "\n", + "description_chain = LLMChain(llm=llm, prompt=description_prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 47, + "id": "6eec47ff", + "metadata": {}, + "outputs": [], + "source": [ + "diy_description_template = \"\"\"Given a DIY project, give a short and simple recipe step-by-step guide on how to complete the project and a materials list.\n", + "% DIY_PROJECT\n", + "{diy_project}\n", + "\n", + "YOUR RESPONSE:\n", + "\"\"\"\n", + "diy_prompt_template = PromptTemplate(input_variables=[\"diy_project\"], template=diy_description_template)\n", + "\n", + "diy_chain = LLMChain(llm=llm, prompt=diy_prompt_template)" + ] + }, + { + "cell_type": "code", + "execution_count": 48, + "id": "84a15aea", + "metadata": {}, + "outputs": [], + "source": [ + "overall_chain = SimpleSequentialChain(chains=[description_chain, diy_chain], verbose=True)" + ] + }, + { + "cell_type": "code", + "execution_count": 49, + "id": "15928f72", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "\n", + "\u001b[1m> Entering new SimpleSequentialChain chain...\u001b[0m\n", + "\u001b[36;1m\u001b[1;3mDIY Sparkling Fairy Wand:\n", + "\n", + "Materials needed:\n", + "- Wooden dowel or stick\n", + "- Glitter foam sheets in various colors\n", + "- Ribbon or tulle\n", + "- Craft glue\n", + "- Scissors\n", + "- Decorative gems or sequins\n", + "- Glitter glue (optional)\n", + "\n", + "Instructions:\n", + "1. Begin by cutting out a star shape from one of the glitter foam sheets. This will be the top of the wand.\n", + "2. Cut out a long strip of foam from another color and wrap it around the wooden dowel or stick, starting from the bottom. Secure it with craft glue.\n", + "3. Cut out smaller shapes like hearts, butterflies, or flowers from different colored glitter foam sheets.\n", + "4. Use craft glue to stick these shapes onto the wrapped foam strip, creating a beautiful pattern. Let them dry completely.\n", + "5. Once the foam shapes are secure, add some extra sparkle by applying glitter glue to the edges or adding decorative gems or sequins.\n", + "6. Finally, tie ribbons or tulle strands to the bottom of the wooden dowel or stick for an extra touch of magic.\n", + "7. Let the wand dry completely before giving it to the 5-year-old girl to play with.\n", + "\n", + "This DIY project will allow the 5-year-old girl to express her creativity and imagination as she creates her very own sparkling fairy wand. She can use it for pretend play, dress-up parties, or even as a room decoration.\u001b[0m\n", + "\u001b[33;1m\u001b[1;3mDIY Sparkling Fairy Wand:\n", + "\n", + "Materials needed:\n", + "- Wooden dowel or stick\n", + "- Glitter foam sheets in various colors\n", + "- Ribbon or tulle\n", + "- Craft glue\n", + "- Scissors\n", + "- Decorative gems or sequins\n", + "- Glitter glue (optional)\n", + "\n", + "Instructions:\n", + "1. Cut out a star shape from a glitter foam sheet for the top of the wand.\n", + "2. Wrap a long strip of foam from another color around the wooden dowel or stick, securing it with craft glue.\n", + "3. Cut out smaller shapes like hearts, butterflies, or flowers from different colored glitter foam sheets.\n", + "4. Use craft glue to stick these shapes onto the wrapped foam strip to create a pattern. Let them dry completely.\n", + "5. Add extra sparkle by applying glitter glue to the edges or adding decorative gems or sequins.\n", + "6. Tie ribbons or tulle strands to the bottom of the wooden dowel or stick for an extra touch of magic.\n", + "7. Let the wand dry completely before giving it to the 5-year-old girl to play with.\n", + "\n", + "This DIY project allows the 5-year-old girl to express her creativity and imagination. She can use the sparkling fairy wand for pretend play, dress-up parties, or even as a room decoration.\u001b[0m\n", + "\n", + "\u001b[1m> Finished chain.\u001b[0m\n" + ] + } + ], + "source": [ + "review = overall_chain.run(\"5-year-old girl\")" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "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.4" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +}