mirror of
https://github.com/langchain-ai/langchain.git
synced 2026-10-07 02:15:19 +03:00
Big docs refactor! Motivation is to make it easier for people to find resources they are looking for. To accomplish this, there are now three main sections: - Getting Started: steps for getting started, walking through most core functionality - Modules: these are different modules of functionality that langchain provides. Each part here has a "getting started", "how to", "key concepts" and "reference" section (except in a few select cases where it didnt easily fit). - Use Cases: this is to separate use cases (like summarization, question answering, evaluation, etc) from the modules, and provide a different entry point to the code base. There is also a full reference section, as well as extra resources (glossary, gallery, etc) Co-authored-by: Shreya Rajpal <ShreyaR@users.noreply.github.com>
7.5 KiB
7.5 KiB
In [1]:
from langchain.llms import OpenAI
from langchain.chains import ConversationChain
from langchain.chains.conversation.memory import ConversationBufferMemory
llm = OpenAI(temperature=0)In [2]:
# Here it is by default set to "AI"
conversation = ConversationChain(
llm=llm,
verbose=True,
memory=ConversationBufferMemory()
)In [3]:
conversation.predict(input="Hi there!")Out [3]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe 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: Hi there! AI:[0m [1m> Finished ConversationChain chain.[0m
" Hi there! It's nice to meet you. How can I help you today?"
In [4]:
conversation.predict(input="What's the weather?")Out [4]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe 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: Hi there! AI: Hi there! It's nice to meet you. How can I help you today? Human: What's the weather? AI:[0m [1m> Finished ConversationChain chain.[0m
' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the next few days is sunny with temperatures in the mid-70s.'
In [5]:
# Now we can override it and set it to "AI Assistant"
from langchain.prompts.prompt import PromptTemplate
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.
Current conversation:
{history}
Human: {input}
AI Assistant:"""
PROMPT = PromptTemplate(
input_variables=["history", "input"], template=template
)
conversation = ConversationChain(
prompt=PROMPT,
llm=llm,
verbose=True,
memory=ConversationBufferMemory(ai_prefix="AI Assistant")
)In [6]:
conversation.predict(input="Hi there!")Out [6]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe 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: Hi there! AI Assistant:[0m [1m> Finished ConversationChain chain.[0m
" Hi there! It's nice to meet you. How can I help you today?"
In [7]:
conversation.predict(input="What's the weather?")Out [7]:
[1m> Entering new ConversationChain chain...[0m Prompt after formatting: [32;1m[1;3mThe 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: Hi there! AI Assistant: Hi there! It's nice to meet you. How can I help you today? Human: What's the weather? AI Assistant:[0m [1m> Finished ConversationChain chain.[0m
' The current weather is sunny and warm with a temperature of 75 degrees Fahrenheit. The forecast for the rest of the day is sunny with a high of 78 degrees and a low of 65 degrees.'
In [ ]: