-
Notifications
You must be signed in to change notification settings - Fork 0
/
scouting_cmd.py
36 lines (28 loc) · 1.1 KB
/
scouting_cmd.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
"""Simple example of a chatbot (with memory) with command line interface."""
# please put your API key in os.environ["OPENAI_API_KEY"]
from langchain.chat_models import ChatOpenAI
from langchain.prompts.chat import (
ChatPromptTemplate,
SystemMessagePromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain.chains import ConversationChain
from langchain.memory import ConversationBufferMemory
# prompts setup
template = "You are a helpful assistant who generates suggestions for the user."
chat_prompt = ChatPromptTemplate(messages=[
SystemMessagePromptTemplate.from_template(template),
MessagesPlaceholder(variable_name="history"),
HumanMessagePromptTemplate.from_template("{input}")
])
# note that return_messages=True is required for the memory to work
memory = ConversationBufferMemory(memory_key="history", return_messages=True)
# AI model setup
chain = ConversationChain(
llm=ChatOpenAI(), prompt=chat_prompt, memory=memory)
# chat loop
while True:
prompt = input("User: ")
response = chain.run({"input": prompt})
print("AI: ", response, "\n")