{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Advanced Techniques\n", "## 1. ReAct\n", "\n", "LLMs abilities for reasoning (e.g. chain-of-thought CoT prompting) and acting have primarily been studied as separate topics. **ReAct** [Shunyu Yao et al. ICLR 2023](https://arxiv.org/pdf/2210.03629.pdf) (Reason and Act) is a method to generate both reasoning traces and task-specific actions in an interleaved manner.\n", "\n", "In simple words, we define specific patterns for the language model to follow. This allows the model to act (usually through tools) and reason. Hence the model create a squence of interleaved thoughts and actions. Such systems that act on an enviroment are usually called **agents** (borrowed from reinforcement learning).\n", "\n", "" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Requirements" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# %%writefile requirements.txt\n", "# langchain\n", "# langchain-experimental\n", "# langchainhub\n", "# wikipedia\n", "# duckduckgo-search\n", "# boto3\n", "# pandas" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "# !pip install -r requirements.txt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "import os\n", "import boto3\n", "import pandas as pd\n", "\n", "from langchain.agents import Tool\n", "from langchain.llms.bedrock import Bedrock\n", "from langchain.tools import DuckDuckGoSearchRun\n", "from langchain.utilities import WikipediaAPIWrapper\n", "from langchain_experimental.utilities import PythonREPL" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We use our credentials to connect to a [Bedrock](https://aws.amazon.com/bedrock/) client. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "LLAMA2_70B_CHAT = \"meta.llama2-70b-chat-v1\"\n", "LLAMA2_13B_CHAT = \"meta.llama2-13b-chat-v1\"\n", "\n", "# We'll default to the smaller 13B model for speed; change to LLAMA2_70B_CHAT for more advanced (but slower) generations\n", "DEFAULT_MODEL = LLAMA2_70B_CHAT\n", "\n", "llm = Bedrock(credentials_profile_name='default', model_id=DEFAULT_MODEL)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can now use the Bedrock client to communicate with the language model. You can use the standard kwargs for chat or completion. We loaded a chat model here. Let's test it. We use `temperature=0.0` here for consistency." ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [], "source": [ "question = \"What is the largest city in Vermont?\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "The largest city in Vermont is Burlington, which is located in the northwestern part of the state, on the shores of Lake Champlain. Burlington has a population of approximately 42,000 people, making it the largest city in Vermont and the seat of Chittenden County. It is also home to the state's largest airport, the Burlington International Airport, and is a hub for commerce, education, and culture in the state.\n" ] } ], "source": [ "response_text = llm.invoke(\n", " question,\n", " temperature=0.0,\n", " max_gen_len=128,\n", ")\n", "print(response_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Problem Setup\n", "We want our model to answer a question about a real time event so that it will need to interact with internet to pull the info. Otherwise the answer won't be accurate. In this example, we ask about the market cap of the company Nvidia. Since the model knowledge cut-off is in the past, the model answers the question incorrectly." ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "Nvidia market cap as of April 1, 2023 is $558.84B.\n", "Nvidia market cap history\n", "Nvidia IPO was on January 22, 1999. The IPO price was $12 per share.\n", "Nvidia market capitalization has been steadily increasing over the years. In 2016, Nvidia market cap was around $30B. In 2018, it reached $100B. In 2020, it reached $200B. And in 2\n" ] } ], "source": [ "question = \"What is Nvidia market cap?\"\n", "\n", "response_text = llm.invoke(\n", " question,\n", " temperature=0.0,\n", " max_gen_len=128,\n", ")\n", "print(response_text)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can see that the answer is incorrect.\n", "\n", "### Preparing Tools\n", "\n", "There are many tools you can use when working with LLMs. Here we use three of tools available at [LangChain](https://python.langchain.com/docs/integrations/tools) but you can use many other tools or create your own tool. \n", "\n", "The important thing is a very clear and distint definition for each tool because that will be way of communicating the tool application with the model. Here we create three tools to show that the model is capable of identifying the right tool given a strong model and good descriptions." ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "duckduckgo_search_run = DuckDuckGoSearchRun()\n", "duckduckgo_tool = Tool(\n", " name=\"duckduckgo_tool\",\n", " func=duckduckgo_search_run.run,\n", " description=\"Useful for when you need to search online about facts and events or retrieve news.\"\n", ")\n", "\n", "wikipedia = WikipediaAPIWrapper()\n", "wikipedia_tool = Tool(\n", " name=\"wikipedia_tool\",\n", " func=wikipedia.run,\n", " description=\"Useful for when you need to answer general questions about people, places, companies, facts, historical events, or other subjects. Input should be a search query.\",\n", ")\n", "\n", "python_repl = PythonREPL()\n", "repl_tool = Tool(\n", " name=\"repl_tool\",\n", " description=\"A Python shell. Use this to execute python commands or to calculate math expressions. Input should be a valid python command.\",\n", " func=python_repl.run,\n", ")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here is an example of running one of the tools so we know what will be exposed to the model when using these tools.\n", "\n", "