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
dynamic_agent_tools
Mark Pors 1 year ago committed by GitHub
parent 5bc7082e82
commit 8fd4d5d117
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -3,7 +3,9 @@
{
"cell_type": "markdown",
"id": "ba5f8741",
"metadata": {},
"metadata": {
"id": "ba5f8741"
},
"source": [
"# Custom LLM Agent (with a ChatModel)\n",
"\n",
@ -33,7 +35,9 @@
{
"cell_type": "markdown",
"id": "fea4812c",
"metadata": {},
"metadata": {
"id": "fea4812c"
},
"source": [
"## Set up environment\n",
"\n",
@ -42,9 +46,25 @@
},
{
"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",
"metadata": {},
"metadata": {
"id": "9af9734e"
},
"outputs": [],
"source": [
"from langchain.agents import Tool, AgentExecutor, LLMSingleActionAgent, AgentOutputParser\n",
@ -53,13 +73,16 @@
"from langchain.chat_models import ChatOpenAI\n",
"from typing import List, Union\n",
"from langchain.schema import AgentAction, AgentFinish, HumanMessage\n",
"import re"
"import re\n",
"from getpass import getpass"
]
},
{
"cell_type": "markdown",
"id": "6df0253f",
"metadata": {},
"metadata": {
"id": "6df0253f"
},
"source": [
"## Set up tool\n",
"\n",
@ -68,13 +91,27 @@
},
{
"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",
"metadata": {},
"metadata": {
"id": "becda2a1"
},
"outputs": [],
"source": [
"# 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",
" Tool(\n",
" name = \"Search\",\n",
@ -87,7 +124,9 @@
{
"cell_type": "markdown",
"id": "2e7a075c",
"metadata": {},
"metadata": {
"id": "2e7a075c"
},
"source": [
"## Prompt Template\n",
"\n",
@ -100,9 +139,11 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 5,
"id": "339b1bb8",
"metadata": {},
"metadata": {
"id": "339b1bb8"
},
"outputs": [],
"source": [
"# Set up the base template\n",
@ -133,9 +174,11 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 6,
"id": "fd969d31",
"metadata": {},
"metadata": {
"id": "fd969d31"
},
"outputs": [],
"source": [
"# Set up a prompt template\n",
@ -165,9 +208,11 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 7,
"id": "798ef9fb",
"metadata": {},
"metadata": {
"id": "798ef9fb"
},
"outputs": [],
"source": [
"prompt = CustomPromptTemplate(\n",
@ -182,7 +227,9 @@
{
"cell_type": "markdown",
"id": "ef3a1af3",
"metadata": {},
"metadata": {
"id": "ef3a1af3"
},
"source": [
"## Output Parser\n",
"\n",
@ -193,9 +240,11 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 8,
"id": "7c6fe0d3",
"metadata": {},
"metadata": {
"id": "7c6fe0d3"
},
"outputs": [],
"source": [
"class CustomOutputParser(AgentOutputParser):\n",
@ -222,9 +271,11 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 9,
"id": "d278706a",
"metadata": {},
"metadata": {
"id": "d278706a"
},
"outputs": [],
"source": [
"output_parser = CustomOutputParser()"
@ -233,7 +284,9 @@
{
"cell_type": "markdown",
"id": "170587b1",
"metadata": {},
"metadata": {
"id": "170587b1"
},
"source": [
"## Set up LLM\n",
"\n",
@ -242,18 +295,34 @@
},
{
"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",
"metadata": {},
"metadata": {
"id": "f9d4c374"
},
"outputs": [],
"source": [
"llm = ChatOpenAI(temperature=0)"
"llm = ChatOpenAI(openai_api_key=OPENAI_API_KEY, temperature=0)"
]
},
{
"cell_type": "markdown",
"id": "caeab5e4",
"metadata": {},
"metadata": {
"id": "caeab5e4"
},
"source": [
"## Define the stop sequence\n",
"\n",
@ -265,7 +334,9 @@
{
"cell_type": "markdown",
"id": "34be9f65",
"metadata": {},
"metadata": {
"id": "34be9f65"
},
"source": [
"## Set up the Agent\n",
"\n",
@ -274,9 +345,11 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 12,
"id": "9b1cc2a2",
"metadata": {},
"metadata": {
"id": "9b1cc2a2"
},
"outputs": [],
"source": [
"# LLM chain consisting of the LLM and a prompt\n",
@ -285,9 +358,11 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 13,
"id": "e4f5092f",
"metadata": {},
"metadata": {
"id": "e4f5092f"
},
"outputs": [],
"source": [
"tool_names = [tool.name for tool in tools]\n",
@ -302,7 +377,9 @@
{
"cell_type": "markdown",
"id": "aa8a5326",
"metadata": {},
"metadata": {
"id": "aa8a5326"
},
"source": [
"## Use the Agent\n",
"\n",
@ -311,9 +388,11 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 14,
"id": "490604e9",
"metadata": {},
"metadata": {
"id": "490604e9"
},
"outputs": [],
"source": [
"agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)"
@ -321,13 +400,20 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 15,
"id": "653b1617",
"metadata": {},
"metadata": {
"id": "653b1617",
"outputId": "82f7dc8f-c09f-46f3-ae45-9acf7e4e3d94",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 264
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"name": "stdout",
"text": [
"\n",
"\n",
@ -344,27 +430,22 @@
]
},
{
"output_type": "execute_result",
"data": {
"text/plain": [
"\"Leo DiCaprio's current girlfriend is Camila Morrone.\""
]
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
"execution_count": 15
}
],
"source": [
"agent_executor.run(\"Search for Leo DiCaprio's girlfriend on the internet.\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "adefb4c2",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -389,6 +470,9 @@
"interpreter": {
"hash": "18784188d7ecd866c0586ac068b02361a6896dc3a29b64f5cc957f09c590acef"
}
},
"colab": {
"provenance": []
}
},
"nbformat": 4,

Loading…
Cancel
Save