-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
48 lines (40 loc) · 1.1 KB
/
main.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
37
38
39
40
41
42
43
44
45
46
47
48
from dotenv import load_dotenv
from langchain.chains import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.memory import (
ConversationBufferMemory,
ConversationSummaryMemory,
FileChatMessageHistory,
)
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
load_dotenv()
chat = ChatOpenAI(verbose=True)
# memory = ConversationBufferMemory(
# memory_key="messages",
# return_messages=True,
# chat_memory=FileChatMessageHistory("messages.json"),
# )
memory = ConversationSummaryMemory(
memory_key="messages",
return_messages=True,
llm=chat,
)
prompt = ChatPromptTemplate(
input_variables=["content", "messages"],
messages=[
MessagesPlaceholder(variable_name="messages"),
HumanMessagePromptTemplate.from_template("{content}"),
],
)
chain = LLMChain(llm=chat, prompt=prompt, memory=memory, verbose=True)
while True:
try:
content = input(">> ")
result = chain({"content": content})
print(result["text"])
except Exception as e:
print(e)