
LangChain ã® HOW-TO EXAMPLES (3) - ãã§ãŒã³
ãLangChainãã®ããã§ãŒã³ããæäŸããæ©èœã玹ä»ãã HOW-TO EXAMPLES ããŸãšããŸããã
åå
1. ãã§ãŒã³ã®æ©èœ
ããã§ãŒã³ãã¯ãåŠçãè¡ãåºæ¬ãªããžã§ã¯ãã§ããã§ãŒã³ãç¹ããããšã§ãäžé£ã®åŠçãå®è¡ããããšãã§ããŸãããã§ãŒã³ã¯ãããªããã£ã(promptsãllmsãutils) ãŸã㯠ä»ã®ãã§ãŒã³ ã®ããããã§æ§æãããŸãã
ãã§ãŒã³ã®çš®é¡ã¯ã次ã®ãšããã§ãã
ã»ãžã§ããªãã¯ãã§ãŒã³ : ãã§ãŒã³æ§ç¯ã®ããã®ãã§ãŒã³
ã»ããã¥ã¡ã³ããã§ãŒã³ : ããã¥ã¡ã³ãæäœã®ããã®ãã§ãŒã³
ã»ãŠãŒãã£ãªãã£ãã§ãŒã³ : ãŠãŒãã£ãªãã£é£æºã®ããã®ãã§ãŒã³
2. ãžã§ããªãã¯ãã§ãŒã³
ç¹å®ã®ç®çãæããã®ã§ã¯ãªãããã§ãŒã³ãæ§ç¯ããããã«äœ¿çšãããã§ãŒã³ã§ãã
2-1. LLMChain
ãŠãŒã¶ãŒå ¥åãåãåãããããåºã«PromptTemplateã§ããã³ãããçæãããããLLMã«æž¡ããŠåºåãè¿ããã§ãŒã³ã§ãã
â ã·ã³ã°ã«å ¥å
from langchain import PromptTemplate, OpenAI, LLMChain
# ãã³ãã¬ãŒãã®æºå
template = """Q: {question}
A: äžæ©äžæ©èããŠã¿ãŸãããã"""
# ããã³ãããã³ãã¬ãŒãã®æºå
prompt = PromptTemplate(
template=template,
input_variables=["question"]
)
# LLMChainã®æºå
llm_chain = LLMChain(
llm=OpenAI(temperature=0),
prompt=prompt,
verbose=True
)
# LLMChainã®å®è¡
question = "ã®ã¿ãŒãäžéããã«ã¯ïŒ"
print(llm_chain.predict(question=question))> Entering new LLMChain chain...
Prompt after formatting:
Q: ã®ã¿ãŒãäžéããã«ã¯ïŒ
A: äžæ©äžæ©èããŠã¿ãŸãããã
> Finished LLMChain chain.
1. ã®ã¿ãŒã楜ããããšãã®ã¿ãŒã楜ããããšãäžçªå€§åã§ããã®ã¿ãŒã楜ããããšã§ã楜ããæéãéããããšãã§ããŸãã
2. ç·Žç¿ãç©ã¿éããããšãã®ã¿ãŒãäžéããã«ã¯ãæ¯æ¥ã®ç·Žç¿ãå¿
èŠã§ããæ¯æ¥å°ããã€ã§ããæ¯æ¥ã®ç·Žç¿ãç©ã¿éããŠããããšã倧åã§ãã
3. æå
ã®åããæèããããšãã®ã¿ãŒãäžéããã«ã¯ãæå
ã®åããæèããŠç·Žç¿ããããšã倧åã§ããæå
â ãã«ãå ¥å
# ãã³ãã¬ãŒãã®æºå
template = """{subject}ã顿ã«{adjective}ããšã ãæžããŠãã ããã"""
# ããã³ãããã³ãã¬ãŒãã®æºå
prompt = PromptTemplate(
template=template,
input_variables=["adjective", "subject"]
)
# LLMChainã®æºå
llm_chain = LLMChain(
prompt=prompt,
llm=OpenAI(temperature=0),
verbose=True
)
# LLMChainã®å®è¡
print(llm_chain.predict(adjective="ãããã", subject="ç«"))> Entering new LLMChain chain...
Prompt after formatting:
ç«ã顿ã«ããããããšã ãæžããŠãã ããã
> Finished LLMChain chain.
ç«ã¯å¯æããŠã\nãã®ç³ã¯å®ç³ã®ããã«èŒããŠããã\nãã®èº«äœã¯æãããã
ãã®è§Šæã¯æ¯çŸœç«ã£ã極äžã®ãã®ã
ç«ã¯ãã®åªãã声ã§
ç§ãæ
°ããŠãããã
ãã®ãè
¹ãæ«ã§ããš
ãã®å¿ãæž©ãããªãã
ç«ã¯ç§ã®å¿ãæºãããŠãããã
ãããŠç§ãå®ã£ãŠãããã
ãããªç«ãæããŠããã
ãããŠãããæ°žé ã«å®ãããã2-2. TransformChain
ä»»æã®Python颿°ãåãåãããããä»ã®ãã§ãŒã³ã®å ¥åºåã«é©çšãããã§ãŒã³ã§ãã
2-3. SequentialChainã»SimpleSequentialChain
è€æ°ã®ãã§ãŒã³ãé çªã«å®è¡ãããã§ãŒã³ã§ãã
詳ããã¯ã以äžã§è§£èª¬ããŠããŸãã
3. ããã¥ã¡ã³ããã§ãŒã³
質åå¿çãèŠçŽãªã©ãããã¥ã¡ã³ãæäœãç°¡åã«ããããšãç®çãšãããã§ãŒã³ã§ãã
3-1. StuffDocumentsChain
ãã¹ãŠã®é¢é£ããŒã¿ãã³ã³ããã¹ããšããŠããã³ããã«è©°ã蟌ã¿ãèšèªã¢ãã«ã«æž¡ããã§ãŒã³ã§ãã
3-2. MapReduceDocumentsChain
é¢é£ããŒã¿ããã£ã³ã¯ã«åå²ãããã£ã³ã¯ããšã«ããã³ãããçæããŠLLMãåŒã³åºããæåŸã«å šãŠã®çµæãçµåããããã³ããã§LLMãåŒã³åºããã§ãŒã³ã§ãã
3-3. MapReduceDocumentsChain
é¢é£ããŒã¿ããã£ã³ã¯ã«åå²ããæåã®ãã£ã³ã¯ããšã«ããã³ãããçæããŠLLMãåŒã³åºãããã®åºåãšæ¬¡ã®ãã£ã³ã¯ã§ããã³ãããçæããŠLLMãåŒã³åºãããããç¹°ãè¿ããã§ãŒã³ã§ãã
詳ããã¯ã以äžã§è§£èª¬ããŠããŸãã
4. ãŠãŒãã£ãªãã£ãã§ãŒã³
ç¹å®ã®ãŠãŒãã£ãªãã£ãšçžäºäœçšããLLMChainã§æ§æããããã§ãŒã³ã§ãã
4-1. LLMMathChain
ãŠãŒã¶ãŒå ¥å (æ°åŠã®åé¡) ãåãåããLLMChain ã䜿çšããŠãããPythonã³ãŒãã«å€æããPython REPLã§å®è¡ããŠããããããçµæãšããŠè¿ããã§ãŒã³ã§ãã
from langchain import OpenAI, LLMMathChain
# LLMã®æºå
llm = OpenAI(temperature=0)
# LLMMathChainã®æºå
llm_math = LLMMathChain(
ããããllm=llm,
ããããverbose=True
)
# LLMMathChainã®å®è¡
print(llm_math.run("How many of the integers between 0 and 99 inclusive are divisible by 8?"))> Entering new LLMMathChain chain...
How many of the integers between 0 and 99 inclusive are divisible by 8?
Answer: 12
> Finished LLMMathChain chain.
Answer: 124-2. PALChain
ãŠãŒã¶ãŒå ¥å (æšè«ã®è³ªå) ãåãåããLLMChainã§ãããPython REPLã§å®è¡ããPythonã³ãŒãã«å€æãããããçµæãšããŠè¿ããã§ãŒã³ã§ãã
from langchain.chains import PALChain
from langchain import OpenAI
# LLMã®æºå
llm = OpenAI(
model_name='code-davinci-002',
temperature=0,
max_tokens=512)
# PALChainã®æºå
pal_chain = PALChain.from_math_prompt(
llm,
verbose=True
)
# PALChainã®å®è¡
# (Janã¯Marciaã®3åã®ãããã飌ã£ãŠããŸãã Marciaã¯Cindy ãã2å¹å€ããããã飌ã£ãŠããŸããã·ã³ãã£ã4å¹ã®ãããã飌ã£ãŠããå Žåã3 人ã飌ã£ãŠãããããã®ç·æ°ã¯?)
question = "Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?"
print(pal_chain.run(question))> Entering new PALChain chain...
def solution():
"""Jan has three times the number of pets as Marcia. Marcia has two more pets than Cindy. If Cindy has four pets, how many total pets do the three have?"""
cindy_pets = 4
marcia_pets = cindy_pets + 2
jan_pets = marcia_pets * 3
total_pets = cindy_pets + marcia_pets + jan_pets
result = total_pets
return result
> Finished PALChain chain.
284-3. SQLDatabaseChain
ãŠãŒã¶ãŒå ¥å (質å) ãåãåããæåã®LLMãã§ãŒã³ã§SQLããŒã¿ããŒã¹ã«å¯ŸããŠå®è¡ããSQLã¯ãšãªãäœæããæ¬¡ã«å¥ã®LLMChainã§ãã®ã¯ãšãªã®çµæãååŸããããã䜿çšããŠå ã®è³ªåã«åçãããã§ãŒã³ã§ãã
ãã®ãµã³ãã«ã¯ãChinook ããŒã¿ããŒã¹ã䜿çšããŸãã ã»ããã¢ããããã«ã¯ãhttps://database.guide/2-sample-databases-sqlite/ ã®æç€ºã«åŸãã.db ãé 眮ããå¿ èŠããããŸãã
from langchain import OpenAI, SQLDatabase, SQLDatabaseChain
# SQLDatabaseã®æºå
db = SQLDatabase.from_uri("sqlite:///../../../notebooks/Chinook.db")
# LLMã®æºå
llm = OpenAI(temperature=0)
# SQLDatabaseChainã®æºå
db_chain = SQLDatabaseChain(llm=llm, database=db, verbose=True)
# SQLDatabaseChainã®å®è¡
# (åŸæ¥å¡ã¯äœäººã§ããïŒ)
print(db_chain.run("How many employees are there?"))> Entering new chain...
How many employees are there?
SQLQuery: SELECT COUNT(*) FROM Employee
SQLResult: [(8,)]
Answer: 8
> Finished chain.
84-4. LLMBashChain
ãŠãŒã¶ãŒå ¥å (質å) ãåãåããLLMãã§ãŒã³ã䜿çšããŠãããbashã³ãã³ãã«å€æããŠã¿ãŒããã«ã§å®è¡ããçµæãšããŠãããè¿ããã§ãŒã³ã§ãã
4-5. LLMCheckerChain
ãŠãŒã¶ãŒå ¥å (質å) ãåãåããLLMãã§ãŒã³ã䜿çšããŠãã®è³ªåã«åçããä»ã®LLMChainsã§ãã®åçãèªå·±ãã§ãã¯ãããã§ãŒã³ã§ãã
4-6. LLMRequestsChain
URLãšãã®ä»ã®å ¥åãåãåããRequestsã䜿çšããŠãã®URLã§ããŒã¿ãååŸãããããä»ã®å ¥åãšãšãã«LLMChainã«æž¡ããŠå¿çãçæãããã§ãŒã³ã§ãã
4-7. OpenAIModerationChain
OpenAIã®ã³ã³ãã³ãã¢ãã¬ãŒã·ã§ã³APIã䜿çšãããã§ãŒã³ã§ãã