langchain-practice/L6-Agents.ipynb

488 lines
10 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"id": "52824b89-532a-4e54-87e9-1410813cd39e",
"metadata": {},
"source": [
"# LangChain: Agents\n",
"\n",
"## Outline:\n",
"\n",
"* Using built in LangChain tools: DuckDuckGo search and Wikipedia\n",
"* Defining your own tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7ed03ed-1322-49e3-b2a2-33e94fb592ef",
"metadata": {
"height": 149,
"tags": []
},
"outputs": [],
"source": [
"import os\n",
"\n",
"from dotenv import load_dotenv, find_dotenv\n",
"_ = load_dotenv(find_dotenv()) # read local .env file\n",
"\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"id": "36fd0194-9f3d-4468-9390-f23df83e83c4",
"metadata": {},
"source": [
"Note: LLM's do not always produce the same results. When executing the code in your notebook, you may get slightly different answers that those in the video."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b7269176-8094-40ce-bedc-4c6421b16e98",
"metadata": {
"height": 251
},
"outputs": [],
"source": [
"# account for deprecation of LLM model\n",
"import datetime\n",
"# Get the current date\n",
"current_date = datetime.datetime.now().date()\n",
"\n",
"# Define the date after which the model should be set to \"gpt-3.5-turbo\"\n",
"target_date = datetime.date(2024, 6, 12)\n",
"\n",
"# Set the model variable based on the current date\n",
"if current_date > target_date:\n",
" llm_model = \"gpt-3.5-turbo\"\n",
"else:\n",
" llm_model = \"gpt-3.5-turbo-0301\""
]
},
{
"cell_type": "markdown",
"id": "0e3b9ad2",
"metadata": {},
"source": [
"## Built-in LangChain tools"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "974acf8e-8f88-42de-88f8-40a82cb58e8b",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"#!pip install -U wikipedia"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7b93d0e8",
"metadata": {
"height": 132,
"tags": []
},
"outputs": [],
"source": [
"from langchain.agents.agent_toolkits import create_python_agent\n",
"from langchain.agents import load_tools, initialize_agent\n",
"from langchain.agents import AgentType\n",
"from langchain.tools.python.tool import PythonREPLTool\n",
"from langchain.python import PythonREPL\n",
"from langchain.chat_models import ChatOpenAI"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "7aadd5c1",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0, model=llm_model)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "394924ac",
"metadata": {
"height": 47,
"tags": []
},
"outputs": [],
"source": [
"tools = load_tools([\"llm-math\",\"wikipedia\"], llm=llm)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "02cd4956",
"metadata": {
"height": 132,
"tags": []
},
"outputs": [],
"source": [
"agent= initialize_agent(\n",
" tools, \n",
" llm, \n",
" agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
" handle_parsing_errors=True,\n",
" verbose = True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "44601fe4-5f24-4ff2-a5df-a7e194a5b4f8",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"agent(\"What is the 25% of 300?\")"
]
},
{
"cell_type": "markdown",
"id": "10ee3df6-ded9-4c96-ba47-a6cf636c1b9b",
"metadata": {},
"source": [
"## Wikipedia example"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "441df699-0a52-4812-a0a7-4605204f0aa8",
"metadata": {
"height": 98,
"tags": []
},
"outputs": [],
"source": [
"question = \"Tom M. Mitchell is an American computer scientist \\\n",
"and the Founders University Professor at Carnegie Mellon University (CMU)\\\n",
"what book did he write?\"\n",
"result = agent(question) "
]
},
{
"cell_type": "markdown",
"id": "90299a00-eb79-4755-bb4b-bf61d88c7fed",
"metadata": {},
"source": [
"## Python Agent"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c7617576-d855-4ede-9c1a-be742f1504cf",
"metadata": {
"height": 99,
"tags": []
},
"outputs": [],
"source": [
"agent = create_python_agent(\n",
" llm,\n",
" tool=PythonREPLTool(),\n",
" verbose=True\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d05e7dc6-f07e-43df-a2f6-9a0816291861",
"metadata": {
"height": 150,
"tags": []
},
"outputs": [],
"source": [
"customer_list = [[\"Harrison\", \"Chase\"], \n",
" [\"Lang\", \"Chain\"],\n",
" [\"Dolly\", \"Too\"],\n",
" [\"Elle\", \"Elem\"], \n",
" [\"Geoff\",\"Fusion\"], \n",
" [\"Trance\",\"Former\"],\n",
" [\"Jen\",\"Ayai\"]\n",
" ]"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f887f8f9-216e-489a-945a-f38fbccbe5c4",
"metadata": {
"height": 65,
"tags": []
},
"outputs": [],
"source": [
"agent.run(f\"\"\"Sort these customers by \\\n",
"last name and then first name \\\n",
"and print the output: {customer_list}\"\"\") "
]
},
{
"cell_type": "markdown",
"id": "74466cec-2d98-438b-805f-33e31fdc3035",
"metadata": {},
"source": [
"#### View detailed outputs of the chains"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "12548579-810b-416e-afb5-178296685c2c",
"metadata": {
"height": 116,
"tags": []
},
"outputs": [],
"source": [
"import langchain\n",
"langchain.debug=True\n",
"agent.run(f\"\"\"Sort these customers by \\\n",
"last name and then first name \\\n",
"and print the output: {customer_list}\"\"\") \n",
"langchain.debug=False"
]
},
{
"cell_type": "markdown",
"id": "2d2a4685",
"metadata": {},
"source": [
"## Define your own tool"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70b32cfe",
"metadata": {
"height": 31,
"tags": []
},
"outputs": [],
"source": [
"#!pip install DateTime"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "73f8b595",
"metadata": {
"height": 48,
"tags": []
},
"outputs": [],
"source": [
"from langchain.agents import tool\n",
"from datetime import date"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "61c64268",
"metadata": {
"height": 167,
"tags": []
},
"outputs": [],
"source": [
"@tool\n",
"def time(text: str) -> str:\n",
" \"\"\"Returns todays date, use this for any \\\n",
" questions related to knowing todays date. \\\n",
" The input should always be an empty string, \\\n",
" and this function will always return todays \\\n",
" date - any date mathmatics should occur \\\n",
" outside this function.\"\"\"\n",
" return str(date.today())"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d2fd9ee",
"metadata": {
"height": 132,
"tags": []
},
"outputs": [],
"source": [
"agent= initialize_agent(\n",
" tools + [time], \n",
" llm, \n",
" agent=AgentType.CHAT_ZERO_SHOT_REACT_DESCRIPTION,\n",
" handle_parsing_errors=True,\n",
" verbose = True)"
]
},
{
"cell_type": "markdown",
"id": "f9f30e7c-77f1-43fc-a966-140f05160378",
"metadata": {},
"source": [
"**Note**: \n",
"\n",
"The agent will sometimes come to the wrong conclusion (agents are a work in progress!). \n",
"\n",
"If it does, please try running it again."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "1fcd13cb",
"metadata": {
"height": 82,
"tags": []
},
"outputs": [],
"source": [
"try:\n",
" result = agent(\"whats the date today?\") \n",
"except: \n",
" print(\"exception on external access\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "178403f1",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "da13800c",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "0d07a599",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "67306deb",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ccc16102",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "5fc99309",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "9b26537f",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "84683fd1",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "ac295066",
"metadata": {
"height": 31
},
"outputs": [],
"source": []
}
],
"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.9.17"
}
},
"nbformat": 4,
"nbformat_minor": 5
}