From 3b91beec0e3d445f2bbcfd5c41f67c1c8ecd2fad Mon Sep 17 00:00:00 2001 From: Sheepsta300 <128811766+Sheepsta300@users.noreply.github.com> Date: Fri, 6 Sep 2024 15:40:02 +1200 Subject: [PATCH] work on comment/review changes --- getting_started.ipynb | 173 ++++++++++++++++++++++++++---------------- 1 file changed, 108 insertions(+), 65 deletions(-) diff --git a/getting_started.ipynb b/getting_started.ipynb index 8d6d18a0204f2a..def170f106ac00 100644 --- a/getting_started.ipynb +++ b/getting_started.ipynb @@ -29,7 +29,7 @@ "id": "3b21c348-6a01-41d4-9df5-0d1d633367ef", "metadata": {}, "source": [ - "Using the [**Azure sign up page**](https://azure.microsoft.com/en-us/pricing/purchase-options/azure-account) one can sign up and create an Azure OpenAI resource, giving access to all necessary credentials." + "Using the [**Azure sign up page**](https://azure.microsoft.com/pricing/purchase-options/azure-account) one can sign up and create an Azure OpenAI resource, giving access to all necessary credentials." ] }, { @@ -51,7 +51,7 @@ "id": "448ce3be-b98f-4d46-8815-36fda94e9d30", "metadata": {}, "source": [ - "#### Install the most current version of `langchain` and `langchain_openai`." + "#### Install the most recent version of `langchain` and `langchain_openai`." ] }, { @@ -68,7 +68,7 @@ "metadata": {}, "source": [ "#### Install Packages in a Virtual Environment (Optional)\n", - "Set up a virtual environment by going to your project directory and executing the following command. This will create a new virtual environment in a folder named `.venv`" + "Set up a virtual environment by going to your project directory and executing the following command. This will create a new virtual environment in a folder named `.venv`." ] }, { @@ -149,7 +149,7 @@ "\n", "`AZURE_OPENAI_API_KEY` = <\"Your key here\">\n", "\n", - "`AZURE_OPENAI_API_ENDPOINT` = <\"Your endpoint here\">" + "`AZURE_OPENAI_ENDPOINT` = <\"Your endpoint here\">" ] }, { @@ -165,7 +165,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 1, "id": "471ae21c", "metadata": {}, "outputs": [ @@ -175,14 +175,14 @@ "(True, 'COMPLETIONS_MODEL', 'Your model here')" ] }, - "execution_count": 10, + "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from dotenv import set_key\n", - "set_key(\".env\", \"AZURE_OPENAI_API_VERSION\", \"Your api version here\")\n", + "set_key(\".env\", \"OPENAI_API_VERSION\", \"Your api version here\")\n", "set_key(\".env\", \"COMPLETIONS_MODEL\", \"Your model here\")" ] }, @@ -227,7 +227,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 3, "id": "b456baeb-fe51-45c8-940e-7f6ec52da0df", "metadata": {}, "outputs": [], @@ -241,10 +241,10 @@ "API_KEY = os.environ['AZURE_OPENAI_API_KEY']\n", "\n", "# The base URL for your Azure OpenAI resource. e.g. \"https://.openai.azure.com\"\n", - "ENDPOINT = os.environ['AZURE_OPENAI_API_ENDPOINT']\n", + "ENDPOINT = os.environ['AZURE_OPENAI_ENDPOINT']\n", "\n", "# The API version required\n", - "VERSION = os.environ['AZURE_OPENAI_API_VERSION']" + "VERSION = os.environ['OPENAI_API_VERSION']" ] }, { @@ -254,12 +254,12 @@ "source": [ "## Creating an AzureChatOpenAI Model\n", "\n", - "LangChain's [**AzureChatOpenAI Info**](https://python.langchain.com/v0.2/docs/integrations/chat/azure_chat_openai/)" + "More information on LangChain's AzureChatOpenAI support can be found in [the integration documentation](https://python.langchain.com/v0.2/docs/integrations/chat/azure_chat_openai/)." ] }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 5, "id": "035bd2cf-c045-4cdc-a0d1-f9bf60314f8d", "metadata": {}, "outputs": [], @@ -272,8 +272,8 @@ "id": "172e4ff3-b3fd-4f10-bd2b-18ac1e259973", "metadata": {}, "source": [ - "- Environment variables can be manually passed as parameters\n", - "- The constuctor can search for environment variables of the corresponding names" + "- Environment variable values can be passed as parameters.\n", + "- Alternatively, if not passed in, the constructor will search for environment variables with corresponding names." ] }, { @@ -291,7 +291,6 @@ " max_tokens=200,\n", " timeout=60,\n", " max_retries=10,\n", - " # organization=\"...\",\n", " # model=\"gpt-35-turbo\",\n", " # model_version=\"0125\",\n", " # other params...\n", @@ -303,7 +302,7 @@ "id": "23b80bbe-442c-4374-a885-87bd6408dc28", "metadata": {}, "source": [ - "**AZURE_OPENAI_API_VERSION** and **AZURE_OPENAI_API_ENDPOINT** are both being passed, but **AZURE_OPENAI_API_KEY** is being retrieved within the constructor." + "In the above code sample, **OPENAI_API_VERSION** and **AZURE_OPENAI_ENDPOINT** are both being passed in, but **AZURE_OPENAI_API_KEY** is being retrieved within the constructor." ] }, { @@ -321,15 +320,13 @@ "\n", "- `max_retries` sets the number of times the API request should be retried in case of failure before giving up.\n", "\n", - "- `organization` is used to specify the organization ID if required for API access.\n", - "\n", "- `model` specifies the model to be used.\n", "\n", "- `model_version` indicates the specific version of the chosen model to use.\n", "\n", - "- See the [**API Reference**](https://api.python.langchain.com/en/latest/chat_models/langchain_openai.chat_models.azure.AzureChatOpenAI.html) for more details\n", + "- See the [**API Reference**](https://api.python.langchain.com/en/latest/chat_models/langchain_openai.chat_models.azure.AzureChatOpenAI.html) for more details.\n", "\n", - "- Other parameters may be available in different SDK's" + "- Other parameters may be available in different SDK's." ] }, { @@ -342,7 +339,7 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 51, "id": "7600d5eb", "metadata": {}, "outputs": [], @@ -367,7 +364,7 @@ "source": [ "- LangChain-compatible chat models take a list of `messages` as `input` and return the AI message as `output`.\n", "\n", - "- All messages have `role` and `content` properties. However, below the roles are set using `SystemMessage` and `HumanMessage`. We'll cover more on this later.\n", + "- All messages have `role` and `content` properties. In the sample below, the roles are set by using the `SystemMessage` and `HumanMessage` classes. [**We'll cover more on this later**](#assigning-roles) .\n", "\n", "- Additional provider-specific information can be incorporated using the `Additional Properties`." ] @@ -427,7 +424,7 @@ "\n", "- Prompts are the inputs to language models, refined from raw user inputs to be ready for processing by the models.\n", "\n", - "- [Prompting](https://www.datacamp.com/tutorial/prompt-engineering-with-langchain) involves crafting text inputs that clearly communicate with the models, outlining the specific task we want it to accomplish. This can include:\n", + "- [**Prompting**](https://www.datacamp.com/tutorial/prompt-engineering-with-langchain) involves crafting text inputs that clearly communicate with the models, outlining the specific task we want it to accomplish. This can include:\n", " - Selecting the appropriate wording and setting a particular tone or style.\n", " - Providing necessary context.\n", " - Assigning a role, such as asking it to respond as if it were a native speaker of a certain language." @@ -461,11 +458,12 @@ "from langchain_core.prompts import PromptTemplate \n", "\n", "prompt_template = PromptTemplate.from_template(\n", - " \"What vege crops can I grow in {month} in {city}, New Zealand?\"\n", + " \"What vegetable crops can I grow in {month} in {city}, New Zealand?\"\n", ")\n", "\n", "prompt_value = prompt_template.format(month = \"December\", city = \"Rotorua\")\n", "\n", + "\n", "# print(prompt_template) # <- uncomment to see\n", "# print(prompt_value) # <- uncomment to see" ] @@ -501,12 +499,12 @@ "\n", "This is optimized for a conversation-like format. The prompt is a list of chat messages. Each chat message is associated with `role` and `content`. In the example code, `.from_messages` is used to include multiple messages.\n", "\n", - "Here, we will hardcode roles in the chat prompt, as opposed to using `SystemMessage` or `HumanMessage` like earlier." + "Here, we will hardcode roles in the chat prompt, as opposed to using the pre-built roles `SystemMessage` or `HumanMessage` like earlier." ] }, { "cell_type": "code", - "execution_count": 18, + "execution_count": 17, "id": "46e869bf-370d-4742-837f-c7d5eb76a891", "metadata": {}, "outputs": [], @@ -516,9 +514,9 @@ "chat_template = ChatPromptTemplate.from_messages(\n", " [\n", " (\"system\", \"\"\"\n", - " You are a travel agent helping customers plan their trips.\n", - " Recommend them regarding popular Accommodation, Food and Activities of the country customer is asking.\n", - " \"\"\"), \n", + " You're a travel agent helping customers plan their trips.\n", + " Offer recommendations on natural features to visit, local cuisine, and activities based on the country the customer is asking about.\n", + " \"\"\"),\n", " (\"ai\", \"Hi there, What can I help you with today?\"),\n", " (\"human\", \"Hi I'm {name}, I'm planning a trip to {country}. Any recommendations\")\n", " ]\n", @@ -532,17 +530,17 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": 18, "id": "c87adc53-1248-441d-aa46-44f410c24796", "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "\"Hi Lucy! New Zealand is an incredible destination with stunning landscapes, rich culture, and plenty of activities. Here are some recommendations for your trip:\\n\\n### Accommodation:\\n1. **Auckland:**\\n - **SkyCity Hotel:** Located in the heart of the city, this hotel offers luxurious rooms and easy access to major attractions.\\n - **Hotel DeBrett:** A boutique hotel with a unique design and a great location in the city center.\\n\\n2. **Queenstown:**\\n - **Eichardt's Private Hotel:** Known for its elegance and stunning lake views.\\n - **The Rees Hotel:** Offers luxurious accommodations with beautiful views of Lake Wakatipu.\\n\\n3. **Rotorua:**\\n - **Regent of Rotorua:** A stylish boutique hotel with a central location.\\n - **Solitaire Lodge:** A luxury lodge offering stunning views of Lake Tarawera.\\n\\n### Food:\\n1. **Auckland:**\\n - **Depot Eatery & Oyster Bar:**\"" + "\"Hi Lucy! New Zealand is a fantastic choice with its stunning landscapes, rich culture, and exciting activities. Here are some recommendations to make your trip memorable:\\n\\n### Natural Features\\n1. **Fiordland National Park**: Home to the famous Milford Sound and Doubtful Sound, this area offers breathtaking fjords, waterfalls, and rainforests.\\n2. **Tongariro National Park**: Known for its dramatic volcanic landscape, you can hike the Tongariro Alpine Crossing, one of the best one-day hikes in the world.\\n3. **Rotorua**: Famous for its geothermal activity, you can see geysers, hot springs, and mud pools. Don't miss the Wai-O-Tapu Thermal Wonderland.\\n4. **Aoraki/Mount Cook**: The highest mountain in New Zealand, offering stunning views, glaciers, and excellent hiking trails.\\n5. **Bay of Islands**: A beautiful coastal area with over 140 subtropical islands, perfect for sailing, fishing,\"" ] }, - "execution_count": 19, + "execution_count": 18, "metadata": {}, "output_type": "execute_result" } @@ -552,13 +550,20 @@ "response.content" ] }, + { + "cell_type": "markdown", + "id": "d83fef4b", + "metadata": {}, + "source": [ + "\n", + "#### Assigning Roles Using LangChain Messages" + ] + }, { "cell_type": "markdown", "id": "cb6794d6-ba1f-4367-bae6-af0136212713", "metadata": {}, "source": [ - "#### Assigning Roles Using LangChain Messages\n", - "\n", "Compared to hardcoding the roles like above, LangChain Messages allow for more flexibility and better management, especially with complex conversations involving multiple roles. It also simplifies the visualization of the conversation flow.\n", "\n", "It is therefore recommended to use LangChain messages where possible.\n", @@ -636,7 +641,7 @@ "metadata": {}, "source": [ "#### `MessagePlaceHolder`\n", - "This is used to select which messages to include when formatting." + "This is used to select which messages to include when formatting." ] }, { @@ -923,17 +928,17 @@ }, { "cell_type": "code", - "execution_count": 67, + "execution_count": 49, "id": "561dfbfb-0933-4650-8230-23f1bc2b1f46", "metadata": {}, "outputs": [], "source": [ - "from langchain_community.callbacks.manager import get_openai_callback" + "from langchain_community.callbacks import get_openai_callback" ] }, { "cell_type": "code", - "execution_count": 68, + "execution_count": 52, "id": "c9081591-0b58-44b3-89c7-c3a512f84f4c", "metadata": {}, "outputs": [], @@ -954,7 +959,7 @@ }, { "cell_type": "code", - "execution_count": 70, + "execution_count": 53, "id": "178153e8-c37c-4ce2-bbb4-15ec6feb58f6", "metadata": {}, "outputs": [ @@ -998,7 +1003,7 @@ }, { "cell_type": "code", - "execution_count": 73, + "execution_count": 20, "id": "84527cdd-6d45-4d18-a3c4-887a8537c9a5", "metadata": {}, "outputs": [], @@ -1022,7 +1027,7 @@ }, { "cell_type": "code", - "execution_count": 75, + "execution_count": 43, "id": "05f0ff77-07bd-4041-9ab5-c2ba13ca644c", "metadata": {}, "outputs": [], @@ -1034,15 +1039,15 @@ " '''Information about a given person'''\n", "\n", " name: str = Field(..., description=\"The name of a person\")\n", - " alive: bool = Field(..., description=\"Thether the person is alive or not\")\n", - " profession: str = Field(..., description=\"What the person does for work or professionally\")\n", + " alive: bool = Field(..., description=\"Whether the person is alive or not\")\n", + " place_of_birth: str = Field(..., description=\"Where the person was born\")\n", " noteable_features: str = Field(..., description=\"Any noteworthy features/achievements about the person\")\n", - " hobbies: Optional[str] = Field(description=\"Anything hobbies the person may have\")" + " hobbies: str = Field(..., description=\"Any hobbies the person may have\")" ] }, { "cell_type": "code", - "execution_count": 76, + "execution_count": 44, "id": "c5d9b7cc-d127-4f28-ab8f-829f236ef419", "metadata": { "scrolled": true @@ -1051,10 +1056,10 @@ { "data": { "text/plain": [ - "Person(name='Kate Sheppard', alive=False, profession='Suffragist', noteable_features=\"Kate Sheppard was the most prominent member of the women's suffrage movement in New Zealand. She played a key role in making New Zealand the first country to grant women the right to vote in 1893.\", hobbies=None)" + "Person(name='Kate Sheppard', alive=False, place_of_birth='Liverpool, England', noteable_features=\"Leader of the women's suffrage movement in New Zealand, instrumental in making New Zealand the first country to grant women the right to vote in 1893.\", hobbies='Activism, writing, public speaking')" ] }, - "execution_count": 76, + "execution_count": 44, "metadata": {}, "output_type": "execute_result" } @@ -1075,7 +1080,7 @@ }, { "cell_type": "code", - "execution_count": 78, + "execution_count": 45, "id": "63f05dc8-7832-429c-b710-ceb72a56b492", "metadata": {}, "outputs": [ @@ -1085,15 +1090,15 @@ "text": [ "Kate Sheppard\n", "False\n", - "Suffragist\n", - "Kate Sheppard was the most prominent member of the women's suffrage movement in New Zealand. She played a key role in making New Zealand the first country to grant women the right to vote in 1893.\n" + "Liverpool, England\n", + "Leader of the women's suffrage movement in New Zealand, instrumental in making New Zealand the first country to grant women the right to vote in 1893.\n" ] } ], "source": [ "print(response.name)\n", "print(response.alive)\n", - "print(response.profession)\n", + "print(response.place_of_birth)\n", "print(response.noteable_features)" ] }, @@ -1107,7 +1112,7 @@ }, { "cell_type": "code", - "execution_count": 80, + "execution_count": 8, "id": "2f688856-6992-4ed5-bf4b-62ddc592aee3", "metadata": {}, "outputs": [], @@ -1123,26 +1128,64 @@ "Models can also be explicitly told to respond in a JSON structured format. This could then be used for future API calls or for easier access to information. However, **the word \"json\" must be included in the message string.**" ] }, + { + "cell_type": "code", + "execution_count": 46, + "id": "42478d15-0aa2-4e47-b279-e807397d9f31", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "'{\\n \"name\": \"Jane Doe\",\\n \"alive\": true,\\n \"place_of_birth\": \"Springfield, Illinois, USA\"\\n}'" + ] + }, + "execution_count": 46, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "json_model = model.bind(response_format={\"type\": \"json_object\"})\n", + "\n", + "response = json_model.invoke(\n", + " '''Return a JSON object of a random person with features like name,\n", + " alive (if they're alive or not) and their place of birth.''' \n", + ")\n", + "\n", + "response.content" + ] + }, { "cell_type": "markdown", - "id": "7aea759b-de79-48be-8ebe-c979f1c45bc3", + "id": "f9b35d39", "metadata": {}, "source": [ - "**latest update for LangChain seems to have broken the JSON output**" + "The response can then be formatted into a JSON object and accessed using normal JSON notation." ] }, { "cell_type": "code", - "execution_count": 92, - "id": "42478d15-0aa2-4e47-b279-e807397d9f31", + "execution_count": 47, + "id": "2dde7260", "metadata": {}, - "outputs": [], + "outputs": [ + { + "data": { + "text/plain": [ + "{'name': 'Jane Doe',\n", + " 'alive': True,\n", + " 'place_of_birth': 'Springfield, Illinois, USA'}" + ] + }, + "execution_count": 47, + "metadata": {}, + "output_type": "execute_result" + } + ], "source": [ - "#json_model = model.with_structured_output(method=\"json_mode\")\n", - "\n", - "#json_response = model.invoke(\"Return a JSON object of a random person with features like name, alive (if they're alive or not) and their profession.\")\n", - "\n", - "#json_response.content" + "person = json.loads(response.content)\n", + "person" ] }, { @@ -1195,7 +1238,7 @@ "\n", "url = \"https://upload.wikimedia.org/wikipedia/commons/b/bf/Aoraki_Mount_Cook.JPG\"\n", "\n", - "# Use an HTTP get request to retrieve the image\n", + "# Use an HTTP GET request to retrieve the image\n", "response = httpx.get(url)\n", "\n", "# Check the request was successful\n", @@ -1829,7 +1872,7 @@ "id": "a325870f-a0fc-4a38-807e-bab86d44d87d", "metadata": {}, "source": [ - "use embeddings to get the vector representation of a query." + "Use embeddings to get the vector representation of a query." ] }, { @@ -1954,7 +1997,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.12.0" + "version": "3.12.5" } }, "nbformat": 4,