-
Notifications
You must be signed in to change notification settings - Fork 10
/
summarization.py
43 lines (38 loc) · 1.64 KB
/
summarization.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
from langchain.prompts import (
ChatPromptTemplate,
HumanMessagePromptTemplate,
MessagesPlaceholder,
)
from langchain_core.messages import SystemMessage
from langchain_openai import ChatOpenAI
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
class Summarizer:
def __init__(self, openai_api_key):
self.openai_api_key = openai_api_key
self.openai_model = "gpt-4-turbo-preview"
# Needed to store chat history
self.persistent_prompt = ChatPromptTemplate.from_messages(
[
SystemMessage(
content="You are a chatbot having a conversation with a human."),
MessagesPlaceholder(variable_name="chat_history"),
HumanMessagePromptTemplate.from_template("{human_input}")
]
)
def summarize(self, text_to_summarize, summarization_prompt):
memory = ConversationBufferMemory(
memory_key="chat_history", return_messages=True)
llm = ChatOpenAI(model_name=self.openai_model, openai_api_key=self.openai_api_key)
chat_llm_chain = LLMChain(
llm=llm,
prompt=self.persistent_prompt,
verbose=False,
memory=memory,
)
init_prompt = summarization_prompt.format(text_to_summarize=text_to_summarize)
return chat_llm_chain.predict(human_input=init_prompt), chat_llm_chain
@staticmethod
def validate_summarization_prompt(summarization_prompt):
if not "{text_to_summarize}" in summarization_prompt:
raise RuntimeError("Summarization prompt should include \"{ text_to_summarize }\"")