mirror of
https://github.com/hwchase17/langchain.git
synced 2025-07-06 21:20:33 +00:00
Added dependencies to make example executable (#4790)
- Installation of non-colab packages - Get API keys # Added dependencies to make notebook executable on hosted notebooks ## Who can review? Community members can review the PR once tests pass. Tag maintainers/contributors who might be interested: @hwchase17 @vowelparrot
This commit is contained in:
parent
5bc7082e82
commit
8fd4d5d117
@ -3,7 +3,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "ba5f8741",
|
"id": "ba5f8741",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "ba5f8741"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"# Custom LLM Agent (with a ChatModel)\n",
|
"# Custom LLM Agent (with a ChatModel)\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -33,7 +35,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "fea4812c",
|
"id": "fea4812c",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "fea4812c"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Set up environment\n",
|
"## Set up environment\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -42,9 +46,25 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 1,
|
"source": [
|
||||||
|
"!pip install langchain\n",
|
||||||
|
"!pip install google-search-results\n",
|
||||||
|
"!pip install openai"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "mvxi3g8DExu6"
|
||||||
|
},
|
||||||
|
"id": "mvxi3g8DExu6",
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
"id": "9af9734e",
|
"id": "9af9734e",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "9af9734e"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser\n",
|
"from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser\n",
|
||||||
@ -53,13 +73,16 @@
|
|||||||
"from langchain.chat_models import ChatOpenAI\n",
|
"from langchain.chat_models import ChatOpenAI\n",
|
||||||
"from typing import List, Union\n",
|
"from typing import List, Union\n",
|
||||||
"from langchain.schema import AgentAction, AgentFinish, HumanMessage\n",
|
"from langchain.schema import AgentAction, AgentFinish, HumanMessage\n",
|
||||||
"import re"
|
"import re\n",
|
||||||
|
"from getpass import getpass"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "6df0253f",
|
"id": "6df0253f",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "6df0253f"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Set up tool\n",
|
"## Set up tool\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -68,13 +91,27 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 2,
|
"source": [
|
||||||
|
"SERPAPI_API_KEY = getpass()"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "LcSV8a5bFSDE"
|
||||||
|
},
|
||||||
|
"id": "LcSV8a5bFSDE",
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 4,
|
||||||
"id": "becda2a1",
|
"id": "becda2a1",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "becda2a1"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Define which tools the agent can use to answer user queries\n",
|
"# Define which tools the agent can use to answer user queries\n",
|
||||||
"search = SerpAPIWrapper()\n",
|
"search = SerpAPIWrapper(serpapi_api_key=SERPAPI_API_KEY)\n",
|
||||||
"tools = [\n",
|
"tools = [\n",
|
||||||
" Tool(\n",
|
" Tool(\n",
|
||||||
" name = \"Search\",\n",
|
" name = \"Search\",\n",
|
||||||
@ -87,7 +124,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "2e7a075c",
|
"id": "2e7a075c",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "2e7a075c"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Prompt Template\n",
|
"## Prompt Template\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -100,9 +139,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 12,
|
"execution_count": 5,
|
||||||
"id": "339b1bb8",
|
"id": "339b1bb8",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "339b1bb8"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Set up the base template\n",
|
"# Set up the base template\n",
|
||||||
@ -133,9 +174,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 13,
|
"execution_count": 6,
|
||||||
"id": "fd969d31",
|
"id": "fd969d31",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "fd969d31"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# Set up a prompt template\n",
|
"# Set up a prompt template\n",
|
||||||
@ -165,9 +208,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 14,
|
"execution_count": 7,
|
||||||
"id": "798ef9fb",
|
"id": "798ef9fb",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "798ef9fb"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"prompt = CustomPromptTemplate(\n",
|
"prompt = CustomPromptTemplate(\n",
|
||||||
@ -182,7 +227,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "ef3a1af3",
|
"id": "ef3a1af3",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "ef3a1af3"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Output Parser\n",
|
"## Output Parser\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -193,9 +240,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 15,
|
"execution_count": 8,
|
||||||
"id": "7c6fe0d3",
|
"id": "7c6fe0d3",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "7c6fe0d3"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"class CustomOutputParser(AgentOutputParser):\n",
|
"class CustomOutputParser(AgentOutputParser):\n",
|
||||||
@ -222,9 +271,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 16,
|
"execution_count": 9,
|
||||||
"id": "d278706a",
|
"id": "d278706a",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "d278706a"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"output_parser = CustomOutputParser()"
|
"output_parser = CustomOutputParser()"
|
||||||
@ -233,7 +284,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "170587b1",
|
"id": "170587b1",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "170587b1"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Set up LLM\n",
|
"## Set up LLM\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -242,18 +295,34 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 17,
|
"source": [
|
||||||
|
"OPENAI_API_KEY = getpass()"
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"id": "V8UM02AfGyYa"
|
||||||
|
},
|
||||||
|
"id": "V8UM02AfGyYa",
|
||||||
|
"execution_count": null,
|
||||||
|
"outputs": []
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 11,
|
||||||
"id": "f9d4c374",
|
"id": "f9d4c374",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "f9d4c374"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"llm = ChatOpenAI(temperature=0)"
|
"llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "caeab5e4",
|
"id": "caeab5e4",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "caeab5e4"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Define the stop sequence\n",
|
"## Define the stop sequence\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -265,7 +334,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "34be9f65",
|
"id": "34be9f65",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "34be9f65"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Set up the Agent\n",
|
"## Set up the Agent\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -274,9 +345,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 18,
|
"execution_count": 12,
|
||||||
"id": "9b1cc2a2",
|
"id": "9b1cc2a2",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "9b1cc2a2"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"# LLM chain consisting of the LLM and a prompt\n",
|
"# LLM chain consisting of the LLM and a prompt\n",
|
||||||
@ -285,9 +358,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 19,
|
"execution_count": 13,
|
||||||
"id": "e4f5092f",
|
"id": "e4f5092f",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "e4f5092f"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"tool_names = [tool.name for tool in tools]\n",
|
"tool_names = [tool.name for tool in tools]\n",
|
||||||
@ -302,7 +377,9 @@
|
|||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
"id": "aa8a5326",
|
"id": "aa8a5326",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "aa8a5326"
|
||||||
|
},
|
||||||
"source": [
|
"source": [
|
||||||
"## Use the Agent\n",
|
"## Use the Agent\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -311,9 +388,11 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 20,
|
"execution_count": 14,
|
||||||
"id": "490604e9",
|
"id": "490604e9",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "490604e9"
|
||||||
|
},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
|
||||||
@ -321,13 +400,20 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
"execution_count": 21,
|
"execution_count": 15,
|
||||||
"id": "653b1617",
|
"id": "653b1617",
|
||||||
"metadata": {},
|
"metadata": {
|
||||||
|
"id": "653b1617",
|
||||||
|
"outputId": "82f7dc8f-c09f-46f3-ae45-9acf7e4e3d94",
|
||||||
|
"colab": {
|
||||||
|
"base_uri": "https://localhost:8080/",
|
||||||
|
"height": 264
|
||||||
|
}
|
||||||
|
},
|
||||||
"outputs": [
|
"outputs": [
|
||||||
{
|
{
|
||||||
"name": "stdout",
|
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
|
"name": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"\n",
|
"\n",
|
||||||
@ -344,27 +430,22 @@
|
|||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
"output_type": "execute_result",
|
||||||
"data": {
|
"data": {
|
||||||
"text/plain": [
|
"text/plain": [
|
||||||
"\"Leo DiCaprio's current girlfriend is Camila Morrone.\""
|
"\"Leo DiCaprio's current girlfriend is Camila Morrone.\""
|
||||||
]
|
],
|
||||||
|
"application/vnd.google.colaboratory.intrinsic+json": {
|
||||||
|
"type": "string"
|
||||||
|
}
|
||||||
},
|
},
|
||||||
"execution_count": 21,
|
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"output_type": "execute_result"
|
"execution_count": 15
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"source": [
|
"source": [
|
||||||
"agent_executor.run(\"Search for Leo DiCaprio's girlfriend on the internet.\")"
|
"agent_executor.run(\"Search for Leo DiCaprio's girlfriend on the internet.\")"
|
||||||
]
|
]
|
||||||
},
|
|
||||||
{
|
|
||||||
"cell_type": "code",
|
|
||||||
"execution_count": null,
|
|
||||||
"id": "adefb4c2",
|
|
||||||
"metadata": {},
|
|
||||||
"outputs": [],
|
|
||||||
"source": []
|
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
@ -389,6 +470,9 @@
|
|||||||
"interpreter": {
|
"interpreter": {
|
||||||
"hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef"
|
"hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef"
|
||||||
}
|
}
|
||||||
|
},
|
||||||
|
"colab": {
|
||||||
|
"provenance": []
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
Loading…
Reference in New Issue
Block a user