-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path8-memory.py
More file actions
44 lines (37 loc) · 1.1 KB
/
8-memory.py
File metadata and controls
44 lines (37 loc) · 1.1 KB
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
from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv())
from langchain.chains import LLMChain
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder, SystemMessagePromptTemplate, HumanMessagePromptTemplate
from langchain.chat_models import ChatOpenAI
from langchain.memory import ConversationSummaryMemory
# LLM
chat_llm = ChatOpenAI(
# model="text-davinci-002",
model="gpt-3.5-turbo",
verbose=True,
temperature=0.0
)
# Prompt
prompt = ChatPromptTemplate(
messages=[
SystemMessagePromptTemplate.from_template(
"You are a friendly chatbot having a conversation with human."
),
# The 'variable_name' is what must align with memory
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{question}"),
]
)
memory = ConversationSummaryMemory(
llm=chat_llm,
memory_key="chat_history",
return_messages=True
)
# Chain
chain = LLMChain(
prompt=prompt,
llm=chat_llm,
memory=memory,
verbose=True
)
print(chain({"question": "Halo ini dengan Ghifary"}))