AI

A Light Intro to Chaining LLMs, Brokers, and utils through LangChain | by Dr. Varshita Sher | Apr, 2023

[ad_1]

#LLM for newcomers

Perceive the fundamentals of brokers, instruments, and prompts and a few learnings alongside the best way

Viewers: For these feeling overwhelmed with the large (but good) library…

Picture generated by Writer utilizing DALL.E 2

I’d be mendacity if I stated I’ve obtained your entire LangChain library lined — in truth, I’m removed from it. However the buzz surrounding it was sufficient to shake me out of my writing hiatus and provides it a go 🚀.

The preliminary motivation was to see what was it that LangChain was including (on a sensible stage) that set it other than the chatbot I constructed final month utilizing the ChatCompletion.create() operate from the openai package deal. While doing so, I noticed I wanted to grasp the constructing blocks for LangChain first earlier than transferring on to the extra advanced components.

That is what this text does. Heads-up although, this will likely be extra components coming as I’m actually fascinated by the library and can proceed to discover to see what all may be constructed by way of it.

Let’s start by understanding the basic constructing blocks of LangChain — i.e. Chains. If you happen to’d wish to observe alongside, right here’s the GitHub repo.

What are chains in LangChain?

Chains are what you get by connecting a number of massive language fashions (LLMs) in a logical method. (Chains may be constructed of entities apart from LLMs however for now, let’s follow this definition for simplicity).

OpenAI is a sort of LLM (supplier) that you should utilize however there are others like Cohere, Bloom, Huggingface, and so forth.

Word: Just about most of those LLM suppliers will want you to request an API key as a way to use them. So ensure you try this earlier than continuing with the rest of this weblog. For instance:

import os
os.environ["OPENAI_API_KEY"] = "..."

P.S. I’m going to make use of OpenAI for this tutorial as a result of I’ve a key with credit that expire in a month’s time, however be at liberty to exchange it with every other LLM. The ideas lined right here will likely be helpful regardless.

Chains may be easy (i.e. Generic) or specialised (i.e. Utility).

  1. Generic — A single LLM is the only chain. It takes an enter immediate and the title of the LLM after which makes use of the LLM for textual content era (i.e. output for the immediate). Right here’s an instance:

Let’s construct a primary chain — create a immediate and get a prediction

Immediate creation (utilizing PromptTemplate) is a bit fancy in Lanchain however that is in all probability as a result of there are fairly just a few alternative ways prompts may be created relying on the use case (we are going to cowl AIMessagePromptTemplate,
HumanMessagePromptTemplate and so forth. within the subsequent weblog put up). Right here’s a easy one for now:

from langchain.prompts import PromptTemplate

immediate = PromptTemplate(
input_variables=["product"],
template="What is an efficient title for a corporation that makes {product}?",
)

print(immediate.format(product="podcast participant"))

# OUTPUT
# What is an efficient title for a corporation that makes podcast participant?

Word: If you happen to require a number of input_variables, for example: input_variables=["product", "audience"] for a template reminiscent of “What is an efficient title for a corporation that makes {product} for {viewers}”, you should do print(immediate.format(product="podcast participant", viewers="youngsters”) to get the up to date immediate.

After getting constructed a immediate, we are able to name the specified LLM with it. To take action, we create an LLMChain occasion (in our case, we use OpenAI‘s massive language mannequin text-davinci-003). To get the prediction (i.e. AI-generated textual content), we use run operate with the title of the product.

from langchain.llms import OpenAI
from langchain.chains import LLMChain

llm = OpenAI(
model_name="text-davinci-003", # default mannequin
temperature=0.9) #temperature dictates how whacky the output needs to be
llmchain = LLMChain(llm=llm, immediate=immediate)
llmchain.run("podcast participant")

# OUTPUT
# PodConneXion

If you happen to had multiple input_variables, then you definitely received’t be capable to use run. As a substitute, you’ll must cross all of the variables as a dict. For instance, llmchain({“product”: “podcast participant”, “viewers”: “youngsters”}).

Word 1: In keeping with OpenAI, davinci text-generation fashions are 10x costlier than their chat counterparts i.e gpt-3.5-turbo, so I attempted to change from a textual content mannequin to a chat mannequin (i.e. from OpenAI to ChatOpenAI) and the outcomes are just about the identical.

Word 2: You may see some tutorials utilizing OpenAIChatas a substitute of ChatOpenAI. The previous is deprecated and can not be supported and we’re supposed to make use of ChatOpenAI.

from langchain.chat_models import ChatOpenAI

chatopenai = ChatOpenAI(
model_name="gpt-3.5-turbo")
llmchain_chat = LLMChain(llm=chatopenai, immediate=immediate)
llmchain_chat.run("podcast participant")

# OUTPUT
# PodcastStream

This concludes our part on easy chains. It is very important be aware that we hardly ever use generic chains as standalone chains. Extra typically they’re used as constructing blocks for Utility chains (as we are going to see subsequent).

2. Utility — These are specialised chains, comprised of many LLMs to assist resolve a particular activity. For instance, LangChain helps some end-to-end chains (reminiscent of AnalyzeDocumentChain for summarization, QnA, and so forth) and a few particular ones (reminiscent of GraphQnAChain for creating, querying, and saving graphs). We’ll take a look at one particular chain referred to as PalChain on this tutorial for digging deeper.

PAL stands for Programme Aided Language Model. PALChain reads advanced math issues (described in pure language) and generates packages (for fixing the mathematics downside) because the intermediate reasoning steps, however offloads the answer step to a runtime reminiscent of a Python interpreter.

To substantiate that is in truth true, we are able to examine the _call() within the base code here. Below the hood, we are able to see this chain:

P.S. It’s a good follow to examine _call() in base.py for any of the chains in LangChain to see how issues are working beneath the hood.

from langchain.chains import PALChain
palchain = PALChain.from_math_prompt(llm=llm, verbose=True)
palchain.run("If my age is half of my dad's age and he's going to be 60 subsequent 12 months, what's my present age?")

# OUTPUT
# > Coming into new PALChain chain...
# def resolution():
# """If my age is half of my dad's age and he's going to be 60 subsequent 12 months, what's my present age?"""
# dad_age_next_year = 60
# dad_age_now = dad_age_next_year - 1
# my_age_now = dad_age_now / 2
# outcome = my_age_now
# return outcome
#
# > Completed chain.
# '29.5'

Note1: verbose may be set to False if you don’t want to see the intermediate step.

Now a few of chances are you’ll be questioning — however what in regards to the immediate? We actually didn’t cross one as we did for the generic llmchain we constructed. The actual fact is, it’s robotically loaded when utilizing .from_math_prompt(). You’ll be able to test the default immediate utilizing palchain.immediate.template or you’ll be able to instantly examine the immediate file here.

print(palchain.immediate.template)
# OUTPUT
# 'Q: Olivia has $23. She purchased 5 bagels for $3 every. How a lot cash does she have left?nn# resolution in Python:nnndef resolution():n """Olivia has $23. She purchased 5 bagels for $3 every. How a lot cash does she have left?"""n money_initial = 23n bagels = 5n bagel_cost = 3n money_spent = bagels * bagel_costn money_left = money_initial - money_spentn outcome = money_leftn return resultnnnnnnQ: Michael had 58 golf balls. On tuesday, he misplaced 23 golf balls. On wednesday, he misplaced 2 extra. What number of golf balls did he have on the finish of wednesday?nn# resolution in Python:nnndef resolution():n """Michael had 58 golf balls. On tuesday, he misplaced 23 golf balls. On wednesday, he misplaced 2 extra. What number of golf balls did he have on the finish of wednesday?"""n golf_balls_initial = 58n golf_balls_lost_tuesday = 23n golf_balls_lost_wednesday = 2n golf_balls_left = golf_balls_initial - golf_balls_lost_tuesday - golf_balls_lost_wednesdayn outcome = golf_balls_leftn return resultnnnnnnQ: There have been 9 computer systems within the server room. 5 extra computer systems had been put in every day, from monday to thursday. What number of computer systems at the moment are within the server room?nn# resolution in Python:nnndef resolution():n """There have been 9 computer systems within the server room. 5 extra computer systems had been put in every day, from monday to thursday. What number of computer systems at the moment are within the server room?"""n computers_initial = 9n computers_per_day = 5n num_days = 4 # 4 days between monday and thursdayn computers_added = computers_per_day * num_daysn computers_total = computers_initial + computers_addedn outcome = computers_totaln return resultnnnnnnQ: Shawn has 5 toys. For Christmas, he obtained two toys every from his mother and pa. What number of toys does he have now?nn# resolution in Python:nnndef resolution():n """Shawn has 5 toys. For Christmas, he obtained two toys every from his mother and pa. What number of toys does he have now?"""n toys_initial = 5n mom_toys = 2n dad_toys = 2n total_received = mom_toys + dad_toysn total_toys = toys_initial + total_receivedn outcome = total_toysn return resultnnnnnnQ: Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. What number of lollipops did Jason give to Denny?nn# resolution in Python:nnndef resolution():n """Jason had 20 lollipops. He gave Denny some lollipops. Now Jason has 12 lollipops. What number of lollipops did Jason give to Denny?"""n jason_lollipops_initial = 20n jason_lollipops_after = 12n denny_lollipops = jason_lollipops_initial - jason_lollipops_aftern outcome = denny_lollipopsn return resultnnnnnnQ: Leah had 32 candies and her sister had 42. In the event that they ate 35, what number of items have they got left in whole?nn# resolution in Python:nnndef resolution():n """Leah had 32 candies and her sister had 42. In the event that they ate 35, what number of items have they got left in whole?"""n leah_chocolates = 32n sister_chocolates = 42n total_chocolates = leah_chocolates + sister_chocolatesn chocolates_eaten = 35n chocolates_left = total_chocolates - chocolates_eatenn outcome = chocolates_leftn return resultnnnnnnQ: If there are 3 automobiles within the parking zone and a pair of extra automobiles arrive, what number of automobiles are within the parking zone?nn# resolution in Python:nnndef resolution():n """If there are 3 automobiles within the parking zone and a pair of extra automobiles arrive, what number of automobiles are within the parking zone?"""n cars_initial = 3n cars_arrived = 2n total_cars = cars_initial + cars_arrivedn outcome = total_carsn return resultnnnnnnQ: There are 15 bushes within the grove. Grove employees will plant bushes within the grove right this moment. After they're achieved, there will likely be 21 bushes. What number of bushes did the grove employees plant right this moment?nn# resolution in Python:nnndef resolution():n """There are 15 bushes within the grove. Grove employees will plant bushes within the grove right this moment. After they're achieved, there will likely be 21 bushes. What number of bushes did the grove employees plant right this moment?"""n trees_initial = 15n trees_after = 21n trees_added = trees_after - trees_initialn outcome = trees_addedn return resultnnnnnnQ: {query}nn# resolution in Python:nnn'

Word: A lot of the utility chains can have their prompts pre-defined as a part of the library (test them out here). They’re, at occasions, fairly detailed (learn: a number of tokens) so there’s undoubtedly a trade-off between value and the standard of response from the LLM.

Are there any Chains that don’t want LLMs and prompts?

Despite the fact that PalChain requires an LLM (and a corresponding immediate) to parse the consumer’s query written in pure language, there are some chains in LangChain that don’t want one. These are primarily transformation chains that preprocess the immediate, reminiscent of eradicating additional areas, earlier than inputting it into the LLM. You’ll be able to see one other instance here.

Can we get to the nice half and begin creating chains?

In fact, we are able to! We now have all the fundamental constructing blocks we have to begin chaining collectively LLMs logically such that enter from one may be fed to the subsequent. To take action, we are going to use SimpleSequentialChain.

The documentation has some nice examples on this, for instance, you’ll be able to see here the way to have two chains mixed the place chain#1 is used to wash the immediate (take away additional whitespaces, shorten immediate, and so forth) and chain#2 is used to name an LLM with this clear immediate. Right here’s another one the place chain#1 is used to generate a synopsis for a play and chain#2 is used to put in writing a overview primarily based on this synopsis.

Whereas these are glorious examples, I need to concentrate on one thing else. If you happen to keep in mind earlier than, I discussed that chains may be composed of entities apart from LLMs. Extra particularly, I’m concerned about chaining brokers and LLMs collectively. However first, what are brokers?

Utilizing brokers for dynamically calling LLMs

It is going to be a lot simpler to elucidate what an agent does vs. what it’s.

Say, we need to know the climate forecast for tomorrow. If had been to make use of the straightforward ChatGPT API and provides it a immediate Present me the climate for tomorrow in London, it received’t know the reply as a result of it doesn’t have entry to real-time information.

Wouldn’t it’s helpful if we had an association the place we might make the most of an LLM for understanding our question (i.e immediate) in pure language after which name the climate API on our behalf to fetch the info wanted? That is precisely what an agent does (amongst different issues, after all).

An agent has entry to an LLM and a set of instruments for instance Google Search, Python REPL, math calculator, climate APIs, and so forth.

There are fairly just a few brokers that LangChain helps — see here for the whole listing, however fairly frankly the most typical one I got here throughout in tutorials and YT movies was zero-shot-react-description. This agent makes use of ReAct (Cause + Act) framework to choose probably the most usable device (from a listing of instruments), primarily based on what the enter question is.

P.S.: Here’s a pleasant article that goes in-depth into the ReAct framework.

Let’s initialize an agent utilizing initialize_agent and cross it the instruments and LLM it wants. There’s an extended listing of instruments out there here that an agent can use to work together with the skin world. For our instance, we’re utilizing the identical math-solving device as above, referred to as pal-math. This one requires an LLM on the time of initialization, so we cross to it the identical OpenAI LLM occasion as earlier than.

from langchain.brokers import initialize_agent
from langchain.brokers import AgentType
from langchain.brokers import load_tools

llm = OpenAI(temperature=0)
instruments = load_tools(["pal-math"], llm=llm)

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

Let’s try it out on the identical instance as above:

agent.run("If my age is half of my dad's age and he's going to be 60 subsequent 12 months, what's my present age?")

# OUTPUT
# > Coming into new AgentExecutor chain...
# I want to determine my dad's present age after which divide it by two.
# Motion: PAL-MATH
# Motion Enter: What's my dad's present age if he's going to be 60 subsequent 12 months?
# Statement: 59
# Thought: I now know my dad's present age, so I can divide it by two to get my age.
# Motion: Divide 59 by 2
# Motion Enter: 59/2
# Statement: Divide 59 by 2 will not be a sound device, strive one other one.
# Thought: I can use PAL-MATH to divide 59 by 2.
# Motion: PAL-MATH
# Motion Enter: Divide 59 by 2
# Statement: 29.5
# Thought: I now know the ultimate reply.
# Ultimate Reply: My present age is 29.5 years previous.

# > Completed chain.
# 'My present age is 29.5 years previous.'

Word 1: At every step, you’ll discover that an agent does one in every of three issues — it both has an statement, a thought, or it takes an motion. That is primarily because of the ReAct framework and the related immediate that the agent is utilizing:

print(agent.agent.llm_chain.immediate.template)
# OUTPUT
# Reply the next questions as greatest you'll be able to. You have got entry to the next instruments:
# PAL-MATH: A language mannequin that's actually good at fixing advanced phrase math issues. Enter needs to be a completely worded arduous phrase math downside.

# Use the next format:

# Query: the enter query you need to reply
# Thought: it is best to all the time take into consideration what to do
# Motion: the motion to take, needs to be one in every of [PAL-MATH]
# Motion Enter: the enter to the motion
# Statement: the results of the motion
# ... (this Thought/Motion/Motion Enter/Statement can repeat N occasions)
# Thought: I now know the ultimate reply
# Ultimate Reply: the ultimate reply to the unique enter query
# Start!
# Query: {enter}
# Thought:{agent_scratchpad}

Note2: You is likely to be questioning what’s the purpose of getting an agent to do the identical factor that an LLM can do. Some purposes would require not only a predetermined chain of calls to LLMs/different instruments, however probably an unknown chain that relies on the consumer’s enter [Source]. In these kinds of chains, there’s an “agent” which has entry to a set of instruments.
As an example,
here’s an instance of an agent that may fetch the right paperwork (from the vectorstores) for RetrievalQAChain relying on whether or not the query refers to doc A or doc B.

For enjoyable, I attempted making the enter query extra advanced (utilizing Demi Moore’s age as a placeholder for Dad’s precise age).

agent.run("My age is half of my dad's age. Subsequent 12 months he's going to be similar age as Demi Moore. What's my present age?")

Sadly, the reply was barely off because the agent was not utilizing the most recent age for Demi Moore (since Open AI fashions had been skilled on information till 2020). This may be simply mounted by together with one other device —
instruments = load_tools([“pal-math”, "serpapi"], llm=llm). serpapi is beneficial for answering questions on present occasions.

Word: It is very important add as many instruments as you suppose could also be related to the consumer question. The issue with utilizing a single device is that the agent retains making an attempt to make use of the identical device even when it’s not probably the most related for a selected statement/motion step.

Right here’s one other instance of a device you should utilize — podcast-api. It’s worthwhile to get your own API key and plug it into the code under.


instruments = load_tools(["podcast-api"], llm=llm, listen_api_key="...")
agent = initialize_agent(instruments,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True)

agent.run("Present me episodes for cash saving suggestions.")

# OUTPUT
# > Coming into new AgentExecutor chain...
# I ought to seek for podcasts or episodes associated to cash saving
# Motion: Podcast API
# Motion Enter: Cash saving suggestions
# Statement: The API name returned 3 podcasts associated to cash saving suggestions: The Cash Nerds, The Rachel Cruze Present, and The Martin Lewis Podcast. These podcasts provide invaluable cash saving suggestions and recommendation to assist folks take management of their funds and create a life they love.
# Thought: I now have some choices to select from
# Ultimate Reply: The Cash Nerds, The Rachel Cruze Present, and The Martin Lewis Podcast are nice podcast choices for cash saving suggestions.

# > Completed chain.

# 'The Cash Nerds, The Rachel Cruze Present, and The Martin Lewis Podcast are nice podcast choices for cash saving suggestions.'

Note1: There’s a known error with utilizing this API the place you may see, openai.error.InvalidRequestError: This mannequin’s most context size is 4097 tokens, nonetheless you requested XXX tokens (XX in your immediate; XX for the completion). Please cut back your immediate; or completion size. This occurs when the response returned by the API is likely to be too large. To work round this, the documentation suggests returning fewer search outcomes, for instance, by updating the query to "Present me episodes for cash saving suggestions, return only one outcome".

Note2: Whereas tinkering round with this device, I seen some inconsistencies. The responses aren’t all the time full the primary time round, for example listed below are the enter and responses from two consecutive runs:

Enter: “Podcasts for getting higher at French”

Response 1: “The perfect podcast for studying French is the one with the best overview rating.”
Response 2: ‘The perfect podcast for studying French is “FrenchPod101”.

Below the hood, the device is first utilizing an LLMChain for building the API URL primarily based on our enter directions (one thing alongside the traces of https://listen-api.listennotes.com/api/v2/search?q=french&type=podcast&page_size=3) and making the API call. Upon receiving the response, it makes use of one other LLMChain that summarizes the response to get the reply to our unique query. You’ll be able to take a look at the prompts here for each LLMchains which describe the method in additional element.

I’m inclined to guess the inconsistent outcomes seen above are ensuing from the summarization step as a result of I’ve individually debugged and examined the API URL (created by LLMChain#1) through Postman and acquired the proper response. To additional affirm my doubts, I additionally stress-tested the summarization chain as a standalone chain with an empty API URL hoping it could throw an error however obtained the response “Investing’ podcasts had been discovered, containing 3 ends in whole.” 🤷‍♀ I’d be curious to see if others had higher luck than me with this device!

Use Case 2: Mix chains to create an age-appropriate reward generator

Let’s put our information of brokers and sequential chaining to good use and create our personal sequential chain. We’ll mix:

  • Chain #1 — The agent we simply created that may resolve age problems in math.
  • Chain #2 — An LLM that takes the age of an individual and suggests an applicable reward for them.
# Chain1 - resolve math downside, get the age
chain_one = agent

# Chain2 - counsel age-appropriate reward
template = """You're a reward recommender. Given an individual's age,n
it's your job to counsel an applicable reward for them.

Particular person Age:
{age}
Recommend reward:"""
prompt_template = PromptTemplate(input_variables=["age"], template=template)
chain_two = LLMChain(llm=llm, immediate=prompt_template)

Now that we’ve each chains prepared we are able to mix them utilizing SimpleSequentialChain.

from langchain.chains import SimpleSequentialChain

overall_chain = SimpleSequentialChain(
chains=[chain_one, chain_two],
verbose=True)

A few issues to notice:

  • We’d like not explicitly cross input_variables and output_variables for SimpleSequentialChain because the underlying assumption is that the output from chain 1 is handed as enter to chain 2.

Lastly, we are able to run it with the identical math downside as earlier than:

query = "If my age is half of my dad's age and he's going to be 60 subsequent 12 months, what's my present age?"
overall_chain.run(query)

# OUTPUT
# > Coming into new SimpleSequentialChain chain...

# > Coming into new AgentExecutor chain...
# I want to determine my dad's present age after which divide it by two.
# Motion: PAL-MATH
# Motion Enter: What's my dad's present age if he's going to be 60 subsequent 12 months?
# Statement: 59
# Thought: I now know my dad's present age, so I can divide it by two to get my age.
# Motion: Divide 59 by 2
# Motion Enter: 59/2
# Statement: Divide 59 by 2 will not be a sound device, strive one other one.
# Thought: I want to make use of PAL-MATH to divide 59 by 2.
# Motion: PAL-MATH
# Motion Enter: Divide 59 by 2
# Statement: 29.5
# Thought: I now know the ultimate reply.
# Ultimate Reply: My present age is 29.5 years previous.

# > Completed chain.
# My present age is 29.5 years previous.

# Given your age, an amazing reward could be one thing that you should utilize and revel in now like a pleasant bottle of wine, a luxurious watch, a cookbook, or a present card to a favourite retailer or restaurant. Or, you may get one thing that can final for years like a pleasant piece of bijou or a top quality leather-based pockets.

# > Completed chain.

# 'nGiven your age, an amazing reward could be one thing that you should utilize and revel in now like a pleasant bottle of wine, a luxurious watch, a cookbook, or a present card to a favourite retailer or restaurant. Or, you may get one thing that can final for years like a pleasant piece of bijou or a top quality leather-based pockets

There is likely to be occasions when you should cross alongside some extra context to the second chain, along with what it’s receiving from the primary chain. As an example, I need to set a price range for the reward, relying on the age of the person who is returned by the primary chain. We will achieve this utilizing SimpleMemory.

First, let’s replace the immediate for chain_two and cross to it a second variable referred to as price range inside input_variables.

template = """You're a reward recommender. Given an individual's age,n
it's your job to counsel an applicable reward for them. If age is beneath 10,n
the reward ought to value not more than {price range} in any other case it ought to value atleast 10 occasions {price range}.

Particular person Age:
{output}
Recommend reward:"""
prompt_template = PromptTemplate(input_variables=["output", "budget"], template=template)
chain_two = LLMChain(llm=llm, immediate=prompt_template)

If you happen to examine the template we had for SimpleSequentialChain with the one above, you’ll discover that I’ve additionally up to date the primary enter’s variable title from ageoutput. This can be a essential step, failing which an error could be raised on the time of chain validationLacking required enter keys: {age}, solely had {enter, output, price range}.
It’s because the output from the primary entity within the chain (i.e. agent) would be the enter for the second entity within the chain (i.e. chain_two) and subsequently the variable names should match. Upon inspecting agent’s output keys, we see that the output variable known as output, therefore the replace.

print(agent.agent.llm_chain.output_keys)

# OUTPUT
["output"]

Subsequent, let’s replace the form of chain we’re making. We will not work with SimpleSequentialChain as a result of it solely works in instances the place this can be a single enter and single output. Since chain_two is now taking two input_variables, we have to use SequentialChain which is tailor-made to deal with a number of inputs and outputs.

overall_chain = SequentialChain(
input_variables=["input"],
reminiscence=SimpleMemory(recollections={"price range": "100 GBP"}),
chains=[agent, chain_two],
verbose=True)

A few issues to notice:

  • Not like SimpleSequentialChain, passing input_variables parameter is necessary for SequentialChain. It’s a listing containing the title of the enter variables that the primary entity within the chain (i.e. agent in our case) expects.
    Now a few of chances are you’ll be questioning the way to know the precise title used within the enter immediate that the agent goes to make use of. We actually didn’t write the immediate for this agent (as we did for chain_two)! It is really fairly simple to seek out it out by inspecting the immediate template of the llm_chain that the agent is made up of.
print(agent.agent.llm_chain.immediate.template)

# OUTPUT
#Reply the next questions as greatest you'll be able to. You have got entry to the next instruments:

#PAL-MATH: A language mannequin that's actually good at fixing advanced phrase math issues. Enter needs to be a completely worded arduous phrase math downside.

#Use the next format:

#Query: the enter query you need to reply
#Thought: it is best to all the time take into consideration what to do
#Motion: the motion to take, needs to be one in every of [PAL-MATH]
#Motion Enter: the enter to the motion
#Statement: the results of the motion
#... (this Thought/Motion/Motion Enter/Statement can repeat N occasions)
#Thought: I now know the ultimate reply
#Ultimate Reply: the ultimate reply to the unique enter query

#Start!

#Query: {enter}
#Thought:{agent_scratchpad}

As you’ll be able to see towards the top of the immediate, the questions being requested by the end-user is saved in an enter variable by the title enter. If for some cause you needed to manipulate this title within the immediate, ensure you are additionally updating the input_variables on the time of the creation of SequentialChain.

Lastly, you may have came upon the identical data with out going by way of the entire immediate:

print(agent.agent.llm_chain.immediate.input_variables)

# OUTPUT
# ['input', 'agent_scratchpad']

  • SimpleMemory is a straightforward solution to retailer context or different bits of knowledge that shouldn’t ever change between prompts. It requires one parameter on the time of initialization — recollections. You’ll be able to cross parts to it in dict kind. As an example, SimpleMemory(recollections={“price range”: “100 GBP”}).

Lastly, let’s run the brand new chain with the identical immediate as earlier than. You’ll discover, the ultimate output has some luxurious reward suggestions reminiscent of weekend getaways in accordance with the upper price range in our up to date immediate.

overall_chain.run("If my age is half of my dad's age and he's going to be 60 subsequent 12 months, what's my present age?")

# OUTPUT
#> Coming into new SequentialChain chain...

#> Coming into new AgentExecutor chain...
# I want to determine my dad's present age after which divide it by two.
#Motion: PAL-MATH
#Motion Enter: What's my dad's present age if he's going to be 60 subsequent 12 months?
#Statement: 59
#Thought: I now know my dad's present age, so I can divide it by two to get my age.
#Motion: Divide 59 by 2
#Motion Enter: 59/2
#Statement: Divide 59 by 2 will not be a sound device, strive one other one.
#Thought: I can use PAL-MATH to divide 59 by 2.
#Motion: PAL-MATH
#Motion Enter: Divide 59 by 2
#Statement: 29.5
#Thought: I now know the ultimate reply.
#Ultimate Reply: My present age is 29.5 years previous.

#> Completed chain.

# For somebody of your age, a superb reward could be one thing that's each sensible and significant. Contemplate one thing like a pleasant watch, a chunk of bijou, a pleasant leather-based bag, or a present card to a favourite retailer or restaurant.nIf you may have a bigger price range, you may take into account one thing like a weekend getaway, a spa package deal, or a particular expertise.'}

#> Completed chain.

For somebody of your age, a superb reward could be one thing that's each sensible and significant. Contemplate one thing like a pleasant watch, a chunk of bijou, a pleasant leather-based bag, or a present card to a favourite retailer or restaurant.nIf you may have a bigger price range, you may take into account one thing like a weekend getaway, a spa package deal, or a particular expertise.'}

[ad_2]

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button