Update app.py
Browse files
app.py
CHANGED
@@ -8,41 +8,26 @@ from langchain import hub
|
|
8 |
from langchain.schema.runnable import RunnablePassthrough
|
9 |
from langchain.schema.output_parser import StrOutputParser
|
10 |
import os
|
11 |
-
import os
|
12 |
-
os.environ['USER_AGENT'] = 'myagent'
|
13 |
-
# Set your OpenAI API key
|
14 |
-
#openai_api_key = os.environ.get("OPENAI_API_KEY")
|
15 |
|
|
|
16 |
os.environ['OPENAI_API_KEY'] = os.environ.get("OPENAI_API_KEY")
|
17 |
-
#os.environ["OPENAI_API_KEY"] = "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"
|
18 |
|
19 |
-
# Global variable to store the RAG chain object
|
20 |
rag_chain = None
|
21 |
|
22 |
def process_url(url):
|
23 |
try:
|
24 |
-
# Initialize the loader with the specified web path
|
25 |
loader = WebBaseLoader(web_paths=[url])
|
26 |
docs = loader.load()
|
27 |
-
|
28 |
-
# Split the documents
|
29 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
|
30 |
all_splits = text_splitter.split_documents(docs)
|
31 |
-
|
32 |
-
# Create vectorstore
|
33 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
34 |
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
35 |
-
|
36 |
-
# Define the prompt
|
37 |
prompt = hub.pull("rlm/rag-prompt")
|
38 |
-
|
39 |
-
# Define the LLM
|
40 |
llm = ChatOpenAI(model="gpt-4")
|
41 |
-
|
42 |
-
# Define the RAG chain
|
43 |
def format_docs(docs):
|
44 |
return "\n\n".join(doc.page_content for doc in docs)
|
45 |
-
|
46 |
global rag_chain
|
47 |
rag_chain = (
|
48 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
@@ -50,12 +35,11 @@ def process_url(url):
|
|
50 |
| llm
|
51 |
| StrOutputParser()
|
52 |
)
|
53 |
-
|
54 |
return "Successfully processed the URL. You can now ask questions."
|
55 |
except Exception as e:
|
56 |
return f"Error processing URL: {e}"
|
57 |
|
58 |
-
def chat_with_rag_chain(message):
|
59 |
global rag_chain
|
60 |
if rag_chain:
|
61 |
try:
|
@@ -66,23 +50,32 @@ def chat_with_rag_chain(message):
|
|
66 |
else:
|
67 |
return "Please enter a URL first and process it."
|
68 |
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
77 |
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
86 |
|
87 |
-
|
88 |
-
gr.TabbedInterface([url_input_interface, chat_interface], ["URL Processor", "Chat Interface"]).launch(debug=True)
|
|
|
8 |
from langchain.schema.runnable import RunnablePassthrough
|
9 |
from langchain.schema.output_parser import StrOutputParser
|
10 |
import os
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
os.environ['USER_AGENT'] = 'myagent'
|
13 |
os.environ['OPENAI_API_KEY'] = os.environ.get("OPENAI_API_KEY")
|
|
|
14 |
|
|
|
15 |
rag_chain = None
|
16 |
|
17 |
def process_url(url):
|
18 |
try:
|
|
|
19 |
loader = WebBaseLoader(web_paths=[url])
|
20 |
docs = loader.load()
|
|
|
|
|
21 |
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
|
22 |
all_splits = text_splitter.split_documents(docs)
|
|
|
|
|
23 |
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
|
24 |
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
|
|
|
|
|
25 |
prompt = hub.pull("rlm/rag-prompt")
|
|
|
|
|
26 |
llm = ChatOpenAI(model="gpt-4")
|
27 |
+
|
|
|
28 |
def format_docs(docs):
|
29 |
return "\n\n".join(doc.page_content for doc in docs)
|
30 |
+
|
31 |
global rag_chain
|
32 |
rag_chain = (
|
33 |
{"context": retriever | format_docs, "question": RunnablePassthrough()}
|
|
|
35 |
| llm
|
36 |
| StrOutputParser()
|
37 |
)
|
|
|
38 |
return "Successfully processed the URL. You can now ask questions."
|
39 |
except Exception as e:
|
40 |
return f"Error processing URL: {e}"
|
41 |
|
42 |
+
def chat_with_rag_chain(message, history):
|
43 |
global rag_chain
|
44 |
if rag_chain:
|
45 |
try:
|
|
|
50 |
else:
|
51 |
return "Please enter a URL first and process it."
|
52 |
|
53 |
+
with gr.Blocks() as demo:
|
54 |
+
gr.Markdown("# RAG Chain URL Processor and Chat Interface")
|
55 |
+
|
56 |
+
with gr.Tab("URL Processor"):
|
57 |
+
url_input = gr.Textbox(label="Enter URL", placeholder="https://example.com")
|
58 |
+
process_button = gr.Button("Process URL")
|
59 |
+
url_output = gr.Textbox(label="Status")
|
60 |
+
|
61 |
+
process_button.click(process_url, inputs=url_input, outputs=url_output)
|
62 |
+
|
63 |
+
with gr.Tab("Chat Interface"):
|
64 |
+
chatbot = gr.Chatbot()
|
65 |
+
msg = gr.Textbox(label="Your Question")
|
66 |
+
clear = gr.Button("Clear")
|
67 |
|
68 |
+
def user(user_message, history):
|
69 |
+
return "", history + [[user_message, None]]
|
70 |
+
|
71 |
+
def bot(history):
|
72 |
+
bot_message = chat_with_rag_chain(history[-1][0], history)
|
73 |
+
history[-1][1] = bot_message
|
74 |
+
return history
|
75 |
+
|
76 |
+
msg.submit(user, [msg, chatbot], [msg, chatbot], queue=False).then(
|
77 |
+
bot, chatbot, chatbot
|
78 |
+
)
|
79 |
+
clear.click(lambda: None, None, chatbot, queue=False)
|
80 |
|
81 |
+
demo.launch(debug=True)
|
|