
LangChain ã® HOW-TO EXAMPLES (5) - ãšãŒãžã§ã³ã
ãLangChainãã®ããšãŒãžã§ã³ãããæäŸããæ©èœã玹ä»ãã HOW-TO EXAMPLES ããŸãšããŸããã
åå
1. ãšãŒãžã§ã³ãã®æ©èœ
ããšãŒãžã§ã³ããã¯LLMã䜿çšããŠãå®è¡ããã¢ã¯ã·ã§ã³ãšãã®é åºã決å®ããŸããã¢ã¯ã·ã§ã³ã¯ããããŒã«ãå®è¡ããŠãã®åºåã芳å¯ãããŠãŒã¶ãŒã«æ»ããã®ããããã«ãªããŸãããããŒã«ãã¯ãç¹å®ã®æ©èœã®ããšã§ãã
ãšãŒãžã§ã³ãã¯ã次ã®3ã€ã®éšåã§æ§æãããŸãã
ã»ããŒã« : ãšãŒãžã§ã³ãã䜿çšã§ããããŒã«ã
ã»LLMChain : å®è¡ããã¢ã¯ã·ã§ã³ã決å®ããããããã¹ããçæã
ã»ãšãŒãžã§ã³ãã¯ã©ã¹ : LLMChainã®åºåãè§£æããŠãå®è¡ããã¢ã¯ã·ã§ã³ã決å®ã
ãLangChainãã«ã¯ãã«ã¹ã¿ã ããŒã«ãã«ã¹ã¿ã LLMChainãã«ã¹ã¿ã ãšãŒãžã§ã³ãã¯ã©ã¹(Comminc soon)ãå®è£ ããä»çµã¿ãæäŸãããŠããŸãã
2. äžéã¹ããã
Agentã¯ã©ã¹ã®åŒæ°ã«ãreturn_intermediate_steps=Trueããæå®ããããšã§ããšãŒãžã§ã³ããäœãããŠãããããã詳现ã«å¯èŠåã§ããŸãã
(1) ãšãŒãžã§ã³ãã®æºåãšè³ªåå¿çã®å®è¡ã
from langchain.agents import load_tools
from langchain.agents import initialize_agent
from langchain.llms import OpenAI
# ãšãŒãžã§ã³ãã®æºå
agent = initialize_agent(
llm=OpenAI(temperature=0),
tools=load_tools(["serpapi", "llm-math"], llm=OpenAI(temperature=0)),
agent="zero-shot-react-description",
verbose=True,
return_intermediate_steps=True
)
# 質åå¿çã®å®è¡
response = agent({"input":"ãµã¶ãšããã®å¹Žéœ¢ã«2ãæãããšïŒ"})> Entering new AgentExecutor chain...
ãµã¶ãšããã®å¹Žéœ¢ãç¥ãå¿
èŠããã
Action: Search
Action Input: ãµã¶ãšããã®å¹Žéœ¢
Observation: ãã°ç°ãµã¶ãšïŒãµãããããïŒ å¹Žéœ¢ã¯24æ³ã æããæããã竹ãå²ã£ããããªæ§æ Œã§ãçºå
ã®äººæ°è
ã æçãæŽæ¿¯ãè£çž«ãšäž»å©Šæ¥ã¯ãããŠãããªããŸããããããã¹ããšãã£ã¡ããã¡ãããªã®ãããŸã«ããºã
Thought: ãµã¶ãšããã®å¹Žéœ¢ã2åãã
Action: Calculator
Action Input: 24 * 2
Observation: Answer: 48
Thought: I now know the final answer
Final Answer: ãµã¶ãšããã®å¹Žéœ¢ã«2ãæãããš48æ³ã§ãã
> Finished AgentExecutor chain.(2) äžéã¹ãããã®ç¢ºèªã
åºåã¯èŠãããããã«æŽåœ¢ããŠããŸãã
# äžéã¹ãããã®ç¢ºèª
print(response["intermediate_steps"])[
(
AgentAction(
tool='Search',
tool_input='ãµã¶ãšããã®å¹Žéœ¢',
log=' ãµã¶ãšããã®å¹Žéœ¢ãç¥ãå¿
èŠããã\nAction: Search\nAction Input: ãµã¶ãšããã®å¹Žéœ¢'
),
'ãã°ç°ãµã¶ãšïŒãµãããããïŒ å¹Žéœ¢ã¯24æ³ã æããæããã竹ãå²ã£ããããªæ§æ Œã§ãçºå
ã®äººæ°è
ã æçãæŽæ¿¯ãè£çž«ãšäž»å©Šæ¥ã¯ãããŠãããªããŸããããããã¹ããšãã£ã¡ããã¡ãããªã®ãããŸã«ããºã'
),
(
AgentAction(
tool='Calculator',
tool_input='24 * 2',
log=' ãµã¶ãšããã®å¹Žéœ¢ã2åãã\nAction: Calculator\nAction Input: 24 * 2'
),
'Answer: 48'
)
]3. ãã«ãå ¥åããŒã«
ãšãŒãžã§ã³ãã§ãã«ãå ¥åããŒã«ãå©çšããã«ã¯ãæååããã«ãå ¥åã«ããŒã¹ããå°ããªã©ãããŒé¢æ°ãäœæããŸãã
(1) ããŒã«ãšããŠåŒã³åºã颿°ã®æºåã
from langchain.llms import OpenAI
from langchain.agents import initialize_agent, Tool
# 乿°é¢æ°
def multiplier(a, b):
return a * b
# æååãã2ã€ã®åŒæ°ã«å€æ
def parsing_multiplier(string):
a, b = string.split(",")
return multiplier(int(a), int(b))(2) ããŒã«ãšãšãŒãžã§ã³ãã®æºå
# ããŒã«ã®æºå
llm = OpenAI(temperature=0)
tools = [
Tool(
name = "Multiplier",
func=parsing_multiplier,
description="useful for when you need to multiply two numbers together. The input to this tool should be a comma separated list of numbers of length two, representing the two numbers you want to multiply together. For example, `1,2` would be the input if you wanted to multiply 1 by 2."
)
]
# ãšãŒãžã§ã³ãã®æºå
mrkl = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True)(3) ãšãŒãžã§ã³ãã®å®è¡ã
mrkl.run("3ã«4ãæãããšïŒ")> Entering new AgentExecutor chain...
I need to multiply 3 and 4 together
Action: Multiplier
Action Input: 3,4
Observation: 12
Thought: I now know the final answer
Final Answer: 3 Ã 4 = 12
> Finished AgentExecutor chain.
'3 à 4 = 12'4. ã«ã¹ã¿ã ããŒã«
ã«ã¹ã¿ã ããŒã«ã®å®çŸ©ã¯ã次ã®ãšããã§ããããŒã«ã®èª¬æã¯ãªãã·ã§ã³ã§ãããäžéšã®ãšãŒãžã§ã³ãã§å¿ èŠã«ãªããŸãã
class Tool(NamedTuple):
name: str # ããŒã«å
func: Callable[[str], str] # ããŒã«ã®å®è¡é¢æ°
description: Optional[str] = None # ããŒã«ã®èª¬æãšãŒãžã§ã³ãã«ããŒã«ã䜿çšãããã«ã¯ãããŒã«ãªã¹ããæäŸããå¿ èŠããããŸãã
(1) ããã±ãŒãžã®ã€ã³ããŒãã
# ããã±ãŒãžã®ã€ã³ããŒã
from langchain.agents import initialize_agent, Tool
from langchain.llms import OpenAI
from langchain import LLMMathChain, SerpAPIWrapper(2) LLMã®æºåã
# LLMã®æºå
llm = OpenAI(temperature=0)(3) ããŒã«ãªã¹ãã®æºåã
# ããŒã«ãªã¹ãã®æºå
search = SerpAPIWrapper() # æ€çŽ¢
llm_math_chain = LLMMathChain(llm=llm, verbose=True) # èšç®
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
),
Tool(
name="Calculator",
func=llm_math_chain.run,
description="useful for when you need to answer questions about math"
)
](4) ãšãŒãžã§ã³ãã®æºåãšå®è¡ã
# ãšãŒãžã§ã³ãã®æºå
agent = initialize_agent(
tools,
llm,
agent="zero-shot-react-description",
verbose=True
)
# ãšãŒãžã§ã³ãã®å®è¡
agent.run("ãŒã£ã¡ã»ãã»ãã£ãã®äœè
ã®ååãæããŠ")> Entering new AgentExecutor chain...
I need to find out who wrote the book
Action: Search
Action Input: "ãŒã£ã¡ã»ãã»ãã£ãã®äœè
"
Observation: ããŒã£ã¡ã»ãã»ãã£ãïŒãïŒ BOCCHI THE ROCK!ïŒ ã¯ãã¯ãŸãããã«ããæ¥æ¬ã®4ã³ã挫ç»ãã ãŸããã¿ã€ã ãããMAXãïŒè³æç€ŸïŒã«ãŠã2018幎2æå·ãã4æå·ãŸã§ã²ã¹ãé£èŒåŸãå幎5æå·ããé£èŒäžã
Thought: I now know the final answer
Final Answer: ã¯ãŸããã
> Finished AgentExecutor chain.
'ã¯ãŸããã'5. ã«ã¹ã¿ã LLMChain
ã«ã¹ã¿ã LLMChainã®äœæäœæ¥ã®å€ãã¯ãããã³ããã«åž°çããŸããåºåè§£æããããã«æ¢åã®ãšãŒãžã§ã³ãã¯ã©ã¹ã䜿çšããŠãããããããã³ããããã®åœ¢åŒã§ããã¹ãçæããããã«æç€ºããããšãéåžžã«éèŠã§ããããã«çŸåšã以åã®ã¢ã¯ã·ã§ã³ãšèгå¯ã«é¢ããã¡ã¢ãå ¥ãããããããã³ããã®æåŸã«agent_scratchpadãå¿ èŠã§ããããã«å ããŠãå¿ èŠã«å¿ããŠããã³ãããã«ã¹ã¿ãã€ãºã§ããŸãã
ããã³ããã«é©åãªæç€ºãå«ãŸããããã«ããããã«ãZeroShotAgentã¯ã©ã¹ã§ãã«ããŒã¡ãœããã䜿çšããŸãã
ã»tools : ãšãŒãžã§ã³ããã¢ã¯ã»ã¹ã§ããããŒã«ã®ãªã¹ãã
ã»prefix : ããŒã«äžèЧã®åã«ä»ããæååã
ã»suffix : ããŒã«äžèЧã®åŸã«ä»ããæååã
ã»input_variables : æçµããã³ãããæåŸ ããå ¥å倿°ã®ãªã¹ãã
ä»åã¯ããšãŒãžã§ã³ãã«Googleæ€çŽ¢ãžã®ã¢ã¯ã»ã¹ãèš±å¯ãããããã«ã¹ã¿ãã€ãºããŠãäžäººç§°ã¯"ãŒã"ãèªå°Ÿã¯"ãªã®ã "ã䜿çšããããã«ããŸãã
(1) ããã±ãŒãžã®ã€ã³ããŒãã
# ããã±ãŒãžã®ã€ã³ããŒã
from langchain.agents import ZeroShotAgent, Tool, AgentExecutor
from langchain import OpenAI, SerpAPIWrapper, LLMChain(2) ããŒã«ãªã¹ãã®æºå
# ããŒã«ãªã¹ãã®æºå
search = SerpAPIWrapper()
tools = [
Tool(
name = "Search",
func=search.run,
description="useful for when you need to answer questions about current events"
)
](3) ZeroShotAgentã®æºåã
ãã¬ãã£ãã¯ã¹ããµãã£ãã¯ã¹ãã«ã¹ã¿ãã€ãºããŸãã
prefix = """次ã®è³ªåã«ã§ããéãçããŠãã ãããæ¬¡ã®ããŒã«ã«ã¢ã¯ã»ã¹ã§ããŸã:"""
suffix = """å§ããŸããã! æçµçãªçããåºããšãã¯ãäžäººç§°ã¯"ãŒã"ãèªå°Ÿã«ã¯"ãªã®ã "ã䜿çšããŠãã ãã
Question: {input}
{agent_scratchpad}"""
prompt = ZeroShotAgent.create_prompt(
tools,
prefix=prefix,
suffix=suffix,
input_variables=["input", "agent_scratchpad"]
)(4) ããã³ããã®ç¢ºèªã
print(prompt.template)次ã®è³ªåã«ã§ããéãçããŠãã ãããæ¬¡ã®ããŒã«ã«ã¢ã¯ã»ã¹ã§ããŸã:
Search: useful for when you need to answer questions about current events
Use the following format:
Question: the input question you must answer
Thought: you should always think about what to do
Action: the action to take, should be one of [Search]
Action Input: the input to the action
Observation: the result of the action
... (this Thought/Action/Action Input/Observation can repeat N times)
Thought: I now know the final answer
Final Answer: the final answer to the original input question
å§ããŸããã! æçµçãªçããåºããšãã¯ãäžäººç§°ã¯"ãŒã"ãèªå°Ÿã«ã¯"ãªã®ã "ã䜿çšããŠãã ãã
Question: {input}
{agent_scratchpad}(5) AgentExecuterã§å®è¡ã
# LLMChainã®æºå
llm_chain = LLMChain(llm=OpenAI(temperature=0), prompt=prompt)
# ãšãŒãžã§ã³ãã®æºå
agent = ZeroShotAgent(llm_chain=llm_chain, tools=tools)
# AgentExecuterã§å®è¡
agent_executor = AgentExecutor.from_agent_and_tools(agent=agent, tools=tools, verbose=True)
agent_executor.run("2022å¹Žã®æ¥æ¬ã®äººå£ã¯ïŒ")> Entering new AgentExecutor chain...
Thought: ãã®è³ªåã«çããããã«äœããã¹ããèãããªã®ã
Action: Search
Action Input: 2022å¹Žã®æ¥æ¬ã®äººå£
Observation: 2022幎1æ1æ¥æç¹ã®æ¥æ¬ã®äººå£ã¯1å2592äž7902人ã§ãå幎ãã72äž6342人ïŒ0.57ïŒ
ïŒã®æžå°ã
Thought: ãããæçµçãªçããªã®ã
Final Answer: 2022幎1æ1æ¥æç¹ã®æ¥æ¬ã®äººå£ã¯1å2592äž7902人ã§ãå幎ãã72äž6342人ïŒ0.57ïŒ
ïŒã®æžå°ãªã®ã ã
> Finished AgentExecutor chain.
'2022幎1æ1æ¥æç¹ã®æ¥æ¬ã®äººå£ã¯1å2592äž7902人ã§ãå幎ãã72äž6342人ïŒ0.57ïŒ
ïŒã®æžå°ãªã®ã ã'6. ã«ã¹ã¿ã ãšãŒãžã§ã³ãã¯ã©ã¹
以äžã®ããã¥ã¡ã³ãã§ Comminc soon ã«ãªã£ãŠãŸãã