mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-07 02:15:19 +03:00
4.6 KiB
4.6 KiB
In [1]:
from langchain.prompts import (
ChatPromptTemplate,
MessagesPlaceholder,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate
)In [2]:
prompt = ChatPromptTemplate.from_messages([
SystemMessagePromptTemplate.from_template("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."),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template("{input}")
])In [4]:
from langchain.chains import ConversationChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationBufferMemoryIn [5]:
llm = ChatOpenAI(temperature=0)In [6]:
memory = ConversationBufferMemory(return_messages=True)In [8]:
conversation = ConversationChain(memory=memory, prompt=prompt, llm=llm)In [9]:
conversation.predict(input="Hi there!")Out [9]:
'Hello! How can I assist you today?'
In [10]:
conversation.predict(input="I'm doing well! Just having a conversation with an AI.")Out [10]:
"That sounds like fun! I'm happy to chat with you. Is there anything specific you'd like to talk about?"
In [11]:
conversation.predict(input="Tell me about yourself.")Out [11]:
"Sure! I am an AI language model created by OpenAI. I was trained on a large dataset of text from the internet, which allows me to understand and generate human-like language. I can answer questions, provide information, and even have conversations like this one. Is there anything else you'd like to know about me?"
In [ ]: