-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.py
50 lines (41 loc) · 1.61 KB
/
main.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
import gradio as gr
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings import OllamaEmbeddings
import ollama
from config import config
# Function to load, split, and retrieve documents
def load_and_retrieve_docs(url):
loader = WebBaseLoader(web_paths=(url,), bs_kwargs=dict())
docs = loader.load()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200)
splits = text_splitter.split_documents(docs)
embeddings = OllamaEmbeddings(model=config.EMBEDDINGS_MODEL)
vectorstore = Chroma.from_documents(documents=splits, embedding=embeddings)
return vectorstore.as_retriever()
# Function to format documents
def format_docs(docs):
return "\n\n".join(doc.page_content for doc in docs)
# Function that defines the RAG chain
def rag_chain(url, question):
retriever = load_and_retrieve_docs(url)
retrieved_docs = retriever.invoke(question)
formatted_context = format_docs(retrieved_docs)
formatted_prompt = f"Question: {question}\n\nContext: {formatted_context}"
response = ollama.chat(
model=config.LLM_MODEL, messages=[{"role": "user", "content": formatted_prompt}]
)
return response["message"]["content"]
# Gradio interface
iface = gr.Interface(
fn=rag_chain,
inputs=["text", "text"],
outputs="text",
title="RAG Chain Question Answering",
description="Enter a URL and a query to get answers from the RAG chain.",
)
# Launch the app
iface.launch(
# share=True,
)