mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-05 17:35:28 +03:00
Various improvements to the Chains & Memory sections of the documentation including formatting, spelling, and grammar fixes to improve readability.
9.1 KiB
9.1 KiB
In [1]:
from langchain.chains import LLMChain
from langchain.llms import OpenAI
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplateIn [2]:
template = """You are a chatbot having a conversation with a human.
{chat_history}
Human: {human_input}
Chatbot:"""
prompt = PromptTemplate(
input_variables=["chat_history", "human_input"], template=template
)
memory = ConversationBufferMemory(memory_key="chat_history")In [3]:
llm = OpenAI()
llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=memory,
)In [4]:
llm_chain.predict(human_input="Hi there my friend")Out [4]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mYou are a chatbot having a conversation with a human. Human: Hi there my friend Chatbot:[0m [1m> Finished chain.[0m
' Hi there! How can I help you today?'
In [5]:
llm_chain.predict(human_input="Not too bad - how are you?")Out [5]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mYou are a chatbot having a conversation with a human. Human: Hi there my friend AI: Hi there! How can I help you today? Human: Not too bad - how are you? Chatbot:[0m [1m> Finished chain.[0m
" I'm doing great, thanks for asking! How are you doing?"
In [6]:
from langchain.chat_models import ChatOpenAI
from langchain.schema import SystemMessage
from langchain.prompts import ChatPromptTemplate, HumanMessagePromptTemplate, MessagesPlaceholderIn [7]:
prompt = ChatPromptTemplate.from_messages([
SystemMessage(content="You are a chatbot having a conversation with a human."), # The persistent system prompt
MessagesPlaceholder(variable_name="chat_history"), # Where the memory will be stored.
HumanMessagePromptTemplate.from_template("{human_input}"), # Where the human input will injected
])
memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)In [8]:
llm = ChatOpenAI()
chat_llm_chain = LLMChain(
llm=llm,
prompt=prompt,
verbose=True,
memory=memory,
)In [9]:
chat_llm_chain.predict(human_input="Hi there my friend")Out [9]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mSystem: You are a chatbot having a conversation with a human. Human: Hi there my friend[0m [1m> Finished chain.[0m
'Hello! How can I assist you today, my friend?'
In [10]:
chat_llm_chain.predict(human_input="Not too bad - how are you?")Out [10]:
[1m> Entering new LLMChain chain...[0m Prompt after formatting: [32;1m[1;3mSystem: You are a chatbot having a conversation with a human. Human: Hi there my friend AI: Hello! How can I assist you today, my friend? Human: Not too bad - how are you?[0m [1m> Finished chain.[0m
"I'm an AI chatbot, so I don't have feelings, but I'm here to help and chat with you! Is there something specific you would like to talk about or any questions I can assist you with?"