-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebPage_reader.py
More file actions
94 lines (71 loc) · 2.76 KB
/
webPage_reader.py
File metadata and controls
94 lines (71 loc) · 2.76 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
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
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
# Useful to add documents to the chain
from langchain.chains.combine_documents import create_stuff_documents_chain
# Useful to load the URL into documents
from langchain_community.document_loaders import WebBaseLoader
# Split the Web page into multiple chunks
from langchain.text_splitter import RecursiveCharacterTextSplitter
# Create Embeddings
from langchain_openai import OpenAIEmbeddings
# Vector Database FAISS
from langchain_community.vectorstores.faiss import FAISS
# USeful to create the Retrieval part
from langchain.chains import create_retrieval_chain
"""**Setting the Context from a Web Page**"""
from langchain_openai import ChatOpenAI
llm = ChatOpenAI(api_key=openaikey, model_name="gpt-3.5-turbo", temperature = 0.5)
response = llm.invoke("What is LCEL")
print(response)
"""The above assumes the meaning of LCEL , but we want to get the LLM to answer from the following Web page
[LCEL](https://python.langchain.com/docs/expression_language/)
**HOW DO WE WRITE CODE to do that ?**
**HELPER FUNCTIONS**
**RETRIEVE DATA FROM WEBPAGE**
"""
# Retrieve Data
def get_docs():
loader = WebBaseLoader('https://python.langchain.com/docs/expression_language/')
docs = loader.load()
# WE need to split the web page data
# We create chunks of 200 and overlap so no data is missed out
text_splitter = RecursiveCharacterTextSplitter(
chunk_size=200,
chunk_overlap=20
)
splitDocs = text_splitter.split_documents(docs)
return splitDocs
"""**Create a Vector Database**"""
def create_vector_store(docs):
embedding = OpenAIEmbeddings(api_key=openaikey)
vectorStore = FAISS.from_documents(docs, embedding=embedding)
return vectorStore
"""**Putting all together**"""
def create_chain(vectorStore):
model = ChatOpenAI(api_key=openaikey,
temperature=0.4,
model='gpt-3.5-turbo-1106'
)
prompt = ChatPromptTemplate.from_template("""
Answer the user's question.
Context: {context}
Question: {input}
""")
print(prompt)
# chain = prompt | model
# We are creating the chain to add documents
document_chain = create_stuff_documents_chain(
llm=model,
prompt=prompt
)
# Retrieving the top 1 relevant document from the vector store , We can change k to 2 and get top 2 and so on
retriever = vectorStore.as_retriever(search_kwargs={"k": 1})
retrieval_chain = create_retrieval_chain(retriever, document_chain)
return retrieval_chain
"""**Using the Context from the Website - Changes the response with the new context**"""
docs = get_docs()
vectorStore = create_vector_store(docs)
chain = create_chain(vectorStore)
response = chain.invoke({
"input": "What is LCEL?",
})