Agents

(Run this example in Google Colab here (opens in a new tab))

Prompts, chaining, and prompt engineering is important. However, you might not always know what chain or prompts you need to execute prior to receiving user input or new data. This is where automation and agents can help. This is an active area of development, but some very useful tooling is available.

In the following we will explore using LangChain agents (opens in a new tab) with Prediction Guard LLMs to detect and automate LLM actions.

We will use LangChain again, but we will also use a Google search API called SerpAPI. You can get a free API key for SerpAPI here (opens in a new tab).

$ pip install predictionguard langchain google-search-results
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.agents import AgentType
from langchain.llms import PredictionGuard
import os
 
 
os.environ['PREDICTIONGUARD_TOKEN'] = "<your access token>"
os.environ['SERPAPI_API_KEY'] = "<your serpapi api key>"

To setup an agent that will search the internet on-the-fly and use the LLM to generate a response:

# In LangChain, "tools" are like resources that are available to your agent to
# execute certain actions (like a Google Search) while trying to complete a
# set of tasks. An "agent" is the object that you "run" to try and get a "Final Answer."
tools = load_tools(["serpapi"], llm=PredictionGuard(model="Neural-Chat-7B"))
agent = initialize_agent(tools, PredictionGuard(model="Neural-Chat-7B"),
                         agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
 
agent.run("How are Domino's gift cards delivered?")

This will verbosely log the agents activities until it reaching a final answer and generates the response:

> Entering new AgentExecutor chain...
We need to find information about Domino's gift card delivery process
Action: Search
Action Input: Domino's gift card delivery process
Observation: ["Order gift cards & eGift cards from Domino's in electronic or plastic. Submit corporate order or check your remaining balance. Order online today!", "Log in to your Domino's Pizza account · Choose between Carryout or Delivery on the home screen · Add the desired items to your cart · Proceed to ...", "You may use your gift card online, over the phone or in-store. ▽ Can I use a card internationally? Domino's gift cards are only valid in the United ...", "You can. It would be easiest to go into the store and have them process your gift card. I've had people come in with gift cards for large ...", "We'll send your card as soon as possible. For 99% of eGifts, this takes less than 15 minutes. For a small number of orders, it can take up to 24 hours.", 'Real Scam! They provide the guarantee of delivering their pizza within 30 minutes of ordering and inform the customer at the time of order ...', "Get a Domino's Gift Card in seconds with 66 safe payment methods & instant email delivery ... Complete the checkout process safely and quickly ...", 'Delivery of Digital Shop Card is normally within 3 hours of receiving your Costco Order Confirmation email. Digital Shop Cards may take up to 24 hours in ...', "Domino's eGift cards make great last minute gifts. And they're fast and easy to send. Just pick a design. Add your own personal message. Email ...", "... gift card, which lets a person easily order online, over the phone or in-store for delivery or carry-out menu items from participating Domino's Pizza locations."]
Thought:We have gathered various information about Domino's gift card delivery process
Final Answer: Domino's gift cards can be delivered electronically or physically, and can be used for online, phone, or in-store orders. They can be sent as eGift cards or physical cards, and can be used for both delivery and carryout orders.

> Finished chain.
Domino's gift cards can be delivered electronically or physically, and can be used for online, phone, or in-store orders. They can be sent as eGift cards or physical cards, and can be used for both delivery and carryout orders.