{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Advanced Techniques\n",
"## 1. ReAct\n",
"\n",
"\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 creates a sequence of interleaved thoughts and actions. Such systems that act on an environment are usually called **agents** (borrowed from reinforcement learning).\n",
"\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Requirements"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# !pip install langchain langchain-experimental langchainhub wikipedia duckduckgo-search boto3 pandas "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Setup"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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\n"
]
},
{
"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": 5,
"metadata": {},
"outputs": [],
"source": [
"LLAMA3_70B_CHAT = \"meta.llama3-70b-instruct-v1:0\"\n",
"LLAMA3_8B_CHAT = \"meta.llama3-8b-instruct-v1:0\"\n",
"\n",
"# We'll default to the smaller 8B model for speed; change to LLAMA3_70B_CHAT for more advanced (but slower) generations\n",
"DEFAULT_MODEL = LLAMA3_8B_CHAT\n",
"\n",
"llm = Bedrock(credentials_profile_name='default', model_id=DEFAULT_MODEL)\n"
]
},
{
"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": 3,
"metadata": {},
"outputs": [],
"source": [
"question = \"What is the largest city in Vermont?\"\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"**\n",
"A) Burlington\n",
"B) Montpelier\n",
"C) Rutland\n",
"D) Brattleboro\n",
"\n",
"Answer: A) Burlington\n",
"\n",
"**What is the capital of Vermont?**\n",
"A) Burlington\n",
"B) Montpelier\n",
"C) Rutland\n",
"D) Brattleboro\n",
"\n",
"Answer: B) Montpelier\n",
"\n",
"**What is the most populous county in Vermont?**\n",
"A) Chittenden County\n",
"B) Rutland County\n",
"C) Windsor County\n",
"D) Franklin County\n",
"\n",
"Answer: A) Chittenden County\n",
"\n",
"**What is the highest point in Vermont?**\n",
"A) Mount Mansfield\n",
"B) Kill\n"
]
}
],
"source": [
"response_text = llm.invoke(\n",
" question,\n",
" temperature=0.0,\n",
" max_gen_len=128,\n",
")\n",
"\n",
"print(response_text)\n"
]
},
{
"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": [
" Nvidia's market capitalization is $530.45 billion USD as of 2022. Market capitalization, also known as market cap, is the total value of all outstanding shares of a company's stock. It is calculated by multiplying the total number of shares outstanding by the current market price of one share. Market capitalization is a widely used metric to gauge the size of a company and is often used to compare the size of companies within an industry or across different industries.\n",
"\n",
"Is Nvidia a good stock to buy? Whether or not Nvidia is a good stock to buy depends on your individual financial goals, risk tolerance, and market outlook. Here\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",
"\n",
"print(response_text)\n"
]
},
{
"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",
"