Ubuntu
commited on
Commit
•
09ae247
1
Parent(s):
6929c27
rag initial pipeline setup
Browse files- .gitignore +1 -0
- app.py +50 -4
- building.py +53 -0
- src/__pycache__/rag.cpython-310.pyc +0 -0
- src/rag.py +50 -0
.gitignore
ADDED
@@ -0,0 +1 @@
|
|
|
|
|
1 |
+
.env
|
app.py
CHANGED
@@ -1,7 +1,53 @@
|
|
1 |
import gradio as gr
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
+
from src.rag import RAG
|
3 |
|
4 |
+
import bs4
|
5 |
+
from langchain_community.document_loaders import WebBaseLoader
|
6 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
7 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
8 |
|
9 |
+
from dotenv import load_dotenv
|
10 |
+
|
11 |
+
# set the required env variables
|
12 |
+
load_dotenv(".env")
|
13 |
+
|
14 |
+
def rag_handler(web_paths, model_name, temperature, question):
|
15 |
+
print(web_paths)
|
16 |
+
web_paths = web_paths.split(',')
|
17 |
+
print(web_paths)
|
18 |
+
loader = WebBaseLoader(
|
19 |
+
web_paths=web_paths,
|
20 |
+
bs_kwargs=dict(
|
21 |
+
parse_only=bs4.SoupStrainer(
|
22 |
+
class_=("post-content", "post-title", "post-header")
|
23 |
+
)
|
24 |
+
),
|
25 |
+
)
|
26 |
+
|
27 |
+
llm = ChatOpenAI(model_name=model_name, temperature=temperature)
|
28 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200) # TODO: Parameterize this
|
29 |
+
|
30 |
+
rag_pipeline = RAG(llm, loader, text_splitter, OpenAIEmbeddings)
|
31 |
+
|
32 |
+
return rag_pipeline.invoke(question)
|
33 |
+
|
34 |
+
def create_rag_interface():
|
35 |
+
return gr.Interface(
|
36 |
+
fn=rag_handler,
|
37 |
+
inputs=[
|
38 |
+
gr.Textbox(value="https://lilianweng.github.io/posts/2023-06-23-agent/"),
|
39 |
+
gr.Dropdown(["gpt-3.5-turbo"], type="value"),
|
40 |
+
gr.Slider(0, 1, step=0.1),
|
41 |
+
'text'
|
42 |
+
],
|
43 |
+
outputs="text"
|
44 |
+
)
|
45 |
+
|
46 |
+
|
47 |
+
if __name__ == '__main__':
|
48 |
+
interface_list = []
|
49 |
+
interface_list.append(create_rag_interface())
|
50 |
+
|
51 |
+
demo = gr.TabbedInterface(interface_list)
|
52 |
+
|
53 |
+
demo.launch()
|
building.py
ADDED
@@ -0,0 +1,53 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import getpass
|
2 |
+
from dotenv import dotenv_values, load_dotenv
|
3 |
+
|
4 |
+
# p = getpass.getpass() # for user input
|
5 |
+
|
6 |
+
config = dict(dotenv_values(".env"))
|
7 |
+
load_dotenv(".env")
|
8 |
+
|
9 |
+
|
10 |
+
import bs4
|
11 |
+
from langchain import hub
|
12 |
+
from langchain_community.document_loaders import WebBaseLoader
|
13 |
+
from langchain_community.vectorstores import Chroma
|
14 |
+
from langchain_core.output_parsers import StrOutputParser
|
15 |
+
from langchain_core.runnables import RunnablePassthrough
|
16 |
+
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
|
17 |
+
from langchain_text_splitters import RecursiveCharacterTextSplitter
|
18 |
+
|
19 |
+
# Load, chunk and index the contents of the blog.
|
20 |
+
loader = WebBaseLoader(
|
21 |
+
web_paths=("https://lilianweng.github.io/posts/2023-06-23-agent/",),
|
22 |
+
bs_kwargs=dict(
|
23 |
+
parse_only=bs4.SoupStrainer(
|
24 |
+
class_=("post-content", "post-title", "post-header")
|
25 |
+
)
|
26 |
+
),
|
27 |
+
)
|
28 |
+
docs = loader.load()
|
29 |
+
|
30 |
+
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
|
31 |
+
splits = text_splitter.split_documents(docs)
|
32 |
+
vectorstore = Chroma.from_documents(documents=splits, embedding=OpenAIEmbeddings())
|
33 |
+
|
34 |
+
# Retrieve and generate using the relevant snippets of the blog.
|
35 |
+
retriever = vectorstore.as_retriever()
|
36 |
+
prompt = hub.pull("rlm/rag-prompt")
|
37 |
+
llm = ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0)
|
38 |
+
|
39 |
+
|
40 |
+
def format_docs(docs):
|
41 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
42 |
+
|
43 |
+
|
44 |
+
rag_chain = (
|
45 |
+
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
46 |
+
| prompt
|
47 |
+
| llm
|
48 |
+
| StrOutputParser()
|
49 |
+
)
|
50 |
+
|
51 |
+
print(rag_chain.invoke("What is Task Decomposition?"))
|
52 |
+
|
53 |
+
|
src/__pycache__/rag.cpython-310.pyc
ADDED
Binary file (2.06 kB). View file
|
|
src/rag.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from langchain import hub
|
2 |
+
from langchain_core.output_parsers import StrOutputParser
|
3 |
+
from langchain_core.runnables import RunnablePassthrough
|
4 |
+
from langchain_community.vectorstores import Chroma
|
5 |
+
|
6 |
+
# class for `Retreival Augmented Generation Pipeline`
|
7 |
+
class RAG:
|
8 |
+
def __init__(
|
9 |
+
self,
|
10 |
+
llm,
|
11 |
+
loader,
|
12 |
+
text_splitter,
|
13 |
+
embedding,
|
14 |
+
prompt = None
|
15 |
+
):
|
16 |
+
self.llm = llm
|
17 |
+
self.embedding = embedding
|
18 |
+
self.loader = loader
|
19 |
+
self.text_splitter = text_splitter
|
20 |
+
self.prompt = prompt if prompt else hub.pull("rlm/rag-prompt")
|
21 |
+
|
22 |
+
self.docs = self.load_docs()
|
23 |
+
self.splits = self.create_splits()
|
24 |
+
self.vectorstore = Chroma.from_documents(documents=self.splits, embedding=self.embedding())
|
25 |
+
self.retriever = self.get_retreiver()
|
26 |
+
self.rag_chain = self.generate_rag_chain()
|
27 |
+
|
28 |
+
def create_splits(self):
|
29 |
+
return self.text_splitter.split_documents(self.docs)
|
30 |
+
|
31 |
+
def load_docs(self):
|
32 |
+
return self.loader.load()
|
33 |
+
|
34 |
+
def get_retreiver(self):
|
35 |
+
return self.vectorstore.as_retriever()
|
36 |
+
|
37 |
+
def format_docs(self, docs):
|
38 |
+
return "\n\n".join(doc.page_content for doc in docs)
|
39 |
+
|
40 |
+
def generate_rag_chain(self):
|
41 |
+
return (
|
42 |
+
{"context": self.retriever | self.format_docs, "question": RunnablePassthrough()}
|
43 |
+
| self.prompt
|
44 |
+
| self.llm
|
45 |
+
| StrOutputParser()
|
46 |
+
)
|
47 |
+
|
48 |
+
def invoke(self, question):
|
49 |
+
return self.rag_chain.invoke(question)
|
50 |
+
|