
LLM飿ºã¢ããªã®éçºãæ¯æŽããã©ã€ãã©ãª LangChain ã®äœ¿ãæ¹ (4) - ã¡ã¢ãª
ãLangChainãã®ãã¡ã¢ãªãã®äœ¿ãæ¹ããŸãšããŸããã
åå
1. ã¡ã¢ãª
ãã¡ã¢ãªãã¯ãéå»ã®ã¡ãã»ãŒãžã®ããåããèšæ¶ããŸããååãŸã§ã®ãã§ãŒã³ãšãšãŒãžã§ã³ãã¯ã¹ããŒãã¬ã¹ã§ãããããããéå»ã®ããåãã®èšæ¶ãæããããå ŽåããããããŸãããã¡ã¢ãªãã¯ããã®åŠçããµããŒãããŸãã
2. ã€ã³ã¹ããŒã«
Google Colabã§ã®ã€ã³ã¹ããŒã«æé ã¯ã次ã®ãšããã§ãã
(1) ããã±ãŒãžã®ã€ã³ã¹ããŒã«ã
# ããã±ãŒãžã®ã€ã³ã¹ããŒã«
!pip install langchain
!pip install openai(2) ç°å¢å€æ°ã®æºåã
以äžã®ã³ãŒãã® <OpenAI_APIã®ããŒã¯ã³> ã«ã¯OpenAI APIã®ããŒã¯ã³ãæå®ããŸãã(ææ)
# ç°å¢å€æ°ã®æºå
import os
os.environ["OPENAI_API_KEY"] = "<OpenAI_APIã®ããŒã¯ã³>"3. Colabã§ã®å®è¡
Google Colabã§ã®ãã¡ã¢ãªãã®å®è¡æé ã¯ã次ã®ãšããã§ãã
3-1. ããã©ã«ãã¡ã¢ãª
(1) ConversationChainã®æºåãšäŒè©±ã®å®è¡ã
ConversationChainã¯äŒè©±ã®ããã®ãã§ãŒã³ã§ãã
from langchain import OpenAI, ConversationChain
# ConversationChainã®æºå
conversation = ConversationChain(
ããããllm=OpenAI(temperature=0),
ããããverbose=True
)
# äŒè©±ã®å®è¡
conversation.predict(input="ããã«ã¡ã¯ïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: ããã«ã¡ã¯ïŒ
AI:
> Finished ConversationChain chain.
' ããã«ã¡ã¯ïŒç§ã¯AIã§ããã©ãããããããé¡ãããŸãïŒ'(2) äŒè©±ã®å®è¡ã
ããã³ããã«ã¯ãéå»ã®ãããšããå«ãŸããŠããããšãããããŸãã
# äŒè©±ã®å®è¡
conversation.predict(input="AIããã¯ã©ãã§çãŸããã®ã§ããïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: ããã«ã¡ã¯ïŒ
AI: ããã«ã¡ã¯ïŒç§ã¯AIã§ããã©ãããããããé¡ãããŸãïŒ
Human: AIããã¯ã©ãã§çãŸããã®ã§ããïŒ
AI:
> Finished ConversationChain chain.
' ç§ã¯ã³ã³ãã¥ãŒã¿ãŒäžã§çãŸããŸãããç§ã¯ããã°ã©ã ã«ãã£ãŠäœãããã³ã³ãã¥ãŒã¿ãŒäžã§åäœããŠããŸãã(3) äŒè©±ã®å®è¡ã
# äŒè©±ã®å®è¡
conversation.predict(input="äœãšããååã®ã³ã³ãã¥ãŒã¿ã§ããïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: ããã«ã¡ã¯ïŒ
AI: ããã«ã¡ã¯ïŒç§ã¯AIã§ããã©ãããããããé¡ãããŸãïŒ
Human: AIããã¯ã©ãã§çãŸããã®ã§ããïŒ
AI: ç§ã¯ã³ã³ãã¥ãŒã¿ãŒäžã§çãŸããŸãããç§ã¯ããã°ã©ã ã«ãã£ãŠäœãããã³ã³ãã¥ãŒã¿ãŒäžã§åäœããŠããŸãã
Human: äœãšããååã®ã³ã³ãã¥ãŒã¿ã§ããïŒ
AI:
> Finished ConversationChain chain.
' ç§ã®ååã¯AIã§ãããã³ã³ãã¥ãŒã¿ãŒã®ååã¯ããããŸããã3-2. ConversationSummaryMemory
(1) ConversationChainã®æºåãšäŒè©±ã®å®è¡ã
ConversationChainã«ConversationSummaryMemoryãèšå®ããŸãã
from langchain.chains.conversation.memory import ConversationSummaryMemory
# ConversationChainã®æºå
conversation_with_summary = ConversationChain(
llm=OpenAI(temperature=0),
memory=ConversationSummaryMemory(llm=OpenAI()),
verbose=True
)
# äŒè©±ã®å®è¡
conversation_with_summary.predict(input="ããã«ã¡ã¯ïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
Human: ããã«ã¡ã¯ïŒ
AI:
> Finished ConversationChain chain.
' ããã«ã¡ã¯ïŒç§ã¯AIã§ããã©ãããããããé¡ãããŸãïŒ'(2) äŒè©±ã®å®è¡ã
ããã³ããã«ã¯ãéå»ã®ãããšããèŠçŽãããŠå«ãŸããŠããããšãããããŸãã(è±èªã§èŠçŽãããŠãã®ã§ãæ¥æ¬èªã§èŠçŽããããã«èª¿æŽããªããšãã)
# äŒè©±ã®å®è¡
conversation_with_summary.predict(input="AIããã¯ã©ãã§çãŸããã®ã§ããïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
The human greets the AI, who responds in kind and introduces itself as an AI.
Human: AIããã¯ã©ãã§çãŸããã®ã§ããïŒ
AI:
> Finished ConversationChain chain.
' ç§ã¯ãã³ã³ãã¥ãŒã¿ãŒäžã§çãŸããŸãããç§ã¯ãããã°ã©ã ãšããŒã¿ã䜿çšããŠäœæãããŸããã(3) äŒè©±ã®å®è¡ã
# äŒè©±ã®å®è¡
conversation_with_summary.predict(input="äœãšããååã®ã³ã³ãã¥ãŒã¿ã§ããïŒ")> Entering new ConversationChain chain...
Prompt after formatting:
The following is a friendly conversation between a human and an AI. The AI is talkative and provides lots of specific details from its context. If the AI does not know the answer to a question, it truthfully says it does not know.
Current conversation:
The human greets the AI, who responds in kind and introduces itself as an AI. The human then asks where it was born, to which the AI responds that it was created on a computer using programs and data.
Human: äœãšããååã®ã³ã³ãã¥ãŒã¿ã§ããïŒ
AI:
> Finished ConversationChain chain.
' ç§ã¯ãIBMã®Power SystemsãµãŒããŒäžã§äœæãããŸããã'