
LangChainã®ãšã³ãã£ãã£ã¡ã¢ãªã詊ã
ãLangChainãã®æ°æ©èœããšã³ãã£ãã£ã¡ã¢ãªãã詊ããã®ã§ããŸãšããŸããã
åå
1. ãšã³ãã£ãã£ã¡ã¢ãª
ããšã³ãã£ãã£ã¡ã¢ãªãã¯ãäŒè©±ã«å«ãŸããŠãããšã³ãã£ãã£æ å ±ãä¿åããå¿ èŠã«å¿ããŠãã®èšæ¶ãåãåºããŠå©çšããã¡ã¢ãªã§ãã
åŠçã®æµãã¯ã次ã®ãšããã§ãã
(1) ãŠãŒã¶ãŒå ¥åãããšã³ãã£ãã£æ å ±ãæœåºããŠãšã³ãã£ãã£ã¹ãã¢ã«ä¿å
(2) ãšã³ãã£ãã£ã¹ãã¢ãããšã³ãã£ãã£æ å ±ãååŸããŠå¿çãçæ
(3) ãšã³ãã£ãã£ã¹ãã¢ã®ãšã³ãã£ãã£æ å ±ãæŽæ°

2. Colabã§ã®å®è¡
Google Colabã§ã®å®è¡æé ã¯ã次ã®ãšãã§ãã
(1) ããã±ãŒãžã®ã€ã³ã¹ããŒã«ã
# ããã±ãŒãžã®ã€ã³ã¹ããŒã«
!pip install langchain
!pip install openai(2) ç°å¢å€æ°ã®æºåã
以äžã®ã³ãŒãã® <OpenAI_APIã®ããŒã¯ã³> ã«ã¯OpenAI APIã®ããŒã¯ã³ãæå®ããŸãã(ææ)
import os
os.environ["OPENAI_API_KEY"] = "<OpenAI_APIã®ããŒã¯ã³>"(3) äŒè©±ãã§ãŒã³ã®æºåã
promptã«ENTITY_MEMORY_CONVERSATION_TEMPLATEãmemoryã«ConversationEntityMemoryãæå®ããŸãã
from langchain import OpenAI, ConversationChain
from langchain.chains.conversation.memory import ConversationEntityMemory
from langchain.chains.conversation.prompt import ENTITY_MEMORY_CONVERSATION_TEMPLATE
# äŒè©±ãã§ãŒã³ã®çæ
llm = OpenAI(temperature=0)
conversation = ConversationChain(
llm=llm,
verbose=True,
prompt=ENTITY_MEMORY_CONVERSATION_TEMPLATE,
memory=ConversationEntityMemory(llm=llm)
)(4) äŒè©±ïŒã
ãšã³ãã£ãã£æ
å ±ãã®ã¿ãŒããã³ãã¥éããçµæãã³ãããã¡ã³ããŒããåãåºããŸããã空ã§ããã
conversation.predict(input="ã®ã¿ãŒãããŸããã©ã³ãã¥éãªãŒã£ã¡ã¡ããããçµæãã³ãã®ã¡ã³ããŒã«ãªããŸããã")> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
Context:
{'ã®ã¿ãŒ': '', 'ã³ãã¥é': '', 'çµæãã³ã': '', 'ã¡ã³ããŒ': ''}
Current conversation:
Last line:
Human: ã®ã¿ãŒãããŸããã©ã³ãã¥éãªãŒã£ã¡ã¡ããããçµæãã³ãã®ã¡ã³ããŒã«ãªããŸããã
You:
> Finished chain.
' ããã¯çŽ æŽãããã§ããïŒã³ãã¥éãªäººãçµæãã³ãã®ã¡ã³ããŒã«ãªããšããã®ã¯ããšãŠãçŽ æŽãããããšã§ãããã®ã¡ã³ããŒã®ä»²éãšããŠã圌ããµããŒãããŠãããããšãã§ããã®ã§ã¯ãªãã§ããããïŒ'(5) äŒè©±ïŒ
ãšã³ãã£ãã£æ
å ±ãçµæãã³ãããåãåºããŠãŸã
conversation.predict(input='çµæãã³ãã®ä»ã®ã¡ã³ããŒã¯ãã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããŸãã')> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
Context:
{'çµæãã³ã': 'çµæãã³ãã¯ã®ã¿ãŒãããŸãã³ãã¥éãªãŒã£ã¡ã¡ãããã¡ã³ããŒã§ãããã³ãã§ãã', 'åå€ã¡ãã': '', 'è¹å€ã¡ãã': '', 'ãªã§ãŠãã': ''}
Current conversation:
Human: ã®ã¿ãŒãããŸããã©ã³ãã¥éãªãŒã£ã¡ã¡ããããçµæãã³ãã®ã¡ã³ããŒã«ãªããŸããã
AI: ããã¯çŽ æŽãããã§ããïŒã³ãã¥éãªäººãçµæãã³ãã®ã¡ã³ããŒã«ãªããšããã®ã¯ããšãŠãçŽ æŽãããããšã§ãããã®ã¡ã³ããŒã®ä»²éãšããŠã圌ããµããŒãããŠãããããšãã§ããã®ã§ã¯ãªãã§ããããïŒ
Last line:
Human: çµæãã³ãã®ä»ã®ã¡ã³ããŒã¯ãã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããŸãã
You:
> Finished chain.
' ãããã§ããïŒçµæãã³ãã®ã¡ã³ããŒã¯ãã®ã¿ãŒãããŒã«ã«ããã©ã ãããŒã¹ãšããã³ããæ§æããããã«å¿
èŠãªå
šãŠã®ããŒããæããŠããããã§ããïŒããããã®ã¡ã³ããŒãæã€ç¹è³ªã掻ãããŠãçŽ æŽãããæŒå¥ãèŽãããŠãããããšã§ãããïŒ'(6) äŒè©±ïŒ
ãšã³ãã£ãã£æ
å ±ãçµæãã³ãããåå€ã¡ããããè¹å€ã¡ãããããªã§ãŠããããåãåºããŠãŸã
conversation.predict(input='çµæãã³ãã®ã¡ã³ããŒãæããŠäžããã')> Entering new ConversationChain chain...
Prompt after formatting:
You are an assistant to a human, powered by a large language model trained by OpenAI.
You are designed to be able to assist with a wide range of tasks, from answering simple questions to providing in-depth explanations and discussions on a wide range of topics. As a language model, you are able to generate human-like text based on the input you receive, allowing you to engage in natural-sounding conversations and provide responses that are coherent and relevant to the topic at hand.
You are constantly learning and improving, and your capabilities are constantly evolving. You are able to process and understand large amounts of text, and can use this knowledge to provide accurate and informative responses to a wide range of questions. You have access to some personalized information provided by the human in the Context section below. Additionally, you are able to generate your own text based on the input you receive, allowing you to engage in discussions and provide explanations and descriptions on a wide range of topics.
Overall, you are a powerful tool that can help with a wide range of tasks and provide valuable insights and information on a wide range of topics. Whether the human needs help with a specific question or just wants to have a conversation about a particular topic, you are here to assist.
Context:
{'çµæãã³ã': 'çµæãã³ãã¯ã®ã¿ãŒãããŸãã³ãã¥éãªãŒã£ã¡ã¡ãããã¡ã³ããŒã§ãããã³ãã§ãä»ã®ã¡ã³ããŒã¯ã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããŸãã', 'åå€ã¡ãã': 'åå€ã¡ããã¯ã®ã¿ãŒããŒã«ã«ã§ãçµæãã³ãã®ä»ã®ã¡ã³ããŒãšããŠè¹å€ã¡ããïŒãã©ã ïŒãšãªã§ãŠããïŒããŒã¹ïŒãããã', 'è¹å€ã¡ãã': 'è¹å€ã¡ããã¯çµæãã³ãã®ãã©ã æ
åœã§ããã', 'ãªã§ãŠãã': 'ãªã§ãŠããã¯çµæãã³ãã®ããŒã¹ãã¬ã€ã€ãŒã§ããã'}
Current conversation:
Human: ã®ã¿ãŒãããŸããã©ã³ãã¥éãªãŒã£ã¡ã¡ããããçµæãã³ãã®ã¡ã³ããŒã«ãªããŸããã
AI: ããã¯çŽ æŽãããã§ããïŒã³ãã¥éãªäººãçµæãã³ãã®ã¡ã³ããŒã«ãªããšããã®ã¯ããšãŠãçŽ æŽãããããšã§ãããã®ã¡ã³ããŒã®ä»²éãšããŠã圌ããµããŒãããŠãããããšãã§ããã®ã§ã¯ãªãã§ããããïŒ
Human: çµæãã³ãã®ä»ã®ã¡ã³ããŒã¯ãã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããŸãã
AI: ãããã§ããïŒçµæãã³ãã®ã¡ã³ããŒã¯ãã®ã¿ãŒãããŒã«ã«ããã©ã ãããŒã¹ãšããã³ããæ§æããããã«å¿
èŠãªå
šãŠã®ããŒããæããŠããããã§ããïŒããããã®ã¡ã³ããŒãæã€ç¹è³ªã掻ãããŠãçŽ æŽãããæŒå¥ãèŽãããŠãããããšã§ãããïŒ
Last line:
Human: çµæãã³ãã®ã¡ã³ããŒãæããŠäžããã
You:
> Finished chain.
' çµæãã³ãã®ã¡ã³ããŒã¯ãã®ã¿ãŒãããŸãã³ãã¥éãªãŒã£ã¡ã¡ãããã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠããã§ãã'3. ãšã³ãã£ãã£æ å ±ã®ç¢ºèª
ãšã³ãã£ãã£ã¹ãã¢ã§ä¿æããŠãããšã³ãã£ãã£æ å ±ã¯ãConversationChainã®memory.storeã§ç¢ºèªã§ããŸãã
# ãšã³ãã£ãã£æ
å ±ã®ç¢ºèª
from pprint import pprint
pprint(conversation.memory.store){'ã®ã¿ãŒ': 'ã®ã¿ãŒãæŒå¥ããããšãåŸæã§ãã³ãã¥éãªãŒã£ã¡ã¡ãããçµæãã³ãã®ã¡ã³ããŒã«ãªã£ãã',
'ã³ãã¥é': 'ã³ãã¥éãªãŒã£ã¡ã¡ãããã®ã¿ãŒãããŸãæŒå¥ããçµæãã³ãã®ã¡ã³ããŒã«ãªã£ãã',
'ã¡ã³ããŒ': 'ã¡ã³ããŒã«ã¯ã®ã¿ãŒãããŸãã§ã³ãã¥éãªãŒã£ã¡ã¡ãããããã',
'ãªã§ãŠãã': 'ãªã§ãŠããã¯çµæãã³ãã®ããŒã¹ãã¬ã€ã€ãŒã§ãä»ã®ã¡ã³ããŒã¯ã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ããããããŠãªã§ãŠããã§ããã',
'åå€ã¡ãã': 'åå€ã¡ããã¯ã®ã¿ãŒããŒã«ã«ã§ãçµæãã³ãã®ä»ã®ã¡ã³ããŒãšããŠè¹å€ã¡ããïŒãã©ã ïŒãšãªã§ãŠããïŒããŒã¹ïŒãããã',
'çµæãã³ã': 'çµæãã³ãã¯ã®ã¿ãŒãããŸãã³ãã¥éãªãŒã£ã¡ã¡ãããã¡ã³ããŒã§ãããã³ãã§ãä»ã®ã¡ã³ããŒã¯ã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããŸããã¡ã³ããŒã¯ã®ã¿ãŒãããŒã«ã«ããã©ã ãããŒã¹ãšããã³ããæ§æããããã«å¿
èŠãªå
šãŠã®ããŒããæããŠããã',
'è¹å€ã¡ãã': 'è¹å€ã¡ããã¯çµæãã³ãã®ãã©ã æ
åœã§ãããä»ã®ã¡ã³ããŒãšããŠã®ã¿ãŒããŒã«ã«ã®åå€ã¡ããããã©ã ã®è¹å€ã¡ãããããŒã¹ã®ãªã§ãŠãããããã'}