forked from langchain-ai/langsmith-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vanilla_chain.py
29 lines (24 loc) · 1000 Bytes
/
vanilla_chain.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
from datetime import datetime
from langchain import LLMChain
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate, MessagesPlaceholder
from langchain.memory import ConversationBufferMemory
def get_llm_chain(system_prompt: str, memory: ConversationBufferMemory) -> LLMChain:
"""Return a basic LLMChain with memory."""
prompt = ChatPromptTemplate.from_messages(
[
(
"system",
system_prompt + "\nIt's currently {time}.",
),
MessagesPlaceholder(variable_name="chat_history"),
("human", "{input}"),
]
).partial(time=lambda: str(datetime.now()))
llm = ChatOpenAI(temperature=0.7)
chain = LLMChain(prompt=prompt, llm=llm, memory=memory)
return chain
if __name__ == "__main__":
chain, _ = get_llm_chain()
print(chain.invoke({"input": "Hi there, I'm a human!"})["text"])
print(chain.invoke({"input": "What's your name?"})["text"])