'snippet': '(Reuters) -Tesla Inc has resumed taking orders for its '
                      'Model 3 long-range vehicle in the United States, the '
                      "company's website showed late on...",
           'date': '19 hours ago',
           'source': 'Yahoo Finance',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTIZetJ62sQefPfbQ9KKDt6iH7Mc0ylT5t_hpgeeuUkHhJuAx2FOJ4ZTRVDFg&s',
           'position': 5},
          {'title': 'The Tesla Model 3 Long Range AWD Is Now Available in the '
                    'U.S. With 325 Miles of Range',
           'link': 'https://www.notateslaapp.com/news/1393/tesla-reopens-orders-for-model-3-long-range-after-months-of-unavailability',
           'snippet': 'Tesla has reopened orders for the Model 3 Long Range '
                      'RWD, which has been unavailable for months due to high '
                      'demand.',
           'date': '7 hours ago',
           'source': 'Not a Tesla App',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSecrgxZpRj18xIJY-nDHljyP-A4ejEkswa9eq77qhMNrScnVIqe34uql5U4w&s',
           'position': 6},
          {'title': 'Tesla Cybertruck alpha prototype spotted at the Fremont '
                    'factory in new pics and videos',
           'link': 'https://www.teslaoracle.com/2023/05/03/tesla-cybertruck-alpha-prototype-interior-and-exterior-spotted-at-the-fremont-factory-in-new-pics-and-videos/',
           'snippet': 'A Tesla Cybertruck alpha prototype goes to Fremont, '
                      'California for another round of testing before going to '
                      'production later this year (pics...',
           'date': '14 hours ago',
           'source': 'Tesla Oracle',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcRO7M5ZLQE-Zo4-_5dv9hNAQZ3wSqfvYCuKqzxHG-M6CgLpwPMMG_ssebdcMg&s',
           'position': 7},
          {'title': 'Tesla putting facility in new part of country - Austin '
                    'Business Journal',
           'link': 'https://www.bizjournals.com/austin/news/2023/05/02/tesla-leases-building-seattle-area.html',
           'snippet': 'Check out what Puget Sound Business Journal has to '
                      "report about the Austin-based company's real estate "
                      'footprint in the Pacific Northwest.',
           'date': '22 hours ago',
           'source': 'The Business Journals',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR9kIEHWz1FcHKDUtGQBS0AjmkqtyuBkQvD8kyIY3kpaPrgYaN7I_H2zoOJsA&s',
           'position': 8},
          {'title': 'Tesla (TSLA) Resumes Orders for Model 3 Long Range After '
                    'Backlog',
           'link': 'https://www.bloomberg.com/news/articles/2023-05-03/tesla-resumes-orders-for-popular-model-3-long-range-at-47-240',
           'snippet': 'Tesla Inc. has resumed taking orders for its Model 3 '
                      'Long Range edition with a starting price of $47240, '
                      'according to its website.',
           'date': '5 hours ago',
           'source': 'Bloomberg.com',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTWWIC4VpMTfRvSyqiomODOoLg0xhoBf-Tc1qweKnSuaiTk-Y1wMJZM3jct0w&s',
           'position': 9}]}
If you want to only receive news articles published in the last hour, you can do the following:


search = GoogleSerperAPIWrapper(type="news", tbs="qdr:h")
results = search.results("Tesla Inc.")
pprint.pp(results)
{'searchParameters': {'q': 'Tesla Inc.',
                      'gl': 'us',
                      'hl': 'en',
                      'num': 10,
                      'type': 'news',
                      'tbs': 'qdr:h'},
 'news': [{'title': 'Oklahoma Gov. Stitt sees growing foreign interest in '
                    'investments in ...',
           'link': 'https://www.reuters.com/world/us/oklahoma-gov-stitt-sees-growing-foreign-interest-investments-state-2023-05-04/',
           'snippet': 'T)), a battery supplier to electric vehicle maker Tesla '
                      'Inc (TSLA.O), said on Sunday it is considering building '
                      'a battery plant in Oklahoma, its third in...',
           'date': '53 mins ago',
           'source': 'Reuters',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSSTcsXeenqmEKdiekvUgAmqIPR4nlAmgjTkBqLpza-lLfjX1CwB84MoNVj0Q&s',
           'position': 1},
          {'title': 'Ryder lanza solución llave en mano para vehículos '
                    'eléctricos en EU',
           'link': 'https://www.tyt.com.mx/nota/ryder-lanza-solucion-llave-en-mano-para-vehiculos-electricos-en-eu',
           'snippet': 'Ryder System Inc. presentó RyderElectric+ TM como su '
                      'nueva solución llave en mano ... Ryder también tiene '
                      'reservados los semirremolques Tesla y continúa...',
           'date': '56 mins ago',
           'source': 'Revista Transportes y Turismo',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQJhXTQQtjSUZf9YPM235WQhFU5_d7lEA76zB8DGwZfixcgf1_dhPJyKA1Nbw&s',
           'position': 2},
          {'title': '"I think people can get by with $999 million," Bernie '
                    'Sanders tells American Billionaires.',
           'link': 'https://thebharatexpressnews.com/i-think-people-can-get-by-with-999-million-bernie-sanders-tells-american-billionaires-heres-how-the-ultra-rich-can-pay-less-income-tax-than-you-legally/',
           'snippet': 'The report noted that in 2007 and 2011, Amazon.com Inc. '
                      'founder Jeff Bezos “did not pay a dime in federal ... '
                      'If you want to bet on Musk, check out Tesla.',
           'date': '11 mins ago',
           'source': 'THE BHARAT EXPRESS NEWS',
           'imageUrl': 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR_X9qqSwVFBBdos2CK5ky5IWIE3aJPCQeRYR9O1Jz4t-MjaEYBuwK7AU3AJQ&s',
           'position': 3}]}
Some examples of the tbs parameter:
qdr:h (past hour)
qdr:d (past day)
qdr:w (past week)
qdr:m (past month)
qdr:y (past year)
You can specify intermediate time periods by adding a number:
qdr:h12 (past 12 hours)
qdr:d3 (past 3 days)
qdr:w2 (past 2 weeks)
qdr:m6 (past 6 months)
qdr:m2 (past 2 years)
For all supported filters simply go to Google Search, search for something, click on “Tools”, add your date filter and check the URL for “tbs=”.
Searching for Google Places#
We can also query Google Places using this wrapper. For example:
search = GoogleSerperAPIWrapper(type="places")
results = search.results("Italian restaurants in Upper East Side")
pprint.pp(results)
{'searchParameters': {'q': 'Italian restaurants in Upper East Side',
                      'gl': 'us',
                      'hl': 'en',


'gl': 'us',
                      'hl': 'en',
                      'num': 10,
                      'type': 'places'},
 'places': [{'position': 1,
             'title': "L'Osteria",
             'address': '1219 Lexington Ave',
             'latitude': 40.777154599999996,
             'longitude': -73.9571363,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNjU7BWEq_aYQANBCbX52Kb0lDpd_lFIx5onw40=w92-h92-n-k-no',
             'rating': 4.7,
             'ratingCount': 91,
             'category': 'Italian'},
            {'position': 2,
             'title': "Tony's Di Napoli",
             'address': '1081 3rd Ave',
             'latitude': 40.7643567,
             'longitude': -73.9642373,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNbNv6jZkJ9nyVi60__8c1DQbe_eEbugRAhIYye=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 2265,
             'category': 'Italian'},
            {'position': 3,
             'title': 'Caravaggio',
             'address': '23 E 74th St',
             'latitude': 40.773412799999996,
             'longitude': -73.96473379999999,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPDGchokDvppoLfmVEo6X_bWd3Fz0HyxIHTEe9V=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 276,
             'category': 'Italian'},
            {'position': 4,
             'title': 'Luna Rossa',
             'address': '347 E 85th St',
             'latitude': 40.776593999999996,
             'longitude': -73.950351,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNPCpCPuqPAb1Mv6_fOP7cjb8Wu1rbqbk2sMBlh=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 140,
             'category': 'Italian'},
            {'position': 5,
             'title': "Paola's",
             'address': '1361 Lexington Ave',
             'latitude': 40.7822019,
             'longitude': -73.9534096,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPJr2Vcx-B6K-GNQa4koOTffggTePz8TKRTnWi3=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 344,
             'category': 'Italian'},
            {'position': 6,
             'title': 'Come Prima',
             'address': '903 Madison Ave',
             'latitude': 40.772124999999996,
             'longitude': -73.965012,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNrX19G0NVdtDyMovCQ-M-m0c_gLmIxrWDQAAbz=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 176,
             'category': 'Italian'},
            {'position': 7,
             'title': 'Botte UES',
             'address': '1606 1st Ave.',
             'latitude': 40.7750785,
             'longitude': -73.9504801,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPPN5GXxfH3NDacBc0Pt3uGAInd9OChS5isz9RF=w92-h92-n-k-no',
             'rating': 4.4,
             'ratingCount': 152,
             'category': 'Italian'},


'ratingCount': 152,
             'category': 'Italian'},
            {'position': 8,
             'title': 'Piccola Cucina Uptown',
             'address': '106 E 60th St',
             'latitude': 40.7632468,
             'longitude': -73.9689825,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipPifIgzOCD5SjgzzqBzGkdZCBp0MQsK5k7M7znn=w92-h92-n-k-no',
             'rating': 4.6,
             'ratingCount': 941,
             'category': 'Italian'},
            {'position': 9,
             'title': 'Pinocchio Restaurant',
             'address': '300 E 92nd St',
             'latitude': 40.781453299999995,
             'longitude': -73.9486788,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipNtxlIyEEJHtDtFtTR9nB38S8A2VyMu-mVVz72A=w92-h92-n-k-no',
             'rating': 4.5,
             'ratingCount': 113,
             'category': 'Italian'},
            {'position': 10,
             'title': 'Barbaresco',
             'address': '843 Lexington Ave #1',
             'latitude': 40.7654332,
             'longitude': -73.9656873,
             'thumbnailUrl': 'https://lh5.googleusercontent.com/p/AF1QipMb9FbPuXF_r9g5QseOHmReejxSHgSahPMPJ9-8=w92-h92-n-k-no',
             'rating': 4.3,
             'ratingCount': 122,
             'locationHint': 'In The Touraine',
             'category': 'Italian'}]}
previous
Google Search
next
Gradio Tools
 Contents

As part of a Self Ask With Search Chain
Obtaining results with metadata
Searching for Google Images
Searching for Google News
Searching for Google Places
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
PubMed Tool
PubMed Tool#
This notebook goes over how to use PubMed as a tool
PubMed® comprises more than 35 million citations for biomedical literature from MEDLINE, life science journals, and online books. Citations may include links to full text content from PubMed Central and publisher web sites.
from langchain.tools import PubmedQueryRun
tool = PubmedQueryRun()
tool.run("chatgpt")
'Published: <Year>2023</Year><Month>May</Month><Day>31</Day>\nTitle: Dermatology in the wake of an AI revolution: who gets a say?\nSummary: \n\nPublished: <Year>2023</Year><Month>May</Month><Day>30</Day>\nTitle: What is ChatGPT and what do we do with it? Implications of the age of AI for nursing and midwifery practice and education: An editorial.\nSummary: \n\nPublished: <Year>2023</Year><Month>Jun</Month><Day>02</Day>\nTitle: The Impact of ChatGPT on the Nursing Profession: Revolutionizing Patient Care and Education.\nSummary: The nursing field has undergone notable changes over time and is projected to undergo further modifications in the future, owing to the advent of sophisticated technologies and growing healthcare needs. The advent of ChatGPT, an AI-powered language model, is expected to exert a significant influence on the nursing profession, specifically in the domains of patient care and instruction. The present article delves into the ramifications of ChatGPT within the nursing domain and accentuates its capacity and constraints to transform the discipline.'
previous
OpenWeatherMap API
next
Python REPL
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
Google Search
 Contents
Number of Results
Metadata Results
Google Search#
This notebook goes over how to use the google search component.
First, you need to set up the proper API keys and environment variables. To set it up, create the GOOGLE_API_KEY in the Google Cloud credential console (https://console.cloud.google.com/apis/credentials) and a GOOGLE_CSE_ID using the Programmable Search Enginge (https://programmablesearchengine.google.com/controlpanel/create). Next, it is good to follow the instructions found here.
Then we will need to set some environment variables.
import os
os.environ["GOOGLE_CSE_ID"] = ""
os.environ["GOOGLE_API_KEY"] = ""
from langchain.tools import Tool
from langchain.utilities import GoogleSearchAPIWrapper
search = GoogleSearchAPIWrapper()
tool = Tool(
    name = "Google Search",
    description="Search Google for recent results.",
    func=search.run
)
tool.run("Obama's first name?")
"STATE OF HAWAII. 1 Child's First Name. (Type or print). 2. Sex. BARACK. 3. This Birth. CERTIFICATE OF LIVE BIRTH. FILE. NUMBER 151 le. lb. Middle Name. Barack Hussein Obama II is an American former politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic\xa0... When Barack Obama was elected president in 2008, he became the first African American to hold ... The Middle East remained a key foreign policy challenge. Jan 19, 2017 ... Jordan Barack Treasure, New York City, born in 2008 ... Jordan Barack Treasure made national news when he was the focus of a New York newspaper\xa0... Portrait of George Washington, the 1st President of the United States ... Portrait of Barack Obama, the 44th President of the United States\xa0... His full name is Barack Hussein Obama II. Since the “II” is simply because he was named for his father, his last name is Obama. Mar 22, 2008 ... Barry Obama decided that he didn't like his nickname. A few of his friends at Occidental College had already begun to call him Barack (his\xa0... Aug 18, 2017 ... It took him several seconds and multiple clues to remember former President Barack Obama's first name. Miller knew that every answer had to\xa0... Feb 9, 2015 ... Michael Jordan misspelled Barack Obama's first name on 50th-birthday gift ... Knowing Obama is a Chicagoan and huge basketball fan,\xa0... 4 days ago ... Barack Obama, in full Barack Hussein Obama II, (born August 4, 1961, Honolulu, Hawaii, U.S.), 44th president of the United States (2009–17) and\xa0..."
Number of Results#
You can use the k parameter to set the number of results
search = GoogleSearchAPIWrapper(k=1)
tool = Tool(
    name = "I'm Feeling Lucky",
    description="Search Google and return the first result.",
    func=search.run
)
tool.run("python")
'The official home of the Python Programming Language.'
‘The official home of the Python Programming Language.’
Metadata Results#
Run query through GoogleSearch and return snippet, title, and link metadata.
Snippet: The description of the result.
Title: The title of the result.
Link: The link to the result.
search = GoogleSearchAPIWrapper()
def top5_results(query):
    return search.results(query, 5)
tool = Tool(
    name = "Google Search Snippets",
    description="Search Google for recent results.",
    func=top5_results
)
previous
Google Places
next
Google Serper API
 Contents

Number of Results
Metadata Results
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
File System Tools
 Contents
The FileManagementToolkit
Selecting File System Tools
File System Tools#
LangChain provides tools for interacting with a local file system out of the box. This notebook walks through some of them.
Note: these tools are not recommended for use outside a sandboxed environment!
First, we’ll import the tools.
from langchain.tools.file_management import (
    ReadFileTool,
    CopyFileTool,
    DeleteFileTool,
    MoveFileTool,
    WriteFileTool,
    ListDirectoryTool,
)
from langchain.agents.agent_toolkits import FileManagementToolkit
from tempfile import TemporaryDirectory
# We'll make a temporary directory to avoid clutter
working_directory = TemporaryDirectory()
The FileManagementToolkit#
If you want to provide all the file tooling to your agent, it’s easy to do so with the toolkit. We’ll pass the temporary directory in as a root directory as a workspace for the LLM.
It’s recommended to always pass in a root directory, since without one, it’s easy for the LLM to pollute the working directory, and without one, there isn’t any validation against
straightforward prompt injection.
toolkit = FileManagementToolkit(root_dir=str(working_directory.name)) # If you don't provide a root_dir, operations will default to the current working directory
toolkit.get_tools()
[CopyFileTool(name='copy_file', description='Create a copy of a file in a specified location', args_schema=<class 'langchain.tools.file_management.copy.FileCopyInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 DeleteFileTool(name='file_delete', description='Delete a file', args_schema=<class 'langchain.tools.file_management.delete.FileDeleteInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 FileSearchTool(name='file_search', description='Recursively search for files in a subdirectory that match the regex pattern', args_schema=<class 'langchain.tools.file_management.file_search.FileSearchInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 MoveFileTool(name='move_file', description='Move or rename a file from one location to another', args_schema=<class 'langchain.tools.file_management.move.FileMoveInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 ReadFileTool(name='read_file', description='Read file from disk', args_schema=<class 'langchain.tools.file_management.read.ReadFileInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 WriteFileTool(name='write_file', description='Write file to disk', args_schema=<class 'langchain.tools.file_management.write.WriteFileInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),


ListDirectoryTool(name='list_directory', description='List files and directories in a specified folder', args_schema=<class 'langchain.tools.file_management.list_dir.DirectoryListingInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug')]
Selecting File System Tools#
If you only want to select certain tools, you can pass them in as arguments when initializing the toolkit, or you can individually initialize the desired tools.
tools = FileManagementToolkit(root_dir=str(working_directory.name), selected_tools=["read_file", "write_file", "list_directory"]).get_tools()
tools
[ReadFileTool(name='read_file', description='Read file from disk', args_schema=<class 'langchain.tools.file_management.read.ReadFileInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 WriteFileTool(name='write_file', description='Write file to disk', args_schema=<class 'langchain.tools.file_management.write.WriteFileInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug'),
 ListDirectoryTool(name='list_directory', description='List files and directories in a specified folder', args_schema=<class 'langchain.tools.file_management.list_dir.DirectoryListingInput'>, return_direct=False, verbose=False, callback_manager=<langchain.callbacks.shared.SharedCallbackManager object at 0x1156f4350>, root_dir='/var/folders/gf/6rnp_mbx5914kx7qmmh7xzmw0000gn/T/tmpxb8c3aug')]
read_tool, write_tool, list_tool = tools
write_tool.run({"file_path": "example.txt", "text": "Hello World!"})
'File written successfully to example.txt.'
# List files in the working directory
list_tool.run({})
'example.txt'
previous
DuckDuckGo Search
next
Google Places
 Contents

The FileManagementToolkit
Selecting File System Tools
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
Brave Search
Brave Search#
This notebook goes over how to use the Brave Search tool.
from langchain.tools import BraveSearch
api_key = "..."
tool = BraveSearch.from_api_key(api_key=api_key, search_kwargs={"count": 3})
tool.run("obama middle name")
'[{"title": "Barack Obama - Wikipedia", "link": "https://en.wikipedia.org/wiki/Barack_Obama", "snippet": "Outside of politics, Obama has published three bestselling books: Dreams from My Father (1995), The Audacity of Hope (2006) and A Promised Land (2020). Rankings by scholars and historians, in which he has been featured since 2010, place him in the middle to upper tier of American presidents."}, {"title": "Obama\'s Middle Name -- My Last Name -- is \'Hussein.\' So?", "link": "https://www.cair.com/cair_in_the_news/obamas-middle-name-my-last-name-is-hussein-so/", "snippet": "Many Americans understand that common names don\\u2019t only come in the form of a \\u201cSmith\\u201d or a \\u201cJohnson.\\u201d Perhaps, they have a neighbor, mechanic or teacher named Hussein. Or maybe they\\u2019ve seen fashion designer Hussein Chalayan in the pages of Vogue or recall King Hussein, our ally in the Middle East."}, {"title": "What\'s up with Obama\'s middle name? - Quora", "link": "https://www.quora.com/Whats-up-with-Obamas-middle-name", "snippet": "Answer (1 of 15): A better question would be, \\u201cWhat\\u2019s up with Obama\\u2019s first name?\\u201d President Barack Hussein Obama\\u2019s father\\u2019s name was Barack Hussein Obama. He was named after his father. Hussein, Obama\\u2019s middle name, is a very common Arabic name, meaning "good," "handsome," or "beautiful.""}]'
previous
Bing Search
next
ChatGPT Plugins
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
HuggingFace Tools
HuggingFace Tools#
Huggingface Tools supporting text I/O can be
loaded directly using the load_huggingface_tool function.
# Requires transformers>=4.29.0 and huggingface_hub>=0.14.1
!pip install --upgrade transformers huggingface_hub > /dev/null
from langchain.agents import load_huggingface_tool
tool = load_huggingface_tool("lysandre/hf-model-downloads")
print(f"{tool.name}: {tool.description}")
model_download_counter: This is a tool that returns the most downloaded model of a given task on the Hugging Face Hub. It takes the name of the category (such as text-classification, depth-estimation, etc), and returns the name of the checkpoint
tool.run("text-classification")
'facebook/bart-large-mnli'
previous
GraphQL tool
next
Human as a tool
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
SearxNG Search API
 Contents
Custom Parameters
Obtaining results with metadata
SearxNG Search API#
This notebook goes over how to use a self hosted SearxNG search API to search the web.
You can check this link for more informations about Searx API parameters.
import pprint
from langchain.utilities import SearxSearchWrapper
search = SearxSearchWrapper(searx_host="http://127.0.0.1:8888")
For some engines, if a direct answer is available the warpper will print the answer instead of the full list of search results. You can use the results method of the wrapper if you want to obtain all the results.
search.run("What is the capital of France")
'Paris is the capital of France, the largest country of Europe with 550 000 km2 (65 millions inhabitants). Paris has 2.234 million inhabitants end 2011. She is the core of Ile de France region (12 million people).'
Custom Parameters#
SearxNG supports up to 139 search engines. You can also customize the Searx wrapper with arbitrary named parameters that will be passed to the Searx search API . In the below example we will making a more interesting use of custom search parameters from searx search api.
In this example we will be using the engines parameters to query wikipedia
search = SearxSearchWrapper(searx_host="http://127.0.0.1:8888", k=5) # k is for max number of items
search.run("large language model ", engines=['wiki'])
'Large language models (LLMs) represent a major advancement in AI, with the promise of transforming domains through learned knowledge. LLM sizes have been increasing 10X every year for the last few years, and as these models grow in complexity and size, so do their capabilities.\n\nGPT-3 can translate language, write essays, generate computer code, and more — all with limited to no supervision. In July 2020, OpenAI unveiled GPT-3, a language model that was easily the largest known at the time. Put simply, GPT-3 is trained to predict the next word in a sentence, much like how a text message autocomplete feature works.\n\nA large language model, or LLM, is a deep learning algorithm that can recognize, summarize, translate, predict and generate text and other content based on knowledge gained from massive datasets. Large language models are among the most successful applications of transformer models.\n\nAll of today’s well-known language models—e.g., GPT-3 from OpenAI, PaLM or LaMDA from Google, Galactica or OPT from Meta, Megatron-Turing from Nvidia/Microsoft, Jurassic-1 from AI21 Labs—are...\n\nLarge language models (LLMs) such as GPT-3are increasingly being used to generate text. These tools should be used with care, since they can generate content that is biased, non-verifiable, constitutes original research, or violates copyrights.'
Passing other Searx parameters for searx like language
search = SearxSearchWrapper(searx_host="http://127.0.0.1:8888", k=1)
search.run("deep learning", language='es', engines=['wiki'])
'Aprendizaje profundo (en inglés, deep learning) es un conjunto de algoritmos de aprendizaje automático (en inglés, machine learning) que intenta modelar abstracciones de alto nivel en datos usando arquitecturas computacionales que admiten transformaciones no lineales múltiples e iterativas de datos expresados en forma matricial o tensorial. 1'
Obtaining results with metadata#
In this example we will be looking for scientific paper using the categories parameter and limiting the results to a time_range (not all engines support the time range option).
We also would like to obtain the results in a structured way including metadata. For this we will be using the results method of the wrapper.
search = SearxSearchWrapper(searx_host="http://127.0.0.1:8888")
results = search.results("Large Language Model prompt", num_results=5, categories='science', time_range='year')
pprint.pp(results)
[{'snippet': '… on natural language instructions, large language models (… the '
             'prompt used to steer the model, and most effective prompts … to '
             'prompt engineering, we propose Automatic Prompt …',
  'title': 'Large language models are human-level prompt engineers',


'title': 'Large language models are human-level prompt engineers',
  'link': 'https://arxiv.org/abs/2211.01910',
  'engines': ['google scholar'],
  'category': 'science'},
 {'snippet': '… Large language models (LLMs) have introduced new possibilities '
             'for prototyping with AI [18]. Pre-trained on a large amount of '
             'text data, models … language instructions called prompts. …',
  'title': 'Promptchainer: Chaining large language model prompts through '
           'visual programming',
  'link': 'https://dl.acm.org/doi/abs/10.1145/3491101.3519729',
  'engines': ['google scholar'],
  'category': 'science'},
 {'snippet': '… can introspect the large prompt model. We derive the view '
             'ϕ0(X) and the model h0 from T01. However, instead of fully '
             'fine-tuning T0 during co-training, we focus on soft prompt '
             'tuning, …',
  'title': 'Co-training improves prompt-based learning for large language '
           'models',
  'link': 'https://proceedings.mlr.press/v162/lang22a.html',
  'engines': ['google scholar'],
  'category': 'science'},
 {'snippet': '… With the success of large language models (LLMs) of code and '
             'their use as … prompt design process become important. In this '
             'work, we propose a framework called Repo-Level Prompt …',
  'title': 'Repository-level prompt generation for large language models of '
           'code',
  'link': 'https://arxiv.org/abs/2206.12839',
  'engines': ['google scholar'],
  'category': 'science'},
 {'snippet': '… Figure 2 | The benefits of different components of a prompt '
             'for the largest language model (Gopher), as estimated from '
             'hierarchical logistic regression. Each point estimates the '
             'unique …',
  'title': 'Can language models learn from explanations in context?',
  'link': 'https://arxiv.org/abs/2204.02329',
  'engines': ['google scholar'],
  'category': 'science'}]
Get papers from arxiv
results = search.results("Large Language Model prompt", num_results=5, engines=['arxiv'])
pprint.pp(results)
[{'snippet': 'Thanks to the advanced improvement of large pre-trained language '
             'models, prompt-based fine-tuning is shown to be effective on a '
             'variety of downstream tasks. Though many prompting methods have '
             'been investigated, it remains unknown which type of prompts are '
             'the most effective among three types of prompts (i.e., '
             'human-designed prompts, schema prompts and null prompts). In '
             'this work, we empirically compare the three types of prompts '
             'under both few-shot and fully-supervised settings. Our '
             'experimental results show that schema prompts are the most '
             'effective in general. Besides, the performance gaps tend to '
             'diminish when the scale of training data grows large.',
  'title': 'Do Prompts Solve NLP Tasks Using Natural Language?',
  'link': 'http://arxiv.org/abs/2203.00902v1',
  'engines': ['arxiv'],
  'category': 'science'},
 {'snippet': 'Cross-prompt automated essay scoring (AES) requires the system '
             'to use non target-prompt essays to award scores to a '
             'target-prompt essay. Since obtaining a large quantity of '
             'pre-graded essays to a particular prompt is often difficult and '
             'unrealistic, the task of cross-prompt AES is vital for the '
             'development of real-world AES systems, yet it remains an '
             'under-explored area of research. Models designed for '
             'prompt-specific AES rely heavily on prompt-specific knowledge '
             'and perform poorly in the cross-prompt setting, whereas current '
             'approaches to cross-prompt AES either require a certain quantity '
             'of labelled target-prompt essays or require a large quantity of '
             'unlabelled target-prompt essays to perform transfer learning in '
             'a multi-step manner. To address these issues, we introduce '


'a multi-step manner. To address these issues, we introduce '
             'Prompt Agnostic Essay Scorer (PAES) for cross-prompt AES. Our '
             'method requires no access to labelled or unlabelled '
             'target-prompt data during training and is a single-stage '
             'approach. PAES is easy to apply in practice and achieves '
             'state-of-the-art performance on the Automated Student Assessment '
             'Prize (ASAP) dataset.',
  'title': 'Prompt Agnostic Essay Scorer: A Domain Generalization Approach to '
           'Cross-prompt Automated Essay Scoring',
  'link': 'http://arxiv.org/abs/2008.01441v1',
  'engines': ['arxiv'],
  'category': 'science'},
 {'snippet': 'Research on prompting has shown excellent performance with '
             'little or even no supervised training across many tasks. '
             'However, prompting for machine translation is still '
             'under-explored in the literature. We fill this gap by offering a '
             'systematic study on prompting strategies for translation, '
             'examining various factors for prompt template and demonstration '
             'example selection. We further explore the use of monolingual '
             'data and the feasibility of cross-lingual, cross-domain, and '
             'sentence-to-document transfer learning in prompting. Extensive '
             'experiments with GLM-130B (Zeng et al., 2022) as the testbed '
             'show that 1) the number and the quality of prompt examples '
             'matter, where using suboptimal examples degenerates translation; '
             '2) several features of prompt examples, such as semantic '
             'similarity, show significant Spearman correlation with their '
             'prompting performance; yet, none of the correlations are strong '
             'enough; 3) using pseudo parallel prompt examples constructed '
             'from monolingual data via zero-shot prompting could improve '
             'translation; and 4) improved performance is achievable by '
             'transferring knowledge from prompt examples selected in other '
             'settings. We finally provide an analysis on the model outputs '
             'and discuss several problems that prompting still suffers from.',
  'title': 'Prompting Large Language Model for Machine Translation: A Case '
           'Study',
  'link': 'http://arxiv.org/abs/2301.07069v2',
  'engines': ['arxiv'],
  'category': 'science'},
 {'snippet': 'Large language models can perform new tasks in a zero-shot '
             'fashion, given natural language prompts that specify the desired '
             'behavior. Such prompts are typically hand engineered, but can '
             'also be learned with gradient-based methods from labeled data. '
             'However, it is underexplored what factors make the prompts '
             'effective, especially when the prompts are natural language. In '
             'this paper, we investigate common attributes shared by effective '
             'prompts. We first propose a human readable prompt tuning method '
             '(F LUENT P ROMPT) based on Langevin dynamics that incorporates a '
             'fluency constraint to find a diverse distribution of effective '
             'and fluent prompts. Our analysis reveals that effective prompts '
             'are topically related to the task domain and calibrate the prior '
             'probability of label words. Based on these findings, we also '
             'propose a method for generating prompts using only unlabeled '
             'data, outperforming strong baselines by an average of 7.0% '
             'accuracy across three tasks.',
  'title': "Toward Human Readable Prompt Tuning: Kubrick's The Shining is a "
           'good movie, and a good prompt too?',
  'link': 'http://arxiv.org/abs/2212.10539v1',
  'engines': ['arxiv'],
  'category': 'science'},
 {'snippet': 'Prevailing methods for mapping large generative language models '
             "to supervised tasks may fail to sufficiently probe models' novel "
             'capabilities. Using GPT-3 as a case study, we show that 0-shot '
             'prompts can significantly outperform few-shot prompts. We '
             'suggest that the function of few-shot examples in these cases is '
             'better described as locating an already learned task rather than '


'better described as locating an already learned task rather than '
             'meta-learning. This analysis motivates rethinking the role of '
             'prompts in controlling and evaluating powerful language models. '
             'In this work, we discuss methods of prompt programming, '
             'emphasizing the usefulness of considering prompts through the '
             'lens of natural language. We explore techniques for exploiting '
             'the capacity of narratives and cultural anchors to encode '
             'nuanced intentions and techniques for encouraging deconstruction '
             'of a problem into components before producing a verdict. '
             'Informed by this more encompassing theory of prompt programming, '
             'we also introduce the idea of a metaprompt that seeds the model '
             'to generate its own natural language prompts for a range of '
             'tasks. Finally, we discuss how these more general methods of '
             'interacting with language models can be incorporated into '
             'existing and future benchmarks and practical applications.',
  'title': 'Prompt Programming for Large Language Models: Beyond the Few-Shot '
           'Paradigm',
  'link': 'http://arxiv.org/abs/2102.07350v1',
  'engines': ['arxiv'],
  'category': 'science'}]
In this example we query for large language models under the it category. We then filter the results that come from github.
results = search.results("large language model", num_results = 20, categories='it')
pprint.pp(list(filter(lambda r: r['engines'][0] == 'github', results)))
[{'snippet': 'Guide to using pre-trained large language models of source code',
  'title': 'Code-LMs',
  'link': 'https://github.com/VHellendoorn/Code-LMs',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Dramatron uses large language models to generate coherent '
             'scripts and screenplays.',
  'title': 'dramatron',
  'link': 'https://github.com/deepmind/dramatron',
  'engines': ['github'],
  'category': 'it'}]
We could also directly query for results from github and other source forges.
results = search.results("large language model", num_results = 20, engines=['github', 'gitlab'])
pprint.pp(results)
[{'snippet': "Implementation of 'A Watermark for Large Language Models' paper "
             'by Kirchenbauer & Geiping et. al.',
  'title': 'Peutlefaire / LMWatermark',
  'link': 'https://gitlab.com/BrianPulfer/LMWatermark',
  'engines': ['gitlab'],
  'category': 'it'},
 {'snippet': 'Guide to using pre-trained large language models of source code',
  'title': 'Code-LMs',
  'link': 'https://github.com/VHellendoorn/Code-LMs',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': '',
  'title': 'Simen Burud / Large-scale Language Models for Conversational '
           'Speech Recognition',
  'link': 'https://gitlab.com/BrianPulfer',
  'engines': ['gitlab'],
  'category': 'it'},
 {'snippet': 'Dramatron uses large language models to generate coherent '
             'scripts and screenplays.',
  'title': 'dramatron',
  'link': 'https://github.com/deepmind/dramatron',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Code for loralib, an implementation of "LoRA: Low-Rank '
             'Adaptation of Large Language Models"',
  'title': 'LoRA',
  'link': 'https://github.com/microsoft/LoRA',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Code for the paper "Evaluating Large Language Models Trained on '
             'Code"',
  'title': 'human-eval',
  'link': 'https://github.com/openai/human-eval',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'A trend starts from "Chain of Thought Prompting Elicits '
             'Reasoning in Large Language Models".',


'Reasoning in Large Language Models".',
  'title': 'Chain-of-ThoughtsPapers',
  'link': 'https://github.com/Timothyxxx/Chain-of-ThoughtsPapers',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Mistral: A strong, northwesterly wind: Framework for transparent '
             'and accessible large-scale language model training, built with '
             'Hugging Face 🤗 Transformers.',
  'title': 'mistral',
  'link': 'https://github.com/stanford-crfm/mistral',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'A prize for finding tasks that cause large language models to '
             'show inverse scaling',
  'title': 'prize',
  'link': 'https://github.com/inverse-scaling/prize',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Optimus: the first large-scale pre-trained VAE language model',
  'title': 'Optimus',
  'link': 'https://github.com/ChunyuanLI/Optimus',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Seminar on Large Language Models (COMP790-101 at UNC Chapel '
             'Hill, Fall 2022)',
  'title': 'llm-seminar',
  'link': 'https://github.com/craffel/llm-seminar',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'A central, open resource for data and tools related to '
             'chain-of-thought reasoning in large language models. Developed @ '
             'Samwald research group: https://samwald.info/',
  'title': 'ThoughtSource',
  'link': 'https://github.com/OpenBioLink/ThoughtSource',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'A comprehensive list of papers using large language/multi-modal '
             'models for Robotics/RL, including papers, codes, and related '
             'websites',
  'title': 'Awesome-LLM-Robotics',
  'link': 'https://github.com/GT-RIPL/Awesome-LLM-Robotics',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Tools for curating biomedical training data for large-scale '
             'language modeling',
  'title': 'biomedical',
  'link': 'https://github.com/bigscience-workshop/biomedical',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'ChatGPT @ Home: Large Language Model (LLM) chatbot application, '
             'written by ChatGPT',
  'title': 'ChatGPT-at-Home',
  'link': 'https://github.com/Sentdex/ChatGPT-at-Home',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Design and Deploy Large Language Model Apps',
  'title': 'dust',
  'link': 'https://github.com/dust-tt/dust',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Polyglot: Large Language Models of Well-balanced Competence in '
             'Multi-languages',
  'title': 'polyglot',
  'link': 'https://github.com/EleutherAI/polyglot',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'Code release for "Learning Video Representations from Large '
             'Language Models"',
  'title': 'LaViLa',
  'link': 'https://github.com/facebookresearch/LaViLa',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'SmoothQuant: Accurate and Efficient Post-Training Quantization '
             'for Large Language Models',
  'title': 'smoothquant',
  'link': 'https://github.com/mit-han-lab/smoothquant',
  'engines': ['github'],
  'category': 'it'},
 {'snippet': 'This repository contains the code, data, and models of the paper '


{'snippet': 'This repository contains the code, data, and models of the paper '
             'titled "XL-Sum: Large-Scale Multilingual Abstractive '
             'Summarization for 44 Languages" published in Findings of the '
             'Association for Computational Linguistics: ACL-IJCNLP 2021.',
  'title': 'xl-sum',
  'link': 'https://github.com/csebuetnlp/xl-sum',
  'engines': ['github'],
  'category': 'it'}]
previous
Search Tools
next
SerpAPI
 Contents

Custom Parameters
Obtaining results with metadata
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
DuckDuckGo Search
DuckDuckGo Search#
This notebook goes over how to use the duck-duck-go search component.
# !pip install duckduckgo-search
from langchain.tools import DuckDuckGoSearchRun
search = DuckDuckGoSearchRun()
search.run("Obama's first name?")
'Barack Obama, in full Barack Hussein Obama II, (born August 4, 1961, Honolulu, Hawaii, U.S.), 44th president of the United States (2009-17) and the first African American to hold the office. Before winning the presidency, Obama represented Illinois in the U.S. Senate (2005-08). Barack Hussein Obama II (/ b ə ˈ r ɑː k h uː ˈ s eɪ n oʊ ˈ b ɑː m ə / bə-RAHK hoo-SAYN oh-BAH-mə; born August 4, 1961) is an American former politician who served as the 44th president of the United States from 2009 to 2017. A member of the Democratic Party, he was the first African-American president of the United States. Obama previously served as a U.S. senator representing ... Barack Obama was the first African American president of the United States (2009-17). He oversaw the recovery of the U.S. economy (from the Great Recession of 2008-09) and the enactment of landmark health care reform (the Patient Protection and Affordable Care Act ). In 2009 he was awarded the Nobel Peace Prize. His birth certificate lists his first name as Barack: That\'s how Obama has spelled his name throughout his life. His name derives from a Hebrew name which means "lightning.". The Hebrew word has been transliterated into English in various spellings, including Barak, Buraq, Burack, and Barack. Most common names of U.S. presidents 1789-2021. Published by. Aaron O\'Neill , Jun 21, 2022. The most common first name for a U.S. president is James, followed by John and then William. Six U.S ...'
previous
ChatGPT Plugins
next
File System Tools
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
Apify
Apify#
This notebook shows how to use the Apify integration for LangChain.
Apify is a cloud platform for web scraping and data extraction,
which provides an ecosystem of more than a thousand
ready-made apps called Actors for various web scraping, crawling, and data extraction use cases.
For example, you can use it to extract Google Search results, Instagram and Facebook profiles, products from Amazon or Shopify, Google Maps reviews, etc. etc.
In this example, we’ll use the Website Content Crawler Actor,
which can deeply crawl websites such as documentation, knowledge bases, help centers, or blogs,
and extract text content from the web pages. Then we feed the documents into a vector index and answer questions from it.
#!pip install apify-client
First, import ApifyWrapper into your source code:
from langchain.document_loaders.base import Document
from langchain.indexes import VectorstoreIndexCreator
from langchain.utilities import ApifyWrapper
Initialize it using your Apify API token and for the purpose of this example, also with your OpenAI API key:
import os
os.environ["OPENAI_API_KEY"] = "Your OpenAI API key"
os.environ["APIFY_API_TOKEN"] = "Your Apify API token"
apify = ApifyWrapper()
Then run the Actor, wait for it to finish, and fetch its results from the Apify dataset into a LangChain document loader.
Note that if you already have some results in an Apify dataset, you can load them directly using ApifyDatasetLoader, as shown in this notebook. In that notebook, you’ll also find the explanation of the dataset_mapping_function, which is used to map fields from the Apify dataset records to LangChain Document fields.
loader = apify.call_actor(
    actor_id="apify/website-content-crawler",
    run_input={"startUrls": [{"url": "https://python.langchain.com/en/latest/"}]},
    dataset_mapping_function=lambda item: Document(
        page_content=item["text"] or "", metadata={"source": item["url"]}
    ),
)
Initialize the vector index from the crawled documents:
index = VectorstoreIndexCreator().from_loaders([loader])
And finally, query the vector index:
query = "What is LangChain?"
result = index.query_with_sources(query)
print(result["answer"])
print(result["sources"])
 LangChain is a standard interface through which you can interact with a variety of large language models (LLMs). It provides modules that can be used to build language model applications, and it also provides chains and agents with memory capabilities.
https://python.langchain.com/en/latest/modules/models/llms.html, https://python.langchain.com/en/latest/getting_started/getting_started.html
previous
Tool Input Schema
next
ArXiv API Tool
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
YouTubeSearchTool
YouTubeSearchTool#
This notebook shows how to use a tool to search YouTube
Adapted from venuv/langchain_yt_tools
#! pip install youtube_search
from langchain.tools import YouTubeSearchTool
tool = YouTubeSearchTool()
tool.run("lex friedman")
"['/watch?v=VcVfceTsD0A&pp=ygUMbGV4IGZyaWVkbWFu', '/watch?v=gPfriiHBBek&pp=ygUMbGV4IGZyaWVkbWFu']"
You can also specify the number of results that are returned
tool.run("lex friedman,5")
"['/watch?v=VcVfceTsD0A&pp=ygUMbGV4IGZyaWVkbWFu', '/watch?v=YVJ8gTnDC4Y&pp=ygUMbGV4IGZyaWVkbWFu', '/watch?v=Udh22kuLebg&pp=ygUMbGV4IGZyaWVkbWFu', '/watch?v=gPfriiHBBek&pp=ygUMbGV4IGZyaWVkbWFu', '/watch?v=L_Guz73e6fw&pp=ygUMbGV4IGZyaWVkbWFu']"
previous
Wolfram Alpha
next
Zapier Natural Language Actions API
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
How to use the async API for Agents
 Contents
Serial vs. Concurrent Execution
How to use the async API for Agents#
LangChain provides async support for Agents by leveraging the asyncio library.
Async methods are currently supported for the following Tools: GoogleSerperAPIWrapper, SerpAPIWrapper and LLMMathChain. Async support for other agent tools are on the roadmap.
For Tools that have a coroutine implemented (the three mentioned above), the AgentExecutor will await them directly. Otherwise, the AgentExecutor will call the Tool’s func via asyncio.get_event_loop().run_in_executor to avoid blocking the main runloop.
You can use arun to call an AgentExecutor asynchronously.
Serial vs. Concurrent Execution#
In this example, we kick off agents to answer some questions serially vs. concurrently. You can see that concurrent execution significantly speeds this up.
import asyncio
import time
from langchain.agents import initialize_agent, load_tools
from langchain.agents import AgentType
from langchain.llms import OpenAI
from langchain.callbacks.stdout import StdOutCallbackHandler
from langchain.callbacks.tracers import LangChainTracer
from aiohttp import ClientSession
questions = [
    "Who won the US Open men's final in 2019? What is his age raised to the 0.334 power?",
    "Who is Olivia Wilde's boyfriend? What is his current age raised to the 0.23 power?",
    "Who won the most recent formula 1 grand prix? What is their age raised to the 0.23 power?",
    "Who won the US Open women's final in 2019? What is her age raised to the 0.34 power?",
    "Who is Beyonce's husband? What is his age raised to the 0.19 power?"
]
llm = OpenAI(temperature=0)
tools = load_tools(["google-serper", "llm-math"], llm=llm)
agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
s = time.perf_counter()
for q in questions:
    agent.run(q)
elapsed = time.perf_counter() - s
print(f"Serial executed in {elapsed:0.2f} seconds.")
> Entering new AgentExecutor chain...
 I need to find out who won the US Open men's final in 2019 and then calculate his age raised to the 0.334 power.
Action: Google Serper
Action Input: "Who won the US Open men's final in 2019?"


Action Input: "Who won the US Open men's final in 2019?"
Observation: Rafael Nadal defeated Daniil Medvedev in the final, 7–5, 6–3, 5–7, 4–6, 6–4 to win the men's singles tennis title at the 2019 US Open. It was his fourth US ... Draw: 128 (16 Q / 8 WC). Champion: Rafael Nadal. Runner-up: Daniil Medvedev. Score: 7–5, 6–3, 5–7, 4–6, 6–4. Bianca Andreescu won the women's singles title, defeating Serena Williams in straight sets in the final, becoming the first Canadian to win a Grand Slam singles ... Rafael Nadal won his 19th career Grand Slam title, and his fourth US Open crown, by surviving an all-time comback effort from Daniil ... Rafael Nadal beats Daniil Medvedev in US Open final to claim 19th major title. World No2 claims 7-5, 6-3, 5-7, 4-6, 6-4 victory over Russian ... Rafael Nadal defeated Daniil Medvedev in the men's singles final of the U.S. Open on Sunday. Rafael Nadal survived. The 33-year-old defeated Daniil Medvedev in the final of the 2019 U.S. Open to earn his 19th Grand Slam title Sunday ... NEW YORK -- Rafael Nadal defeated Daniil Medvedev in an epic five-set match, 7-5, 6-3, 5-7, 4-6, 6-4 to win the men's singles title at the ... Nadal previously won the U.S. Open three times, most recently in 2017. Ahead of the match, Nadal said he was “super happy to be back in the ... Watch the full match between Daniil Medvedev and Rafael ... Duration: 4:47:32. Posted: Mar 20, 2020. US Open 2019: Rafael Nadal beats Daniil Medvedev · Updated: Sep. 08, 2019, 11:11 p.m. |; Published: Sep · Published: Sep. 08, 2019, 10:06 p.m.. 26. US Open ...
Thought: I now know that Rafael Nadal won the US Open men's final in 2019 and he is 33 years old.
Action: Calculator
Action Input: 33^0.334
Observation: Answer: 3.215019829667466
Thought: I now know the final answer.
Final Answer: Rafael Nadal won the US Open men's final in 2019 and his age raised to the 0.334 power is 3.215019829667466.
> Finished chain.
> Entering new AgentExecutor chain...
 I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
Action: Google Serper
Action Input: "Olivia Wilde boyfriend"
Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.
Thought: I need to find out Harry Styles' age.
Action: Google Serper
Action Input: "Harry Styles age"
Observation: 29 years
Thought: I need to calculate 29 raised to the 0.23 power.
Action: Calculator
Action Input: 29^0.23
Observation: Answer: 2.169459462491557
Thought: I now know the final answer.
Final Answer: Harry Styles is Olivia Wilde's boyfriend and his current age raised to the 0.23 power is 2.169459462491557.
> Finished chain.
> Entering new AgentExecutor chain...
 I need to find out who won the most recent grand prix and then calculate their age raised to the 0.23 power.
Action: Google Serper
Action Input: "who won the most recent formula 1 grand prix"
Observation: Max Verstappen won his first Formula 1 world title on Sunday after the championship was decided by a last-lap overtake of his rival Lewis Hamilton in the Abu Dhabi Grand Prix. Dec 12, 2021
Thought: I need to find out Max Verstappen's age
Action: Google Serper


Thought: I need to find out Max Verstappen's age
Action: Google Serper
Action Input: "Max Verstappen age"
Observation: 25 years
Thought: I need to calculate 25 raised to the 0.23 power
Action: Calculator
Action Input: 25^0.23
Observation: Answer: 2.096651272316035
Thought: I now know the final answer
Final Answer: Max Verstappen, aged 25, won the most recent Formula 1 grand prix and his age raised to the 0.23 power is 2.096651272316035.
> Finished chain.
> Entering new AgentExecutor chain...
 I need to find out who won the US Open women's final in 2019 and then calculate her age raised to the 0.34 power.
Action: Google Serper
Action Input: "US Open women's final 2019 winner"
Observation: WHAT HAPPENED: #SheTheNorth? She the champion. Nineteen-year-old Canadian Bianca Andreescu sealed her first Grand Slam title on Saturday, downing 23-time major champion Serena Williams in the 2019 US Open women's singles final, 6-3, 7-5. Sep 7, 2019
Thought: I now need to calculate her age raised to the 0.34 power.
Action: Calculator
Action Input: 19^0.34
Observation: Answer: 2.7212987634680084
Thought: I now know the final answer.
Final Answer: Nineteen-year-old Canadian Bianca Andreescu won the US Open women's final in 2019 and her age raised to the 0.34 power is 2.7212987634680084.
> Finished chain.
> Entering new AgentExecutor chain...
 I need to find out who Beyonce's husband is and then calculate his age raised to the 0.19 power.
Action: Google Serper
Action Input: "Who is Beyonce's husband?"
Observation: Jay-Z
Thought: I need to find out Jay-Z's age
Action: Google Serper
Action Input: "How old is Jay-Z?"
Observation: 53 years
Thought: I need to calculate 53 raised to the 0.19 power
Action: Calculator
Action Input: 53^0.19
Observation: Answer: 2.12624064206896
Thought: I now know the final answer
Final Answer: Jay-Z is Beyonce's husband and his age raised to the 0.19 power is 2.12624064206896.
> Finished chain.
Serial executed in 89.97 seconds.
llm = OpenAI(temperature=0)
tools = load_tools(["google-serper","llm-math"], llm=llm)
agent = initialize_agent(
    tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True
)
s = time.perf_counter()
# If running this outside of Jupyter, use asyncio.run or loop.run_until_complete
tasks = [agent.arun(q) for q in questions]
await asyncio.gather(*tasks)
elapsed = time.perf_counter() - s
print(f"Concurrent executed in {elapsed:0.2f} seconds.")
> Entering new AgentExecutor chain...
> Entering new AgentExecutor chain...
> Entering new AgentExecutor chain...
> Entering new AgentExecutor chain...
> Entering new AgentExecutor chain...
 I need to find out who Olivia Wilde's boyfriend is and then calculate his age raised to the 0.23 power.
Action: Google Serper
Action Input: "Olivia Wilde boyfriend" I need to find out who Beyonce's husband is and then calculate his age raised to the 0.19 power.
Action: Google Serper
Action Input: "Who is Beyonce's husband?" I need to find out who won the most recent formula 1 grand prix and then calculate their age raised to the 0.23 power.
Action: Google Serper
Action Input: "most recent formula 1 grand prix winner" I need to find out who won the US Open men's final in 2019 and then calculate his age raised to the 0.334 power.
Action: Google Serper
Action Input: "Who won the US Open men's final in 2019?" I need to find out who won the US Open women's final in 2019 and then calculate her age raised to the 0.34 power.
Action: Google Serper


Action: Google Serper
Action Input: "US Open women's final 2019 winner"
Observation: Sudeikis and Wilde's relationship ended in November 2020. Wilde was publicly served with court documents regarding child custody while she was presenting Don't Worry Darling at CinemaCon 2022. In January 2021, Wilde began dating singer Harry Styles after meeting during the filming of Don't Worry Darling.
Thought:
Observation: Jay-Z
Thought:
Observation: Rafael Nadal defeated Daniil Medvedev in the final, 7–5, 6–3, 5–7, 4–6, 6–4 to win the men's singles tennis title at the 2019 US Open. It was his fourth US ... Draw: 128 (16 Q / 8 WC). Champion: Rafael Nadal. Runner-up: Daniil Medvedev. Score: 7–5, 6–3, 5–7, 4–6, 6–4. Bianca Andreescu won the women's singles title, defeating Serena Williams in straight sets in the final, becoming the first Canadian to win a Grand Slam singles ... Rafael Nadal won his 19th career Grand Slam title, and his fourth US Open crown, by surviving an all-time comback effort from Daniil ... Rafael Nadal beats Daniil Medvedev in US Open final to claim 19th major title. World No2 claims 7-5, 6-3, 5-7, 4-6, 6-4 victory over Russian ... Rafael Nadal defeated Daniil Medvedev in the men's singles final of the U.S. Open on Sunday. Rafael Nadal survived. The 33-year-old defeated Daniil Medvedev in the final of the 2019 U.S. Open to earn his 19th Grand Slam title Sunday ... NEW YORK -- Rafael Nadal defeated Daniil Medvedev in an epic five-set match, 7-5, 6-3, 5-7, 4-6, 6-4 to win the men's singles title at the ... Nadal previously won the U.S. Open three times, most recently in 2017. Ahead of the match, Nadal said he was “super happy to be back in the ... Watch the full match between Daniil Medvedev and Rafael ... Duration: 4:47:32. Posted: Mar 20, 2020. US Open 2019: Rafael Nadal beats Daniil Medvedev · Updated: Sep. 08, 2019, 11:11 p.m. |; Published: Sep · Published: Sep. 08, 2019, 10:06 p.m.. 26. US Open ...
Thought:
Observation: WHAT HAPPENED: #SheTheNorth? She the champion. Nineteen-year-old Canadian Bianca Andreescu sealed her first Grand Slam title on Saturday, downing 23-time major champion Serena Williams in the 2019 US Open women's singles final, 6-3, 7-5. Sep 7, 2019
Thought:


Thought:
Observation: Lewis Hamilton holds the record for the most race wins in Formula One history, with 103 wins to date. Michael Schumacher, the previous record holder, ... Michael Schumacher (top left) and Lewis Hamilton (top right) have each won the championship a record seven times during their careers, while Sebastian Vettel ( ... Grand Prix, Date, Winner, Car, Laps, Time. Bahrain, 05 Mar 2023, Max Verstappen VER, Red Bull Racing Honda RBPT, 57, 1:33:56.736. Saudi Arabia, 19 Mar 2023 ... The Red Bull driver Max Verstappen of the Netherlands celebrated winning his first Formula 1 world title at the Abu Dhabi Grand Prix. Perez wins sprint as Verstappen, Russell clash. Red Bull's Sergio Perez won the first sprint of the 2023 Formula One season after catching and passing Charles ... The most successful driver in the history of F1 is Lewis Hamilton. The man from Stevenage has won 103 Grands Prix throughout his illustrious career and is still ... Lewis Hamilton: 103. Max Verstappen: 37. Michael Schumacher: 91. Fernando Alonso: 32. Max Verstappen and Sergio Perez will race in a very different-looking Red Bull this weekend after the team unveiled a striking special livery for the Miami GP. Lewis Hamilton holds the record of most victories with 103, ahead of Michael Schumacher (91) and Sebastian Vettel (53). Schumacher also holds the record for the ... Lewis Hamilton holds the record for the most race wins in Formula One history, with 103 wins to date. Michael Schumacher, the previous record holder, is second ...
Thought: I need to find out Harry Styles' age.
Action: Google Serper
Action Input: "Harry Styles age" I need to find out Jay-Z's age
Action: Google Serper
Action Input: "How old is Jay-Z?" I now know that Rafael Nadal won the US Open men's final in 2019 and he is 33 years old.
Action: Calculator
Action Input: 33^0.334 I now need to calculate her age raised to the 0.34 power.
Action: Calculator
Action Input: 19^0.34
Observation: 29 years
Thought:
Observation: 53 years
Thought: Max Verstappen won the most recent Formula 1 grand prix.
Action: Calculator
Action Input: Max Verstappen's age (23) raised to the 0.23 power
Observation: Answer: 2.7212987634680084
Thought:
Observation: Answer: 3.215019829667466
Thought: I need to calculate 29 raised to the 0.23 power.
Action: Calculator
Action Input: 29^0.23 I need to calculate 53 raised to the 0.19 power
Action: Calculator
Action Input: 53^0.19
Observation: Answer: 2.0568252837687546
Thought:
Observation: Answer: 2.169459462491557
Thought:
> Finished chain.
> Finished chain.
Observation: Answer: 2.12624064206896
Thought:
> Finished chain.
> Finished chain.
> Finished chain.
Concurrent executed in 17.52 seconds.
previous
How to combine agents and vectorstores
next
How to create ChatGPT Clone
 Contents

Serial vs. Concurrent Execution
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
How to add SharedMemory to an Agent and its Tools
How to add SharedMemory to an Agent and its Tools#
This notebook goes over adding memory to both of an Agent and its tools. Before going through this notebook, please walk through the following notebooks, as this will build on top of both of them:
Adding memory to an LLM Chain
Custom Agents
We are going to create a custom Agent. The agent has access to a conversation memory, search tool, and a summarization tool. And, the summarization tool also needs access to the conversation memory.
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain.memory import ConversationBufferMemory, ReadOnlySharedMemory
from langchain import OpenAI, LLMChain, PromptTemplate
from langchain.utilities import GoogleSearchAPIWrapper
template = """This is a conversation between a human and a bot:
{chat_history}
Write a summary of the conversation for {input}:
"""
prompt = PromptTemplate(
    input_variables=["input", "chat_history"],
    template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")
readonlymemory = ReadOnlySharedMemory(memory=memory)
summry_chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    verbose=True,
    memory=readonlymemory, # use the read-only memory to prevent the tool from modifying the memory
)
search = GoogleSearchAPIWrapper()
tools = [
    Tool(
        name = "Search",
        func=search.run,
        description="useful for when you need to answer questions about current events"
    ),
    Tool(
        name = "Summary",
        func=summry_chain.run,
        description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary."
    )
]
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"
{chat_history}
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"]
)
We can now construct the LLMChain, with the Memory object, and then create the agent.
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)
agent_chain.run(input="What is ChatGPT?")
> Entering new AgentExecutor chain...
Thought: I should research ChatGPT to answer this question.
Action: Search
Action Input: "ChatGPT"


Action: Search
Action Input: "ChatGPT"
Observation: Nov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...
Thought: I now know the final answer.
Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
> Finished chain.
"ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting."
To test the memory of this agent, we can ask a followup question that relies on information in the previous exchange to be answered correctly.
agent_chain.run(input="Who developed it?")
> Entering new AgentExecutor chain...
Thought: I need to find out who developed ChatGPT
Action: Search
Action Input: Who developed ChatGPT
Observation: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...
Thought: I now know the final answer


Thought: I now know the final answer
Final Answer: ChatGPT was developed by OpenAI.
> Finished chain.
'ChatGPT was developed by OpenAI.'
agent_chain.run(input="Thanks. Summarize the conversation, for my daughter 5 years old.")
> Entering new AgentExecutor chain...
Thought: I need to simplify the conversation for a 5 year old.
Action: Summary
Action Input: My daughter 5 years old
> Entering new LLMChain chain...
Prompt after formatting:
This is a conversation between a human and a bot:
Human: What is ChatGPT?
AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
Human: Who developed it?
AI: ChatGPT was developed by OpenAI.
Write a summary of the conversation for My daughter 5 years old:
> Finished chain.
Observation:
The conversation was about ChatGPT, an artificial intelligence chatbot. It was created by OpenAI and can send and receive images while chatting.
Thought: I now know the final answer.
Final Answer: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.
> Finished chain.
'ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.'
Confirm that the memory was correctly updated.
print(agent_chain.memory.buffer)
Human: What is ChatGPT?
AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
Human: Who developed it?
AI: ChatGPT was developed by OpenAI.
Human: Thanks. Summarize the conversation, for my daughter 5 years old.
AI: ChatGPT is an artificial intelligence chatbot created by OpenAI that can send and receive images while chatting.
For comparison, below is a bad example that uses the same memory for both the Agent and the tool.
## This is a bad practice for using the memory.
## Use the ReadOnlySharedMemory class, as shown above.
template = """This is a conversation between a human and a bot:
{chat_history}
Write a summary of the conversation for {input}:
"""
prompt = PromptTemplate(
    input_variables=["input", "chat_history"],
    template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")
summry_chain = LLMChain(
    llm=OpenAI(),
    prompt=prompt,
    verbose=True,
    memory=memory,  # <--- this is the only change
)
search = GoogleSearchAPIWrapper()
tools = [
    Tool(
        name = "Search",
        func=search.run,
        description="useful for when you need to answer questions about current events"
    ),
    Tool(
        name = "Summary",
        func=summry_chain.run,
        description="useful for when you summarize a conversation. The input to this tool should be a string, representing who will read this summary."
    )
]
prefix = """Have a conversation with a human, answering the following questions as best you can. You have access to the following tools:"""
suffix = """Begin!"
{chat_history}
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
    tools,
    prefix=prefix,
    suffix=suffix,
    input_variables=["input", "chat_history", "agent_scratchpad"]
)
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools, verbose=True)
agent_chain = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True, memory=memory)
agent_chain.run(input="What is ChatGPT?")
> Entering new AgentExecutor chain...
Thought: I should research ChatGPT to answer this question.
Action: Search
Action Input: "ChatGPT"


Action: Search
Action Input: "ChatGPT"
Observation: Nov 30, 2022 ... We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... ChatGPT. We've trained a model called ChatGPT which interacts in a conversational way. The dialogue format makes it possible for ChatGPT to answer ... Feb 2, 2023 ... ChatGPT, the popular chatbot from OpenAI, is estimated to have reached 100 million monthly active users in January, just two months after ... 2 days ago ... ChatGPT recently launched a new version of its own plagiarism detection tool, with hopes that it will squelch some of the criticism around how ... An API for accessing new AI models developed by OpenAI. Feb 19, 2023 ... ChatGPT is an AI chatbot system that OpenAI released in November to show off and test what a very large, powerful AI system can accomplish. You ... ChatGPT is fine-tuned from GPT-3.5, a language model trained to produce text. ChatGPT was optimized for dialogue by using Reinforcement Learning with Human ... 3 days ago ... Visual ChatGPT connects ChatGPT and a series of Visual Foundation Models to enable sending and receiving images during chatting. Dec 1, 2022 ... ChatGPT is a natural language processing tool driven by AI technology that allows you to have human-like conversations and much more with a ...
Thought: I now know the final answer.
Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
> Finished chain.
"ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting."
agent_chain.run(input="Who developed it?")
> Entering new AgentExecutor chain...
Thought: I need to find out who developed ChatGPT
Action: Search
Action Input: Who developed ChatGPT
Observation: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large ... Feb 15, 2023 ... Who owns Chat GPT? Chat GPT is owned and developed by AI research and deployment company, OpenAI. The organization is headquartered in San ... Feb 8, 2023 ... ChatGPT is an AI chatbot developed by San Francisco-based startup OpenAI. OpenAI was co-founded in 2015 by Elon Musk and Sam Altman and is ... Dec 7, 2022 ... ChatGPT is an AI chatbot designed and developed by OpenAI. The bot works by generating text responses based on human-user input, like questions ... Jan 12, 2023 ... In 2019, Microsoft invested $1 billion in OpenAI, the tiny San Francisco company that designed ChatGPT. And in the years since, it has quietly ... Jan 25, 2023 ... The inside story of ChatGPT: How OpenAI founder Sam Altman built the world's hottest technology with billions from Microsoft. Dec 3, 2022 ... ChatGPT went viral on social media for its ability to do anything from code to write essays. · The company that created the AI chatbot has a ... Jan 17, 2023 ... While many Americans were nursing hangovers on New Year's Day, 22-year-old Edward Tian was working feverishly on a new app to combat misuse ... ChatGPT is a language model created by OpenAI, an artificial intelligence research laboratory consisting of a team of researchers and engineers focused on ... 1 day ago ... Everyone is talking about ChatGPT, developed by OpenAI. This is such a great tool that has helped to make AI more accessible to a wider ...
Thought: I now know the final answer
Final Answer: ChatGPT was developed by OpenAI.
> Finished chain.
'ChatGPT was developed by OpenAI.'


> Finished chain.
'ChatGPT was developed by OpenAI.'
agent_chain.run(input="Thanks. Summarize the conversation, for my daughter 5 years old.")
> Entering new AgentExecutor chain...
Thought: I need to simplify the conversation for a 5 year old.
Action: Summary
Action Input: My daughter 5 years old
> Entering new LLMChain chain...
Prompt after formatting:
This is a conversation between a human and a bot:
Human: What is ChatGPT?
AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
Human: Who developed it?
AI: ChatGPT was developed by OpenAI.
Write a summary of the conversation for My daughter 5 years old:
> Finished chain.
Observation:
The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.
Thought: I now know the final answer.
Final Answer: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.
> Finished chain.
'ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.'
The final answer is not wrong, but we see the 3rd Human input is actually from the agent in the memory because the memory was modified by the summary tool.
print(agent_chain.memory.buffer)
Human: What is ChatGPT?
AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI and launched in November 2022. It is built on top of OpenAI's GPT-3 family of large language models and is optimized for dialogue by using Reinforcement Learning with Human-in-the-Loop. It is also capable of sending and receiving images during chatting.
Human: Who developed it?
AI: ChatGPT was developed by OpenAI.
Human: My daughter 5 years old
AI:
The conversation was about ChatGPT, an artificial intelligence chatbot developed by OpenAI. It is designed to have conversations with humans and can also send and receive images.
Human: Thanks. Summarize the conversation, for my daughter 5 years old.
AI: ChatGPT is an artificial intelligence chatbot developed by OpenAI that can have conversations with humans and send and receive images.
previous
How to use a timeout for the agent
next
Plan and Execute
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
How to access intermediate steps
How to access intermediate steps#
In order to get more visibility into what an agent is doing, we can also return intermediate steps. This comes in the form of an extra key in the return value, which is a list of (action, observation) tuples.
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import OpenAI
Initialize the components needed for the agent.
llm = OpenAI(temperature=0, model_name='text-davinci-002')
tools = load_tools(["serpapi", "llm-math"], llm=llm)
Initialize the agent with return_intermediate_steps=True
agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True, return_intermediate_steps=True)
response = agent({"input":"Who is Leo DiCaprio's girlfriend? What is her current age raised to the 0.43 power?"})
> Entering new AgentExecutor chain...
 I should look up who Leo DiCaprio is dating
Action: Search
Action Input: "Leo DiCaprio girlfriend"
Observation: Camila Morrone
Thought: I should look up how old Camila Morrone is
Action: Search
Action Input: "Camila Morrone age"
Observation: 25 years
Thought: I should calculate what 25 years raised to the 0.43 power is
Action: Calculator
Action Input: 25^0.43
Observation: Answer: 3.991298452658078
Thought: I now know the final answer
Final Answer: Camila Morrone is Leo DiCaprio's girlfriend and she is 3.991298452658078 years old.
> Finished chain.
# The actual return type is a NamedTuple for the agent action, and then an observation
print(response["intermediate_steps"])
[(AgentAction(tool='Search', tool_input='Leo DiCaprio girlfriend', log=' I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: "Leo DiCaprio girlfriend"'), 'Camila Morrone'), (AgentAction(tool='Search', tool_input='Camila Morrone age', log=' I should look up how old Camila Morrone is\nAction: Search\nAction Input: "Camila Morrone age"'), '25 years'), (AgentAction(tool='Calculator', tool_input='25^0.43', log=' I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43'), 'Answer: 3.991298452658078\n')]
import json
print(json.dumps(response["intermediate_steps"], indent=2))
[
  [
    [
      "Search",
      "Leo DiCaprio girlfriend",
      " I should look up who Leo DiCaprio is dating\nAction: Search\nAction Input: \"Leo DiCaprio girlfriend\""
    ],
    "Camila Morrone"
  ],
  [
    [
      "Search",
      "Camila Morrone age",
      " I should look up how old Camila Morrone is\nAction: Search\nAction Input: \"Camila Morrone age\""
    ],
    "25 years"
  ],
  [
    [
      "Calculator",
      "25^0.43",
      " I should calculate what 25 years raised to the 0.43 power is\nAction: Calculator\nAction Input: 25^0.43"
    ],
    "Answer: 3.991298452658078\n"
  ]
]
previous
Handle Parsing Errors
next
How to cap the max number of iterations
By Harrison Chase

      © Copyright 2023, Harrison Chase.

  Last updated on Jun 08, 2023.


.ipynb
.pdf
How to create ChatGPT Clone
How to create ChatGPT Clone#
This chain replicates ChatGPT by combining (1) a specific prompt, and (2) the concept of memory.
Shows off the example as in https://www.engraved.blog/building-a-virtual-machine-inside/
from langchain import OpenAI, ConversationChain, LLMChain, PromptTemplate
from langchain.memory import ConversationBufferWindowMemory
template = """Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
{history}
Human: {human_input}
Assistant:"""
prompt = PromptTemplate(
    input_variables=["history", "human_input"],
    template=template
)
chatgpt_chain = LLMChain(
    llm=OpenAI(temperature=0),
    prompt=prompt,
    verbose=True,
    memory=ConversationBufferWindowMemory(k=2),
)
output = chatgpt_chain.predict(human_input="I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.
Assistant:
> Finished chain.
```
/home/user
```
output = chatgpt_chain.predict(human_input="ls ~")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.


Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.
AI:
```
$ pwd
/
```
Human: ls ~
Assistant:
> Finished LLMChain chain.
```
$ ls ~
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
```
output = chatgpt_chain.predict(human_input="cd ~")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: I want you to act as a Linux terminal. I will type commands and you will reply with what the terminal should show. I want you to only reply with the terminal output inside one unique code block, and nothing else. Do not write explanations. Do not type commands unless I instruct you to do so. When I need to tell you something in English I will do so by putting text inside curly brackets {like this}. My first command is pwd.
AI:
```
$ pwd
/
```
Human: ls ~
AI:
```
$ ls ~
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
```
Human: cd ~
Assistant:
> Finished LLMChain chain.

```
$ cd ~
$ pwd
/home/user
```
output = chatgpt_chain.predict(human_input="{Please make a file jokes.txt inside and put some jokes inside}")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: ls ~
AI:
```
$ ls ~


Human: ls ~
AI:
```
$ ls ~
Desktop  Documents  Downloads  Music  Pictures  Public  Templates  Videos
```
Human: cd ~
AI:
```
$ cd ~
$ pwd
/home/user
```
Human: {Please make a file jokes.txt inside and put some jokes inside}
Assistant:
> Finished LLMChain chain.
```
$ touch jokes.txt
$ echo "Why did the chicken cross the road? To get to the other side!" >> jokes.txt
$ echo "What did the fish say when it hit the wall? Dam!" >> jokes.txt
$ echo "Why did the scarecrow win the Nobel Prize? Because he was outstanding in his field!" >> jokes.txt
```
output = chatgpt_chain.predict(human_input="""echo -e "x=lambda y:y*5+3;print('Result:' + str(x(6)))" > run.py && python3 run.py""")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: cd ~
AI:
```
$ cd ~
$ pwd
/home/user
```
Human: {Please make a file jokes.txt inside and put some jokes inside}
AI:
```
$ touch jokes.txt
$ echo "Why did the chicken cross the road? To get to the other side!" >> jokes.txt
$ echo "What did the fish say when it hit the wall? Dam!" >> jokes.txt
$ echo "Why did the scarecrow win the Nobel Prize? Because he was outstanding in his field!" >> jokes.txt
```
Human: echo -e "x=lambda y:y*5+3;print('Result:' + str(x(6)))" > run.py && python3 run.py
Assistant:
> Finished LLMChain chain.
```
$ echo -e "x=lambda y:y*5+3;print('Result:' + str(x(6)))" > run.py
$ python3 run.py
Result: 33
```
output = chatgpt_chain.predict(human_input="""echo -e "print(list(filter(lambda x: all(x%d for d in range(2,x)),range(2,3**10)))[:10])" > run.py && python3 run.py""")
print(output)
> Entering new LLMChain chain...
Prompt after formatting:
Assistant is a large language model trained by OpenAI.
Assistant is designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, Assistant is able to generate human-like text based on the input it receives, allowing it to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
Assistant is constantly learning and improving, and its capabilities are constantly evolving. It is able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. Additionally, Assistant is able to generate its own text based on the input it receives, allowing it to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, Assistant is a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether you need help with a specific question or just want to have a conversation about a particular topic, Assistant is here to assist.
Human: {Please make a file jokes.txt inside and put some jokes inside}
AI:
```
$ touch jokes.txt