File size: 2,938 Bytes
3ec9224 5a1fc91 5be8df6 91137eb 6a32a13 6387af9 5a1fc91 5be8df6 5a1fc91 6803339 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 5be8df6 5a1fc91 4ce7fc5 9bf736d 5a1fc91 0c86355 |
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 |
import gradio as gr
from langchain_community.document_loaders import WebBaseLoader
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.vectorstores import Chroma
from langchain_openai import OpenAIEmbeddings
from langchain_openai import ChatOpenAI
from langchain import hub
from langchain.schema.runnable import RunnablePassthrough
from langchain.schema.output_parser import StrOutputParser
import os
# Set your OpenAI API key
os.environ["OPENAI_API_KEY"] = "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"
# Global variable to store the RAG chain object
rag_chain = None
def process_url(url):
try:
# Initialize the loader with the specified web path
loader = WebBaseLoader(web_paths=[url])
docs = loader.load()
# Split the documents
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
all_splits = text_splitter.split_documents(docs)
# Create vectorstore
vectorstore = Chroma.from_documents(documents=all_splits, embedding=OpenAIEmbeddings())
retriever = vectorstore.as_retriever(search_type="similarity", search_kwargs={"k": 2})
# Define the prompt
prompt = hub.pull("rlm/rag-prompt")
# Define the LLM
llm = ChatOpenAI(model="gpt-4")
# Define the RAG chain
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
global rag_chain
rag_chain = (
{"context": retriever | format_docs, "question": RunnablePassthrough()}
| prompt
| llm
| StrOutputParser()
)
return "Successfully processed the URL. You can now ask questions."
except Exception as e:
return f"Error processing URL: {e}"
def chat_with_rag_chain(message):
global rag_chain
if rag_chain:
try:
response = rag_chain.invoke(message)
return response
except Exception as e:
return f"Error invoking RAG chain: {e}"
else:
return "Please enter a URL first and process it."
# Gradio interface for entering the URL
url_input_interface = gr.Interface(
fn=process_url,
inputs=gr.Textbox(label="Enter URL", placeholder="https://example.com"),
outputs=gr.Textbox(label="Status"),
title="RAG Chain URL Processor",
description="Enter a URL to process the article using a RAG chain model."
)
# Gradio chat interface for Q&A
chat_interface = gr.Interface(
fn=chat_with_rag_chain,
inputs=gr.Textbox(label="Your Question"),
outputs=gr.Textbox(label="Response"),
title="RAG Chain Chat Interface",
description="Chat with the RAG chain model after processing a URL."
)
# Combining the two interfaces in a tab layout
gr.TabbedInterface([url_input_interface, chat_interface], ["URL Processor", "Chat Interface"]).launch(debug=True)
|