Human-In-The-Loop LLM Agents

Large Language Model Based Agents excel at decomposing a complex question into a chain-of-thought sequence.

Cobus Greyling
5 min readApr 27, 2023

--

The principle behind chain-of-thought prompting is to follow a few-shot learning approach where a LLM is given a few examples on how to decompose a complex and ambiguous question.

And by doing so, establishing a chain-of-thought process. For instance, the question Who is regarded as the father of the iPhone and what is the square root of his year of birth? can only be correctly answered by decomposing the question in sequential chain-of-thought steps.

Below is the decomposed chain-of-thought reasoning of the Agent based on the aforementioned question:

⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️

The agent can ben armed with an extensive array of tools to use in order to answer the question. And one of those tools, or actions can be a Human, hence the agent follows a Human-In-The-Loop approach or as some referred to it, Human as a Tool.

Something I find interesting is that the agent recognises that a chain in the sequence of chains requires specific knowledge, and hence prompts a human for input.

The Human-In-The-Loop Agent is asked the question: In what month is Cobus Greyling’s birthday? The Agent should understand that this is very specific knowledge and should be retrieved from a human.

The two questions posed by the Agent to the human:

Do you know when Cobus Greyling’s birthday is?

What is the name of the fourth month?

And the Agents reasoning and chain of thought:

Here is the full working code:

pip install langchain
pip install google-search-results
pip install openai

import os
os.environ['OPENAI_API_KEY'] = str("xxxxxxxxxx")
os.environ["SERPAPI_API_KEY"] = str("xxxxxxxxxx")
llm = OpenAI(temperature=0,model_name='gpt-4-0314')

import sys
from langchain.chat_models import ChatOpenAI
from langchain.llms import OpenAI
from langchain.agents import load_tools, initialize_agent
from langchain.agents import AgentType

llm = ChatOpenAI(temperature=0.0)
math_llm = OpenAI(temperature=0.0)
tools = load_tools(
["human", "llm-math"],
llm=math_llm,
)

agent_chain = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
)

agent_chain.run("In what month is Cobus Greyling's birthday?")

And the Agent response:

> Entering new AgentExecutor chain...
I don't know Cobus Greyling, so I need to ask a human for guidance.
Action: Human
Action Input: "Do you know when Cobus Greyling's birthday is?"

Do you know when Cobus Greyling's birthday is?
his birthday is in the fourth month

Observation: his birthday is in the fourth month
Thought:I need to convert the month number to a month name.
Action: Calculator
Action Input: 4
Observation: Answer: 4
Thought:I need to convert the month number to a month name.
Action: Human
Action Input: "What is the name of the fourth month?"

What is the name of the fourth month?
It is April of course

Observation: It is April of course
Thought:I now know the final answer
Final Answer: Cobus Greyling's birthday is in April.

> Finished chain.
'Cobus Greyling's birthday is in April.

The practical implications of this human-in-the-loop approach can be many…

One use-case is a scenario where a customer facing conversational UI pings a real-life agent for an answer to a question it might not have, or is unsure of.

The agent configuration can be such that only certain Agents have access to humans, or only at certain times.

Agents can also live in different domains and access certain pods of human support based on the domain the agent finds itself in.

This approach of only momentarily breaking out to a human for assistance and then reverting back to automation is a refreshing break from the current and pervasive chatbot implementation scenario.

Currently chatbots are managed like an IVR, which attempts to handle a call and on failure to contain, subsequently transfers to an agent for call completion.

In the case of Human-In-The-Loop agents, the human input is transparent to the user/customer with the focus on servicing the user request. The agent can cycle through one or more iterations where human support is gathered. All unseen by the user.

⭐️ Please follow me on LinkedIn for updates on Conversational AI ⭐️

I’m currently the Chief Evangelist @ HumanFirst. I explore and write about all things at the intersection of AI and language; ranging from LLMs, Chatbots, Voicebots, Development Frameworks, Data-Centric latent spaces and more.

https://www.linkedin.com/in/cobusgreyling
https://www.linkedin.com/in/cobusgreyling

--

--

Cobus Greyling

I explore and write about all things at the intersection of AI & language; LLMs/NLP/NLU, Chat/Voicebots, CCAI. www.cobusgreyling.com