-
Notifications
You must be signed in to change notification settings - Fork 0
/
exp-langchain-inet-page-summary.py
94 lines (81 loc) · 3.42 KB
/
exp-langchain-inet-page-summary.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import questionary as q
from langchain.chains.retrieval_qa.base import RetrievalQA
from langchain.document_loaders.web_base import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.vectorstores.chroma import Chroma
from langchain.callbacks.manager import CallbackManager
from langchain.callbacks.streaming_stdout import StreamingStdOutCallbackHandler
from langchain_community.llms.ollama import Ollama
from langchain.prompts import PromptTemplate, ChatPromptTemplate
from langchain.chains.llm import LLMChain
from langchain.embeddings.ollama import OllamaEmbeddings
from langchain_core.documents import Document
MODEL = "dolphin-mixtral"
# MODEL = "llama3:latest"
TEMPERATURE = 0.8
VERBOSE = True
TEMPLATES = {
"dolphin-mixtral": """
<|im_start|>system
You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you asked anything about real-time information or information from the Internet - assume that it is retrived for you and is in the Context.
If you don't know the answer, just say that you don't know.
<|im_end|>
<|im_start|>user
Question: {question}
Context: {context}
<|im_end|>
<|im_start|>assistant
""",
"llama3": """
You are an assistant for question-answering tasks.
Use the following pieces of retrieved context to answer the question.
If you asked anything about real-time information or information from the Internet - assume that it is retrived for you and is in the Context.
If you don't know the answer, just say that you don't know.
Question: {question}
Context: {context}
The answer:
""",
}
def load_website_content(url):
docs: list[Document] = WebBaseLoader(url).load()
print(f"Loaded {len(docs)} docs...")
return docs[0].page_content
def create_retrieval_qa_chain(db: Chroma):
QA_CHAIN_PROMPT_TEMPLATE = TEMPLATES[MODEL]
prompt = PromptTemplate(
input_variables=["question", "context"],
template=QA_CHAIN_PROMPT_TEMPLATE,
)
return RetrievalQA.from_chain_type(
llm=Ollama(
model=MODEL,
temperature=TEMPERATURE,
callback_manager=CallbackManager([StreamingStdOutCallbackHandler()]),
),
retriever=db.as_retriever(search_kwargs={"k": 1}),
chain_type_kwargs={"prompt": prompt},
verbose=VERBOSE,
)
def main():
url = q.select(
"What site do you want to pull the latest hottest headlines from?",
["https://news.ycombinator.com/", "your option"],
).ask()
url = q.text("Enter the site URL").ask() if url == "your option" else url
content = load_website_content(url)
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1500, chunk_overlap=200)
texts = text_splitter.create_documents([content])
db = Chroma.from_documents(texts, OllamaEmbeddings(model=MODEL))
chain = create_retrieval_qa_chain(db)
question = f"""
What are the top (10 maximum) latest and the most popular
(based on number of points and comments if such information's available)
headlines that you can derive from the given context which is a content of a web-page?"""
chain.invoke(question)
# just some empty space for the output readability
for i in range(3):
print()
if __name__ == "__main__":
main()